- Timestamp:
- 05/02/2022 01:58:48 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php
r53152 r53333 23 23 24 24 /** 25 * An instance of WP_REST_Pattern_Directory_Controller class. 26 * 27 * @since 6.0.0 28 * 29 * @var WP_REST_Pattern_Directory_Controller 30 */ 31 private static $controller; 32 33 /** 25 34 * Set up class test fixtures. 26 35 * … … 35 44 ) 36 45 ); 46 47 static::$controller = new WP_REST_Pattern_Directory_Controller(); 37 48 } 38 49 … … 43 54 */ 44 55 public function assertPatternMatchesSchema( $pattern ) { 45 $schema = ( new WP_REST_Pattern_Directory_Controller() )->get_item_schema();56 $schema = static::$controller->get_item_schema(); 46 57 $pattern_id = isset( $pattern->id ) ? $pattern->id : '{pattern ID is missing}'; 47 58 … … 323 334 */ 324 335 public function test_prepare_item() { 325 $controller = new WP_REST_Pattern_Directory_Controller();326 336 $raw_patterns = json_decode( self::get_raw_response( 'browse-all' ) ); 327 337 $raw_patterns[0]->extra_field = 'this should be removed'; 328 338 329 $prepared_pattern = $controller->prepare_response_for_collection(330 $controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )339 $prepared_pattern = static::$controller->prepare_response_for_collection( 340 static::$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() ) 331 341 ); 332 342 … … 341 351 */ 342 352 public function test_prepare_item_search() { 343 $controller = new WP_REST_Pattern_Directory_Controller();344 353 $raw_patterns = json_decode( self::get_raw_response( 'search' ) ); 345 354 $raw_patterns[0]->extra_field = 'this should be removed'; 346 355 347 $prepared_pattern = $controller->prepare_response_for_collection(348 $controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )356 $prepared_pattern = static::$controller->prepare_response_for_collection( 357 static::$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() ) 349 358 ); 350 359 … … 398 407 public function test_get_item_schema() { 399 408 $this->markTestSkipped( "The controller's schema is hardcoded, so tests would not be meaningful." ); 409 } 410 411 /** 412 * Tests if the transient key gets generated correctly. 413 * 414 * @dataProvider data_get_query_parameters 415 * 416 * @covers WP_REST_Pattern_Directory_Controller::get_transient_key 417 * 418 * @since 6.0.0 419 * 420 * @ticket 55617 421 * 422 * @param array $parameters_1 Expected query arguments. 423 * @param array $parameters_2 Actual query arguments. 424 * @param string $message An error message to display. 425 * @param bool $assert_same Assertion type (assertSame vs assertNotSame). 426 */ 427 public function test_transient_keys_get_generated_correctly( $parameters_1, $parameters_2, $message, $assert_same = true ) { 428 $reflection_method = new ReflectionMethod( static::$controller, 'get_transient_key' ); 429 $reflection_method->setAccessible( true ); 430 431 $result_1 = $reflection_method->invoke( self::$controller, $parameters_1 ); 432 $result_2 = $reflection_method->invoke( self::$controller, $parameters_2 ); 433 434 $this->assertIsString( $result_1, 'Transient key #1 must be a string.' ); 435 $this->assertNotEmpty( $result_1, 'Transient key #1 must not be empty.' ); 436 437 $this->assertIsString( $result_2, 'Transient key #2 must be a string.' ); 438 $this->assertNotEmpty( $result_2, 'Transient key #2 must not be empty.' ); 439 440 if ( $assert_same ) { 441 $this->assertSame( $result_1, $result_2, $message ); 442 } else { 443 $this->assertNotSame( $result_1, $result_2, $message ); 444 } 445 446 } 447 448 /** 449 * @since 6.0.0 450 * 451 * @ticket 55617 452 */ 453 public function data_get_query_parameters() { 454 return array( 455 'same key and empty slugs' => array( 456 'parameters_1' => array( 457 'parameter_1' => 1, 458 'slug' => array(), 459 ), 460 'parameters_2' => array( 461 'parameter_1' => 1, 462 ), 463 'message' => 'Empty slugs should not affect the transient key.', 464 ), 465 'same key and slugs in different order' => array( 466 'parameters_1' => array( 467 'parameter_1' => 1, 468 'slug' => array( 0, 2 ), 469 ), 470 'parameters_2' => array( 471 'parameter_1' => 1, 472 'slug' => array( 2, 0 ), 473 ), 474 'message' => 'The order of slugs should not affect the transient key.', 475 ), 476 'same key and different slugs' => array( 477 'parameters_1' => array( 478 'parameter_1' => 1, 479 'slug' => array( 'some_slug' ), 480 ), 481 'parameters_2' => array( 482 'parameter_1' => 1, 483 'slug' => array( 'some_other_slug' ), 484 ), 485 'message' => 'Transient keys must not match.', 486 false, 487 ), 488 'different keys' => array( 489 'parameters_1' => array( 490 'parameter_1' => 1, 491 ), 492 'parameters_2' => array( 493 'parameter_2' => 1, 494 ), 495 'message' => 'Transient keys must depend on array keys.', 496 false, 497 ), 498 ); 400 499 } 401 500
Note: See TracChangeset
for help on using the changeset viewer.