Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3b29871
Block Hooks: Add controller to expose hooked blocks for a given ancho…
ockham Feb 2, 2024
dfff06a
Set context to template if applicable
ockham Feb 2, 2024
2b91b16
Add get_context() method to resolve the requested entity context
tjcafferkey Feb 6, 2024
b58c917
Get all hooked blocks grouped by anchor block
tjcafferkey Feb 7, 2024
5727b03
Fix get_items() to include hooked blocks by filters
tjcafferkey Feb 8, 2024
ceaf354
Check if array is empty
tjcafferkey Feb 8, 2024
d93fbd7
Make get_items() work with the block hooks filter:
tjcafferkey Feb 8, 2024
00bc67f
Add TODO
tjcafferkey Feb 8, 2024
39b65b8
Add item schema
tjcafferkey Feb 8, 2024
fd24615
get_items(): Fix logic for hooked blocks by position
tjcafferkey Feb 9, 2024
e481e1c
Rename var
ockham Feb 9, 2024
0f60bd9
Use array_column
ockham Feb 9, 2024
8eb0f86
Missed one
ockham Feb 9, 2024
6f9d82b
Change schema title
ockham Feb 9, 2024
4790aaf
Try suppressing hooked blocks for context creation
ockham Feb 9, 2024
e7370b5
Refactor suppression of hooked blocks whilst getting context
tjcafferkey Feb 9, 2024
f5a494a
Initialize id
ockham Feb 9, 2024
ab8fc5b
Allow specifying no context
ockham Feb 9, 2024
ec2bb3d
Add route for namespace only
ockham Feb 9, 2024
5924d63
Add basic test coverage
ockham Feb 9, 2024
ebbfe81
Tweak schema
ockham Feb 9, 2024
648703a
Refactor getting hooked blocks via filter hook into its own method
tjcafferkey Feb 9, 2024
4f75d51
Update block registry reference
tjcafferkey Feb 9, 2024
e193ecd
Inline block names
ockham Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add basic test coverage
  • Loading branch information
ockham committed Feb 9, 2024
commit 5924d639a49492deb31c42d6d7c9d65618aec0ad
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<?php
/**
* Unit tests covering Gutenberg_REST_Hooked_Blocks_Controller_6_5 functionality.
*
* @package WordPress
* @subpackage REST_API
* @since 6.5.0
*
* @covers WP_REST_Block_Types_Controller
*
* @group restapi-blocks
* @group restapi
*/
class Gutenberg_REST_Hooked_Blocks_Controller_Test extends WP_Test_REST_Controller_Testcase {

/**
* Admin user ID.
*
* @since 6.5.0
*
* @var int $subscriber_id
*/
protected static $admin_id;

/**
* Subscriber user ID.
*
* @since 6.5.0
*
* @var int $subscriber_id
*/
protected static $subscriber_id;

protected static $anchor_block_name = 'fake/anchor-block';
protected static $hooked_block_name = 'fake/hooked-block';

protected static $other_anchor_block_name = 'fake/other-anchor-block';
protected static $other_hooked_block_name = 'fake/other-hooked-block';

/**
* Create fake data before our tests run.
*
* @since 6.5.0
*
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
self::$subscriber_id = $factory->user->create(
array(
'role' => 'subscriber',
)
);

$anchor_block_settings = array(
'icon' => 'text',
);

$hooked_block_settings = array(
'block_hooks' => array(
'fake/anchor-block' => 'after',
)
);

register_block_type( self::$anchor_block_name, $anchor_block_settings );
register_block_type( self::$hooked_block_name, $hooked_block_settings );

$other_hooked_block_settings = array(
'block_hooks' => array(
'fake/other-anchor-block' => 'first_child',
)
);

register_block_type( self::$other_anchor_block_name, array() );
register_block_type( self::$other_hooked_block_name, $other_hooked_block_settings );
}

public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
self::delete_user( self::$subscriber_id );
unregister_block_type( self::$anchor_block_name );
unregister_block_type( self::$hooked_block_name );
unregister_block_type( self::$other_anchor_block_name );
unregister_block_type( self::$other_hooked_block_name );
}

public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/hooked-blocks', $routes );
$this->assertCount( 1, $routes['/wp/v2/hooked-blocks'] );
$this->assertArrayHasKey( '/wp/v2/hooked-blocks/(?P<namespace>[a-zA-Z0-9_-]+)', $routes );
$this->assertCount( 1, $routes['/wp/v2/hooked-blocks/(?P<namespace>[a-zA-Z0-9_-]+)'] );
$this->assertArrayHasKey( '/wp/v2/hooked-blocks/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)', $routes );
$this->assertCount( 1, $routes['/wp/v2/hooked-blocks/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)'] );
}

public function test_context_param() {
// Collection.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/hooked-blocks' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/hooked-blocks/fake/test' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}

public function test_get_items() {
$block_name = 'fake/test';
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/fake' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertCount( 2, $data );
$this->assertSame( $data, array(
self::$anchor_block_name => array(
'after' => array( self::$hooked_block_name )
),
self::$other_anchor_block_name => array(
'first_child' => array( self::$other_hooked_block_name )
),
) );
}

public function test_get_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/' . self::$anchor_block_name );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( $data, array(
'after' => array( self::$hooked_block_name )
) );
}

public function test_get_item_with_no_hooked_blocks() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/' . self::$hooked_block_name );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( $data, array() );
}

public function test_get_item_with_hooked_block_added_by_filter() {
$add_hooked_block = function( $hooked_block_types, $relative_position, $anchor_block_type ) {
if ( 'last_child' === $relative_position && self::$anchor_block_name === $anchor_block_type ) {
$hooked_block_types[] = 'fake/hooked-block-added-by-filter';
}
return $hooked_block_types;
};
add_filter( 'hooked_block_types', $add_hooked_block, 10, 3 );

wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/' . self::$anchor_block_name );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

remove_filter( 'hooked_block_types', $add_hooked_block, 10 );
$this->assertSame( $data, array(
'after' => array( self::$hooked_block_name ),
'last_child' => array( 'fake/hooked-block-added-by-filter' )
) );
}

public function test_get_block_invalid_name() {
$block_type = 'fake/block';
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/' . $block_type );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_block_type_invalid', $response, 404 );
}

public function test_get_item_schema() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/hooked-blocks' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 1, $properties );
$this->assertArrayHasKey( 'block_name', $properties );
}

public function test_get_items_wrong_permission() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 );
}

public function test_get_item_wrong_permission() {
wp_set_current_user( self::$subscriber_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/fake/test' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 );
}

public function test_get_items_no_permission() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 );
}

public function test_get_item_no_permission() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', '/wp/v2/hooked-blocks/fake/test' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 );
}

public function test_prepare_item() {
$registry = new WP_Block_Type_Registry();
$settings = array(
'icon' => 'text',
'render_callback' => '__return_null',
);
$registry->register( 'fake/line', $settings );
$block_type = $registry->get_registered( 'fake/line' );
$endpoint = new WP_REST_Block_Types_Controller();
$request = new WP_REST_Request();
$request->set_param( 'context', 'edit' );
$response = $endpoint->prepare_item_for_response( $block_type, $request );
// $this->assertSame();
}

public function test_prepare_item_limit_fields() {
$registry = new WP_Block_Type_Registry();
$settings = array(
'icon' => 'text',
'render_callback' => '__return_null',
);
$registry->register( 'fake/line', $settings );
$block_type = $registry->get_registered( 'fake/line' );
$request = new WP_REST_Request();
$endpoint = new WP_REST_Block_Types_Controller();
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'name' );
$response = $endpoint->prepare_item_for_response( $block_type, $request );
$this->assertSame(
array(
'name',
),
array_keys( $response->get_data() )
);
}

/**
* The create_item() method does not exist for hooked blocks.
*
* @doesNotPerformAssertions
*/
public function test_create_item() {
// Controller does not implement create_item().
}

/**
* The update_item() method does not exist for hooked blocks.
*
* @doesNotPerformAssertions
*/
public function test_update_item() {
// Controller does not implement create_item().
}

/**
* The delete_item() method does not exist for hooked blocks.
*
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not implement delete_item().
}
}