WordPress.org

Make WordPress Core

Ticket #35213: 35213.3.diff

File 35213.3.diff, 6.2 KB (added by obenland, 3 years ago)

Updated WpDeleteTerm tests with stricter assertion

  • src/wp-includes/taxonomy.php

     
    21582158        // Get the term before deleting it or its term relationships so we can pass to actions below.
    21592159        $deleted_term = get_term( $term, $taxonomy );
    21602160
    2161         $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
     2161        $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
    21622162
    2163         foreach ( (array) $objects as $object ) {
    2164                 $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
     2163        foreach ( $object_ids as $object_id ) {
     2164                $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) );
    21652165                if ( 1 == count($terms) && isset($default) ) {
    21662166                        $terms = array($default);
    21672167                } else {
     
    21702170                                $terms = array_merge($terms, array($default));
    21712171                }
    21722172                $terms = array_map('intval', $terms);
    2173                 wp_set_object_terms($object, $terms, $taxonomy);
     2173                wp_set_object_terms( $object_id, $terms, $taxonomy );
    21742174        }
    21752175
    21762176        // Clean the relationship caches for all object types using this term.
    21772177        $tax_object = get_taxonomy( $taxonomy );
    21782178        foreach ( $tax_object->object_type as $object_type )
    2179                 clean_object_term_cache( $objects, $object_type );
     2179                clean_object_term_cache( $object_ids, $object_type );
    21802180
    21812181        $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
    21822182        foreach ( $term_meta_ids as $mid ) {
     
    22122212         * Fires after a term is deleted from the database and the cache is cleaned.
    22132213         *
    22142214         * @since 2.5.0
     2215         * @since 4.5.0 Introduced `$object_ids` argument.
    22152216         *
    22162217         * @param int     $term         Term ID.
    22172218         * @param int     $tt_id        Term taxonomy ID.
    22182219         * @param string  $taxonomy     Taxonomy slug.
    22192220         * @param mixed   $deleted_term Copy of the already-deleted term, in the form specified
    22202221         *                              by the parent function. WP_Error otherwise.
     2222         * @param array   $object_ids   List of term object IDs.
    22212223         */
    2222         do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
     2224        do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids );
    22232225
    22242226        /**
    22252227         * Fires after a term in a specific taxonomy is deleted.
     
    22282230         * taxonomy the term belonged to.
    22292231         *
    22302232         * @since 2.3.0
     2233         * @since 4.5.0 Introduced `$object_ids` argument.
    22312234         *
    22322235         * @param int     $term         Term ID.
    22332236         * @param int     $tt_id        Term taxonomy ID.
    22342237         * @param mixed   $deleted_term Copy of the already-deleted term, in the form specified
    22352238         *                              by the parent function. WP_Error otherwise.
     2239         * @param array   $object_ids   List of term object IDs.
    22362240         */
    2237         do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
     2241        do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term, $object_ids );
    22382242
    22392243        return true;
    22402244}
  • tests/phpunit/tests/term/wpDeleteTerm.php

     
    55 */
    66class Tests_Term_WpDeleteTerm extends WP_UnitTestCase {
    77        protected $deleted_term;
     8        protected $object_ids;
    89
    910        /**
    1011         * @ticket 33485
     12         * @ticket 35213
    1113         */
    1214        public function test_count_property_passed_to_filters_should_reflect_pre_deleted_term() {
    1315                register_taxonomy( 'wptests_tax', 'post' );
     
    1618                        'taxonomy' => 'wptests_tax',
    1719                ) );
    1820
    19                 $p = self::factory()->post->create();
     21                $post_id = self::factory()->post->create();
    2022
    21                 wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax' );
     23                wp_set_object_terms( $post_id, array( $terms[0] ), 'wptests_tax' );
    2224
    23                 add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 4 );
     25                add_action( 'delete_term', array( $this, 'catch_deleted_term' ), 10, 5 );
    2426
    2527                wp_delete_term( $terms[0], 'wptests_tax' );
    2628                $this->assertEquals( 1, $this->deleted_term->count );
     29                $this->assertSame( $this->object_ids, array( "$post_id" ) );
    2730
    2831                wp_delete_term( $terms[1], 'wptests_tax' );
    2932                $this->assertEquals( 0, $this->deleted_term->count );
     33                $this->assertSame( $this->object_ids, array() );
    3034        }
    3135
    32         public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term ) {
     36        public function catch_deleted_term( $term_id, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
    3337                $this->deleted_term = $deleted_term;
     38                $this->object_ids = $object_ids;
    3439        }
    3540}
  • tests/phpunit/tests/term/wpInsertTerm.php

     
    3737                $this->assertTrue( term_exists($t['term_id']) > 0 );
    3838
    3939                // now delete it
    40                 add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
     40                add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
    4141                $this->assertTrue( wp_delete_term( $t['term_id'], $taxonomy ) );
    42                 remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
     42                remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
    4343                $this->assertNull( term_exists($term) );
    4444                $this->assertNull( term_exists($t['term_id']) );
    4545                $this->assertEquals( $initial_count, wp_count_terms( $taxonomy ) );
     
    644644
    645645        /** Helpers **********************************************************/
    646646
    647         public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) {
     647        public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
    648648                $this->assertInternalType( 'object', $deleted_term );
    649649                $this->assertInternalType( 'int', $term );
     650                $this->assertInternalType( 'array', $object_ids );
    650651                // Pesky string $this->assertInternalType( 'int', $tt_id );
    651652                $this->assertEquals( $term, $deleted_term->term_id );
    652653                $this->assertEquals( $taxonomy, $deleted_term->taxonomy );
    653654                $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id );
     655                $this->assertEmpty( $object_ids );
    654656        }
    655657
    656658        public function _pre_insert_term_callback() {