Changeset 60720
- Timestamp:
- 09/08/2025 11:27:57 AM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/block-bindings/render.php
r60684 r60720 32 32 ), 33 33 'render_callback' => function ( $attributes ) { 34 return '<p>' . esc_html( $attributes['myAttribute'] ) . '</p>'; 34 if ( isset( $attributes['myAttribute'] ) ) { 35 return '<p>' . esc_html( $attributes['myAttribute'] ) . '</p>'; 36 } 35 37 }, 36 38 ) 39 ); 40 } 41 42 /** 43 * Sets up the test fixture. 44 * 45 * @since 6.9.0 46 */ 47 public function set_up() { 48 parent::set_up(); 49 50 add_filter( 51 'block_bindings_supported_attributes_test/block', 52 function ( $supported_attributes ) { 53 $supported_attributes[] = 'myAttribute'; 54 return $supported_attributes; 55 } 37 56 ); 38 57 } … … 84 103 '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">test source value</a></div>', 85 104 ), 105 'test block' => array( 106 'myAttribute', 107 <<<HTML 108 <!-- wp:test/block --> 109 <p>This should not appear</p> 110 <!-- /wp:test/block --> 111 HTML 112 , 113 '<p>test source value</p>', 114 ), 86 115 ); 87 116 } … … 134 163 } 135 164 136 /** 137 * Test if the block_bindings_supported_attributes_{$block_type} filter is applied correctly. 138 * 139 * @ticket 62090 140 */ 141 public function test_filter_block_bindings_supported_attributes() { 142 $get_value_callback = function () { 143 return 'test source value'; 144 }; 145 146 register_block_bindings_source( 147 self::SOURCE_NAME, 148 array( 149 'label' => self::SOURCE_LABEL, 150 'get_value_callback' => $get_value_callback, 151 ) 152 ); 153 154 add_filter( 155 'block_bindings_supported_attributes_test/block', 156 function ( $supported_attributes ) { 157 $supported_attributes[] = 'myAttribute'; 158 return $supported_attributes; 159 } 160 ); 161 162 $block_content = <<<HTML 163 <!-- wp:test/block {"metadata":{"bindings":{"myAttribute":{"source":"test/source"}}}} --> 164 <p>This should not appear</p> 165 <!-- /wp:test/block --> 166 HTML; 167 $parsed_blocks = parse_blocks( $block_content ); 168 $block = new WP_Block( $parsed_blocks[0] ); 169 $result = $block->render(); 170 171 $this->assertSame( 172 'test source value', 173 $block->attributes['myAttribute'], 174 "The 'myAttribute' attribute should be updated with the value returned by the source." 175 ); 176 $this->assertSame( 177 '<p>test source value</p>', 178 trim( $result ), 179 'The block content should be updated with the value returned by the source.' 165 public function data_different_get_value_callbacks() { 166 return array( 167 'pass arguments to source' => array( 168 function ( $source_args, $block_instance, $attribute_name ) { 169 $value = $source_args['key']; 170 return "The attribute name is '$attribute_name' and its binding has argument 'key' with value '$value'."; 171 }, 172 "<p>The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>", 173 ), 174 'unsafe HTML should be sanitized' => array( 175 function () { 176 return '<script>alert("Unsafe HTML")</script>'; 177 }, 178 '<p>alert("Unsafe HTML")</p>', 179 ), 180 'symbols and numbers should be rendered correctly' => array( 181 function () { 182 return '$12.50'; 183 }, 184 '<p>$12.50</p>', 185 ), 180 186 ); 181 187 } … … 185 191 * 186 192 * @ticket 60282 193 * @ticket 60651 194 * @ticket 61385 195 * @ticket 63840 187 196 * 188 197 * @covers ::register_block_bindings_source 189 */ 190 public function test_passing_arguments_to_source() { 191 $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { 192 $value = $source_args['key']; 193 return "The attribute name is '$attribute_name' and its binding has argument 'key' with value '$value'."; 194 }; 195 198 * 199 * @dataProvider data_different_get_value_callbacks 200 */ 201 public function test_different_get_value_callbacks( $get_value_callback, $expected ) { 196 202 register_block_bindings_source( 197 203 self::SOURCE_NAME, … … 212 218 213 219 $this->assertSame( 214 "The attribute name is 'content' and its binding has argument 'key' with value 'test'.", 215 $block->attributes['content'], 216 "The 'content' attribute should be updated with the value returned by the source." 217 ); 218 $this->assertSame( 219 "<p>The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>", 220 $expected, 220 221 trim( $result ), 221 222 'The block content should be updated with the value returned by the source.' … … 227 228 * 228 229 * @ticket 60525 230 * @ticket 61642 229 231 * 230 232 * @covers ::register_block_bindings_source 231 233 */ 232 234 public function test_passing_uses_context_to_source() { 233 $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { 235 $get_value_callback = function ( $source_args, $block_instance ) { 236 $this->assertArrayNotHasKey( 237 'forbiddenSourceContext', 238 $block_instance->context, 239 "Only context that was made available through the source's uses_context property should be accessible." 240 ); 234 241 $value = $block_instance->context['sourceContext']; 235 242 return "Value: $value"; … … 246 253 247 254 $block_content = <<<HTML 248 <!-- wp: paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "test"}}}}} -->255 <!-- wp:test/block {"metadata":{"bindings":{"myAttribute":{"source":"test/source", "args": {"key": "test"}}}}} --> 249 256 <p>This should not appear</p> 250 <!-- /wp:paragraph --> 251 HTML; 252 $parsed_blocks = parse_blocks( $block_content ); 253 $block = new WP_Block( $parsed_blocks[0], array( 'sourceContext' => 'source context value' ) ); 254 $result = $block->render(); 255 256 $this->assertSame( 257 'Value: source context value', 258 $block->attributes['content'], 259 "The 'content' should be updated with the value of the source context." 260 ); 261 $this->assertSame( 262 '<p>Value: source context value</p>', 263 trim( $result ), 264 'The block content should be updated with the value of the source context.' 265 ); 266 } 267 268 /** 269 * Tests that blocks can only access the context from the specific source. 270 * 271 * @ticket 61642 272 * 273 * @covers ::register_block_bindings_source 274 */ 275 public function test_blocks_can_just_access_the_specific_uses_context() { 276 register_block_bindings_source( 277 'test/source-one', 278 array( 279 'label' => 'Test Source One', 280 'get_value_callback' => function () { 281 return; 282 }, 283 'uses_context' => array( 'contextOne' ), 284 ) 285 ); 286 287 register_block_bindings_source( 288 'test/source-two', 289 array( 290 'label' => 'Test Source Two', 291 'get_value_callback' => function ( $source_args, $block_instance, $attribute_name ) { 292 $value = $block_instance->context['contextTwo']; 293 // Try to use the context from source one, which shouldn't be available. 294 if ( ! empty( $block_instance->context['contextOne'] ) ) { 295 $value = $block_instance->context['contextOne']; 296 } 297 return "Value: $value"; 298 }, 299 'uses_context' => array( 'contextTwo' ), 300 ) 301 ); 302 303 $block_content = <<<HTML 304 <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source-two", "args": {"key": "test"}}}}} --> 305 <p>Default content</p> 306 <!-- /wp:paragraph --> 257 <!-- /wp:test/block --> 307 258 HTML; 308 259 $parsed_blocks = parse_blocks( $block_content ); … … 310 261 $parsed_blocks[0], 311 262 array( 312 ' contextOne' => 'source one context value',313 ' contextTwo' => 'source two context value',263 'sourceContext' => 'source context value', 264 'forbiddenSourceContext' => 'forbidden donut', 314 265 ) 315 266 ); … … 317 268 318 269 $this->assertSame( 319 'Value: source twocontext value',320 $block->attributes[' content'],321 "The ' content' should be updated with the value of the second source context value."322 ); 323 $this->assertSame( 324 '<p>Value: source twocontext value</p>',270 'Value: source context value', 271 $block->attributes['myAttribute'], 272 "The 'myAttribute' should be updated with the value of the source context." 273 ); 274 $this->assertSame( 275 '<p>Value: source context value</p>', 325 276 trim( $result ), 326 277 'The block content should be updated with the value of the source context.' … … 371 322 372 323 /** 373 * Tests if the block content is sanitized when unsafe HTML is passed.374 *375 * @ticket 60651376 *377 * @covers ::register_block_bindings_source378 */379 public function test_source_value_with_unsafe_html_is_sanitized() {380 $get_value_callback = function () {381 return '<script>alert("Unsafe HTML")</script>';382 };383 384 register_block_bindings_source(385 self::SOURCE_NAME,386 array(387 'label' => self::SOURCE_LABEL,388 'get_value_callback' => $get_value_callback,389 )390 );391 392 $block_content = <<<HTML393 <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->394 <p>This should not appear</p>395 <!-- /wp:paragraph -->396 HTML;397 $parsed_blocks = parse_blocks( $block_content );398 $block = new WP_Block( $parsed_blocks[0] );399 $result = $block->render();400 401 $this->assertSame(402 '<p>alert("Unsafe HTML")</p>',403 trim( $result ),404 'The block content should be updated with the value returned by the source.'405 );406 }407 408 /**409 * Tests that including symbols and numbers works well with bound attributes.410 *411 * @ticket 61385412 *413 * @covers WP_Block::process_block_bindings414 */415 public function test_using_symbols_in_block_bindings_value() {416 $get_value_callback = function () {417 return '$12.50';418 };419 420 register_block_bindings_source(421 self::SOURCE_NAME,422 array(423 'label' => self::SOURCE_LABEL,424 'get_value_callback' => $get_value_callback,425 )426 );427 428 $block_content = <<<HTML429 <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->430 <p>Default content</p>431 <!-- /wp:paragraph -->432 HTML;433 $parsed_blocks = parse_blocks( $block_content );434 $block = new WP_Block( $parsed_blocks[0] );435 $result = $block->render();436 437 $this->assertSame(438 '<p>$12.50</p>',439 trim( $result ),440 'The block content should properly show the symbol and numbers.'441 );442 }443 444 /**445 324 * Tests if the `__default` attribute is replaced with real attributes for 446 325 * pattern overrides. … … 453 332 public function test_default_binding_for_pattern_overrides() { 454 333 $block_content = <<<HTML 455 <!-- wp: paragraph{"metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test"}} -->334 <!-- wp:test/block {"metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test"}} --> 456 335 <p>This should not appear</p> 457 <!-- /wp: paragraph-->336 <!-- /wp:test/block --> 458 337 HTML; 459 338 460 339 $expected_content = 'This is the content value'; 461 340 $parsed_blocks = parse_blocks( $block_content ); 462 $block = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( ' content' => $expected_content ) ) ) );341 $block = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'myAttribute' => $expected_content ) ) ) ); 463 342 464 343 $result = $block->render(); … … 471 350 472 351 $expected_bindings_metadata = array( 473 ' content' => array( 'source' => 'core/pattern-overrides' ),352 'myAttribute' => array( 'source' => 'core/pattern-overrides' ), 474 353 ); 475 354 $this->assertSame(
Note: See TracChangeset
for help on using the changeset viewer.