Changeset 57590 for trunk/tests/phpunit/tests/blocks/register.php
- Timestamp:
- 02/12/2024 11:40:49 AM (11 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/blocks/register.php
r57565 r57590 53 53 */ 54 54 public function tear_down() { 55 $registry = WP_Block_Type_Registry::get_instance(); 56 57 foreach ( array( 'core/test-static', 'core/test-dynamic', 'tests/notice' ) as $block_name ) { 58 if ( $registry->is_registered( $block_name ) ) { 59 $registry->unregister( $block_name ); 55 // Removes test block types registered by test cases. 56 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); 57 foreach ( $block_types as $block_type ) { 58 $block_name = $block_type->name; 59 if ( str_starts_with( $block_name, 'tests/' ) ) { 60 unregister_block_type( $block_name ); 60 61 } 61 62 } 62 63 63 64 foreach ( wp_scripts()->registered as $script_handle => $script ) { 64 if ( str_starts_with( $script_handle, ' unit-tests-' ) ) {65 if ( str_starts_with( $script_handle, 'tests-' ) ) { 65 66 wp_deregister_script( $script_handle ); 66 67 } … … 83 84 */ 84 85 public function test_register_affects_main_registry() { 85 $name = ' core/test-static';86 $name = 'tests/static'; 86 87 $settings = array( 87 88 'icon' => 'text', … … 98 99 */ 99 100 public function test_unregister_affects_main_registry() { 100 $name = ' core/test-static';101 $name = 'tests/static'; 101 102 $settings = array( 102 103 'icon' => 'text', … … 142 143 */ 143 144 public function test_generate_block_asset_handle() { 144 $block_name = ' unit-tests/my-block';145 146 $this->assertSame( 147 ' unit-tests-my-block-editor-script',145 $block_name = 'tests/my-block'; 146 147 $this->assertSame( 148 'tests-my-block-editor-script', 148 149 generate_block_asset_handle( $block_name, 'editorScript' ) 149 150 ); 150 151 $this->assertSame( 151 ' unit-tests-my-block-script',152 'tests-my-block-script', 152 153 generate_block_asset_handle( $block_name, 'script', 0 ) 153 154 ); 154 155 $this->assertSame( 155 ' unit-tests-my-block-view-script-100',156 'tests-my-block-view-script-100', 156 157 generate_block_asset_handle( $block_name, 'viewScript', 99 ) 157 158 ); 158 159 $this->assertSame( 159 ' unit-tests-my-block-view-script-module',160 'tests-my-block-view-script-module', 160 161 generate_block_asset_handle( $block_name, 'viewScriptModule' ) 161 162 ); 162 163 $this->assertSame( 163 ' unit-tests-my-block-view-script-module-2',164 'tests-my-block-view-script-module-2', 164 165 generate_block_asset_handle( $block_name, 'viewScriptModule', 1 ) 165 166 ); 166 167 $this->assertSame( 167 ' unit-tests-my-block-view-script-module-100',168 'tests-my-block-view-script-module-100', 168 169 generate_block_asset_handle( $block_name, 'viewScriptModule', 99 ) 169 170 ); 170 171 $this->assertSame( 171 ' unit-tests-my-block-editor-style-2',172 'tests-my-block-editor-style-2', 172 173 generate_block_asset_handle( $block_name, 'editorStyle', 1 ) 173 174 ); 174 175 $this->assertSame( 175 ' unit-tests-my-block-style',176 'tests-my-block-style', 176 177 generate_block_asset_handle( $block_name, 'style' ) 177 178 ); 178 179 // @ticket 59673 179 180 $this->assertSame( 180 ' unit-tests-my-block-view-style',181 'tests-my-block-view-style', 181 182 generate_block_asset_handle( $block_name, 'viewStyle' ), 182 183 'asset handle for viewStyle is not generated correctly' … … 336 337 $metadata = array( 337 338 'file' => __FILE__, 338 'name' => ' unit-tests/test-block',339 'name' => 'tests/test-block', 339 340 'viewScriptModule' => 'file:./blocks/notice/missing-asset.js', 340 341 ); 341 342 $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); 342 343 343 $this->assertSame( ' unit-tests-test-block-view-script-module', $result );344 $this->assertSame( 'tests-test-block-view-script-module', $result ); 344 345 } 345 346 … … 377 378 $metadata = array( 378 379 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 379 'name' => ' unit-tests/test-block',380 'name' => 'tests/test-block', 380 381 'viewScriptModule' => 'file:./block.js', 381 382 ); 382 383 $result = register_block_script_module_id( $metadata, 'viewScriptModule' ); 383 384 384 $this->assertSame( ' unit-tests-test-block-view-script-module', $result );385 386 // Test the behavior directly within the unit test 385 $this->assertSame( 'tests-test-block-view-script-module', $result ); 386 387 // Test the behavior directly within the unit test. 387 388 $this->assertFalse( 388 389 strpos( … … 431 432 $metadata = array( 432 433 'file' => __FILE__, 433 'name' => ' unit-tests/test-block',434 'name' => 'tests/test-block', 434 435 'script' => 'file:./blocks/notice/missing-asset.js', 435 436 ); 436 437 $result = register_block_script_handle( $metadata, 'script' ); 437 438 438 $this->assertSame( ' unit-tests-test-block-script', $result );439 $this->assertSame( 'tests-test-block-script', $result ); 439 440 } 440 441 … … 445 446 $metadata = array( 446 447 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 447 'name' => ' unit-tests/test-block',448 'name' => 'tests/test-block', 448 449 'script' => 'file:./block.js', 449 450 ); 450 451 $result = register_block_script_handle( $metadata, 'script' ); 451 452 452 $this->assertSame( ' unit-tests-test-block-script', $result );453 454 // Test the behavior directly within the unit test 453 $this->assertSame( 'tests-test-block-script', $result ); 454 455 // Test the behavior directly within the unit test. 455 456 $this->assertFalse( 456 457 strpos( … … 465 466 trailingslashit( wp_normalize_path( get_stylesheet_directory() ) ) 466 467 ) === 0 468 ); 469 } 470 471 /** 472 * @ticket 60485 473 */ 474 public function test_success_register_block_script_handle_with_custom_handle_name() { 475 $custom_script_handle = 'tests-my-shared-script'; 476 $metadata = array( 477 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 478 'name' => 'tests/sample-block', 479 'script' => 'file:./shared-script.js', 480 ); 481 $result = register_block_script_handle( $metadata, 'script' ); 482 483 $this->assertSame( $custom_script_handle, $result ); 484 $this->assertStringEndsWith( 485 'shared-script.js', 486 wp_scripts()->registered[ $custom_script_handle ]->src 487 ); 488 } 489 490 /** 491 * @ticket 60485 492 */ 493 public function test_reuse_registered_block_script_handle_with_custom_handle_name() { 494 $custom_script_handle = 'tests-my-shared-script'; 495 $custom_script_src = 'https://example.com/foo.js'; 496 wp_register_script( $custom_script_handle, $custom_script_src ); 497 498 $this->assertTrue( 499 wp_script_is( $custom_script_handle, 'registered' ) 500 ); 501 502 $metadata = array( 503 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 504 'name' => 'tests/sample-block', 505 'script' => 'file:./shared-script.js', 506 ); 507 $result = register_block_script_handle( $metadata, 'script' ); 508 509 $this->assertSame( $custom_script_handle, $result ); 510 $this->assertSame( 511 $custom_script_src, 512 wp_scripts()->registered[ $custom_script_handle ]->src 467 513 ); 468 514 } … … 621 667 $metadata = array( 622 668 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 623 'name' => ' unit-tests/test-block',669 'name' => 'tests/test-block', 624 670 'style' => 'file:./block.css', 625 671 'viewStyle' => 'file:./block-view.css', … … 627 673 $result = register_block_style_handle( $metadata, 'style' ); 628 674 629 $this->assertSame( ' unit-tests-test-block-style', $result );630 $this->assertFalse( wp_styles()->get_data( ' unit-tests-test-block-style', 'rtl' ) );675 $this->assertSame( 'tests-test-block-style', $result ); 676 $this->assertFalse( wp_styles()->get_data( 'tests-test-block-style', 'rtl' ) ); 631 677 632 678 // @ticket 50328 633 679 $this->assertSame( 634 680 wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ), 635 wp_normalize_path( wp_styles()->get_data( ' unit-tests-test-block-style', 'path' ) )681 wp_normalize_path( wp_styles()->get_data( 'tests-test-block-style', 'path' ) ) 636 682 ); 637 683 638 684 // Test viewStyle property 639 685 $result = register_block_style_handle( $metadata, 'viewStyle' ); 640 $this->assertSame( ' unit-tests-test-block-view-style', $result );686 $this->assertSame( 'tests-test-block-view-style', $result ); 641 687 642 688 // @ticket 59673 643 689 $this->assertSame( 644 690 wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ), 645 wp_normalize_path( wp_styles()->get_data( ' unit-tests-test-block-view-style', 'path' ) ),691 wp_normalize_path( wp_styles()->get_data( 'tests-test-block-view-style', 'path' ) ), 646 692 'viewStyle asset path is not correct' 647 693 ); 648 694 649 // Test the behavior directly within the unit test 695 // Test the behavior directly within the unit test. 650 696 $this->assertFalse( 651 697 strpos( … … 676 722 $metadata = array( 677 723 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 678 'name' => ' unit-tests/test-block-rtl',724 'name' => 'tests/test-block-rtl', 679 725 'style' => 'file:./block.css', 680 726 ); … … 684 730 685 731 $handle = register_block_style_handle( $metadata, 'style' ); 686 $extra_rtl = wp_styles()->get_data( ' unit-tests-test-block-rtl-style', 'rtl' );687 $extra_suffix = wp_styles()->get_data( ' unit-tests-test-block-rtl-style', 'suffix' );688 $extra_path = wp_normalize_path( wp_styles()->get_data( ' unit-tests-test-block-rtl-style', 'path' ) );732 $extra_rtl = wp_styles()->get_data( 'tests-test-block-rtl-style', 'rtl' ); 733 $extra_suffix = wp_styles()->get_data( 'tests-test-block-rtl-style', 'suffix' ); 734 $extra_path = wp_normalize_path( wp_styles()->get_data( 'tests-test-block-rtl-style', 'path' ) ); 689 735 690 736 $wp_locale->text_direction = $orig_text_dir; 691 737 692 738 $this->assertSame( 693 ' unit-tests-test-block-rtl-style',739 'tests-test-block-rtl-style', 694 740 $handle, 695 741 'The handle did not match the expected handle.' … … 721 767 $metadata = array( 722 768 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 723 'name' => ' unit-tests/test-block-nonexistent-stylesheet',769 'name' => 'tests/test-block-nonexistent-stylesheet', 724 770 'style' => 'file:./nonexistent.css', 725 771 ); … … 727 773 728 774 global $wp_styles; 729 $this->assertFalse( $wp_styles->registered[' unit-tests-test-block-nonexistent-stylesheet-style']->src );775 $this->assertFalse( $wp_styles->registered['tests-test-block-nonexistent-stylesheet-style']->src ); 730 776 } 731 777 … … 1045 1091 $this->assertSame( 1046 1092 wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ), 1047 wp_normalize_path( wp_styles()->get_data( ' unit-tests-test-block-style', 'path' ) )1093 wp_normalize_path( wp_styles()->get_data( 'tests-test-block-style', 'path' ) ) 1048 1094 ); 1049 1095 … … 1051 1097 $this->assertSame( 1052 1098 wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block-view.css' ) ), 1053 wp_normalize_path( wp_styles()->get_data( ' unit-tests-test-block-view-style', 'path' ) ),1099 wp_normalize_path( wp_styles()->get_data( 'tests-test-block-view-style', 'path' ) ), 1054 1100 'viewStyle asset path is not correct' 1055 1101 ); … … 1090 1136 public function test_register_block_type_accepts_editor_script_array( $editor_script, $expected ) { 1091 1137 $settings = array( 'editor_script' => $editor_script ); 1092 register_block_type( ' core/test-static', $settings );1138 register_block_type( 'tests/static', $settings ); 1093 1139 1094 1140 $registry = WP_Block_Type_Registry::get_instance(); 1095 $block_type = $registry->get_registered( ' core/test-static' );1141 $block_type = $registry->get_registered( 'tests/static' ); 1096 1142 $this->assertObjectHasProperty( 'editor_script_handles', $block_type ); 1097 1143 $actual_script = $block_type->editor_script; … … 1156 1202 public function test_register_block_type_throws_doing_it_wrong( $editor_script, $expected ) { 1157 1203 $settings = array( 'editor_script' => $editor_script ); 1158 register_block_type( ' core/test-static', $settings );1204 register_block_type( 'tests/static', $settings ); 1159 1205 1160 1206 $registry = WP_Block_Type_Registry::get_instance(); 1161 $block_type = $registry->get_registered( ' core/test-static' );1207 $block_type = $registry->get_registered( 'tests/static' ); 1162 1208 $this->assertObjectHasProperty( 'editor_script_handles', $block_type ); 1163 1209 $actual_script = $block_type->editor_script; … … 1249 1295 */ 1250 1296 public function test_get_dynamic_block_names() { 1251 register_block_type( ' core/test-static', array() );1252 register_block_type( ' core/test-dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) );1297 register_block_type( 'tests/static', array() ); 1298 register_block_type( 'tests/dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) ); 1253 1299 1254 1300 $dynamic_block_names = get_dynamic_block_names(); 1255 1301 1256 $this->assertContains( ' core/test-dynamic', $dynamic_block_names );1257 $this->assertNotContains( ' core/test-static', $dynamic_block_names );1302 $this->assertContains( 'tests/dynamic', $dynamic_block_names ); 1303 $this->assertNotContains( 'tests/static', $dynamic_block_names ); 1258 1304 } 1259 1305
Note: See TracChangeset
for help on using the changeset viewer.