Chris@0: drupalLogin($this->createUser([ Chris@0: 'administer blocks', Chris@0: 'access contextual links', Chris@0: 'access toolbar', Chris@0: ])); Chris@0: $this->placeBlock('system_powered_by_block', ['id' => 'powered']); Chris@0: } Chris@0: Chris@0: /** Chris@14: * Tests enabling and disabling edit mode. Chris@0: */ Chris@14: public function testEditModeEnableDisalbe() { Chris@0: $web_assert = $this->assertSession(); Chris@14: $page = $this->getSession()->getPage(); Chris@14: // Get the page twice to ensure edit mode remains enabled after a new page Chris@14: // request. Chris@14: for ($page_get_count = 0; $page_get_count < 2; $page_get_count++) { Chris@14: $this->drupalGet('user'); Chris@14: $expected_restricted_tab_count = 1 + count($page->findAll('css', '[data-contextual-id]')); Chris@0: Chris@14: // After the page loaded we need to additionally wait until the settings Chris@14: // tray Ajax activity is done. Chris@14: $web_assert->assertWaitOnAjaxRequest(); Chris@0: Chris@14: if ($page_get_count == 0) { Chris@14: $unrestricted_tab_count = $this->getTabbableElementsCount(); Chris@14: $this->assertGreaterThan($expected_restricted_tab_count, $unrestricted_tab_count); Chris@14: Chris@14: // Enable edit mode. Chris@14: // After the first page load the page will be in edit mode when loaded. Chris@14: $this->pressToolbarEditButton(); Chris@14: } Chris@14: Chris@14: $this->assertAnnounceEditMode(); Chris@14: $this->assertSame($expected_restricted_tab_count, $this->getTabbableElementsCount()); Chris@14: Chris@14: // Disable edit mode. Chris@14: $this->pressToolbarEditButton(); Chris@14: $this->assertAnnounceLeaveEditMode(); Chris@14: $this->assertSame($unrestricted_tab_count, $this->getTabbableElementsCount()); Chris@14: // Enable edit mode again. Chris@14: $this->pressToolbarEditButton(); Chris@14: // Finally assert that the 'edit mode enabled' announcement is still Chris@14: // correct after toggling the edit mode at least once. Chris@14: $this->assertAnnounceEditMode(); Chris@14: $this->assertSame($expected_restricted_tab_count, $this->getTabbableElementsCount()); Chris@14: } Chris@14: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presses the toolbar edit mode. Chris@0: */ Chris@0: protected function pressToolbarEditButton() { Chris@0: $edit_button = $this->getSession()->getPage()->find('css', '#toolbar-bar div.contextual-toolbar-tab button'); Chris@0: $edit_button->press(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that the correct message was announced when entering edit mode. Chris@0: */ Chris@0: protected function assertAnnounceEditMode() { Chris@0: $web_assert = $this->assertSession(); Chris@0: // Wait for contextual trigger button. Chris@0: $web_assert->waitForElementVisible('css', '.contextual trigger'); Chris@0: $web_assert->elementContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is constrained to a set of'); Chris@0: $web_assert->elementNotContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is no longer constrained by the Contextual module.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert that the correct message was announced when leaving edit mode. Chris@0: */ Chris@0: protected function assertAnnounceLeaveEditMode() { Chris@0: $web_assert = $this->assertSession(); Chris@0: $page = $this->getSession()->getPage(); Chris@0: // Wait till all the contextual links are hidden. Chris@0: $page->waitFor(1, function () use ($page, $web_assert) { Chris@0: return empty($page->find('css', '.contextual .trigger.visually-hidden')); Chris@0: }); Chris@0: $web_assert->elementContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is no longer constrained by the Contextual module.'); Chris@0: $web_assert->elementNotContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is constrained to a set of'); Chris@0: } Chris@0: Chris@14: /** Chris@14: * Gets the number of elements that are tabbable. Chris@14: * Chris@14: * @return int Chris@14: * The number of tabbable elements. Chris@14: */ Chris@14: protected function getTabbableElementsCount() { Chris@14: // Mark all tabbable elements. Chris@14: $this->getSession()->executeScript("jQuery(':tabbable').attr('data-marked', '');"); Chris@14: // Count all marked elements. Chris@14: $count = count($this->getSession()->getPage()->findAll('css', "[data-marked]")); Chris@14: // Remove set attributes. Chris@14: $this->getSession()->executeScript("jQuery('[data-marked]').removeAttr('data-marked');"); Chris@14: return $count; Chris@14: } Chris@14: Chris@0: }