mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Introduce get_block_asset_url Utility Function.
This commit introduces a valuable utility function, get_block_asset_url, designed to simplify the retrieval of block asset URLs, such as those for CSS and JavaScript files. This utility eliminates redundancy in both register_block_script_handle and register_block_style_handle. Additionally, `get_block_asset_url` incorporates an early exit mechanism to optimize performance. This update includes comprehensive unit tests, covering various scenarios, including asset registration from core (wp-includes), themes, child themes, plugins, and mu-plugins. Props spacedmonkey, joemcgill, flixos90, gziolo. Fixes #58525. git-svn-id: https://develop.svn.wordpress.org/trunk@56683 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information
1 parent
54c4de1
commit a68650e
Showing
11 changed files
with
196 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"apiVersion": 2, | ||
"title": "Example Theme Block", | ||
"name": "block-theme/example-block", | ||
"description": "Custom block registered from within a theme", | ||
"editorScript": "file:./index.js", | ||
"style": "file:./style.css" | ||
} |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/editor-style-rtl.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test CSS file - RTL version */ |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/editor-style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test CSS file */ |
6 changes: 6 additions & 0 deletions
6
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/index.asset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return array( | ||
'dependencies' => array( 'wp-element', 'wp-blocks' ), | ||
'version' => 'test', | ||
); |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test JavaScript file. */ |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/style-rtl.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test CSS file - RTL version */ |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test CSS file */ |
6 changes: 6 additions & 0 deletions
6
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/view.asset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return array( | ||
'dependencies' => array( 'wp-element', 'wp-blocks' ), | ||
'version' => 'test', | ||
); |
1 change: 1 addition & 0 deletions
1
tests/phpunit/data/themedir1/block-theme-child/blocks/example-block/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Test JavaScript file. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Tests for block asset urls. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @group blocks | ||
* @covers ::get_block_asset_url | ||
*/ | ||
class Tests_Get_Block_Asset_Url extends WP_UnitTestCase { | ||
/** | ||
* Original theme directory. | ||
* | ||
* @var string[] | ||
*/ | ||
private $orig_theme_dir; | ||
|
||
public function set_up() { | ||
global $wp_theme_directories; | ||
|
||
parent::set_up(); | ||
|
||
// Sets up the `wp-content/themes/` directory to ensure consistency when running tests. | ||
$this->orig_theme_dir = $wp_theme_directories; | ||
$wp_theme_directories = array( WP_CONTENT_DIR . '/themes', realpath( DIR_TESTDATA . '/themedir1' ) ); | ||
|
||
wp_clean_themes_cache(); | ||
unset( $GLOBALS['wp_themes'] ); | ||
} | ||
|
||
public function tear_down() { | ||
global $wp_theme_directories; | ||
|
||
$wp_theme_directories = $this->orig_theme_dir; | ||
|
||
wp_clean_themes_cache(); | ||
unset( $GLOBALS['wp_themes'] ); | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_core_block() { | ||
$path = ABSPATH . WPINC . '/blocks/file/view.min.js'; | ||
$url = get_block_asset_url( $path ); | ||
|
||
$this->assertStringNotContainsString( ABSPATH . WPINC, 'The return block asset url should not contain include path.' ); | ||
$this->assertSame( includes_url( '/blocks/file/view.min.js' ), $url, 'The return block asset url should match includes url.' ); | ||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_parent_theme() { | ||
switch_theme( 'block-theme' ); | ||
|
||
$path = wp_normalize_path( realpath( DIR_TESTDATA . '/themedir1/block-theme/blocks/example-block/view.js' ) ); | ||
$url = get_block_asset_url( $path ); | ||
|
||
$this->assertSame( get_template_directory_uri() . '/blocks/example-block/view.js', $url ); | ||
|
||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_child_theme() { | ||
switch_theme( 'block-theme-child' ); | ||
|
||
$path = wp_normalize_path( realpath( DIR_TESTDATA . '/themedir1/block-theme-child/blocks/example-block/view.js' ) ); | ||
$url = get_block_asset_url( $path ); | ||
|
||
$this->assertSame( get_stylesheet_directory_uri() . '/blocks/example-block/view.js', $url ); | ||
|
||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_plugin() { | ||
$path = WP_PLUGIN_DIR . '/test-plugin/blocks/example-block/view.js'; | ||
$url = get_block_asset_url( $path ); | ||
|
||
$this->assertStringNotContainsString( WP_PLUGIN_DIR, $url, 'The return block asset url should not contain plugin path.' ); | ||
$this->assertSame( plugins_url( 'view.js', $path ), $url, 'The return block asset url should match plugin url.' ); | ||
$this->assertStringStartsWith( WP_PLUGIN_URL, $url, 'The return block asset url should contain the url that support with the mu plugin url.' ); | ||
|
||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_muplugin() { | ||
$path = WPMU_PLUGIN_DIR . '/test-plugin/example-block/view.js'; | ||
$url = get_block_asset_url( $path ); | ||
|
||
$this->assertStringNotContainsString( WPMU_PLUGIN_DIR, $url, 'The return block asset url should not contain plugin path.' ); | ||
$this->assertSame( plugins_url( 'view.js', $path ), $url, 'The return block asset url should match plugin url.' ); | ||
$this->assertStringStartsWith( WPMU_PLUGIN_URL, $url, 'The return block asset url should contain the url that support with the mu plugin url.' ); | ||
} | ||
|
||
/** | ||
* @ticket 58525 | ||
*/ | ||
public function test_empty() { | ||
$url = get_block_asset_url( '' ); | ||
|
||
$this->assertFalse( $url ); | ||
} | ||
|
||
} |