Changeset 55924
- Timestamp:
- 06/15/2023 03:23:25 AM (2 years ago)
- Location:
- trunk/tests/phpunit/tests/term
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term/getTagLink.php
r46586 r55924 6 6 */ 7 7 class Tests_Term_GetTagLink extends WP_UnitTestCase { 8 public function test_success() { 9 $t = self::factory()->term->create( 8 /** 9 * Tag ID. 10 * 11 * @var int 12 */ 13 public static $tag_id; 14 15 /** 16 * Test taxonomy term ID. 17 * 18 * @var int 19 */ 20 public static $term_id; 21 22 /** 23 * Set up shared fixtures. 24 * 25 * @param WP_UnitTest_Factory $factory 26 */ 27 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 28 self::$tag_id = $factory->term->create( 10 29 array( 11 30 'taxonomy' => 'post_tag', 12 'slug' => 'te rm-slug',31 'slug' => 'test-tag', 13 32 ) 14 33 ); 15 34 16 $found = get_tag_link( $t ); 17 $expected = home_url( '?tag=term-slug' ); 35 register_taxonomy( 'wptests_tax', 'post' ); 36 self::$term_id = self::factory()->term->create( 37 array( 38 'taxonomy' => 'wptests_tax', 39 'slug' => 'test-term', 40 ) 41 ); 42 } 43 44 /** 45 * Set up the test fixture. 46 */ 47 public function set_up() { 48 parent::set_up(); 49 // Required as taxonomies are reset between tests. 50 register_taxonomy( 'wptests_tax', 'post' ); 51 } 52 53 public function test_success() { 54 $tag_id = self::$tag_id; 55 56 $found = get_tag_link( $tag_id ); 57 $expected = home_url( '?tag=test-tag' ); 18 58 19 59 $this->assertSame( $expected, $found ); … … 24 64 */ 25 65 public function test_should_return_link_for_term_from_another_taxonomy_on_primed_cache() { 26 register_taxonomy( 'wptests_tax', 'post' );66 $term_id = self::$term_id; 27 67 28 $t = self::factory()->term->create( 29 array( 30 'taxonomy' => 'wptests_tax', 31 'slug' => 'test-term', 32 ) 33 ); 68 $term = get_term( $term_id ); 34 69 35 $term = get_term( $t ); 36 37 $found = get_tag_link( $t ); 70 $found = get_tag_link( $term_id ); 38 71 $expected = home_url( '?wptests_tax=test-term' ); 39 72 … … 45 78 */ 46 79 public function test_should_return_link_for_term_from_another_taxonomy_on_empty_cache() { 47 register_taxonomy( 'wptests_tax', 'post' );80 $term_id = self::$term_id; 48 81 49 $t = self::factory()->term->create( 50 array( 51 'taxonomy' => 'wptests_tax', 52 'slug' => 'test-term', 53 ) 54 ); 82 clean_term_cache( $term_id ); 55 83 56 clean_term_cache( $t ); 57 58 $found = get_tag_link( $t ); 84 $found = get_tag_link( $term_id ); 59 85 $expected = home_url( '?wptests_tax=test-term' ); 60 86 -
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' ); -
trunk/tests/phpunit/tests/term/getTermField.php
r52389 r55924 3 3 /** 4 4 * @group taxonomy 5 * 6 * @covers ::get_term_field 5 7 */ 6 8 class Tests_Term_getTermField extends WP_UnitTestCase { 7 9 8 public $taxonomy = 'wptests_tax'; 10 public static $taxonomy = 'wptests_tax'; 11 12 public static $term; 13 14 /** 15 * Set up shared fixtures. 16 * 17 * @param WP_UnitTest_Factory $factory 18 */ 19 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 20 register_taxonomy( self::$taxonomy, 'post' ); 21 self::$term = $factory->term->create_and_get( 22 array( 23 'taxonomy' => self::$taxonomy, 24 'description' => wpautop( 'Test term description' ), 25 ) 26 ); 27 } 9 28 10 29 public function set_up() { 11 30 parent::set_up(); 12 13 register_taxonomy( $this->taxonomy, 'post' );31 // Required as taxonomies are reset between tests. 32 register_taxonomy( self::$taxonomy, 'post' ); 14 33 } 15 34 … … 18 37 */ 19 38 public function test_get_term_field_should_not_return_error_for_empty_taxonomy() { 20 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );39 $term = self::$term; 21 40 22 41 $found = get_term_field( 'taxonomy', $term->term_id, '' ); 23 42 $this->assertNotWPError( $found ); 24 $this->assertSame( $this->taxonomy, $found );43 $this->assertSame( self::$taxonomy, $found ); 25 44 } 26 45 … … 29 48 */ 30 49 public function test_get_term_field_supplying_a_taxonomy() { 31 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );50 $term = self::$term; 32 51 33 52 $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy ); 34 $this->assertSame( $this->taxonomy, $found );53 $this->assertSame( self::$taxonomy, $found ); 35 54 } 36 55 … … 39 58 */ 40 59 public function test_get_term_field_supplying_no_taxonomy() { 41 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );60 $term = self::$term; 42 61 43 62 $found = get_term_field( 'taxonomy', $term->term_id ); 44 $this->assertSame( $this->taxonomy, $found );63 $this->assertSame( self::$taxonomy, $found ); 45 64 } 46 65 … … 49 68 */ 50 69 public function test_get_term_field_should_accept_a_WP_Term_id_or_object() { 51 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );70 $term = self::$term; 52 71 53 72 $this->assertInstanceOf( 'WP_Term', $term ); … … 61 80 */ 62 81 public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() { 63 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );82 $term = self::$term; 64 83 65 84 $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' ); … … 72 91 */ 73 92 public function test_get_term_field_invalid_term_should_return_WP_Error() { 74 $found = get_term_field( 'taxonomy', 0, $this->taxonomy );93 $found = get_term_field( 'taxonomy', 0, self::$taxonomy ); 75 94 76 95 $this->assertWPError( $found ); … … 84 103 85 104 public function test_get_term_field_term_id() { 86 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );105 $term = self::$term; 87 106 88 107 $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) ); … … 97 116 array( 98 117 'name' => $name, 99 'taxonomy' => $this->taxonomy,118 'taxonomy' => self::$taxonomy, 100 119 ) 101 120 ); … … 111 130 $term = self::factory()->term->create_and_get( 112 131 array( 113 'taxonomy' => $this->taxonomy,132 'taxonomy' => self::$taxonomy, 114 133 'slug' => $slug, 115 134 ) … … 126 145 $term = self::factory()->term->create_and_get( 127 146 array( 128 'taxonomy' => $this->taxonomy,147 'taxonomy' => self::$taxonomy, 129 148 'name' => $name, 130 149 ) … … 139 158 $term = self::factory()->term->create_and_get( 140 159 array( 141 'taxonomy' => $this->taxonomy,160 'taxonomy' => self::$taxonomy, 142 161 ) 143 162 ); … … 149 168 150 169 public function test_get_term_field_taxonomy() { 151 $term = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );152 153 $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) );154 $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) );155 $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );170 $term = self::$term; 171 172 $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term ) ); 173 $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->data ) ); 174 $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->term_id ) ); 156 175 } 157 176 158 177 public function test_get_term_field_description() { 159 $desc = wpautop( 'baz' ); 160 161 $term = self::factory()->term->create_and_get( 162 array( 163 'taxonomy' => $this->taxonomy, 164 'description' => $desc, 165 ) 166 ); 167 168 $this->assertSame( $desc, get_term_field( 'description', $term ) ); 169 $this->assertSame( $desc, get_term_field( 'description', $term->data ) ); 170 $this->assertSame( $desc, get_term_field( 'description', $term->term_id ) ); 178 $description = wpautop( 'Test term description' ); 179 180 $term = self::$term; 181 182 $this->assertSame( $description, get_term_field( 'description', $term ) ); 183 $this->assertSame( $description, get_term_field( 'description', $term->data ) ); 184 $this->assertSame( $description, get_term_field( 'description', $term->term_id ) ); 171 185 } 172 186 173 187 public function test_get_term_field_parent() { 174 $parent = self:: factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );188 $parent = self::$term; 175 189 $term = self::factory()->term->create_and_get( 176 190 array( 177 'taxonomy' => $this->taxonomy,191 'taxonomy' => self::$taxonomy, 178 192 'parent' => $parent->term_id, 179 193 )
Note: See TracChangeset
for help on using the changeset viewer.