Files
chrnx-dev-wp/tests/php/EditorAssetsTest.php
T
2026-08-15 14:58:19 -05:00

191 lines
6.3 KiB
PHP

<?php
/**
* Tests for block editor asset loading.
*
* @package CHRNX
*/
/**
* Verify editor scripts are tightly scoped to the post block editor.
*/
class EditorAssetsTest extends WP_UnitTestCase {
/**
* Original current screen.
*
* @var WP_Screen|null
*/
private $original_screen;
/**
* Preserve screen state and reset script handles.
*/
public function set_up() {
parent::set_up();
$this->original_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
$this->reset_editor_scripts();
}
/**
* Restore screen state and registered scripts.
*/
public function tear_down() {
$this->reset_editor_scripts();
if ( $this->original_screen instanceof WP_Screen ) {
set_current_screen( $this->original_screen->id );
get_current_screen()->is_block_editor( $this->original_screen->is_block_editor() );
} else {
$GLOBALS['current_screen'] = null;
}
parent::tear_down();
}
/**
* Clear only the CHRNX editor script state.
*/
private function reset_editor_scripts() {
foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-track' ) as $handle ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
}
}
/**
* Set an actual WordPress screen and its editor mode.
*
* @param string $screen_id Screen ID.
* @param bool $is_block_editor Whether block editor mode is active.
* @return WP_Screen Configured screen.
*/
private function set_screen( $screen_id, $is_block_editor ) {
set_current_screen( $screen_id );
$screen = get_current_screen();
$screen->is_block_editor( $is_block_editor );
return $screen;
}
/**
* Post block editor enqueues exactly the two versioned local scripts.
*/
public function test_post_block_editor_enqueues_exact_editor_scripts() {
$screen = $this->set_screen( 'post', true );
$this->assertTrue( chrnx_is_post_block_editor_screen( $screen ) );
do_action( 'enqueue_block_editor_assets' );
$scripts = wp_scripts();
$handles = array_values(
array_filter(
$scripts->queue,
static function ( $handle ) {
return 0 === strpos( $handle, 'chrnx-editor-' );
}
)
);
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-track' ), $handles );
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' );
foreach (
array(
'chrnx-editor-taxonomies' => 'assets/js/editor-taxonomies.js',
'chrnx-editor-podcast' => 'assets/js/editor-podcast.js',
) as $handle => $asset
) {
$this->assertArrayHasKey( $handle, $scripts->registered );
$this->assertSame( get_theme_file_uri( $asset ), $scripts->registered[ $handle ]->src );
$this->assertSame( $expected_dependencies, $scripts->registered[ $handle ]->deps );
$this->assertSame( CHRNX_VERSION, $scripts->registered[ $handle ]->ver );
$this->assertSame( 'chrnx', $scripts->registered[ $handle ]->textdomain );
}
}
/**
* Frontend, page, list, and classic-editor contexts enqueue nothing.
*/
public function test_irrelevant_contexts_do_not_enqueue_editor_scripts() {
$contexts = array(
array( null, false ),
array( 'page', true ),
array( 'edit-post', false ),
array( 'post', false ),
);
foreach ( $contexts as $context ) {
$this->reset_editor_scripts();
if ( null === $context[0] ) {
$GLOBALS['current_screen'] = null;
} else {
$this->set_screen( $context[0], $context[1] );
}
do_action( 'enqueue_block_editor_assets' );
$this->assertFalse( wp_script_is( 'chrnx-editor-taxonomies', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'chrnx-editor-podcast', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'chrnx-editor-track', 'enqueued' ) );
}
}
/** Related Stories editor metadata registers one local translated script with exact dependencies. */
public function test_related_posts_registers_exact_editor_asset_metadata() {
do_action( 'init' );
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/related-posts' );
$this->assertInstanceOf( WP_Block_Type::class, $type );
$this->assertCount( 1, $type->editor_script_handles );
$handle = $type->editor_script_handles[0];
$this->assertNotEmpty( $handle );
$manifest_path = get_theme_file_path( 'assets/js/editor-related-posts.asset.php' );
$this->assertFileExists( $manifest_path );
$manifest = require $manifest_path;
$this->assertSame(
array( 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render' ),
$manifest['dependencies']
);
$scripts = wp_scripts();
$this->assertArrayHasKey( $handle, $scripts->registered );
$registered = $scripts->registered[ $handle ];
$this->assertSame( get_theme_file_uri( 'assets/js/editor-related-posts.js' ), $registered->src );
$this->assertSame( $manifest['dependencies'], $registered->deps );
$this->assertSame( CHRNX_VERSION, $registered->ver );
$this->assertSame( 'chrnx', $registered->textdomain );
}
/** Related Stories remains metadata-registered on post, Site Editor, and screenless init. */
public function test_related_posts_registration_is_not_post_screen_guarded() {
foreach ( array( null, 'post', 'site-editor' ) as $screen_id ) {
if ( null === $screen_id ) {
$GLOBALS['current_screen'] = null;
} else {
$this->set_screen( $screen_id, true );
}
do_action( 'init' );
$this->assertInstanceOf(
WP_Block_Type::class,
WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/related-posts' ),
(string) $screen_id
);
}
}
/** Related Stories view metadata registers the exact existing local controller only. */
public function test_related_posts_view_script_resolves_to_local_controller() {
do_action( 'init' );
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/related-posts' );
$this->assertInstanceOf( WP_Block_Type::class, $type );
$this->assertCount( 1, $type->view_script_handles );
$handle = $type->view_script_handles[0];
$this->assertNotEmpty( $handle );
$scripts = wp_scripts();
$this->assertArrayHasKey( $handle, $scripts->registered );
$registered = $scripts->registered[ $handle ];
$this->assertSame( get_theme_file_uri( 'assets/js/related-posts.js' ), $registered->src );
$this->assertSame( array(), $registered->deps );
$this->assertFileExists( get_theme_file_path( 'assets/js/related-posts.js' ) );
}
}