Changeset 55924 for trunk/tests/phpunit/tests/term/getTerm.php
- Timestamp:
- 06/15/2023 03:23:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term/getTerm.php
r55745 r55924 3 3 /** 4 4 * @group taxonomy 5 * 6 * @covers ::get_term 5 7 */ 6 8 class Tests_Term_GetTerm extends WP_UnitTestCase { 9 /** 10 * Shared terms. 11 * 12 * @var array[] 13 */ 14 public static $shared_terms = array(); 15 16 /** 17 * Test taxonomy term object. 18 * 19 * @var WP_Term 20 */ 21 public static $term; 22 23 /** 24 * Set up shared fixtures. 25 * 26 * @param WP_UnitTest_Factory $factory 27 */ 28 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 29 register_taxonomy( 'wptests_tax', 'post' ); 30 self::$shared_terms = self::generate_shared_terms(); 31 32 self::$term = $factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) ); 33 } 34 35 /** 36 * Set up the test fixtures. 37 */ 7 38 public function set_up() { 8 39 parent::set_up(); 40 // Required as taxonomies are reset between tests. 9 41 register_taxonomy( 'wptests_tax', 'post' ); 42 register_taxonomy( 'wptests_tax_2', 'post' ); 10 43 } 11 44 … … 15 48 * @return array Array of term_id/old_term_id/term_taxonomy_id triplets. 16 49 */ 17 protected function generate_shared_terms() {50 protected static function generate_shared_terms() { 18 51 global $wpdb; 19 52 20 53 register_taxonomy( 'wptests_tax_2', 'post' ); 21 54 22 $t 1 = wp_insert_term( 'Foo', 'wptests_tax' );23 $t 2 = wp_insert_term( 'Foo', 'wptests_tax_2' );55 $term_1 = wp_insert_term( 'Foo', 'wptests_tax' ); 56 $term_2 = wp_insert_term( 'Foo', 'wptests_tax_2' ); 24 57 25 58 // Manually modify because shared terms shouldn't naturally occur. 26 59 $wpdb->update( 27 60 $wpdb->term_taxonomy, 28 array( 'term_id' => $t 1['term_id'] ),29 array( 'term_taxonomy_id' => $t 2['term_taxonomy_id'] ),61 array( 'term_id' => $term_1['term_id'] ), 62 array( 'term_taxonomy_id' => $term_2['term_taxonomy_id'] ), 30 63 array( '%d' ), 31 64 array( '%d' ) 32 65 ); 33 66 34 clean_term_cache( $t 1['term_id'] );67 clean_term_cache( $term_1['term_id'] ); 35 68 36 69 return array( 37 70 array( 38 'term_id' => $t 1['term_id'],39 'old_term_id' => $t 1['term_id'],40 'term_taxonomy_id' => $t 1['term_taxonomy_id'],71 'term_id' => $term_1['term_id'], 72 'old_term_id' => $term_1['term_id'], 73 'term_taxonomy_id' => $term_1['term_taxonomy_id'], 41 74 ), 42 75 array( 43 'term_id' => $t 1['term_id'],44 'old_term_id' => $t 2['term_id'],45 'term_taxonomy_id' => $t 2['term_taxonomy_id'],76 'term_id' => $term_1['term_id'], 77 'old_term_id' => $term_2['term_id'], 78 'term_taxonomy_id' => $term_2['term_taxonomy_id'], 46 79 ), 47 80 ); … … 61 94 62 95 public function test_passing_term_object_should_skip_database_query_when_filter_property_is_empty() { 63 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );96 $term = self::$term; 64 97 clean_term_cache( $term->term_id, 'wptests_tax' ); 65 98 … … 81 114 82 115 public function test_cache_should_be_populated_by_successful_fetch() { 83 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );84 clean_term_cache( $t , 'wptests_tax' );116 $term_id = self::$term->term_id; 117 clean_term_cache( $term_id, 'wptests_tax' ); 85 118 86 119 // Prime cache. 87 $term_a = get_term( $t , 'wptests_tax' );120 $term_a = get_term( $term_id, 'wptests_tax' ); 88 121 $num_queries = get_num_queries(); 89 122 90 123 // Second call shouldn't require a database query. 91 $term_b = get_term( $t , 'wptests_tax' );124 $term_b = get_term( $term_id, 'wptests_tax' ); 92 125 $this->assertSame( $num_queries, get_num_queries() ); 93 126 $this->assertEquals( $term_a, $term_b ); … … 95 128 96 129 public function test_output_object() { 97 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );98 $this->assertIsObject( get_term( $t , 'wptests_tax', OBJECT ) );130 $term_id = self::$term->term_id; 131 $this->assertIsObject( get_term( $term_id, 'wptests_tax', OBJECT ) ); 99 132 } 100 133 101 134 public function test_output_array_a() { 102 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );103 $term = get_term( $t, 'wptests_tax', ARRAY_A );135 $term_id = self::$term->term_id; 136 $term = get_term( $term_id, 'wptests_tax', ARRAY_A ); 104 137 $this->assertIsArray( $term ); 105 138 $this->assertArrayHasKey( 'term_id', $term ); … … 107 140 108 141 public function test_output_array_n() { 109 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );110 $term = get_term( $t, 'wptests_tax', ARRAY_N );142 $term_id = self::$term->term_id; 143 $term = get_term( $term_id, 'wptests_tax', ARRAY_N ); 111 144 $this->assertIsArray( $term ); 112 145 $this->assertArrayNotHasKey( 'term_id', $term ); … … 117 150 118 151 public function test_output_should_fall_back_to_object_for_invalid_input() { 119 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );120 $this->assertIsObject( get_term( $t , 'wptests_tax', 'foo' ) );152 $term_id = self::$term->term_id; 153 $this->assertIsObject( get_term( $term_id, 'wptests_tax', 'foo' ) ); 121 154 } 122 155 … … 128 161 global $wpdb; 129 162 130 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );163 $term_id = self::$term->term_id; 131 164 132 165 // Get raw data from the database. 133 $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 ) );166 $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", $term_id ) ); 134 167 135 168 $contexts = array( 'raw', 'edit', 'db', 'display', 'rss', 'attribute', 'js' ); … … 151 184 */ 152 185 public function test_should_return_null_when_provided_taxonomy_does_not_match_actual_term_taxonomy() { 153 $term_id = self:: factory()->term->create( array( 'taxonomy' => 'post_tag' ) );186 $term_id = self::$term->term_id; 154 187 $this->assertNull( get_term( $term_id, 'category' ) ); 155 188 } … … 159 192 */ 160 193 public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_is_specified() { 161 $terms = $this->generate_shared_terms();194 $terms = self::$shared_terms; 162 195 163 196 $found = get_term( $terms[0]['term_id'] ); … … 170 203 */ 171 204 public function test_should_return_term_when_term_is_shared_and_correct_taxonomy_is_specified() { 172 $terms = $this->generate_shared_terms();205 $terms = self::$shared_terms; 173 206 174 207 $found = get_term( $terms[0]['term_id'], 'wptests_tax' ); … … 182 215 */ 183 216 public function test_should_return_null_when_term_is_shared_and_incorrect_taxonomy_is_specified() { 184 $terms = $this->generate_shared_terms();217 $terms = self::$shared_terms; 185 218 186 219 $found = get_term( $terms[0]['term_id'], 'post_tag' ); … … 193 226 */ 194 227 public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() { 195 $terms = $this->generate_shared_terms();228 $terms = self::$shared_terms; 196 229 197 230 // Prime cache for 'wptests_tax'. … … 212 245 */ 213 246 public function test_should_return_error_when_only_matching_term_is_in_an_invalid_taxonomy() { 214 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );247 $term_id = self::$term->term_id; 215 248 216 249 _unregister_taxonomy( 'wptests_tax' ); 217 250 218 $found = get_term( $t );251 $found = get_term( $term_id ); 219 252 $this->assertWPError( $found ); 220 253 $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); … … 225 258 */ 226 259 public function test_term_should_be_returned_when_id_is_shared_only_with_invalid_taxonomies() { 227 $terms = $this->generate_shared_terms();260 $terms = self::$shared_terms; 228 261 229 262 _unregister_taxonomy( 'wptests_tax' );
Note: See TracChangeset
for help on using the changeset viewer.