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' ) ); } } }