Make WordPress Core

Ticket #51292: 51292.2.diff

File 51292.2.diff, 10.1 KB (added by peterwilsoncc, 6 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index c0e5e1fc70..010c8e0cfc 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' );
     4395                } else {
     4396                        $default_term_id = (int) get_option( 'default_term_' . $taxonomy );
     4397                }
     4398                wp_set_post_terms( $post->ID, $default_term_id, $taxonomy );
     4399        }
     4400
    43784401        $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
    43794402
    43804403        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..eb9545082c 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
     725        /**
     726         * Ensure wp_publish_post does not add default term in error.
     727         *
     728         * @ticket 51292
     729         */
     730        function test_wp_publish_post_respects_current_terms() {
     731                // Create custom taxonomy to test with.
     732                register_taxonomy(
     733                        'tax_51292',
     734                        'post',
     735                        array(
     736                                'hierarchical' => true,
     737                                'public'       => true,
     738                                'default_term' => array(
     739                                        'name' => 'Default 51292',
     740                                        'slug' => 'default-51292',
     741                                ),
     742                        )
     743                );
     744
     745                $post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
     746                $term_id = $this->factory->term->create( array( 'taxonomy' => 'tax_51292' ) );
     747                wp_set_object_terms( $post_id, array( $term_id ), 'tax_51292' );
     748                wp_publish_post( $post_id );
     749
     750                $post_terms = get_the_terms( $post_id, 'tax_51292' );
     751                $this->assertNotEmpty( $post_terms );
     752                $this->assertCount( 1, $post_terms );
     753                $this->assertSame( $term_id, $post_terms[0]->term_id );
     754        }
     755
     756        /**
     757         * Ensure wp_publish_post adds default term.
     758         *
     759         * @ticket 51292
     760         */
     761        function test_wp_publish_post_adds_default_term() {
     762                // Create custom taxonomy to test with.
     763                register_taxonomy(
     764                        'tax_51292',
     765                        'post',
     766                        array(
     767                                'hierarchical' => true,
     768                                'public'       => true,
     769                                'default_term' => array(
     770                                        'name' => 'Default 51292',
     771                                        'slug' => 'default-51292',
     772                                ),
     773                        )
     774                );
     775
     776                $post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) );
     777
     778                wp_publish_post( $post_id );
     779
     780                $post_terms = get_the_terms( $post_id, 'tax_51292' );
     781                $this->assertNotEmpty( $post_terms );
     782                $this->assertCount( 1, $post_terms );
     783                $this->assertSame( get_term_by( 'slug', 'default-51292', 'tax_51292' )->term_id, $post_terms[0]->term_id );
     784        }
     785
    678786        /**
    679787         * @ticket 23708
    680788         */
  • 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..a2b1396663
    - +  
     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 increments correctly when post status becomes published.
     28         *
     29         * @dataProvider data_term_counts_incremented_on_publish
     30         * @ticket 40351
     31         * @ticket 51292
     32         *
     33         * @param string $original_post_status Post status prior to change to publish.
     34         * @param int    $change               Expected change upon publish.
     35         */
     36        public function test_term_counts_incremented_on_publish( $original_post_status, $change ) {
     37                $post_id    = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     38                $term_count = get_term( get_option( 'default_category' ) )->count;
     39
     40                wp_publish_post( $post_id );
     41
     42                $expected = $term_count + $change;
     43                $this->assertSame( $expected, get_term( get_option( 'default_category' ) )->count );
     44        }
     45
     46        /**
     47         * Data provider for test_term_count_changes_for_post_statuses.
     48         *
     49         * @return array[] {
     50         *     @type string $original_post_status Post status prior to change to publish.
     51         *     @type int    $change               Expected change upon publish.
     52         * }
     53         */
     54        function data_term_counts_incremented_on_publish() {
     55                return array(
     56                        // 0. Published post
     57                        array( 'publish', 0 ),
     58                        // 1. Auto draft
     59                        array( 'auto-draft', 1 ),
     60                        // 2. Draft
     61                        array( 'draft', 1 ),
     62                        // 3. Private post
     63                        array( 'private', 1 ),
     64                );
     65        }
     66
     67        /**
     68         * Term counts increments correctly when post status becomes published.
     69         *
     70         * @dataProvider data_term_counts_incremented_on_publish_with_attachments
     71         * @ticket 40351
     72         * @ticket 51292
     73         *
     74         * @param string $original_post_status Post status prior to change to publish.
     75         * @param int    $change               Expected change upon publish.
     76         */
     77        public function test_term_counts_incremented_on_publish_with_attachments( $original_post_status, $change ) {
     78                $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     79                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     80                $attachment_id = self::factory()->attachment->create_object(
     81                        array(
     82                                'file'        => 'image.jpg',
     83                                'post_parent' => $post_id,
     84                                'post_status' => 'inherit',
     85                        )
     86                );
     87                wp_add_object_terms( $attachment_id, self::$attachment_term, 'wp_test_tax_counts' );
     88                $term_count = get_term( self::$attachment_term )->count;
     89
     90                wp_publish_post( $post_id );
     91
     92                $expected = $term_count + $change;
     93                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     94        }
     95
     96        /**
     97         * Data provider for test_term_count_changes_for_post_statuses_with_attachments.
     98         *
     99         * @return array[] {
     100         *     @type string $original_post_status Post status prior to change to publish.
     101         *     @type int    $change               Expected change upon publish.
     102         * }
     103         */
     104        function data_term_counts_incremented_on_publish_with_attachments() {
     105                return array(
     106                        // 0. Published post
     107                        array( 'publish', 0 ),
     108                        // 1. Auto draft
     109                        array( 'auto-draft', 2 ),
     110                        // 2. Draft
     111                        array( 'draft', 2 ),
     112                        // 3. Private post
     113                        array( 'private', 2 ),
     114                );
     115        }
     116
     117        /**
     118         * Term counts increments correctly when post status becomes published.
     119         *
     120         * @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments
     121         * @ticket 40351
     122         * @ticket 51292
     123         *
     124         * @param string $original_post_status Post status prior to change to publish.
     125         * @param int    $change               Expected change upon publish.
     126         */
     127        public function test_term_counts_incremented_on_publish_with_untermed_attachments( $original_post_status, $change ) {
     128                $post_id = self::factory()->post->create( array( 'post_status' => $original_post_status ) );
     129                wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
     130                $attachment_id = self::factory()->attachment->create_object(
     131                        array(
     132                                'file'        => 'image.jpg',
     133                                'post_parent' => $post_id,
     134                                'post_status' => 'inherit',
     135                        )
     136                );
     137                $term_count    = get_term( self::$attachment_term )->count;
     138
     139                wp_publish_post( $post_id );
     140
     141                $expected = $term_count + $change;
     142                $this->assertSame( $expected, get_term( self::$attachment_term )->count );
     143        }
     144
     145        /**
     146         * Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments.
     147         *
     148         * @return array[] {
     149         *     @type string $original_post_status Post status prior to change to publish.
     150         *     @type int    $change               Expected change upon publish.
     151         * }
     152         */
     153        function data_term_counts_incremented_on_publish_with_untermed_attachments() {
     154                return array(
     155                        // 0. Published post
     156                        array( 'publish', 0 ),
     157                        // 1. Auto draft
     158                        array( 'auto-draft', 1 ),
     159                        // 2. Draft
     160                        array( 'draft', 1 ),
     161                        // 3. Private post
     162                        array( 'private', 1 ),
     163                );
     164        }
     165}