Skip to content

Commit

Permalink
Support Atomic rest API and move tests to block patterns category
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed Apr 14, 2022
1 parent 0852a0b commit 06f8501
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Block_Patterns_From_API {
public function __construct( Block_Patterns_Utils $utils = null ) {
$this->patterns_sources = array( 'block_patterns' );

// While we're still testing the FSE patterns, limit activation via a filter.
// Switch the patterns source based on whether we're using a block-based theme.
if ( apply_filters( 'a8c_enable_fse_block_patterns_api', false ) ) {
$this->patterns_sources[] = 'fse_block_patterns';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@

use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/../../full-site-editing-plugin.php';
require_once __DIR__ . '/../class-block-patterns-from-api.php';

/**
* Class Coming_Soon_Test
* Tests block pattern registration in the API context.
*/
class Block_Patterns_From_Api_Test extends TestCase {
/**
* PHPUnit_Framework_MockObject_MockObject.
*
* @var object
*/
protected $utils_mock;

/**
* Representation of a Pattern as returned by the API.
*
Expand Down Expand Up @@ -174,4 +168,66 @@ public function test_patterns_request_succeeds_with_override_source_site() {

remove_filter( 'a8c_override_patterns_source_site', $example_site );
}

/**
* Tests the given patterns registration mock against multiple REST API routes.
*
* @param object $block_patterns_utils_mock A mock object for the block pattern utilities class.
* @param array $test_routes An array of strings of routes to test.
*/
private function test_pattern_registration_on_routes( $block_patterns_utils_mock, $test_routes ) {
foreach ( $test_routes as $route ) {
$request_mock = $this->createMock( \WP_REST_Request::class );
$request_mock->method( 'get_route' )->willReturn( $route );

load_block_patterns_from_api(
function () use ( $block_patterns_utils_mock ) {
$block_patterns_utils_mock->register_patterns();
}
)( null, $request_mock );
}
}

/**
* Tests that pattern registration does occur on API routes related to block patterns.
*/
public function test_load_block_patterns_from_api_runs_in_correct_request_context() {
add_filter( 'a8c_enable_block_patterns_api', '__return_true' );
$test_routes = array(
'/wp/v2/block-patterns/categories',
'/wp/v2/block-patterns/patterns',
'/wp/v2/sites/178915379/block-patterns/categories',
'/wp/v2/sites/178915379/block-patterns/patterns',
);

$utils_mock = $this->createBlockPatternsUtilsMock( array( $this->pattern_mock_object ) );
$utils_mock->expects( $this->exactly( count( $test_routes ) ) )->method( 'register_patterns' );

$this->test_pattern_registration_on_routes( $utils_mock, $test_routes );
}

/**
* Tests that pattern registration does not occur on rest API routes unrelated
* to block patterns.
*/
public function test_load_block_patterns_from_api_is_skipped_in_wrong_request_context() {
add_filter( 'a8c_enable_block_patterns_api', '__return_true' );

$utils_mock = $this->createBlockPatternsUtilsMock( array( $this->pattern_mock_object ) );
$utils_mock->expects( $this->never() )->method( 'register_patterns' );

$test_routes = array(
'/rest/v1.1/help/olark/mine',
'/wpcom/v2/sites/178915379/post-counts',
'/rest/v1.1/me/shopping-cart/',
'/wpcom/v3/sites/178915379/gutenberg',
'/wp/v2/sites/178915379/types',
'/wp/v2/sites/178915379/block-patterns/3ategories',
'/wp/v2//block-patterns/patterns',
'/wp/v2block-patterns/categories',
'/wp/v2/123/block-patterns/categories',
);

$this->test_pattern_registration_on_routes( $utils_mock, $test_routes );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ function load_block_patterns_from_api( $register_patterns_func ) {
* @param WP_REST_Request $request
*/
return function ( $response, $request ) use ( $register_patterns_func ) {
$route = $request->get_route();
$request_allowed = preg_match( '/^\/wp\/v2\/sites\/[0-9]+\/block\-patterns\/(patterns|categories)$/', $route );
$route = $request->get_route();
// Matches either /wp/v2/sites/123/block-patterns/patterns or /wp/v2/block-patterns/patterns
// to handle the API format of both WordPress.com and WordPress core.
$request_allowed = preg_match( '/^\/wp\/v2\/(sites\/[0-9]+\/)?block\-patterns\/(patterns|categories)$/', $route );

if ( ! $request_allowed || ! apply_filters( 'a8c_enable_block_patterns_api', false ) ) {
return $response;
Expand Down
3 changes: 0 additions & 3 deletions apps/editing-toolkit/editing-toolkit-plugin/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@
<testsuite name="block-patterns">
<directory suffix="-test.php">./block-patterns/test/</directory>
</testsuite>
<testsuite name="full-site-editing-plugin">
<directory suffix="-test.php">./test/</directory>
</testsuite>
</testsuites>
</phpunit>

This file was deleted.

0 comments on commit 06f8501

Please sign in to comment.