Loading admin_toolbar.module +2 −0 Original line number Diff line number Diff line Loading @@ -96,7 +96,9 @@ function toolbar_tools_menu_navigation_links(array $tree) { // @phpstan-ignore offsetAssign.dimType $element->options['attributes']['class'][] = 'toolbar-icon'; $string = strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id'])); // @phpstan-ignore assign.propertyType $element->options['attributes']['class'][] = Html::cleanCssIdentifier('toolbar-icon-' . $string); // @phpstan-ignore assign.propertyType $element->options['attributes']['title'] = $link->getDescription(); } return $tree; Loading tests/src/Functional/AdminToolbarToolsSortTest.php +9 −41 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Tests\admin_toolbar\Functional; use Drupal\Tests\admin_toolbar\Traits\AdminToolbarHelperTestTrait; use Drupal\Tests\BrowserTestBase; use Drupal\media\Entity\MediaType; use Drupal\system\Entity\Menu; Loading @@ -13,13 +14,12 @@ use Drupal\system\Entity\Menu; */ class AdminToolbarToolsSortTest extends BrowserTestBase { use AdminToolbarHelperTestTrait; /** * {@inheritdoc} */ protected static $modules = [ 'toolbar', 'breakpoint', 'admin_toolbar', 'admin_toolbar_tools', 'menu_ui', 'media', Loading Loading @@ -81,7 +81,7 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { ]); $chinchilla_media_type->save(); $this->drupalGet('/admin'); $this->assertMenuHasHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkExists('admin/structure/media/manage/chinchilla', 'Chinchilla'); // Assert that adding a menu adds it to the admin toolbar. $menu = Menu::create([ Loading @@ -90,21 +90,21 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { ]); $menu->save(); $this->drupalGet('/admin'); $this->assertMenuHasHref('/admin/structure/menu/manage/chupacabra'); $this->assertAdminToolbarMenuLinkExists('admin/structure/menu/manage/chupacabra', 'Chupacabra'); // Assert that deleting a menu removes it from the admin toolbar. $this->assertMenuHasHref('/admin/structure/menu/manage/armadillo'); $this->assertAdminToolbarMenuLinkExists('admin/structure/menu/manage/armadillo', 'Armadillo'); $menu = Menu::load('armadillo'); $menu->delete(); $this->drupalGet('/admin'); $this->assertMenuDoesNotHaveHref('/admin/structure/menu/manage/armadillo'); $this->assertAdminToolbarMenuLinkNotExists('armadillo'); // Assert that deleting a content entity bundle removes it from admin menu. $this->assertMenuHasHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkExists('admin/structure/media/manage/chinchilla', 'Chinchilla'); $chinchilla_media_type = MediaType::load('chinchilla'); $chinchilla_media_type->delete(); $this->drupalGet('/admin'); $this->assertMenuDoesNotHaveHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkNotExists('chinchilla'); } /** Loading Loading @@ -229,36 +229,4 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { } } /** * Checks that there is a link with the specified url in the admin toolbar. * * @param string $url * The url to assert exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function assertMenuHasHref($url) { $this->assertSession() ->elementExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]'); } /** * Checks that there is no link with the specified url in the admin toolbar. * * @param string $url * The url to assert exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ExpectationException */ protected function assertMenuDoesNotHaveHref($url) { $this->assertSession() ->elementNotExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]'); } } tests/src/Traits/AdminToolbarHelperTestTrait.php 0 → 100644 +99 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\admin_toolbar\Traits; /** * @file * Contains methods used for PHPUNIT test cases for admin toolbar modules. */ /** * Adds common methods for the test classes of the admin toolbar modules. * * Provides common functionality for any class using the trait to implement * Functional or FunctionJavascript test cases: * - Assert a link in the admin menu exists or not. * * @see \Drupal\Tests\admin_toolbar\Functional\AdminToolbarToolsSortTest */ trait AdminToolbarHelperTestTrait { /** * The HTML IDs used to test the display of the admin toolbar. * * @var array<string> */ protected $testAdminToolbarHtmlIds = [ 'admin_tray' => 'toolbar-item-administration-tray', ]; /** * Checks that a specific link exists in the admin toolbar. * * @param string $link_url * The url of the link to assert it exists in the admin menu. * @param string $link_text * The optional text in the link to assert it exists in the admin menu. * @param int $link_position * The optional position of the link in the menu to assert it exists in the * admin menu. The position is 1-based, so the first link is in position 1. * If not provided or 0, the position is not checked. * @param string $link_css_class * The optional CSS class in the link to assert it exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function assertAdminToolbarMenuLinkExists(string $link_url = '', string $link_text = '', int $link_position = 0, string $link_css_class = '') { // Build a CSS selector with the provided conditions. $link_css_conditions = ''; // If a link text is provided, use case sensitive ':contains()' operator. if (!empty($link_text)) { $link_css_conditions .= ':contains("' . $link_text . '")'; } if (!empty($link_url)) { // A CSS selector has to be used here, since xpath 1.0 does not support // 'ends-with'. Use the ends with CSS selector '$=' to support absolute // URLs while still ensuring the expected URL is an exact match. $link_css_conditions .= '[href$="' . $link_url . '"]'; } // If a class is provided, check the classes of the link contain it ('*='). if (!empty($link_css_class)) { $link_css_conditions .= '[class*="' . $link_css_class . '"]'; } // If a position is provided, add a ':nth-child()' selector to the CSS // expression. $link_position_conditions = ''; if ($link_position > 0) { $link_position_conditions = ':nth-child(' . $link_position . ')'; } // Assert whether the link selected with the CSS expression exists in the // admin toolbar. $this->assertSession() ->elementExists('css', 'div[id="' . $this->testAdminToolbarHtmlIds['admin_tray'] . '"] li' . $link_position_conditions . '[class*="menu-item"] a' . $link_css_conditions); } /** * Checks that a specific link does not exist in the admin toolbar. * * @param string $link_url * The url to assert it does not exist in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ExpectationException */ protected function assertAdminToolbarMenuLinkNotExists($link_url) { // A simple xpath expression is enough here, since it should be less // restrictive, in terms of conditions. $this->assertSession() ->elementNotExists('xpath', '//div[@id="' . $this->testAdminToolbarHtmlIds['admin_tray'] . '"]//a[contains(@href, "' . $link_url . '")]'); } } Loading
admin_toolbar.module +2 −0 Original line number Diff line number Diff line Loading @@ -96,7 +96,9 @@ function toolbar_tools_menu_navigation_links(array $tree) { // @phpstan-ignore offsetAssign.dimType $element->options['attributes']['class'][] = 'toolbar-icon'; $string = strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id'])); // @phpstan-ignore assign.propertyType $element->options['attributes']['class'][] = Html::cleanCssIdentifier('toolbar-icon-' . $string); // @phpstan-ignore assign.propertyType $element->options['attributes']['title'] = $link->getDescription(); } return $tree; Loading
tests/src/Functional/AdminToolbarToolsSortTest.php +9 −41 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\Tests\admin_toolbar\Functional; use Drupal\Tests\admin_toolbar\Traits\AdminToolbarHelperTestTrait; use Drupal\Tests\BrowserTestBase; use Drupal\media\Entity\MediaType; use Drupal\system\Entity\Menu; Loading @@ -13,13 +14,12 @@ use Drupal\system\Entity\Menu; */ class AdminToolbarToolsSortTest extends BrowserTestBase { use AdminToolbarHelperTestTrait; /** * {@inheritdoc} */ protected static $modules = [ 'toolbar', 'breakpoint', 'admin_toolbar', 'admin_toolbar_tools', 'menu_ui', 'media', Loading Loading @@ -81,7 +81,7 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { ]); $chinchilla_media_type->save(); $this->drupalGet('/admin'); $this->assertMenuHasHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkExists('admin/structure/media/manage/chinchilla', 'Chinchilla'); // Assert that adding a menu adds it to the admin toolbar. $menu = Menu::create([ Loading @@ -90,21 +90,21 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { ]); $menu->save(); $this->drupalGet('/admin'); $this->assertMenuHasHref('/admin/structure/menu/manage/chupacabra'); $this->assertAdminToolbarMenuLinkExists('admin/structure/menu/manage/chupacabra', 'Chupacabra'); // Assert that deleting a menu removes it from the admin toolbar. $this->assertMenuHasHref('/admin/structure/menu/manage/armadillo'); $this->assertAdminToolbarMenuLinkExists('admin/structure/menu/manage/armadillo', 'Armadillo'); $menu = Menu::load('armadillo'); $menu->delete(); $this->drupalGet('/admin'); $this->assertMenuDoesNotHaveHref('/admin/structure/menu/manage/armadillo'); $this->assertAdminToolbarMenuLinkNotExists('armadillo'); // Assert that deleting a content entity bundle removes it from admin menu. $this->assertMenuHasHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkExists('admin/structure/media/manage/chinchilla', 'Chinchilla'); $chinchilla_media_type = MediaType::load('chinchilla'); $chinchilla_media_type->delete(); $this->drupalGet('/admin'); $this->assertMenuDoesNotHaveHref('/admin/structure/media/manage/chinchilla'); $this->assertAdminToolbarMenuLinkNotExists('chinchilla'); } /** Loading Loading @@ -229,36 +229,4 @@ class AdminToolbarToolsSortTest extends BrowserTestBase { } } /** * Checks that there is a link with the specified url in the admin toolbar. * * @param string $url * The url to assert exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function assertMenuHasHref($url) { $this->assertSession() ->elementExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]'); } /** * Checks that there is no link with the specified url in the admin toolbar. * * @param string $url * The url to assert exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ExpectationException */ protected function assertMenuDoesNotHaveHref($url) { $this->assertSession() ->elementNotExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]'); } }
tests/src/Traits/AdminToolbarHelperTestTrait.php 0 → 100644 +99 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\admin_toolbar\Traits; /** * @file * Contains methods used for PHPUNIT test cases for admin toolbar modules. */ /** * Adds common methods for the test classes of the admin toolbar modules. * * Provides common functionality for any class using the trait to implement * Functional or FunctionJavascript test cases: * - Assert a link in the admin menu exists or not. * * @see \Drupal\Tests\admin_toolbar\Functional\AdminToolbarToolsSortTest */ trait AdminToolbarHelperTestTrait { /** * The HTML IDs used to test the display of the admin toolbar. * * @var array<string> */ protected $testAdminToolbarHtmlIds = [ 'admin_tray' => 'toolbar-item-administration-tray', ]; /** * Checks that a specific link exists in the admin toolbar. * * @param string $link_url * The url of the link to assert it exists in the admin menu. * @param string $link_text * The optional text in the link to assert it exists in the admin menu. * @param int $link_position * The optional position of the link in the menu to assert it exists in the * admin menu. The position is 1-based, so the first link is in position 1. * If not provided or 0, the position is not checked. * @param string $link_css_class * The optional CSS class in the link to assert it exists in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ElementNotFoundException */ protected function assertAdminToolbarMenuLinkExists(string $link_url = '', string $link_text = '', int $link_position = 0, string $link_css_class = '') { // Build a CSS selector with the provided conditions. $link_css_conditions = ''; // If a link text is provided, use case sensitive ':contains()' operator. if (!empty($link_text)) { $link_css_conditions .= ':contains("' . $link_text . '")'; } if (!empty($link_url)) { // A CSS selector has to be used here, since xpath 1.0 does not support // 'ends-with'. Use the ends with CSS selector '$=' to support absolute // URLs while still ensuring the expected URL is an exact match. $link_css_conditions .= '[href$="' . $link_url . '"]'; } // If a class is provided, check the classes of the link contain it ('*='). if (!empty($link_css_class)) { $link_css_conditions .= '[class*="' . $link_css_class . '"]'; } // If a position is provided, add a ':nth-child()' selector to the CSS // expression. $link_position_conditions = ''; if ($link_position > 0) { $link_position_conditions = ':nth-child(' . $link_position . ')'; } // Assert whether the link selected with the CSS expression exists in the // admin toolbar. $this->assertSession() ->elementExists('css', 'div[id="' . $this->testAdminToolbarHtmlIds['admin_tray'] . '"] li' . $link_position_conditions . '[class*="menu-item"] a' . $link_css_conditions); } /** * Checks that a specific link does not exist in the admin toolbar. * * @param string $link_url * The url to assert it does not exist in the admin menu. * * @return void * Nothing to return. * * @throws \Behat\Mink\Exception\ExpectationException */ protected function assertAdminToolbarMenuLinkNotExists($link_url) { // A simple xpath expression is enough here, since it should be less // restrictive, in terms of conditions. $this->assertSession() ->elementNotExists('xpath', '//div[@id="' . $this->testAdminToolbarHtmlIds['admin_tray'] . '"]//a[contains(@href, "' . $link_url . '")]'); } }