fix: guard CHRNX editorial state
This commit is contained in:
8 files changed
+285
-19
No files matched your search
@@ -93,12 +93,63 @@ describe( 'podcast editor helpers', () => {
|
||||
expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
} );
|
||||
|
||||
test( 'gracefully handles missing globals, post data, meta, and unresolved topic terms', () => {
|
||||
test( 'fails closed when required globals or initial editor selector methods are missing', () => {
|
||||
const helpers = require( modulePath );
|
||||
expect( helpers.initialize() ).toBe( false );
|
||||
expect( helpers.initialize( {} ) ).toBe( false );
|
||||
|
||||
for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) {
|
||||
const missingEditorMethod = createWp();
|
||||
missingEditorMethod.wp.data.select = () => editorSelector;
|
||||
expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow();
|
||||
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false );
|
||||
expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled();
|
||||
expect( missingEditorMethod.editPost ).not.toHaveBeenCalled();
|
||||
}
|
||||
|
||||
for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) {
|
||||
const incomplete = createWp();
|
||||
delete incomplete.wp[ key ];
|
||||
expect( helpers.initialize( incomplete.wp ) ).toBe( false );
|
||||
expect( incomplete.registerPlugin ).not.toHaveBeenCalled();
|
||||
expect( incomplete.editPost ).not.toHaveBeenCalled();
|
||||
}
|
||||
} );
|
||||
|
||||
test( 'reactive render tolerates missing post methods, meta, and terms without dispatching', () => {
|
||||
const helpers = require( modulePath );
|
||||
const incomplete = createWp();
|
||||
expect( helpers.initialize( incomplete.wp ) ).toBe( true );
|
||||
incomplete.wp.data.useSelect = ( mapper ) =>
|
||||
mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecord: jest.fn() } ) );
|
||||
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
expect( incomplete.editPost ).not.toHaveBeenCalled();
|
||||
|
||||
incomplete.wp.data.useSelect = ( mapper ) =>
|
||||
mapper( ( store ) =>
|
||||
store === 'core/editor'
|
||||
? {
|
||||
getCurrentPostType: () => 'post',
|
||||
getEditedPostAttribute: jest.fn(),
|
||||
}
|
||||
: {},
|
||||
);
|
||||
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
|
||||
incomplete.wp.data.useSelect = createWp().wp.data.useSelect;
|
||||
incomplete.wp.data.useDispatch = () => ( {} );
|
||||
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
expect( incomplete.editPost ).not.toHaveBeenCalled();
|
||||
|
||||
const unresolved = createWp( null );
|
||||
expect( () => helpers.initialize( unresolved.wp ) ).not.toThrow();
|
||||
unresolved.state.meta = undefined;
|
||||
unresolved.state.topic = undefined;
|
||||
expect( helpers.initialize( unresolved.wp ) ).toBe( true );
|
||||
expect( () => unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
expect( unresolved.editPost ).not.toHaveBeenCalled();
|
||||
} );
|
||||
} );
|
||||
@@ -87,16 +87,64 @@ describe( 'editor taxonomy helpers', () => {
|
||||
} );
|
||||
} );
|
||||
|
||||
test( 'does nothing safely outside post editors or without usable globals or terms', () => {
|
||||
test( 'fails closed when required globals or initial editor selector methods are missing', () => {
|
||||
const helpers = require( modulePath );
|
||||
expect( helpers.initialize() ).toBe( false );
|
||||
expect( helpers.initialize( {} ) ).toBe( false );
|
||||
const { wp, registerPlugin, attributes, terms } = createWp();
|
||||
|
||||
for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) {
|
||||
const missingEditorMethod = createWp();
|
||||
missingEditorMethod.wp.data.select = () => editorSelector;
|
||||
expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow();
|
||||
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false );
|
||||
expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled();
|
||||
expect( missingEditorMethod.editPost ).not.toHaveBeenCalled();
|
||||
}
|
||||
|
||||
for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) {
|
||||
const incomplete = createWp();
|
||||
delete incomplete.wp[ key ];
|
||||
expect( helpers.initialize( incomplete.wp ) ).toBe( false );
|
||||
expect( incomplete.registerPlugin ).not.toHaveBeenCalled();
|
||||
expect( incomplete.editPost ).not.toHaveBeenCalled();
|
||||
}
|
||||
} );
|
||||
|
||||
test( 'reactive render tolerates unresolved editor methods and terms without dispatching', () => {
|
||||
const helpers = require( modulePath );
|
||||
const { wp, registerPlugin, editPost } = createWp();
|
||||
expect( helpers.initialize( wp ) ).toBe( true );
|
||||
|
||||
wp.data.useSelect = ( mapper ) =>
|
||||
mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecords: jest.fn() } ) );
|
||||
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
expect( editPost ).not.toHaveBeenCalled();
|
||||
|
||||
wp.data.useSelect = ( mapper ) =>
|
||||
mapper( ( store ) =>
|
||||
store === 'core/editor'
|
||||
? {
|
||||
getCurrentPostType: () => 'post',
|
||||
getEditedPostAttribute: jest.fn(),
|
||||
}
|
||||
: {},
|
||||
);
|
||||
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
|
||||
wp.data.useSelect = createWp().wp.data.useSelect;
|
||||
wp.data.useDispatch = () => ( {} );
|
||||
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
||||
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
||||
expect( editPost ).not.toHaveBeenCalled();
|
||||
} );
|
||||
|
||||
test( 'does not register taxonomy panels outside post editors', () => {
|
||||
const helpers = require( modulePath );
|
||||
const { wp, registerPlugin, attributes } = createWp();
|
||||
attributes.type = 'page';
|
||||
expect( helpers.initialize( wp ) ).toBe( false );
|
||||
expect( registerPlugin ).not.toHaveBeenCalled();
|
||||
attributes.type = 'post';
|
||||
terms.chrnx_topic = undefined;
|
||||
expect( () => helpers.initialize( wp ) ).not.toThrow();
|
||||
} );
|
||||
} );
|
||||
@@ -0,0 +1,126 @@
|
||||
<?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' ) 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' ), $handles );
|
||||
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', '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 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,7 @@ class PodcastMetaTest extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_canonical_url_sanitizer_accepts_only_https() {
|
||||
$this->assertSame( 'https://example.test/episode?ref=1', chrnx_sanitize_canonical_episode_url( ' https://example.test/episode?ref=1 ' ) );
|
||||
$this->assertSame( 'https://example.test/an%20episode', chrnx_sanitize_canonical_episode_url( 'https://example.test/an episode' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'http://example.test/episode' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'javascript:alert(1)' ) );
|
||||
$this->assertSame( '', chrnx_sanitize_canonical_episode_url( 'not a url' ) );
|
||||
|
||||
Reference in new issue
Block a user