WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/10/2015 03:38:41 AM (6 years ago)
Author:
boonebgorges
Message:

Return WP_Post objects from wp_get_object_terms().

A side effect of this change is that terms stored in the cache no longer have
an object_id associated with them. Previously, object_id had always been
cached when the term cache was populated via wp_get_object_terms(), a
strategy that was mostly harmless but still incorrect.

See #14162.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r34529 r34999  
    499499    }
    500500
     501    /**
     502     * @ticket 14162
     503     */
     504    public function test_should_return_wp_term_objects_for_fields_all() {
     505        register_taxonomy( 'wptests_tax', 'post' );
     506        $p = $this->factory->post->create();
     507        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     508        wp_set_object_terms( $p, $t, 'wptests_tax' );
     509
     510        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     511            'fields' => 'all',
     512        ) );
     513
     514        $this->assertNotEmpty( $found );
     515        foreach ( $found as $f ) {
     516            $this->assertInstanceOf( 'WP_Term', $f );
     517        }
     518    }
     519
     520    /**
     521     * @ticket 14162
     522     */
     523    public function test_should_return_wp_term_objects_for_fields_all_with_object_id() {
     524        register_taxonomy( 'wptests_tax', 'post' );
     525        $p = $this->factory->post->create();
     526        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     527        wp_set_object_terms( $p, $t, 'wptests_tax' );
     528
     529        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     530            'fields' => 'all_with_object_id',
     531        ) );
     532
     533        $this->assertNotEmpty( $found );
     534        foreach ( $found as $f ) {
     535            $this->assertInstanceOf( 'WP_Term', $f );
     536        }
     537    }
     538
     539    /**
     540     * @ticket 14162
     541     */
     542    public function test_should_prime_cache_for_found_terms() {
     543        global $wpdb;
     544
     545        register_taxonomy( 'wptests_tax', 'post' );
     546        $p = $this->factory->post->create();
     547        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     548        wp_set_object_terms( $p, $t, 'wptests_tax' );
     549
     550        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     551            'fields' => 'all_with_object_id',
     552        ) );
     553
     554        $num_queries = $wpdb->num_queries;
     555        $term = get_term( $t );
     556        $this->assertSame( $num_queries, $wpdb->num_queries );
     557    }
     558
     559    /**
     560     * @ticket 14162
     561     */
     562    public function test_object_id_should_not_be_cached_with_term_object() {
     563        register_taxonomy( 'wptests_tax', 'post' );
     564        $p = $this->factory->post->create();
     565        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     566        wp_set_object_terms( $p, $t, 'wptests_tax' );
     567
     568        $found = wp_get_object_terms( $p, 'wptests_tax', array(
     569            'fields' => 'all_with_object_id',
     570        ) );
     571
     572        foreach ( $found as $f ) {
     573            $this->assertSame( $p, $f->object_id );
     574        }
     575
     576        $term = get_term( $t );
     577        $this->assertFalse( isset( $term->object_id ) );
     578    }
     579
     580    /**
     581     * @ticket 14162
     582     */
     583    public function test_term_cache_should_be_primed_for_all_taxonomies() {
     584        global $wpdb;
     585
     586        register_taxonomy( 'wptests_tax1', 'post' );
     587        register_taxonomy( 'wptests_tax2', 'post' );
     588        $p = $this->factory->post->create();
     589        $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     590        $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     591        wp_set_object_terms( $p, $t1, 'wptests_tax1' );
     592        wp_set_object_terms( $p, $t2, 'wptests_tax2' );
     593
     594        $found = wp_get_object_terms( $p, array(
     595            'wptests_tax1',
     596            'wptests_tax2',
     597        ), array(
     598            'fields' => 'all_with_object_id',
     599        ) );
     600
     601        $this->assertEqualSets( array( $t1, $t2 ), wp_list_pluck( $found, 'term_id' ) );
     602
     603        $num_queries = $wpdb->num_queries;
     604        $term1 = get_term( $t1 );
     605        $term2 = get_term( $t2 );
     606        $this->assertSame( $num_queries, $wpdb->num_queries );
     607    }
     608
     609    /**
     610     * @ticket 14162
     611     */
     612    public function test_object_id_should_be_set_on_objects_that_share_terms() {
     613        register_taxonomy( 'wptests_tax', 'post' );
     614        $posts = $this->factory->post->create_many( 2 );
     615        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     616        wp_set_object_terms( $posts[0], $t, 'wptests_tax' );
     617        wp_set_object_terms( $posts[1], $t, 'wptests_tax' );
     618
     619        $found = wp_get_object_terms( $posts, 'wptests_tax', array(
     620            'fields' => 'all_with_object_id',
     621        ) );
     622
     623        $this->assertEqualSets( $posts, wp_list_pluck( $found, 'object_id' ) );
     624    }
     625
    501626    public function filter_get_object_terms( $terms ) {
    502627        $term_ids = wp_list_pluck( $terms, 'term_id' );
Note: See TracChangeset for help on using the changeset viewer.