diff --git a/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinkClickTrait.php b/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinkClickTrait.php index dc1afc3..2866e91 100644 --- a/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinkClickTrait.php +++ b/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinkClickTrait.php @@ -2,8 +2,6 @@ namespace Drupal\Tests\contextual\FunctionalJavascript; -use Behat\Mink\Element\NodeElement; - /** * Functions for testing contextual links. */ @@ -12,28 +10,37 @@ /** * Clicks a contextual link. * - * @param \Behat\Mink\Element\NodeElement $element - * The element that contains the contextual link. + * @param string $selector + * The selector for the element that contains the contextual link. * @param string $link_locator * The link id, title, or text. * @param bool $force_visible * If true then the button will be forced to visible so it can be clicked. */ - protected function clickContextualLink(NodeElement $element, $link_locator, $force_visible = TRUE) { - $button = $element->find('css', '.contextual button'); - $find_button_script = "jQuery('#{$element->getAttribute('id')} .contextual button')"; + protected function clickContextualLink($selector, $link_locator, $force_visible = TRUE) { if ($force_visible) { - // The button to show contextual links is only visible when in focus. - // Force button to be visible. - $this->getSession()->executeScript($find_button_script . '.removeClass("visually-hidden");'); + $this->toggleContextualTriggerVisibility($selector); } - $button->press(); - $link = $element->findLink($link_locator); - $link->click(); + + $element = $this->getSession()->getPage()->find('css', $selector); + $element->find('css', '.contextual button')->press(); + $element->findLink($link_locator)->click(); + if ($force_visible) { - // Hide button again if needed. - $this->getSession()->executeScript($find_button_script . '.addClass("visually-hidden");'); + $this->toggleContextualTriggerVisibility($selector); } } + /** + * Toggles the visibility of a contextual trigger. + * + * @param string $selector + * The selector for the element that contains the contextual link. + */ + protected function toggleContextualTriggerVisibility($selector) { + // Hovering over the element itself with should be enough, but does not + // work. Manually remove the visually-hidden class. + $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').toggleClass('visually-hidden');"); + } + } diff --git a/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php b/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php index d3b10bc..283ef29 100644 --- a/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php +++ b/core/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php @@ -14,6 +14,9 @@ class ContextualLinksTest extends JavascriptTestBase { use ContextualLinkClickTrait; + /** + * {@inheritdoc} + */ protected static $modules = ['block', 'contextual', 'contextual_test']; /** @@ -21,11 +24,8 @@ class ContextualLinksTest extends JavascriptTestBase { */ protected function setUp() { parent::setUp(); - $this->drupalLogin($this->createUser( - [ - 'access contextual links', - ] - )); + + $this->drupalLogin($this->createUser(['access contextual links'])); $this->placeBlock('system_powered_by_block', ['id' => 'powered']); } @@ -36,8 +36,7 @@ public function testContextualLinks() { // Test clicking contextual link without toolbar. $this->drupalGet('user'); $this->assertSession()->assertWaitOnAjaxRequest(); - $page = $this->getSession()->getPage(); - $this->clickContextualLink($page->find('css', '#block-powered'), 'Test Link'); + $this->clickContextualLink('#block-powered', 'Test Link'); $this->assertSession()->pageTextContains('Everything is contextual!'); // Test clicking contextual link with toolbar. @@ -45,9 +44,10 @@ public function testContextualLinks() { $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['access toolbar']); $this->drupalGet('user'); $this->assertSession()->assertWaitOnAjaxRequest(); + // Click "Edit" in toolbar to show contextual links. - $page->find('css', '.contextual-toolbar-tab button')->press(); - $this->clickContextualLink($page->find('css', '#block-powered'), 'Test Link', FALSE); + $this->getSession()->getPage()->find('css', '.contextual-toolbar-tab button')->press(); + $this->clickContextualLink('#block-powered', 'Test Link', FALSE); $this->assertSession()->pageTextContains('Everything is contextual!'); }