Make WordPress Core

Ticket #51292: 51292.diff

File 51292.diff, 12.7 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 c0e5e1fc70..073b4aeddc 100644
    a b function wp_publish_post( $post ) { 
    43754375                return;
    43764376        }
    43774377
     4378        // A published 'post' requires at least one category.
     4379        if (
     4380                'post' === $post->post_type &&
     4381                empty( get_the_category( $post->ID ) )
     4382        ) {
     4383                wp_set_post_categories( $post->ID, get_option( 'default_category' ) );
     4384        }
     4385
    43784386        $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
    43794387
    43804388        clean_post_cache( $post->ID );
  • tests/phpunit/tests/post.php

    diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
    index bacf9b23c5..aa0414ebb1 100644
    a b function test_wp_publish_post_and_avoid_content_filtering() { 
    675675                kses_remove_filters();
    676676        }
    677677
     678        /**
     679         * Ensure wp_publish_post does not add default category in error.
     680         *
     681         * @ticket 51292
     682         */
     683        function test_wp_publish_post_respects_current_categories() {
     684                $post_id     = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
     685                $category_id = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
     686                wp_set_post_categories( $post_id, $category_id );
     687                wp_publish_post( $post_id );
     688
     689                $post_categories = get_the_category( $post_id );
     690                $this->assertCount( 1, $post_categories );
     691                $this->assertSame( $category_id, $post_categories[0]->term_id );
     692        }
     693
     694        /**
     695         * Ensure wp_publish_post adds default category.
     696         *
     697         * @ticket 51292
     698         */
     699        function test_wp_publish_post_adds_default_category() {
     700                $post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
     701
     702                wp_publish_post( $post_id );
     703
     704                $post_categories = get_the_category( $post_id );
     705                $this->assertCount( 1, $post_categories );
     706                $this->assertSame( (int) get_option( 'default_category' ), $post_categories[0]->term_id );
     707        }
     708
     709        /**
     710         * Ensure wp_publish_post adds default category when tagged.
     711         *
     712         * @ticket 51292
     713         */
     714        function test_wp_publish_post_adds_default_category_when_tagged() {
     715                $post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
     716                $tag_id  = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) );
     717                wp_set_post_tags( $post_id, array( $tag_id ) );
     718                wp_publish_post( $post_id );
     719
     720                $post_categories = get_the_category( $post_id );
     721                $this->assertCount( 1, $post_categories );
     722                $this->assertSame( (int) get_option( 'default_category' ), $post_categories[0]->term_id );
     723        }
     724
    678725        /**
    679726         * @ticket 23708
    680727         */
  • 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..4836799333
    - +  
     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        public static function wpSetUpBeforeClass( $factory ) {
     16                register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
     17                self::$attachment_term = $factory->term->create( array( 'taxonomy' => 'wp_test_tax_counts' ) );
     18        }
     19
     20        public function setUp() {
     21                parent::setUp();
     22
     23                register_taxonomy( 'wp_test_tax_counts', array( 'post', 'attachment' ) );
     24        }
     25
     26        /**
     27         * Term counts are not double incremented when post created.
     28         *
     29         * @dataProvider data_term_count_changes_for_post_statuses
     30         * @ticket 40351
     31         * @ticket 51292
     32         */
     33        public function test_term_count_changes_for_post_statuses( $post_status, $change ) {
     34                $term_count = get_term( get_option( 'default_category' ) )->count;
     35                $post_id    = self::factory()->post->create( array( 'post_status' => $post_status ) );
     36
     37                $expected = $term_count + $change;
     38                $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
     39        }
     40
     41        /**
     42         * Data provider for test_term_count_changes_for_post_statuses.
     43         *
     44         * @return array[] {
     45         *     @type string $post_status New post status.
     46         *     @type int    $change      Expected change.
     47         * }
     48         */
     49        function data_term_count_changes_for_post_statuses() {
     50                return array(
     51                        // 0. Published post
     52                        array( 'publish', 1 ),
     53                        // 1. Auto draft
     54                        array( 'auto-draft', 0 ),
     55                        // 2. Draft
     56                        array( 'draft', 0 ),
     57                        // 3. Private post
     58                        array( 'private', 0 ),
     59                );
     60        }
     61
     62        /**
     63         * Term counts increments correctly when post status becomes published.
     64         *
     65         * @dataProvider data_term_counts_incremented_on_publish
     66         * @ticket 40351
     67         * @ticket 51292
     68         */
     69        public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
     70                $post_id    = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     71                $term_count = get_term( get_option( 'default_category' ) )->count;
     72
     73                wp_publish_post( $post_id );
     74
     75                $expected = $term_count + $change;
     76                $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
     77        }
     78
     79        /**
     80         * Data provider for test_term_count_changes_for_post_statuses.
     81         *
     82         * @return array[] {
     83         *     @type string $original_post_status Post status prior to change to publish.
     84         *     @type int    $change               Expected change upon publish.
     85         * }
     86         */
     87        function data_term_counts_incremented_on_publish() {
     88                return array(
     89                        // 0. Published post
     90                        array( 'publish', 0 ),
     91                        // 1. Auto draft
     92                        array( 'auto-draft', 1 ),
     93                        // 2. Draft
     94                        array( 'draft', 1 ),
     95                        // 3. Private post
     96                        array( 'private', 1 ),
     97                );
     98        }
     99
     100        /**
     101         * Test post status transition update term counts correctly.
     102         *
     103         * @dataProvider data_term_count_transitions_update_term_counts
     104         * @ticket 40351
     105         * @ticket 51292
     106         */
     107        function test_term_count_transitions_update_term_counts( $original_post_status, $new_post_status, $change ) {
     108                $post_id    = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     109                $term_count = get_term( get_option( 'default_category' ) )->count;
     110
     111                wp_update_post(
     112                        array(
     113                                'ID'          => $post_id,
     114                                'post_status' => $new_post_status,
     115                        )
     116                );
     117
     118                $expected = $term_count + $change;
     119                $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
     120        }
     121
     122        /**
     123         * Data provider for test_term_count_transitions_update_term_counts.
     124         *
     125         * @return array[] {
     126         *     @type string $original_post_status Post status upon create.
     127         *     @type string $new_post_status      Post status after update.
     128         *     @type int    $change               Expected change upon publish.
     129         * }
     130         */
     131        function data_term_count_transitions_update_term_counts() {
     132                return array(
     133                        // 0. Draft -> published post
     134                        array( 'draft', 'publish', 1 ),
     135                        // 1. Auto draft -> published post
     136                        array( 'auto-draft', 'publish', 1 ),
     137                        // 2. Private -> published post
     138                        array( 'private', 'publish', 1 ),
     139                        // 3. Published -> published post
     140                        array( 'publish', 'publish', 0 ),
     141
     142                        // 4. Draft -> private post
     143                        array( 'draft', 'private', 0 ),
     144                        // 5. Auto draft -> private post
     145                        array( 'auto-draft', 'private', 0 ),
     146                        // 6. Private -> private post
     147                        array( 'private', 'private', 0 ),
     148                        // 7. Published -> private post
     149                        array( 'publish', 'private', -1 ),
     150
     151                        // 8. Draft -> draft post
     152                        array( 'draft', 'draft', 0 ),
     153                        // 9. Auto draft -> draft post
     154                        array( 'auto-draft', 'draft', 0 ),
     155                        // 10. Private -> draft post
     156                        array( 'private', 'draft', 0 ),
     157                        // 11. Published -> draft post
     158                        array( 'publish', 'draft', -1 ),
     159                );
     160        }
     161
     162        /**
     163         * Term counts are not double incremented when post created.
     164         *
     165         * @dataProvider data_term_count_changes_for_post_statuses_with_attachments
     166         * @ticket 40351
     167         */
     168        public function test_term_count_changes_for_post_statuses_with_attachments( $post_status, $change ) {
     169                $term_count = get_term( self::$attachment_term )->count;
     170                $post_id    = self::factory()->post->create( array( 'post_status' => $post_status ) );
     171                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     172                $attachment_id = self::factory()->attachment->create_object(
     173                        array(
     174                                'file'        => 'image.jpg',
     175                                'post_parent' => $post_id,
     176                                'post_status' => 'inherit',
     177                        )
     178                );
     179                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     180
     181                $expected = $term_count + $change;
     182                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     183        }
     184
     185        /**
     186         * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     187         *
     188         * @return array[] {
     189         *     @type string $post_status New post status.
     190         *     @type int    $change      Expected change.
     191         * }
     192         */
     193        function data_term_count_changes_for_post_statuses_with_attachments() {
     194                return array(
     195                        // 0. Published post
     196                        array( 'publish', 2 ),
     197                        // 1. Auto draft
     198                        array( 'auto-draft', 0 ),
     199                        // 2. Draft
     200                        array( 'draft', 0 ),
     201                        // 3. Private post
     202                        array( 'private', 0 ),
     203                );
     204        }
     205
     206        /**
     207         * Term counts increments correctly when post status becomes published.
     208         *
     209         * @dataProvider data_term_counts_incremented_on_publish_with_attachments
     210         * @ticket 40351
     211         */
     212        public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
     213                $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     214                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     215                $attachment_id = self::factory()->attachment->create_object(
     216                        array(
     217                                'file'        => 'image.jpg',
     218                                'post_parent' => $post_id,
     219                                'post_status' => 'inherit',
     220                        )
     221                );
     222                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     223                $term_count = get_term( self::$attachment_term )->count;
     224
     225                wp_publish_post( $post_id );
     226
     227                $expected = $term_count + $change;
     228                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     229        }
     230
     231        /**
     232         * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     233         *
     234         * @return array[] {
     235         *     @type string $original_post_status Post status prior to change to publish.
     236         *     @type int    $change               Expected change upon publish.
     237         * }
     238         */
     239        function data_term_counts_incremented_on_publish_with_attachments() {
     240                return array(
     241                        // 0. Published post
     242                        array( 'publish', 0 ),
     243                        // 1. Auto draft
     244                        array( 'auto-draft', 2 ),
     245                        // 2. Draft
     246                        array( 'draft', 2 ),
     247                        // 3. Private post
     248                        array( 'private', 2 ),
     249                );
     250        }
     251
     252        /**
     253         * Test post status transition update term counts correctly.
     254         *
     255         * @dataProvider data_term_count_transitions_update_term_counts_with_attachments
     256         * @ticket 40351
     257         */
     258        function test_term_count_transitions_update_term_counts_with_attachments( $original_post_status, $new_post_status, $change ) {
     259                $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     260                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     261                $attachment_id = self::factory()->attachment->create_object(
     262                        array(
     263                                'file'        => 'image.jpg',
     264                                'post_parent' => $post_id,
     265                                'post_status' => 'inherit',
     266                        )
     267                );
     268                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     269                $term_count = get_term( self::$attachment_term )->count;
     270
     271                wp_update_post(
     272                        array(
     273                                'ID'          => $post_id,
     274                                'post_status' => $new_post_status,
     275                        )
     276                );
     277
     278                $expected = $term_count + $change;
     279                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     280        }
     281
     282        /**
     283         * Data provider for test_term_count_transitions_update_term_counts_with_attachments.
     284         *
     285         * @return array[] {
     286         *     @type string $original_post_status Post status upon create.
     287         *     @type string $new_post_status      Post status after update.
     288         *     @type int    $change               Expected change upon publish.
     289         * }
     290         */
     291        function data_term_count_transitions_update_term_counts_with_attachments() {
     292                return array(
     293                        // 0. Draft -> published post
     294                        array( 'draft', 'publish', 2 ),
     295                        // 1. Auto draft -> published post
     296                        array( 'auto-draft', 'publish', 2 ),
     297                        // 2. Private -> published post
     298                        array( 'private', 'publish', 2 ),
     299                        // 3. Published -> published post
     300                        array( 'publish', 'publish', 0 ),
     301
     302                        // 4. Draft -> private post
     303                        array( 'draft', 'private', 0 ),
     304                        // 5. Auto draft -> private post
     305                        array( 'auto-draft', 'private', 0 ),
     306                        // 6. Private -> private post
     307                        array( 'private', 'private', 0 ),
     308                        // 7. Published -> private post
     309                        array( 'publish', 'private', -2 ),
     310
     311                        // 8. Draft -> draft post
     312                        array( 'draft', 'draft', 0 ),
     313                        // 9. Auto draft -> draft post
     314                        array( 'auto-draft', 'draft', 0 ),
     315                        // 10. Private -> draft post
     316                        array( 'private', 'draft', 0 ),
     317                        // 11. Published -> draft post
     318                        array( 'publish', 'draft', -2 ),
     319                );
     320        }
     321}