Changeset 52180 for trunk/tests/phpunit/tests/term/getTermLink.php
- Timestamp:
- 11/16/2021 02:55:04 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/term/getTermLink.php
r51568 r52180 3 3 /** 4 4 * @group taxonomy 5 * @covers ::get_term_link 5 6 */ 6 7 class Tests_Term_GetTermLink extends WP_UnitTestCase { 7 8 9 public static $terms; 10 11 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 12 self::register_custom_taxonomy(); 13 14 $taxonomies = array( 'category', 'post_tag', 'wptests_tax' ); 15 foreach ( $taxonomies as $taxonomy ) { 16 self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) ); 17 } 18 } 19 8 20 public function set_up() { 9 21 parent::set_up(); 10 11 register_taxonomy( 'wptests_tax', 'post' ); 22 self::register_custom_taxonomy(); 12 23 } 13 24 … … 208 219 $this->assertStringContainsString( '/foo/term2/', $actual ); 209 220 } 221 222 /** 223 * @dataProvider data_get_term_link 224 * 225 * @ticket 50225 226 * 227 * @param string $taxonomy Taxonomy been tested (used for index of term keys). 228 * @param bool $use_id When true, pass term ID. Else, pass term object. 229 */ 230 public function test_get_term_link_filter_is_object_by_term_id( $taxonomy, $use_id ) { 231 $term = $this->get_term( $taxonomy, $use_id ); 232 233 add_filter( 234 'term_link', 235 function( $location, $term ) { 236 $this->assertInstanceOf( 'WP_Term', $term ); 237 }, 238 10, 239 2 240 ); 241 242 get_term_link( $term, $taxonomy ); 243 } 244 245 /** 246 * @dataProvider data_get_term_link 247 * 248 * @ticket 50225 249 * 250 * @param string $taxonomy Taxonomy been tested (used for index of term keys). 251 * @param bool $use_id When true, pass term ID. Else, pass term object. 252 */ 253 public function test_get_term_link_filter_is_object_by_term_object( $taxonomy, $use_id ) { 254 $term = $this->get_term( $taxonomy, $use_id ); 255 256 add_filter( 257 'term_link', 258 function( $location, $term ) { 259 $this->assertInstanceOf( 'WP_Term', $term ); 260 }, 261 10, 262 2 263 ); 264 265 get_term_link( get_term( $term, $taxonomy ), $taxonomy ); 266 } 267 268 /** 269 * Data provider. 270 * 271 * @return array 272 */ 273 public function data_get_term_link() { 274 return array( 275 'category passing term_id' => array( 276 'taxonomy' => 'category', 277 'use_id' => false, 278 ), 279 'category passing term object' => array( 280 'taxonomy' => 'category', 281 'use_id' => true, 282 ), 283 'post_tag passing term_id' => array( 284 'taxonomy' => 'post_tag', 285 'use_id' => false, 286 ), 287 'post_tag passing term object' => array( 288 'taxonomy' => 'post_tag', 289 'use_id' => true, 290 ), 291 'a custom taxonomy passing term_id' => array( 292 'taxonomy' => 'wptests_tax', 293 'use_id' => false, 294 ), 295 'a custom taxonomy passing term_id' => array( 296 'taxonomy' => 'wptests_tax', 297 'use_id' => true, 298 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post', 299 ), 300 ); 301 } 302 303 /** 304 * Helper to register a custom taxonomy for use in tests. 305 * 306 * @since 5.9.0 307 */ 308 private static function register_custom_taxonomy() { 309 register_taxonomy( 'wptests_tax', 'post' ); 310 } 311 312 /** 313 * Helper to get the term for the given taxonomy. 314 * 315 * @since 5.9.0 316 * 317 * @param string $taxonomy Taxonomy been tested (used for index of term keys). 318 * @param bool $use_id When true, pass term ID. Else, pass term object. 319 * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term. 320 */ 321 private function get_term( $taxonomy, $use_id ) { 322 $term = self::$terms[ $taxonomy ]; 323 if ( $use_id ) { 324 $term = $term->term_id; 325 } 326 327 return $term; 328 } 210 329 }
Note: See TracChangeset
for help on using the changeset viewer.