Make WordPress Core

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

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

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 39e1891..45e8e05 100644
    a b function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $appe 
    41684168        // If $post_categories isn't already an array, make it one:
    41694169        $post_categories = (array) $post_categories;
    41704170        if ( empty( $post_categories ) ) {
    4171                 if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
     4171                // 'post' and CPT associated with 'category' taxonomy require at least one category.
     4172                if ( is_object_in_taxonomy( $post_type, 'category' ) && 'auto-draft' != $post_status ) {
    41724173                        $post_categories = array( get_option( 'default_category' ) );
    41734174                        $append          = false;
    41744175                } else {
  • tests/phpunit/tests/post.php

    diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
    index c0b20bb..a88fbd0 100644
    a b class Tests_Post extends WP_UnitTestCase { 
    13541354                $this->assertEquals( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    13551355        }
    13561356
     1357
     1358        /**
     1359        * Test that setting void category for CPT associated with 'category' taxonomy has default category.
     1360        *
     1361        * @ticket 43516
     1362        */
     1363        function test_set_post_category_cpt_default_category () {
     1364                $term = wp_insert_term( 'Foo', 'category' );
     1365
     1366                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_category' => array( $term['term_id'] ) ) );
     1367                $post = get_post( $post_id );
     1368                $this->assertInternalType( 'array', $post->post_category );
     1369                $this->assertEquals( $term['term_id'], $post->post_category[0] );
     1370
     1371                // Reset categories
     1372                wp_set_post_categories( $post_id, array() );
     1373                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1374
     1375                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1376                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt', 'post_category' => array( $term['term_id'] ) ) );
     1377                $post = get_post( $post_id );
     1378
     1379                // Reset categories
     1380                wp_set_post_categories( $post_id, array() );
     1381                $this->assertInternalType( 'array', $post->post_category );
     1382                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1383        }
     1384
     1385
     1386        /**
     1387         * Test that inserting 'post' and CPT associated with 'category' taxonomy has default category.
     1388         *
     1389         * @ticket 43516
     1390         */
     1391        function test_insert_post_cpt_has_default_category () {
     1392                $id = wp_insert_post( array( 'post_title' => 'Foo' ) );
     1393                $post = get_post( $id );
     1394                $this->assertInternalType( 'array', $post->post_category );
     1395                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1396
     1397                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1398                $id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt' ) );
     1399                $post = get_post( $id );
     1400                $this->assertInternalType( 'array', $post->post_category );
     1401                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1402
     1403        }
    13571404}