diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 22c3328582616..807b3cc03e6d1 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -344,6 +344,201 @@ public static function data_admin_bar_nodes_with_tabindex_meta() { ); } + /** + * The icon markup wraps the SVG of a registered icon. + * + * @ticket 66491 + * + * @covers ::_wp_admin_bar_icon + */ + public function test_admin_bar_icon_wraps_registered_icon() { + $registry = WP_Icons_Registry::get_instance(); + $name = 'core-admin/admin-bar-test-icon'; + + $registered = $registry->register( + $name, + array( + 'label' => 'Admin bar test icon', + 'content' => '', + ) + ); + $output = _wp_admin_bar_icon( $name ); + $expected = ''; + $registry->unregister( $name ); + + $this->assertTrue( $registered, 'The test icon should be registered.' ); + $this->assertSame( $expected, $output ); + $this->assertStringContainsString( 'assertSame( '', _wp_admin_bar_icon( 'core-admin/this-icon-does-not-exist' ) ); + } + + /** + * The sidebar toggle is added in the admin. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_sidebar_toggle + */ + public function test_sidebar_toggle_is_added_in_admin() { + wp_set_current_user( self::$editor_id ); + set_current_screen( 'dashboard' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node_toggle = $wp_admin_bar->get_node( 'menu-toggle' ); + + $this->assertNotNull( $node_toggle ); + $this->assertSame( 'core-admin/menu', $node_toggle->icon ); + $this->assertSame( '#', $node_toggle->href ); + $this->assertSame( 'Menu', $node_toggle->title ); + } + + /** + * The sidebar toggle is not added on the front end. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_sidebar_toggle + */ + public function test_sidebar_toggle_is_not_added_on_front_end() { + wp_set_current_user( self::$editor_id ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + + $this->assertNull( $wp_admin_bar->get_node( 'menu-toggle' ) ); + } + + /** + * The command palette button is added in the admin when the commands script is enqueued. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_command_palette_menu + */ + public function test_command_palette_menu_is_added_in_admin_when_commands_script_is_enqueued() { + $original_scripts = $GLOBALS['wp_scripts'] ?? null; + $original_agent = $_SERVER['HTTP_USER_AGENT'] ?? null; + + $GLOBALS['wp_scripts'] = null; + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'; + wp_register_script( 'wp-core-commands', false ); + wp_enqueue_script( 'wp-core-commands' ); + + wp_set_current_user( self::$editor_id ); + set_current_screen( 'dashboard' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node_palette = $wp_admin_bar->get_node( 'command-palette' ); + + $GLOBALS['wp_scripts'] = $original_scripts; + if ( null === $original_agent ) { + unset( $_SERVER['HTTP_USER_AGENT'] ); + } else { + $_SERVER['HTTP_USER_AGENT'] = $original_agent; + } + + $this->assertNotNull( $node_palette ); + $this->assertSame( 'core-admin/search', $node_palette->icon ); + $this->assertSame( '#', $node_palette->href ); + $this->assertSame( 'hide-if-no-js', $node_palette->meta['class'] ); + $this->assertSame( 'wp.data.dispatch( "core/commands" ).open(); return false;', $node_palette->meta['onclick'] ); + $this->assertSame( + 'Ctrl+K Open command palette', + $node_palette->title + ); + $this->assertStringContainsString( 'meta['html'] ); + $this->assertStringContainsString( 'sourceURL=wp_admin_bar_command_palette_menu', $node_palette->meta['html'] ); + } + + /** + * The command palette button shows the Apple keyboard shortcut for Apple devices. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_command_palette_menu + */ + public function test_command_palette_menu_shows_apple_shortcut_for_apple_user_agent() { + $original_scripts = $GLOBALS['wp_scripts'] ?? null; + $original_agent = $_SERVER['HTTP_USER_AGENT'] ?? null; + + $GLOBALS['wp_scripts'] = null; + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15'; + wp_register_script( 'wp-core-commands', false ); + wp_enqueue_script( 'wp-core-commands' ); + + wp_set_current_user( self::$editor_id ); + set_current_screen( 'dashboard' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node_palette = $wp_admin_bar->get_node( 'command-palette' ); + + $GLOBALS['wp_scripts'] = $original_scripts; + if ( null === $original_agent ) { + unset( $_SERVER['HTTP_USER_AGENT'] ); + } else { + $_SERVER['HTTP_USER_AGENT'] = $original_agent; + } + + $this->assertStringContainsString( '⌘K', $node_palette->title ); + $this->assertStringNotContainsString( 'Ctrl+K', $node_palette->title ); + } + + /** + * The command palette button is not added when the commands script is not enqueued. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_command_palette_menu + */ + public function test_command_palette_menu_is_not_added_without_commands_script() { + $original_scripts = $GLOBALS['wp_scripts'] ?? null; + + $GLOBALS['wp_scripts'] = null; + + wp_set_current_user( self::$editor_id ); + set_current_screen( 'dashboard' ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node_palette = $wp_admin_bar->get_node( 'command-palette' ); + + $GLOBALS['wp_scripts'] = $original_scripts; + + $this->assertNull( $node_palette ); + } + + /** + * The command palette button is not added on the front end. + * + * @ticket 66491 + * + * @covers ::wp_admin_bar_command_palette_menu + */ + public function test_command_palette_menu_is_not_added_on_front_end() { + $original_scripts = $GLOBALS['wp_scripts'] ?? null; + + $GLOBALS['wp_scripts'] = null; + wp_register_script( 'wp-core-commands', false ); + wp_enqueue_script( 'wp-core-commands' ); + + wp_set_current_user( self::$editor_id ); + + $wp_admin_bar = $this->get_standard_admin_bar(); + $node_palette = $wp_admin_bar->get_node( 'command-palette' ); + + $GLOBALS['wp_scripts'] = $original_scripts; + + $this->assertNull( $node_palette ); + } + /** * @ticket 22247 */