mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-01 03:04:34 +00:00
REST API: Fixes /wp/v2/pattern-directory/patterns endpoint response for slug parameter.
[53218] introduced a bug of a wrong response from the `wp/v2/pattern-directory/patterns` endpoint with a `slug` parameter. As the response is cached, it can result in an incorrect list of available patterns supported by the current theme. This commit resolves by: * Limiting the `slug` to an `array` in the query parameters. * When set, parsing and sorting the slug(s) and then serializing the sorted query args as part of the hashed transient keys. Props antonvlasenko, timothyblynjacobs, spacedmonkey, costdev, hellofromTonya. Follow-up to [53218], [53152], [51208]. Fixes #55617. git-svn-id: https://develop.svn.wordpress.org/trunk@53333 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -21,6 +21,15 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
*/
|
||||
protected static $contributor_id;
|
||||
|
||||
/**
|
||||
* An instance of WP_REST_Pattern_Directory_Controller class.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var WP_REST_Pattern_Directory_Controller
|
||||
*/
|
||||
private static $controller;
|
||||
|
||||
/**
|
||||
* Set up class test fixtures.
|
||||
*
|
||||
@@ -34,6 +43,8 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
'role' => 'contributor',
|
||||
)
|
||||
);
|
||||
|
||||
static::$controller = new WP_REST_Pattern_Directory_Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,7 +53,7 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
* @param WP_REST_Response[] $pattern An individual pattern from the REST API response.
|
||||
*/
|
||||
public function assertPatternMatchesSchema( $pattern ) {
|
||||
$schema = ( new WP_REST_Pattern_Directory_Controller() )->get_item_schema();
|
||||
$schema = static::$controller->get_item_schema();
|
||||
$pattern_id = isset( $pattern->id ) ? $pattern->id : '{pattern ID is missing}';
|
||||
|
||||
$this->assertTrue(
|
||||
@@ -322,12 +333,11 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public function test_prepare_item() {
|
||||
$controller = new WP_REST_Pattern_Directory_Controller();
|
||||
$raw_patterns = json_decode( self::get_raw_response( 'browse-all' ) );
|
||||
$raw_patterns[0]->extra_field = 'this should be removed';
|
||||
|
||||
$prepared_pattern = $controller->prepare_response_for_collection(
|
||||
$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )
|
||||
$prepared_pattern = static::$controller->prepare_response_for_collection(
|
||||
static::$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )
|
||||
);
|
||||
|
||||
$this->assertPatternMatchesSchema( $prepared_pattern );
|
||||
@@ -340,12 +350,11 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public function test_prepare_item_search() {
|
||||
$controller = new WP_REST_Pattern_Directory_Controller();
|
||||
$raw_patterns = json_decode( self::get_raw_response( 'search' ) );
|
||||
$raw_patterns[0]->extra_field = 'this should be removed';
|
||||
|
||||
$prepared_pattern = $controller->prepare_response_for_collection(
|
||||
$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )
|
||||
$prepared_pattern = static::$controller->prepare_response_for_collection(
|
||||
static::$controller->prepare_item_for_response( $raw_patterns[0], new WP_REST_Request() )
|
||||
);
|
||||
|
||||
$this->assertPatternMatchesSchema( $prepared_pattern );
|
||||
@@ -399,6 +408,96 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
$this->markTestSkipped( "The controller's schema is hardcoded, so tests would not be meaningful." );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the transient key gets generated correctly.
|
||||
*
|
||||
* @dataProvider data_get_query_parameters
|
||||
*
|
||||
* @covers WP_REST_Pattern_Directory_Controller::get_transient_key
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @ticket 55617
|
||||
*
|
||||
* @param array $parameters_1 Expected query arguments.
|
||||
* @param array $parameters_2 Actual query arguments.
|
||||
* @param string $message An error message to display.
|
||||
* @param bool $assert_same Assertion type (assertSame vs assertNotSame).
|
||||
*/
|
||||
public function test_transient_keys_get_generated_correctly( $parameters_1, $parameters_2, $message, $assert_same = true ) {
|
||||
$reflection_method = new ReflectionMethod( static::$controller, 'get_transient_key' );
|
||||
$reflection_method->setAccessible( true );
|
||||
|
||||
$result_1 = $reflection_method->invoke( self::$controller, $parameters_1 );
|
||||
$result_2 = $reflection_method->invoke( self::$controller, $parameters_2 );
|
||||
|
||||
$this->assertIsString( $result_1, 'Transient key #1 must be a string.' );
|
||||
$this->assertNotEmpty( $result_1, 'Transient key #1 must not be empty.' );
|
||||
|
||||
$this->assertIsString( $result_2, 'Transient key #2 must be a string.' );
|
||||
$this->assertNotEmpty( $result_2, 'Transient key #2 must not be empty.' );
|
||||
|
||||
if ( $assert_same ) {
|
||||
$this->assertSame( $result_1, $result_2, $message );
|
||||
} else {
|
||||
$this->assertNotSame( $result_1, $result_2, $message );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @ticket 55617
|
||||
*/
|
||||
public function data_get_query_parameters() {
|
||||
return array(
|
||||
'same key and empty slugs' => array(
|
||||
'parameters_1' => array(
|
||||
'parameter_1' => 1,
|
||||
'slug' => array(),
|
||||
),
|
||||
'parameters_2' => array(
|
||||
'parameter_1' => 1,
|
||||
),
|
||||
'message' => 'Empty slugs should not affect the transient key.',
|
||||
),
|
||||
'same key and slugs in different order' => array(
|
||||
'parameters_1' => array(
|
||||
'parameter_1' => 1,
|
||||
'slug' => array( 0, 2 ),
|
||||
),
|
||||
'parameters_2' => array(
|
||||
'parameter_1' => 1,
|
||||
'slug' => array( 2, 0 ),
|
||||
),
|
||||
'message' => 'The order of slugs should not affect the transient key.',
|
||||
),
|
||||
'same key and different slugs' => array(
|
||||
'parameters_1' => array(
|
||||
'parameter_1' => 1,
|
||||
'slug' => array( 'some_slug' ),
|
||||
),
|
||||
'parameters_2' => array(
|
||||
'parameter_1' => 1,
|
||||
'slug' => array( 'some_other_slug' ),
|
||||
),
|
||||
'message' => 'Transient keys must not match.',
|
||||
false,
|
||||
),
|
||||
'different keys' => array(
|
||||
'parameters_1' => array(
|
||||
'parameter_1' => 1,
|
||||
),
|
||||
'parameters_2' => array(
|
||||
'parameter_2' => 1,
|
||||
),
|
||||
'message' => 'Transient keys must depend on array keys.',
|
||||
false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate a successful outbound HTTP requests, to keep tests pure and performant.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user