Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions tests/phpunit/tests/adminbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 0h24v24H0z"/></svg>',
)
);
$output = _wp_admin_bar_icon( $name );
$expected = '<span class="ab-icon svg-icon" aria-hidden="true">' . wp_get_icon( $name ) . '</span>';
$registry->unregister( $name );

$this->assertTrue( $registered, 'The test icon should be registered.' );
$this->assertSame( $expected, $output );
$this->assertStringContainsString( '<svg', $output );
}

/**
* No markup is returned for an icon that is not registered.
*
* @ticket 66491
*
* @covers ::_wp_admin_bar_icon
*/
public function test_admin_bar_icon_returns_empty_string_for_unknown_icon() {
$this->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( '<span class="screen-reader-text">Menu</span>', $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(
'<span class="ab-label"><kbd>Ctrl+K</kbd><span class="screen-reader-text"> Open command palette</span></span>',
$node_palette->title
);
$this->assertStringContainsString( '<script', $node_palette->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( '<kbd>⌘K</kbd>', $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
*/
Expand Down
Loading