mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object. Cache support and sanitization filters for individual terms are now more centralized. For example, `get_term_by()` is able to cast results of its query to a `WP_Term` object by passing it through `get_term()`. The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to be unique to a taxonomy (ie, shared terms no longer exist). In cases where `get_term()` detects that the term matching the specified term_id is from the wrong taxonomy, it checks to see if you've requested a shared term, and if so, it splits the term. This is used only for fallback purposes. The elimination of shared terms allows the caching strategy for terms to be simplified. Individual terms are now cached in a single 'terms' bucket. Props flixos90, boonebgorges, scribu, dipesh.kakadiya. See #14162. git-svn-id: https://develop.svn.wordpress.org/trunk@34997 602fd350-edb4-49c9-b593-d223f7449a82
110 lines
3.6 KiB
PHP
110 lines
3.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group taxonomy
|
|
*/
|
|
class Tests_Term_GetTerm extends WP_UnitTestCase {
|
|
public function setUp() {
|
|
parent::setUp();
|
|
register_taxonomy( 'wptests_tax', 'post' );
|
|
}
|
|
|
|
public function test_should_return_error_for_empty_term() {
|
|
$found = get_term( '', 'wptests_tax' );
|
|
$this->assertWPError( $found );
|
|
$this->assertSame( 'invalid_term', $found->get_error_code() );
|
|
}
|
|
|
|
public function test_should_return_error_for_invalid_taxonomy() {
|
|
$found = get_term( 'foo', 'bar' );
|
|
$this->assertWPError( $found );
|
|
$this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
|
|
}
|
|
|
|
public function test_passing_term_object_should_skip_database_query_when_filter_property_is_empty() {
|
|
global $wpdb;
|
|
|
|
$term = $this->factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );
|
|
clean_term_cache( $term->term_id, 'wptests_tax' );
|
|
|
|
$num_queries = $wpdb->num_queries;
|
|
|
|
unset( $term->filter );
|
|
$term_a = get_term( $term, 'wptests_tax' );
|
|
|
|
$this->assertSame( $num_queries, $wpdb->num_queries );
|
|
}
|
|
|
|
public function test_passing_term_string_that_casts_to_int_0_should_return_null() {
|
|
$this->assertSame( null, get_term( 'abc', 'wptests_tax' ) );
|
|
}
|
|
|
|
public function test_should_return_null_for_invalid_term_id() {
|
|
$this->assertSame( null, get_term( 99999999, 'wptests_tax' ) );
|
|
}
|
|
|
|
public function test_cache_should_be_populated_by_successful_fetch() {
|
|
global $wpdb;
|
|
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
clean_term_cache( $t, 'wptests_tax' );
|
|
|
|
// Prime cache.
|
|
$term_a = get_term( $t, 'wptests_tax' );
|
|
$num_queries = $wpdb->num_queries;
|
|
|
|
// Second call shouldn't require a database query.
|
|
$term_b = get_term( $t, 'wptests_tax' );
|
|
$this->assertSame( $num_queries, $wpdb->num_queries );
|
|
$this->assertEquals( $term_a, $term_b );
|
|
}
|
|
|
|
public function test_output_object() {
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
$this->assertInternalType( 'object', get_term( $t, 'wptests_tax', OBJECT ) );
|
|
}
|
|
|
|
public function test_output_array_a() {
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
$term = get_term( $t, 'wptests_tax', ARRAY_A );
|
|
$this->assertInternalType( 'array', $term );
|
|
$this->assertTrue( isset( $term['term_id'] ) );
|
|
}
|
|
|
|
public function test_output_array_n() {
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
$term = get_term( $t, 'wptests_tax', ARRAY_N );
|
|
$this->assertInternalType( 'array', $term );
|
|
$this->assertFalse( isset( $term['term_id'] ) );
|
|
foreach ( $term as $k => $v ) {
|
|
$this->assertInternalType( 'integer', $k );
|
|
}
|
|
}
|
|
|
|
public function test_output_should_fall_back_to_object_for_invalid_input() {
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
$this->assertInternalType( 'object', get_term( $t, 'wptests_tax', 'foo' ) );
|
|
}
|
|
|
|
/**
|
|
* @ticket 14162
|
|
*/
|
|
public function test_numeric_properties_should_be_cast_to_ints() {
|
|
global $wpdb;
|
|
|
|
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
|
|
|
|
// Get raw data from the database.
|
|
$term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $t ) );
|
|
|
|
$found = get_term( $term_data );
|
|
|
|
$this->assertTrue( $found instanceof WP_Term );
|
|
$this->assertInternalType( 'int', $found->term_id );
|
|
$this->assertInternalType( 'int', $found->term_taxonomy_id );
|
|
$this->assertInternalType( 'int', $found->parent );
|
|
$this->assertInternalType( 'int', $found->count );
|
|
$this->assertInternalType( 'int', $found->term_group );
|
|
}
|
|
}
|