@@ -52,11 +52,18 @@ public function render_stub() {}
5252 * @since 5.0.0
5353 */
5454 public function tear_down () {
55- $ registry = WP_Block_Type_Registry ::get_instance ();
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 );
61+ }
62+ }
5663
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 );
64+ foreach ( wp_scripts ()-> registered as $ script_handle ) {
65+ if ( str_starts_with ( $ script_handle , ' tests- ' ) ) {
66+ wp_deregister_script ( $ script_handle );
6067 }
6168 }
6269
@@ -134,26 +141,26 @@ public function test_removes_block_asset_path_prefix_and_current_directory() {
134141 * @ticket 50263
135142 */
136143 public function test_generate_block_asset_handle () {
137- $ block_name = 'unit- tests/my-block ' ;
144+ $ block_name = 'tests/my-block ' ;
138145
139146 $ this ->assertSame (
140- 'unit- tests-my-block-editor-script ' ,
147+ 'tests-my-block-editor-script ' ,
141148 generate_block_asset_handle ( $ block_name , 'editorScript ' )
142149 );
143150 $ this ->assertSame (
144- 'unit- tests-my-block-script ' ,
151+ 'tests-my-block-script ' ,
145152 generate_block_asset_handle ( $ block_name , 'script ' , 0 )
146153 );
147154 $ this ->assertSame (
148- 'unit- tests-my-block-view-script-100 ' ,
155+ 'tests-my-block-view-script-100 ' ,
149156 generate_block_asset_handle ( $ block_name , 'viewScript ' , 99 )
150157 );
151158 $ this ->assertSame (
152- 'unit- tests-my-block-editor-style-2 ' ,
159+ 'tests-my-block-editor-style-2 ' ,
153160 generate_block_asset_handle ( $ block_name , 'editorStyle ' , 1 )
154161 );
155162 $ this ->assertSame (
156- 'unit- tests-my-block-style ' ,
163+ 'tests-my-block-style ' ,
157164 generate_block_asset_handle ( $ block_name , 'style ' )
158165 );
159166 }
@@ -226,7 +233,7 @@ public function test_wrong_array_index_do_not_register_block_script_handle() {
226233 public function test_missing_asset_file_register_block_script_handle () {
227234 $ metadata = array (
228235 'file ' => __FILE__ ,
229- 'name ' => 'unit- tests/test -block ' ,
236+ 'name ' => 'tests/sample -block ' ,
230237 'script ' => 'file:./blocks/notice/missing-asset.js ' ,
231238 );
232239 $ result = register_block_script_handle ( $ metadata , 'script ' );
@@ -264,14 +271,14 @@ public function test_handles_passed_register_block_script_handles() {
264271 public function test_success_register_block_script_handle () {
265272 $ metadata = array (
266273 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
267- 'name ' => 'unit- tests/test -block ' ,
274+ 'name ' => 'tests/sample -block ' ,
268275 'script ' => 'file:./block.js ' ,
269276 );
270277 $ result = register_block_script_handle ( $ metadata , 'script ' );
271278
272- $ this ->assertSame ( 'unit- tests-test -block-script ' , $ result );
279+ $ this ->assertSame ( 'tests-sample -block-script ' , $ result );
273280
274- // Test the behavior directly within the unit test
281+ // Test the behavior directly within the unit test.
275282 $ this ->assertFalse (
276283 strpos (
277284 wp_normalize_path ( realpath ( dirname ( $ metadata ['file ' ] ) . '/ ' . $ metadata ['script ' ] ) ),
@@ -287,6 +294,51 @@ public function test_success_register_block_script_handle() {
287294 );
288295 }
289296
297+ /**
298+ * @ticket TBD...
299+ */
300+ public function test_success_register_block_script_handle_with_custom_handle_name () {
301+ $ custom_script_handle = 'tests-my-shared-script ' ;
302+ $ metadata = array (
303+ 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
304+ 'name ' => 'tests/sample-block ' ,
305+ 'script ' => 'file:./shared-script.js ' ,
306+ );
307+ $ result = register_block_script_handle ( $ metadata , 'script ' );
308+
309+ $ this ->assertSame ( $ custom_script_handle , $ result );
310+ $ this ->assertStringEndsWith (
311+ 'shared-script.js ' ,
312+ wp_scripts ()->registered [ $ custom_script_handle ]->src
313+ );
314+ }
315+
316+ /**
317+ * @ticket TBD...
318+ */
319+ public function test_reuse_registered_block_script_handle_with_custom_handle_name () {
320+ $ custom_script_handle = 'tests-my-shared-script ' ;
321+ $ custom_script_src = 'https://example.com/foo.js ' ;
322+ wp_register_script ( $ custom_script_handle , $ custom_script_src );
323+
324+ $ this ->assertTrue (
325+ wp_script_is ( $ custom_script_handle , 'registered ' )
326+ );
327+
328+ $ metadata = array (
329+ 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
330+ 'name ' => 'tests/sample-block ' ,
331+ 'script ' => 'file:./shared-script.js ' ,
332+ );
333+ $ result = register_block_script_handle ( $ metadata , 'script ' );
334+
335+ $ this ->assertSame ( $ custom_script_handle , $ result );
336+ $ this ->assertSame (
337+ $ custom_script_src ,
338+ wp_scripts ()->registered [ $ custom_script_handle ]->src
339+ );
340+ }
341+
290342 /**
291343 * @ticket 55513
292344 */
@@ -440,21 +492,21 @@ public function test_handles_passed_register_block_style_handles() {
440492 public function test_success_register_block_style_handle () {
441493 $ metadata = array (
442494 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
443- 'name ' => 'unit- tests/test -block ' ,
495+ 'name ' => 'tests/sample -block ' ,
444496 'style ' => 'file:./block.css ' ,
445497 );
446498 $ result = register_block_style_handle ( $ metadata , 'style ' );
447499
448- $ this ->assertSame ( 'unit- tests-test -block-style ' , $ result );
449- $ this ->assertFalse ( wp_styles ()->get_data ( 'unit- tests-test -block-style ' , 'rtl ' ) );
500+ $ this ->assertSame ( 'tests-sample -block-style ' , $ result );
501+ $ this ->assertFalse ( wp_styles ()->get_data ( 'tests-sample -block-style ' , 'rtl ' ) );
450502
451- // @ticket 50328
503+ // @ticket 50328.
452504 $ this ->assertSame (
453505 wp_normalize_path ( realpath ( DIR_TESTDATA . '/blocks/notice/block.css ' ) ),
454- wp_normalize_path ( wp_styles ()->get_data ( 'unit- tests-test -block-style ' , 'path ' ) )
506+ wp_normalize_path ( wp_styles ()->get_data ( 'tests-sample -block-style ' , 'path ' ) )
455507 );
456508
457- // Test the behavior directly within the unit test
509+ // Test the behavior directly within the unit test.
458510 $ this ->assertFalse (
459511 strpos (
460512 wp_normalize_path ( realpath ( dirname ( $ metadata ['file ' ] ) . '/ ' . $ metadata ['style ' ] ) ),
@@ -483,22 +535,22 @@ public function test_register_block_style_handle_should_load_rtl_stylesheets_for
483535
484536 $ metadata = array (
485537 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
486- 'name ' => 'unit- tests/test -block-rtl ' ,
538+ 'name ' => 'tests/sample -block-rtl ' ,
487539 'style ' => 'file:./block.css ' ,
488540 );
489541
490542 $ orig_text_dir = $ wp_locale ->text_direction ;
491543 $ wp_locale ->text_direction = 'rtl ' ;
492544
493545 $ handle = register_block_style_handle ( $ metadata , 'style ' );
494- $ extra_rtl = wp_styles ()->get_data ( 'unit- tests-test -block-rtl-style ' , 'rtl ' );
495- $ extra_suffix = wp_styles ()->get_data ( 'unit- tests-test -block-rtl-style ' , 'suffix ' );
496- $ extra_path = wp_normalize_path ( wp_styles ()->get_data ( 'unit- tests-test -block-rtl-style ' , 'path ' ) );
546+ $ extra_rtl = wp_styles ()->get_data ( 'tests-sample -block-rtl-style ' , 'rtl ' );
547+ $ extra_suffix = wp_styles ()->get_data ( 'tests-sample -block-rtl-style ' , 'suffix ' );
548+ $ extra_path = wp_normalize_path ( wp_styles ()->get_data ( 'tests-sample -block-rtl-style ' , 'path ' ) );
497549
498550 $ wp_locale ->text_direction = $ orig_text_dir ;
499551
500552 $ this ->assertSame (
501- 'unit- tests-test -block-rtl-style ' ,
553+ 'tests-sample -block-rtl-style ' ,
502554 $ handle ,
503555 'The handle did not match the expected handle. '
504556 );
@@ -528,13 +580,13 @@ public function test_register_block_style_handle_should_load_rtl_stylesheets_for
528580 public function test_register_nonexistent_stylesheet () {
529581 $ metadata = array (
530582 'file ' => DIR_TESTDATA . '/blocks/notice/block.json ' ,
531- 'name ' => 'unit- tests/test -block-nonexistent-stylesheet ' ,
583+ 'name ' => 'tests/sample -block-nonexistent-stylesheet ' ,
532584 'style ' => 'file:./nonexistent.css ' ,
533585 );
534586 register_block_style_handle ( $ metadata , 'style ' );
535587
536588 global $ wp_styles ;
537- $ this ->assertFalse ( $ wp_styles ->registered ['unit- tests-test -block-nonexistent-stylesheet-style ' ]->src );
589+ $ this ->assertFalse ( $ wp_styles ->registered ['tests-sample -block-nonexistent-stylesheet-style ' ]->src );
538590 }
539591
540592 /**
@@ -638,13 +690,13 @@ public function test_block_registers_with_metadata_fixture() {
638690 $ result ->provides_context
639691 );
640692 $ this ->assertSameSets ( array ( 'groupId ' ), $ result ->uses_context );
641- // @ticket 57585
693+ // @ticket 57585.
642694 $ this ->assertSame (
643695 array ( 'root ' => '.wp-block-notice ' ),
644696 $ result ->selectors ,
645697 'Block type should contain selectors from metadata. '
646698 );
647- // @ticket 59346
699+ // @ticket 59346.
648700 $ this ->assertSameSets (
649701 array (
650702 'tests/before ' => 'before ' ,
@@ -716,13 +768,13 @@ public function test_block_registers_with_metadata_fixture() {
716768 $ result ->style_handles
717769 );
718770
719- // @ticket 50328
771+ // @ticket 50328.
720772 $ this ->assertSame (
721773 wp_normalize_path ( realpath ( DIR_TESTDATA . '/blocks/notice/block.css ' ) ),
722- wp_normalize_path ( wp_styles ()->get_data ( 'unit- tests-test -block-style ' , 'path ' ) )
774+ wp_normalize_path ( wp_styles ()->get_data ( 'tests-sample -block-style ' , 'path ' ) )
723775 );
724776
725- // @ticket 53148
777+ // @ticket 53148.
726778 $ this ->assertIsCallable ( $ result ->render_callback );
727779 }
728780
0 commit comments