Make WordPress Core

Ticket #51292: 51292.3.diff

File 51292.3.diff, 11.0 KB (added by peterwilsoncc, 3 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 1088d4ffb5..9456bec763 100644
    a b function wp_publish_post( $post ) { 
    43754375                return;
    43764376        }
    43774377
     4378        // Ensure at least one term is applied for taxonomies with a default term.
     4379        foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
     4380                // Skip taxonomy if no default term is set.
     4381                if (
     4382                        'category' !== $taxonomy &&
     4383                        empty( $tax_object->default_term )
     4384                ) {
     4385                        continue;
     4386                }
     4387
     4388                // Do not modify previously set terms.
     4389                if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
     4390                        continue;
     4391                }
     4392
     4393                if ( 'category' === $taxonomy ) {
     4394                        $default_term_id = (int) get_option( 'default_category', 0 );
     4395                } else {
     4396                        $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
     4397                }
     4398
     4399                if ( ! $default_term_id ) {
     4400                        continue;
     4401                }
     4402                wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
     4403        }
     4404
    43784405        $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
    43794406
    43804407        clean_post_cache( $post->ID );
  • new file tests/phpunit/tests/post/wpPublishPosts.php

    diff --git a/tests/phpunit/tests/post/wpPublishPosts.php b/tests/phpunit/tests/post/wpPublishPosts.php
    new file mode 100644
    index 0000000000..42e3884d94
    - +  
     1<?php
     2
     3/**
     4 * @group post
     5 */
     6class Tests_WPPublishPost extends WP_UnitTestCase {
     7
     8        /**
     9         * Auto-draft post ID.
     10         *
     11         * @var int
     12         */
     13        public static $auto_draft_id;
     14
     15        /**
     16         * Create shared fixtures.
     17         *
     18         * @param WP_UnitTest_Factory $factory Test suite factory.
     19         */
     20        public static function wpSetUpBeforeClass( $factory ) {
     21                self::$auto_draft_id = $factory->post->create( array( 'post_status' => 'auto-draft' ) );
     22        }
     23
     24        /**
     25         * Ensure wp_publish_post does not add default category in error.
     26         *
     27         * @ticket 51292
     28         */
     29        function test_wp_publish_post_respects_current_categories() {
     30                $post_id     = self::$auto_draft_id;
     31                $category_id = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
     32                wp_set_post_categories( $post_id, $category_id );
     33                wp_publish_post( $post_id );
     34
     35                $post_categories = get_the_category( $post_id );
     36                $this->assertCount( 1, $post_categories );
     37                $this->assertSame(
     38                        $category_id,
     39                        $post_categories[0]->term_id,
     40                        'wp_publish_post replaced set category.'
     41                );
     42        }
     43
     44        /**
     45         * Ensure wp_publish_post adds default category.
     46         *
     47         * @covers wp_publish_post
     48         * @ticket 51292
     49         */
     50        function test_wp_publish_post_adds_default_category() {
     51                $post_id = self::$auto_draft_id;
     52
     53                wp_publish_post( $post_id );
     54
     55                $post_categories = get_the_category( $post_id );
     56                $this->assertCount( 1, $post_categories );
     57                $this->assertSame(
     58                        (int) get_option( 'default_category' ),
     59                        $post_categories[0]->term_id,
     60                        'wp_publish_post failed to add default category.'
     61                );
     62        }
     63
     64        /**
     65         * Ensure wp_publish_post adds default category when tagged.
     66         *
     67         * @covers wp_publish_post
     68         * @ticket 51292
     69         */
     70        function test_wp_publish_post_adds_default_category_when_tagged() {
     71                $post_id = self::$auto_draft_id;
     72                $tag_id  = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) );
     73                wp_set_post_tags( $post_id, array( $tag_id ) );
     74                wp_publish_post( $post_id );
     75
     76                $post_categories = get_the_category( $post_id );
     77                $this->assertCount( 1, $post_categories );
     78                $this->assertSame(
     79                        (int) get_option( 'default_category' ),
     80                        $post_categories[0]->term_id,
     81                        'wp_publish_post failed to add default category.'
     82                );
     83        }
     84
     85        /**
     86         * Ensure wp_publish_post does not add default term in error.
     87         *
     88         * @covers wp_publish_post
     89         * @ticket 51292
     90         */
     91        function test_wp_publish_post_respects_current_terms() {
     92                // Create custom taxonomy to test with.
     93                register_taxonomy(
     94                        'tax_51292',
     95                        'post',
     96                        array(
     97                                'hierarchical' => true,
     98                                'public'       => true,
     99                                'default_term' => array(
     100                                        'name' => 'Default 51292',
     101                                        'slug' => 'default-51292',
     102                                ),
     103                        )
     104                );
     105
     106                $post_id = self::$auto_draft_id;
     107                $term_id = $this->factory->term->create( array( 'taxonomy' => 'tax_51292' ) );
     108                wp_set_object_terms( $post_id, array( $term_id ), 'tax_51292' );
     109                wp_publish_post( $post_id );
     110
     111                $post_terms = get_the_terms( $post_id, 'tax_51292' );
     112                $this->assertCount( 1, $post_terms );
     113                $this->assertSame(
     114                        $term_id,
     115                        $post_terms[0]->term_id,
     116                        'wp_publish_post replaced set term for custom taxonomy.'
     117                );
     118        }
     119
     120        /**
     121         * Ensure wp_publish_post adds default term.
     122         *
     123         * @covers wp_publish_post
     124         * @ticket 51292
     125         */
     126        function test_wp_publish_post_adds_default_term() {
     127                // Create custom taxonomy to test with.
     128                register_taxonomy(
     129                        'tax_51292',
     130                        'post',
     131                        array(
     132                                'hierarchical' => true,
     133                                'public'       => true,
     134                                'default_term' => array(
     135                                        'name' => 'Default 51292',
     136                                        'slug' => 'default-51292',
     137                                ),
     138                        )
     139                );
     140
     141                $post_id = self::$auto_draft_id;
     142
     143                wp_publish_post( $post_id );
     144
     145                $post_terms = get_the_terms( $post_id, 'tax_51292' );
     146                $this->assertCount( 1, $post_terms );
     147                $this->assertSame(
     148                        get_term_by( 'slug', 'default-51292', 'tax_51292' )->term_id,
     149                        $post_terms[0]->term_id,
     150                        'wp_publish_post failed to add default term for custom taxonomy.'
     151                );
     152        }
     153}
  • new file tests/phpunit/tests/term/termCounts.php

    diff --git a/tests/phpunit/tests/term/termCounts.php b/tests/phpunit/tests/term/termCounts.php
    new file mode 100644
    index 0000000000..284d5f90ea
    - +  
     1<?php
     2
     3/**
     4 * @group taxonomy
     5 */
     6class Tests_Term_termCount extends WP_UnitTestCase {
     7
     8        /**
     9         * Term ID for testing attachment counts.
     10         *
     11         * @var int
     12         */
     13        public static $attachment_term;
     14
     15        /**
     16         * Post IDs of shared posts.
     17         *
     18         * @var int[]
     19         */
     20        public static $post_ids;
     21
     22        /**
     23         * Create shared fixtures.
     24         *
     25         * @param WP_UnitTest_Factory $factory Test suite factory.
     26         */
     27        public static function wpSetUpBeforeClass( $factory ) {
     28                $statuses = array( 'publish', 'auto-draft', 'draft', 'private' );
     29                foreach ( $statuses as $status ) {
     30                        self::$post_ids[ $status ] = $factory->post->create( array( 'post_status' => $status ) );
     31                }
     32
     33                register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
     34                self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) );
     35        }
     36
     37        public function setUp() {
     38                parent::setUp();
     39
     40                register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
     41        }
     42
     43        /**
     44         * Term counts increments correctly when post status becomes published.
     45         *
     46         * @covers wp_publish_post
     47         * @covers wp_count_terms
     48         * @dataProvider data_term_counts_incremented_on_publish
     49         * @ticket 40351
     50         * @ticket 51292
     51         *
     52         * @param string $original_post_status Post status prior to change to publish.
     53         * @param int    $change               Expected change upon publish.
     54         */
     55        public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
     56                $post_id    = self::$post_ids[ $original_post_status ];
     57                $term_count = get_term( get_option( 'default_category' ) )->count;
     58
     59                wp_publish_post( $post_id );
     60
     61                $expected = $term_count + $change;
     62                $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
     63        }
     64
     65        /**
     66         * Data provider for test_term_count_changes_for_post_statuses.
     67         *
     68         * @return array[] {
     69         *     @type string $original_post_status Post status prior to change to publish.
     70         *     @type int    $change               Expected change upon publish.
     71         * }
     72         */
     73        function data_term_counts_incremented_on_publish() {
     74                return array(
     75                        // 0. Published post
     76                        array( 'publish', 0 ),
     77                        // 1. Auto draft
     78                        array( 'auto-draft', 1 ),
     79                        // 2. Draft
     80                        array( 'draft', 1 ),
     81                        // 3. Private post
     82                        array( 'private', 1 ),
     83                );
     84        }
     85
     86        /**
     87         * Term counts increments correctly when post status becomes published.
     88         *
     89         * @covers wp_publish_post
     90         * @dataProvider data_term_counts_incremented_on_publish_with_attachments
     91         * @ticket 40351
     92         * @ticket 51292
     93         *
     94         * @param string $original_post_status Post status prior to change to publish.
     95         * @param int    $change               Expected change upon publish.
     96         */
     97        public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
     98                $post_id = self::$post_ids[ $original_post_status ];
     99                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     100                $attachment_id = self::factory()->attachment->create_object(
     101                        array(
     102                                'file'        => 'image.jpg',
     103                                'post_parent' => $post_id,
     104                                'post_status' => 'inherit',
     105                        )
     106                );
     107                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     108                $term_count = get_term( self::$attachment_term )->count;
     109
     110                wp_publish_post( $post_id );
     111
     112                $expected = $term_count + $change;
     113                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     114        }
     115
     116        /**
     117         * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     118         *
     119         * @return array[] {
     120         *     @type string $original_post_status Post status prior to change to publish.
     121         *     @type int    $change               Expected change upon publish.
     122         * }
     123         */
     124        function data_term_counts_incremented_on_publish_with_attachments() {
     125                return array(
     126                        // 0. Published post
     127                        array( 'publish', 0 ),
     128                        // 1. Auto draft
     129                        array( 'auto-draft', 2 ),
     130                        // 2. Draft
     131                        array( 'draft', 2 ),
     132                        // 3. Private post
     133                        array( 'private', 2 ),
     134                );
     135        }
     136
     137        /**
     138         * Term counts increments correctly when post status becomes published.
     139         *
     140         * @covers wp_publish_post
     141         * @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments
     142         * @ticket 40351
     143         * @ticket 51292
     144         *
     145         * @param string $original_post_status Post status prior to change to publish.
     146         * @param int    $change               Expected change upon publish.
     147         */
     148        public function test_term_counts_incremented_on_publish_with_untermed_attachments( $original_post_status, $change ) {
     149                $post_id = self::$post_ids[ $original_post_status ];
     150                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     151                $attachment_id = self::factory()->attachment->create_object(
     152                        array(
     153                                'file'        => 'image.jpg',
     154                                'post_parent' => $post_id,
     155                                'post_status' => 'inherit',
     156                        )
     157                );
     158                $term_count    = get_term( self::$attachment_term )->count;
     159
     160                wp_publish_post( $post_id );
     161
     162                $expected = $term_count + $change;
     163                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     164        }
     165
     166        /**
     167         * Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments.
     168         *
     169         * @return array[] {
     170         *     @type string $original_post_status Post status prior to change to publish.
     171         *     @type int    $change               Expected change upon publish.
     172         * }
     173         */
     174        function data_term_counts_incremented_on_publish_with_untermed_attachments() {
     175                return array(
     176                        // 0. Published post
     177                        array( 'publish', 0 ),
     178                        // 1. Auto draft
     179                        array( 'auto-draft', 1 ),
     180                        // 2. Draft
     181                        array( 'draft', 1 ),
     182                        // 3. Private post
     183                        array( 'private', 1 ),
     184                );
     185        }
     186}