WordPress.org

Make WordPress Core

Ticket #43516: 43516.default-category-all-cpt.2.patch

File 43516.default-category-all-cpt.2.patch, 3.0 KB (added by enrico.sorcinelli, 6 months ago)
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 500bf54a5b..a7cb035cd7 100644
    function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $appe 
    46794679        // If $post_categories isn't already an array, make it one:
    46804680        $post_categories = (array) $post_categories;
    46814681        if ( empty( $post_categories ) ) {
    4682                 if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
     4682                // 'post' and CPT associated with 'category' taxonomy require at least one category.
     4683                if ( is_object_in_taxonomy( $post_type, 'category' ) && 'auto-draft' !== $post_status ) {
    46834684                        $post_categories = array( get_option( 'default_category' ) );
    46844685                        $append          = false;
    46854686                } else {
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index ca18c41b0e..507da41405 100644
    class Tests_Post extends WP_UnitTestCase { 
    14661466                $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    14671467                $this->assertEqualSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
    14681468        }
     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                $term = wp_insert_term( 'Foo', 'category' );
     1477
     1478                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_category' => array( $term['term_id'] ) ) );
     1479                $post = get_post( $post_id );
     1480                $this->assertInternalType( 'array', $post->post_category );
     1481                $this->assertEquals( $term['term_id'], $post->post_category[0] );
     1482
     1483                // Reset categories.
     1484                wp_set_post_categories( $post_id, array() );
     1485                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1486
     1487                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1488                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt', 'post_category' => array( $term['term_id'] ) ) );
     1489                $post = get_post( $post_id );
     1490
     1491                // Reset categories.
     1492                wp_set_post_categories( $post_id, array() );
     1493                $this->assertInternalType( 'array', $post->post_category );
     1494                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1495        }
     1496
     1497        /**
     1498         * Test that inserting 'post' and CPT associated with 'category' taxonomy has default category.
     1499         *
     1500         * @ticket 43516
     1501         */
     1502        function test_insert_post_cpt_has_default_category () {
     1503                $id = wp_insert_post( array( 'post_title' => 'Foo' ) );
     1504                $post = get_post( $id );
     1505                $this->assertInternalType( 'array', $post->post_category );
     1506                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1507
     1508                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1509                $id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt' ) );
     1510                $post = get_post( $id );
     1511                $this->assertInternalType( 'array', $post->post_category );
     1512                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1513        }
    14691514}