Changeset 56761
- Timestamp:
- 10/03/2023 08:52:54 AM (16 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/blocks/renderBlock.php
r56760 r56761 1 1 <?php 2 2 /** 3 * Tests for block contextfunctions.3 * Tests for render block functions. 4 4 * 5 5 * @package WordPress … … 9 9 * @group blocks 10 10 */ 11 class Tests_Blocks_Context extends WP_UnitTestCase { 12 13 /** 14 * Registered block names. 15 * 16 * @var string[] 17 */ 18 private $registered_block_names = array(); 11 class Tests_Blocks_RenderBlock extends WP_UnitTestCase { 19 12 20 13 /** … … 39 32 */ 40 33 public function tear_down() { 41 while ( ! empty( $this->registered_block_names ) ) { 42 $block_name = array_pop( $this->registered_block_names ); 43 unregister_block_type( $block_name ); 34 // Removes test block types registered by test cases. 35 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); 36 foreach ( $block_types as $block_type ) { 37 $block_name = $block_type->name; 38 if ( str_starts_with( $block_name, 'tests/' ) ) { 39 unregister_block_type( $block_name ); 40 } 44 41 } 45 42 46 43 parent::tear_down(); 47 }48 49 /**50 * Registers a block type.51 *52 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a53 * complete WP_Block_Type instance. In case a WP_Block_Type54 * is provided, the $args parameter will be ignored.55 * @param array $args {56 * Optional. Array of block type arguments. Any arguments may be defined, however the57 * ones described below are supported by default. Default empty array.58 *59 * @type callable $render_callback Callback used to render blocks of this block type.60 * }61 */62 protected function register_block_type( $name, $args ) {63 register_block_type( $name, $args );64 65 $this->registered_block_names[] = $name;66 44 } 67 45 … … 71 49 * 72 50 * @ticket 49927 51 * 52 * @covers ::register_block_type 53 * @covers ::render_block 73 54 */ 74 55 public function test_provides_block_context() { 75 56 $provided_context = array(); 76 57 77 $this->register_block_type(78 ' gutenberg/test-context-provider',58 register_block_type( 59 'tests/context-provider', 79 60 array( 80 61 'attributes' => array( … … 94 75 ), 95 76 'provides_context' => array( 96 ' gutenberg/contextWithAssigned' => 'contextWithAssigned',97 ' gutenberg/contextWithDefault' => 'contextWithDefault',98 ' gutenberg/contextWithoutDefault' => 'contextWithoutDefault',99 ' gutenberg/contextNotRequested' => 'contextNotRequested',77 'tests/contextWithAssigned' => 'contextWithAssigned', 78 'tests/contextWithDefault' => 'contextWithDefault', 79 'tests/contextWithoutDefault' => 'contextWithoutDefault', 80 'tests/contextNotRequested' => 'contextNotRequested', 100 81 ), 101 82 ) 102 83 ); 103 84 104 $this->register_block_type(105 ' gutenberg/test-context-consumer',85 register_block_type( 86 'tests/context-consumer', 106 87 array( 107 88 'uses_context' => array( 108 ' gutenberg/contextWithDefault',109 ' gutenberg/contextWithAssigned',110 ' gutenberg/contextWithoutDefault',89 'tests/contextWithDefault', 90 'tests/contextWithAssigned', 91 'tests/contextWithoutDefault', 111 92 ), 112 93 'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) { … … 119 100 120 101 $parsed_blocks = parse_blocks( 121 '<!-- wp: gutenberg/test-context-provider {"contextWithAssigned":10} -->' .122 '<!-- wp: gutenberg/test-context-consumer /-->' .123 '<!-- /wp: gutenberg/test-context-provider -->'102 '<!-- wp:tests/context-provider {"contextWithAssigned":10} -->' . 103 '<!-- wp:tests/context-consumer /-->' . 104 '<!-- /wp:tests/context-provider -->' 124 105 ); 125 106 … … 128 109 $this->assertSame( 129 110 array( 130 ' gutenberg/contextWithDefault' => 0,131 ' gutenberg/contextWithAssigned' => 10,111 'tests/contextWithDefault' => 0, 112 'tests/contextWithAssigned' => 10, 132 113 ), 133 114 $provided_context[0] … … 140 121 * 141 122 * @ticket 49927 123 * 124 * @covers ::register_block_type 125 * @covers ::render_block 142 126 */ 143 127 public function test_provides_default_context() { … … 146 130 $provided_context = array(); 147 131 148 $this->register_block_type(149 ' gutenberg/test-context-consumer',132 register_block_type( 133 'tests/context-consumer', 150 134 array( 151 135 'uses_context' => array( 'postId', 'postType' ), … … 158 142 ); 159 143 160 $parsed_blocks = parse_blocks( '<!-- wp: gutenberg/test-context-consumer /-->' );144 $parsed_blocks = parse_blocks( '<!-- wp:tests/context-consumer /-->' ); 161 145 162 146 render_block( $parsed_blocks[0] ); … … 175 159 * 176 160 * @ticket 49927 161 * 162 * @covers ::register_block_type 163 * @covers ::render_block 177 164 */ 178 165 public function test_default_context_is_filterable() { 179 166 $provided_context = array(); 180 167 181 $this->register_block_type(182 ' gutenberg/test-context-consumer',168 register_block_type( 169 'tests/context-consumer', 183 170 array( 184 171 'uses_context' => array( 'example' ), … … 196 183 }; 197 184 198 $parsed_blocks = parse_blocks( '<!-- wp: gutenberg/test-context-consumer /-->' );185 $parsed_blocks = parse_blocks( '<!-- wp:tests/context-consumer /-->' ); 199 186 200 187 add_filter( 'render_block_context', $filter_block_context );
Note: See TracChangeset
for help on using the changeset viewer.