From ea502c457090896960109f750347e265c052b276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 3 Oct 2023 08:52:54 +0000 Subject: [PATCH] Tests: Rename and improve the `blocks/context.php` file to follow handbook Renames `context.php` and `Tests_Blocks_Context` to `renderBlock.php` and `Tests_Blocks_RenderBlock`. See https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#test-classes. Simplifies also the tear_down method by using the same convention as in [56759]. Props costdev, ockham. Follow-up [48224]. See #49927. git-svn-id: https://develop.svn.wordpress.org/trunk@56761 602fd350-edb4-49c9-b593-d223f7449a82 --- .../blocks/{context.php => renderBlock.php} | 93 ++++++++----------- 1 file changed, 40 insertions(+), 53 deletions(-) rename tests/phpunit/tests/blocks/{context.php => renderBlock.php} (57%) diff --git a/tests/phpunit/tests/blocks/context.php b/tests/phpunit/tests/blocks/renderBlock.php similarity index 57% rename from tests/phpunit/tests/blocks/context.php rename to tests/phpunit/tests/blocks/renderBlock.php index 3edddcf8ef..0c1c4ce10a 100644 --- a/tests/phpunit/tests/blocks/context.php +++ b/tests/phpunit/tests/blocks/renderBlock.php @@ -1,6 +1,6 @@ registered_block_names ) ) { - $block_name = array_pop( $this->registered_block_names ); - unregister_block_type( $block_name ); + // Removes test block types registered by test cases. + $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); + foreach ( $block_types as $block_type ) { + $block_name = $block_type->name; + if ( str_starts_with( $block_name, 'tests/' ) ) { + unregister_block_type( $block_name ); + } } parent::tear_down(); } - /** - * Registers a block type. - * - * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a - * complete WP_Block_Type instance. In case a WP_Block_Type - * is provided, the $args parameter will be ignored. - * @param array $args { - * Optional. Array of block type arguments. Any arguments may be defined, however the - * ones described below are supported by default. Default empty array. - * - * @type callable $render_callback Callback used to render blocks of this block type. - * } - */ - protected function register_block_type( $name, $args ) { - register_block_type( $name, $args ); - - $this->registered_block_names[] = $name; - } - /** * Tests that a block which provides context makes that context available to * its inner blocks. * * @ticket 49927 + * + * @covers ::register_block_type + * @covers ::render_block */ public function test_provides_block_context() { $provided_context = array(); - $this->register_block_type( - 'gutenberg/test-context-provider', + register_block_type( + 'tests/context-provider', array( 'attributes' => array( 'contextWithAssigned' => array( @@ -93,21 +74,21 @@ class Tests_Blocks_Context extends WP_UnitTestCase { ), ), 'provides_context' => array( - 'gutenberg/contextWithAssigned' => 'contextWithAssigned', - 'gutenberg/contextWithDefault' => 'contextWithDefault', - 'gutenberg/contextWithoutDefault' => 'contextWithoutDefault', - 'gutenberg/contextNotRequested' => 'contextNotRequested', + 'tests/contextWithAssigned' => 'contextWithAssigned', + 'tests/contextWithDefault' => 'contextWithDefault', + 'tests/contextWithoutDefault' => 'contextWithoutDefault', + 'tests/contextNotRequested' => 'contextNotRequested', ), ) ); - $this->register_block_type( - 'gutenberg/test-context-consumer', + register_block_type( + 'tests/context-consumer', array( 'uses_context' => array( - 'gutenberg/contextWithDefault', - 'gutenberg/contextWithAssigned', - 'gutenberg/contextWithoutDefault', + 'tests/contextWithDefault', + 'tests/contextWithAssigned', + 'tests/contextWithoutDefault', ), 'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) { $provided_context[] = $block->context; @@ -118,17 +99,17 @@ class Tests_Blocks_Context extends WP_UnitTestCase { ); $parsed_blocks = parse_blocks( - '' . - '' . - '' + '' . + '' . + '' ); render_block( $parsed_blocks[0] ); $this->assertSame( array( - 'gutenberg/contextWithDefault' => 0, - 'gutenberg/contextWithAssigned' => 10, + 'tests/contextWithDefault' => 0, + 'tests/contextWithAssigned' => 10, ), $provided_context[0] ); @@ -139,14 +120,17 @@ class Tests_Blocks_Context extends WP_UnitTestCase { * render_block. * * @ticket 49927 + * + * @covers ::register_block_type + * @covers ::render_block */ public function test_provides_default_context() { global $post; $provided_context = array(); - $this->register_block_type( - 'gutenberg/test-context-consumer', + register_block_type( + 'tests/context-consumer', array( 'uses_context' => array( 'postId', 'postType' ), 'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) { @@ -157,7 +141,7 @@ class Tests_Blocks_Context extends WP_UnitTestCase { ) ); - $parsed_blocks = parse_blocks( '' ); + $parsed_blocks = parse_blocks( '' ); render_block( $parsed_blocks[0] ); @@ -174,12 +158,15 @@ class Tests_Blocks_Context extends WP_UnitTestCase { * Tests that default block context can be filtered. * * @ticket 49927 + * + * @covers ::register_block_type + * @covers ::render_block */ public function test_default_context_is_filterable() { $provided_context = array(); - $this->register_block_type( - 'gutenberg/test-context-consumer', + register_block_type( + 'tests/context-consumer', array( 'uses_context' => array( 'example' ), 'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) { @@ -195,7 +182,7 @@ class Tests_Blocks_Context extends WP_UnitTestCase { return $context; }; - $parsed_blocks = parse_blocks( '' ); + $parsed_blocks = parse_blocks( '' ); add_filter( 'render_block_context', $filter_block_context );