- Timestamp:
- 01/24/2023 04:52:42 PM (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php
r55029 r55132 32 32 33 33 /** 34 * List of URLs captured. 35 * 36 * @since 6.2.0 37 * 38 * @var string[] 39 */ 40 protected static $http_request_urls; 41 42 /** 34 43 * Set up class test fixtures. 35 44 * … … 45 54 ); 46 55 56 self::$http_request_urls = array(); 57 47 58 static::$controller = new WP_REST_Pattern_Directory_Controller(); 59 } 60 61 /** 62 * Tear down after class. 63 * 64 * @since 6.2.0 65 */ 66 public static function wpTearDownAfterClass() { 67 self::delete_user( self::$contributor_id ); 68 } 69 70 /** 71 * Clear the captured request URLs after each test. 72 * 73 * @since 6.2.0 74 */ 75 public function tear_down() { 76 self::$http_request_urls = array(); 77 parent::tear_down(); 48 78 } 49 79 … … 309 339 310 340 $this->assertSame( 'modified the cache', $patterns[0] ); 341 } 342 343 /** 344 * Tests if the provided query args are passed through to the wp.org API. 345 * 346 * @since 6.2.0 347 * 348 * @ticket 57501 349 * 350 * @covers WP_REST_Pattern_Directory_Controller::get_items 351 * 352 * @dataProvider data_get_items_query_args 353 * 354 * @param string $param Query parameter name (ex, page). 355 * @param mixed $value Query value to test. 356 * @param bool $is_error Whether this value should error or not. 357 * @param mixed $expected Expected value (or expected error code). 358 */ 359 public function test_get_items_query_args( $param, $value, $is_error, $expected ) { 360 wp_set_current_user( self::$contributor_id ); 361 add_filter( 'pre_http_request', array( $this, 'mock_request_to_apiwporg_url' ), 10, 3 ); 362 363 $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); 364 if ( $value ) { 365 $request->set_query_params( array( $param => $value ) ); 366 } 367 368 $response = rest_do_request( $request ); 369 $data = $response->get_data(); 370 if ( $is_error ) { 371 $this->assertSame( $expected, $data['code'], 'Response error code does not match' ); 372 $this->assertStringContainsString( $param, $data['message'], 'Response error message does not match' ); 373 } else { 374 $this->assertCount( 1, self::$http_request_urls, 'The number of HTTP Request URLs is not 1' ); 375 $this->assertStringContainsString( $param . '=' . $expected, self::$http_request_urls[0], 'The param and/or value do not match' ); 376 } 377 } 378 379 /** 380 * Data provider. 381 * 382 * return array[] 383 */ 384 public function data_get_items_query_args() { 385 return array( 386 'per_page default' => array( 387 'param' => 'per_page', 388 'value' => false, 389 'is_error' => false, 390 'expected' => 100, 391 ), 392 'per_page custom-1' => array( 393 'param' => 'per_page', 394 'value' => 5, 395 'is_error' => false, 396 'expected' => 5, 397 ), 398 'per_page custom-2' => array( 399 'param' => 'per_page', 400 'value' => 50, 401 'is_error' => false, 402 'expected' => 50, 403 ), 404 'per_page invalid-1' => array( 405 'param' => 'per_page', 406 'value' => 200, 407 'is_error' => true, 408 'expected' => 'rest_invalid_param', 409 ), 410 'per_page invalid-2' => array( 411 'param' => 'per_page', 412 'value' => 'abc', 413 'is_error' => true, 414 'expected' => 'rest_invalid_param', 415 ), 416 417 'page default' => array( 418 'param' => 'page', 419 'value' => false, 420 'is_error' => false, 421 'expected' => 1, 422 ), 423 'page custom' => array( 424 'param' => 'page', 425 'value' => 5, 426 'is_error' => false, 427 'expected' => 5, 428 ), 429 'page invalid' => array( 430 'param' => 'page', 431 'value' => 'abc', 432 'is_error' => true, 433 'expected' => 'rest_invalid_param', 434 ), 435 436 'offset custom' => array( 437 'param' => 'offset', 438 'value' => 5, 439 'is_error' => false, 440 'expected' => 5, 441 ), 442 'offset invalid-1' => array( 443 'param' => 'offset', 444 'value' => 'abc', 445 'is_error' => true, 446 'expected' => 'rest_invalid_param', 447 ), 448 449 'order default' => array( 450 'param' => 'order', 451 'value' => false, 452 'is_error' => false, 453 'expected' => 'desc', 454 ), 455 'order custom' => array( 456 'param' => 'order', 457 'value' => 'asc', 458 'is_error' => false, 459 'expected' => 'asc', 460 ), 461 'order invalid-1' => array( 462 'param' => 'order', 463 'value' => 10, 464 'is_error' => true, 465 'expected' => 'rest_invalid_param', 466 ), 467 'order invalid-2' => array( 468 'param' => 'order', 469 'value' => 'fake', 470 'is_error' => true, 471 'expected' => 'rest_invalid_param', 472 ), 473 474 'orderby default' => array( 475 'param' => 'orderby', 476 'value' => false, 477 'is_error' => false, 478 'expected' => 'date', 479 ), 480 'orderby custom-1' => array( 481 'param' => 'orderby', 482 'value' => 'title', 483 'is_error' => false, 484 'expected' => 'title', 485 ), 486 'orderby custom-2' => array( 487 'param' => 'orderby', 488 'value' => 'date', 489 'is_error' => false, 490 'expected' => 'date', 491 ), 492 'orderby custom-3' => array( 493 'param' => 'orderby', 494 'value' => 'favorite_count', 495 'is_error' => false, 496 'expected' => 'favorite_count', 497 ), 498 'orderby invalid-1' => array( 499 'param' => 'orderby', 500 'value' => 10, 501 'is_error' => true, 502 'expected' => 'rest_invalid_param', 503 ), 504 'orderby invalid-2' => array( 505 'param' => 'orderby', 506 'value' => 'fake', 507 'is_error' => true, 508 'expected' => 'rest_invalid_param', 509 ), 510 ); 311 511 } 312 512 … … 574 774 ); 575 775 } 776 777 /** 778 * Mock the request to wp.org URL to capture the URLs. 779 * 780 * @since 6.2.0 781 * 782 * @return array faux/mocked response. 783 */ 784 public function mock_request_to_apiwporg_url( $response, $args, $url ) { 785 if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) { 786 return $response; 787 } 788 789 self::$http_request_urls[] = $url; 790 791 // Return a response to prevent external API request. 792 $response = array( 793 'headers' => array(), 794 'response' => array( 795 'code' => 200, 796 'message' => 'OK', 797 ), 798 'body' => '[]', 799 'cookies' => array(), 800 'filename' => null, 801 ); 802 803 return $response; 804 } 576 805 }
Note: See TracChangeset
for help on using the changeset viewer.