mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
REST API: Bring new endpoints for Block Patterns from Gutenberg plugin
Related Gutenberg issue: https://github.com/WordPress/gutenberg/issues/39889. Backporting changes from the Gutenberg plugin: - new Block Patterns REST API endpoint - new Block Pattern Categories REST API endpoint - updates to Query Loop related patterns - support for custom taxonomies in Query Loop block Props hellofromtonya, peterwilsoncc, ntsekouras, zieladam, ironprogrammer, spacedmonkey, timothyblynjacobs, antonvlasenko, jsnajdr. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53152 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -443,8 +443,18 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
|
||||
'order' => 'DESC',
|
||||
'orderby' => 'title',
|
||||
'post__not_in' => array( 1, 2 ),
|
||||
'category__in' => array( 56 ),
|
||||
'tag__in' => array( 3, 11, 10 ),
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'category',
|
||||
'terms' => array( 56 ),
|
||||
'include_children' => false,
|
||||
),
|
||||
array(
|
||||
'taxonomy' => 'post_tag',
|
||||
'terms' => array( 3, 11, 10 ),
|
||||
'include_children' => false,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
|
||||
$patterns = $response->get_data();
|
||||
|
||||
$this->assertSame( 'view', $patterns['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertSame( array( 'view', 'embed' ), $patterns['endpoints'][0]['args']['context']['enum'] );
|
||||
$this->assertSame( array( 'view', 'embed', 'edit' ), $patterns['endpoints'][0]['args']['context']['enum'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -161,6 +161,8 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
'/wp/v2/plugins',
|
||||
'/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
|
||||
'/wp/v2/block-directory/search',
|
||||
'/wp/v2/block-patterns/categories',
|
||||
'/wp/v2/block-patterns/patterns',
|
||||
'/wp/v2/sidebars',
|
||||
'/wp/v2/sidebars/(?P<id>[\w-]+)',
|
||||
'/wp/v2/widget-types',
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Block_Pattern_Categories_Registry functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 6.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for Block Pattern Categories Registry.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @ticket 55505
|
||||
*
|
||||
* @covers WP_REST_Block_Pattern_Categories_Controller
|
||||
*
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* Admin user ID.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* Original instance of WP_Block_Patterns_Registry.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var WP_Block_Patterns_Registry
|
||||
*/
|
||||
protected static $orig_registry;
|
||||
|
||||
/**
|
||||
* Instance of the reflected `instance` property.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var ReflectionProperty
|
||||
*/
|
||||
private static $registry_instance_property;
|
||||
|
||||
/**
|
||||
* The REST API route.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const REQUEST_ROUTE = '/wp/v2/block-patterns/categories';
|
||||
|
||||
/**
|
||||
* Set up class test fixtures.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
|
||||
*/
|
||||
public static function wpSetupBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
|
||||
|
||||
// Setup an empty testing instance of `WP_Block_Pattern_Categories_Registry` and save the original.
|
||||
self::$orig_registry = WP_Block_Pattern_Categories_Registry::get_instance();
|
||||
self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Pattern_Categories_Registry', 'instance' );
|
||||
self::$registry_instance_property->setAccessible( true );
|
||||
$test_registry = new WP_Block_Pattern_Categories_Registry();
|
||||
self::$registry_instance_property->setValue( $test_registry );
|
||||
|
||||
// Register some categories in the test registry.
|
||||
$test_registry->register( 'test', array( 'label' => 'Test' ) );
|
||||
$test_registry->register( 'query', array( 'label' => 'Query' ) );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
self::delete_user( self::$admin_id );
|
||||
|
||||
// Restore the original registry instance.
|
||||
self::$registry_instance_property->setValue( self::$orig_registry );
|
||||
self::$registry_instance_property->setAccessible( false );
|
||||
self::$registry_instance_property = null;
|
||||
self::$orig_registry = null;
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
switch_theme( 'emptytheme' );
|
||||
}
|
||||
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( static::REQUEST_ROUTE, $routes );
|
||||
}
|
||||
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$expected_names = array( 'test', 'query' );
|
||||
$expected_fields = array( 'name', 'label' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$request['_fields'] = 'name,label';
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertCount( count( $expected_names ), $data );
|
||||
foreach ( $data as $idx => $item ) {
|
||||
$this->assertSame( $expected_names[ $idx ], $item['name'] );
|
||||
$this->assertSame( $expected_fields, array_keys( $item ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify capability check for unauthorized request (not logged in).
|
||||
*/
|
||||
public function test_get_items_unauthorized() {
|
||||
// Ensure current user is logged out.
|
||||
wp_logout();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertWPError( $response->as_error() );
|
||||
$this->assertSame( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify capability check for forbidden request (insufficient capability).
|
||||
*/
|
||||
public function test_get_items_forbidden() {
|
||||
// Set current user without `edit_posts` capability.
|
||||
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertWPError( $response->as_error() );
|
||||
$this->assertSame( 403, $response->get_status() );
|
||||
}
|
||||
|
||||
public function test_context_param() {
|
||||
$this->markTestSkipped( 'Controller does not use context_param.' );
|
||||
}
|
||||
|
||||
public function test_get_item() {
|
||||
$this->markTestSkipped( 'Controller does not have get_item route.' );
|
||||
}
|
||||
|
||||
public function test_create_item() {
|
||||
$this->markTestSkipped( 'Controller does not have create_item route.' );
|
||||
}
|
||||
|
||||
public function test_update_item() {
|
||||
$this->markTestSkipped( 'Controller does not have update_item route.' );
|
||||
}
|
||||
|
||||
public function test_delete_item() {
|
||||
$this->markTestSkipped( 'Controller does not have delete_item route.' );
|
||||
}
|
||||
|
||||
public function test_prepare_item() {
|
||||
$this->markTestSkipped( 'Controller does not have prepare_item route.' );
|
||||
}
|
||||
|
||||
public function test_get_item_schema() {
|
||||
$this->markTestSkipped( 'Controller does not have get_item_schema route.' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_REST_Block_Patterns_Controller functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 6.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for Block Patterns.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @ticket 55505
|
||||
*
|
||||
* @covers WP_REST_Block_Patterns_Controller
|
||||
*
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* Admin user ID.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* Original instance of WP_Block_Patterns_Registry.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var WP_Block_Patterns_Registry
|
||||
*/
|
||||
protected static $orig_registry;
|
||||
|
||||
/**
|
||||
* Instance of the reflected `instance` property.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var ReflectionProperty
|
||||
*/
|
||||
private static $registry_instance_property;
|
||||
|
||||
/**
|
||||
* The REST API route.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const REQUEST_ROUTE = '/wp/v2/block-patterns/patterns';
|
||||
|
||||
/**
|
||||
* Set up class test fixtures.
|
||||
*
|
||||
* @since 6.0.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
|
||||
|
||||
// Setup an empty testing instance of `WP_Block_Patterns_Registry` and save the original.
|
||||
self::$orig_registry = WP_Block_Patterns_Registry::get_instance();
|
||||
self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Patterns_Registry', 'instance' );
|
||||
self::$registry_instance_property->setAccessible( true );
|
||||
$test_registry = new WP_Block_Pattern_Categories_Registry();
|
||||
self::$registry_instance_property->setValue( $test_registry );
|
||||
|
||||
// Register some patterns in the test registry.
|
||||
$test_registry->register(
|
||||
'test/one',
|
||||
array(
|
||||
'title' => 'Pattern One',
|
||||
'categories' => array( 'test' ),
|
||||
'viewportWidth' => 1440,
|
||||
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
|
||||
)
|
||||
);
|
||||
|
||||
$test_registry->register(
|
||||
'test/two',
|
||||
array(
|
||||
'title' => 'Pattern Two',
|
||||
'categories' => array( 'test' ),
|
||||
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
self::delete_user( self::$admin_id );
|
||||
|
||||
// Restore the original registry instance.
|
||||
self::$registry_instance_property->setValue( self::$orig_registry );
|
||||
self::$registry_instance_property->setAccessible( false );
|
||||
self::$registry_instance_property = null;
|
||||
self::$orig_registry = null;
|
||||
}
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
switch_theme( 'emptytheme' );
|
||||
}
|
||||
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( static::REQUEST_ROUTE, $routes );
|
||||
}
|
||||
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$request['_fields'] = 'name,content';
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
|
||||
$this->assertGreaterThanOrEqual( 2, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 2 items' );
|
||||
$this->assertSame(
|
||||
array(
|
||||
'name' => 'test/one',
|
||||
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
|
||||
),
|
||||
$data[0],
|
||||
'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
|
||||
);
|
||||
$this->assertSame(
|
||||
array(
|
||||
'name' => 'test/two',
|
||||
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
|
||||
),
|
||||
$data[1],
|
||||
'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify capability check for unauthorized request (not logged in).
|
||||
*/
|
||||
public function test_get_items_unauthorized() {
|
||||
// Ensure current user is logged out.
|
||||
wp_logout();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertWPError( $response->as_error() );
|
||||
$this->assertSame( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify capability check for forbidden request (insufficient capability).
|
||||
*/
|
||||
public function test_get_items_forbidden() {
|
||||
// Set current user without `edit_posts` capability.
|
||||
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
|
||||
$response = rest_do_request( $request );
|
||||
|
||||
$this->assertWPError( $response->as_error() );
|
||||
$this->assertSame( 403, $response->get_status() );
|
||||
}
|
||||
|
||||
public function test_context_param() {
|
||||
$this->markTestSkipped( 'Controller does not use context_param.' );
|
||||
}
|
||||
|
||||
public function test_get_item() {
|
||||
$this->markTestSkipped( 'Controller does not have get_item route.' );
|
||||
}
|
||||
|
||||
public function test_create_item() {
|
||||
$this->markTestSkipped( 'Controller does not have create_item route.' );
|
||||
}
|
||||
|
||||
public function test_update_item() {
|
||||
$this->markTestSkipped( 'Controller does not have update_item route.' );
|
||||
}
|
||||
|
||||
public function test_delete_item() {
|
||||
$this->markTestSkipped( 'Controller does not have delete_item route.' );
|
||||
}
|
||||
|
||||
public function test_prepare_item() {
|
||||
$this->markTestSkipped( 'Controller does not have prepare_item route.' );
|
||||
}
|
||||
|
||||
public function test_get_item_schema() {
|
||||
$this->markTestSkipped( 'Controller does not have get_item_schema route.' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user