| 1469 | |
| 1470 | /** |
| 1471 | * Test that setting void category for CPT associated with 'category' taxonomy has default category. |
| 1472 | * |
| 1473 | * @ticket 43516 |
| 1474 | */ |
| 1475 | function test_set_post_category_cpt_default_category () { |
| 1476 | add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) ); |
| 1477 | |
| 1478 | $term = wp_insert_term( 'Foo', 'category' ); |
| 1479 | |
| 1480 | $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_category' => array( $term['term_id'] ) ) ); |
| 1481 | $post = get_post( $post_id ); |
| 1482 | $this->assertInternalType( 'array', $post->post_category ); |
| 1483 | $this->assertEquals( $term['term_id'], $post->post_category[0] ); |
| 1484 | |
| 1485 | // Reset categories |
| 1486 | wp_set_post_categories( $post_id, array() ); |
| 1487 | $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); |
| 1488 | |
| 1489 | register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) ); |
| 1490 | $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt', 'post_category' => array( $term['term_id'] ) ) ); |
| 1491 | $post = get_post( $post_id ); |
| 1492 | |
| 1493 | // Reset categories |
| 1494 | wp_set_post_categories( $post_id, array() ); |
| 1495 | $this->assertInternalType( 'array', $post->post_category ); |
| 1496 | $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); |
| 1497 | |
| 1498 | remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) ); |
| 1499 | } |
| 1500 | |
| 1501 | /** |
| 1502 | * Test that new 'post' and CPT associated with 'category' taxonomy has default category. |
| 1503 | * |
| 1504 | * @ticket 43516 |
| 1505 | */ |
| 1506 | function test_insert_post_cpt_default_category () { |
| 1507 | add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) ); |
| 1508 | |
| 1509 | $post_id = wp_insert_post( array( 'post_title' => 'Foo' ) ); |
| 1510 | $post = get_post( $post_id ); |
| 1511 | $this->assertInternalType( 'array', $post->post_category ); |
| 1512 | $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); |
| 1513 | |
| 1514 | register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) ); |
| 1515 | $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt' ) ); |
| 1516 | $post = get_post( $post_id ); |
| 1517 | $this->assertInternalType( 'array', $post->post_category ); |
| 1518 | $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); |
| 1519 | |
| 1520 | remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) ); |
| 1521 | } |
| 1522 | |
| 1523 | function filter_default_category_post_types ( $post_types ) { |
| 1524 | $post_types[] = 'cpt'; |
| 1525 | return $post_types; |
| 1526 | } |