Block Editor: Move caching to endpoint for unique responses.

Now that the pattern API request includes the locale and version, the cache key needs to contain a hash of the query args.

Props ocean90, dd32, timothyblynjacobs
Fixes #53435


git-svn-id: https://develop.svn.wordpress.org/trunk@51208 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ian Dunn
2021-06-22 21:23:19 +00:00
parent 41c6786952
commit 83c7cad248
3 changed files with 116 additions and 41 deletions

View File

@@ -260,6 +260,46 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
$this->assertWPError( $response->as_error() );
}
/**
* @covers WP_REST_Pattern_Directory_Controller::get_items
*
* @since 5.8.0
*/
public function test_get_items_prepare_filter() {
wp_set_current_user( self::$contributor_id );
self::mock_successful_response( 'browse-all', true );
// Test that filter changes uncached values.
add_filter(
'rest_prepare_block_pattern',
function( $response ) {
return 'initial value';
}
);
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$response = rest_do_request( $request );
$patterns = $response->get_data();
$this->assertSame( 'initial value', $patterns[0] );
// Test that filter changes cached values (the previous request primed the cache).
add_filter(
'rest_prepare_block_pattern',
function( $response ) {
return 'modified the cache';
},
11
);
// Test that the filter works against cached values.
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$response = rest_do_request( $request );
$patterns = $response->get_data();
$this->assertSame( 'modified the cache', $patterns[0] );
}
public function test_get_item() {
$this->markTestSkipped( 'Controller does not have get_item route.' );
}