annotate core/modules/contextual/tests/src/FunctionalJavascript/EditModeTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\contextual\FunctionalJavascript;
Chris@0 4
Chris@17 5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests edit mode.
Chris@0 9 *
Chris@0 10 * @group contextual
Chris@0 11 */
Chris@17 12 class EditModeTest extends WebDriverTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * CSS selector for Drupal's announce element.
Chris@0 16 */
Chris@0 17 const ANNOUNCE_SELECTOR = '#drupal-live-announce';
Chris@0 18
Chris@0 19 /**
Chris@0 20 * {@inheritdoc}
Chris@0 21 */
Chris@0 22 public static $modules = [
Chris@0 23 'node',
Chris@0 24 'block',
Chris@0 25 'user',
Chris@0 26 'system',
Chris@0 27 'breakpoint',
Chris@0 28 'toolbar',
Chris@0 29 'contextual',
Chris@0 30 ];
Chris@0 31
Chris@0 32 /**
Chris@0 33 * {@inheritdoc}
Chris@0 34 */
Chris@0 35 protected function setUp() {
Chris@0 36 parent::setUp();
Chris@0 37
Chris@0 38 $this->drupalLogin($this->createUser([
Chris@0 39 'administer blocks',
Chris@0 40 'access contextual links',
Chris@0 41 'access toolbar',
Chris@0 42 ]));
Chris@0 43 $this->placeBlock('system_powered_by_block', ['id' => 'powered']);
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@14 47 * Tests enabling and disabling edit mode.
Chris@0 48 */
Chris@14 49 public function testEditModeEnableDisalbe() {
Chris@0 50 $web_assert = $this->assertSession();
Chris@14 51 $page = $this->getSession()->getPage();
Chris@14 52 // Get the page twice to ensure edit mode remains enabled after a new page
Chris@14 53 // request.
Chris@14 54 for ($page_get_count = 0; $page_get_count < 2; $page_get_count++) {
Chris@14 55 $this->drupalGet('user');
Chris@14 56 $expected_restricted_tab_count = 1 + count($page->findAll('css', '[data-contextual-id]'));
Chris@0 57
Chris@14 58 // After the page loaded we need to additionally wait until the settings
Chris@14 59 // tray Ajax activity is done.
Chris@14 60 $web_assert->assertWaitOnAjaxRequest();
Chris@0 61
Chris@14 62 if ($page_get_count == 0) {
Chris@14 63 $unrestricted_tab_count = $this->getTabbableElementsCount();
Chris@14 64 $this->assertGreaterThan($expected_restricted_tab_count, $unrestricted_tab_count);
Chris@14 65
Chris@14 66 // Enable edit mode.
Chris@14 67 // After the first page load the page will be in edit mode when loaded.
Chris@14 68 $this->pressToolbarEditButton();
Chris@14 69 }
Chris@14 70
Chris@14 71 $this->assertAnnounceEditMode();
Chris@14 72 $this->assertSame($expected_restricted_tab_count, $this->getTabbableElementsCount());
Chris@14 73
Chris@14 74 // Disable edit mode.
Chris@14 75 $this->pressToolbarEditButton();
Chris@14 76 $this->assertAnnounceLeaveEditMode();
Chris@14 77 $this->assertSame($unrestricted_tab_count, $this->getTabbableElementsCount());
Chris@14 78 // Enable edit mode again.
Chris@14 79 $this->pressToolbarEditButton();
Chris@14 80 // Finally assert that the 'edit mode enabled' announcement is still
Chris@14 81 // correct after toggling the edit mode at least once.
Chris@14 82 $this->assertAnnounceEditMode();
Chris@14 83 $this->assertSame($expected_restricted_tab_count, $this->getTabbableElementsCount());
Chris@14 84 }
Chris@14 85
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Presses the toolbar edit mode.
Chris@0 90 */
Chris@0 91 protected function pressToolbarEditButton() {
Chris@0 92 $edit_button = $this->getSession()->getPage()->find('css', '#toolbar-bar div.contextual-toolbar-tab button');
Chris@0 93 $edit_button->press();
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Asserts that the correct message was announced when entering edit mode.
Chris@0 98 */
Chris@0 99 protected function assertAnnounceEditMode() {
Chris@0 100 $web_assert = $this->assertSession();
Chris@0 101 // Wait for contextual trigger button.
Chris@0 102 $web_assert->waitForElementVisible('css', '.contextual trigger');
Chris@0 103 $web_assert->elementContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is constrained to a set of');
Chris@0 104 $web_assert->elementNotContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is no longer constrained by the Contextual module.');
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Assert that the correct message was announced when leaving edit mode.
Chris@0 109 */
Chris@0 110 protected function assertAnnounceLeaveEditMode() {
Chris@0 111 $web_assert = $this->assertSession();
Chris@0 112 $page = $this->getSession()->getPage();
Chris@0 113 // Wait till all the contextual links are hidden.
Chris@0 114 $page->waitFor(1, function () use ($page, $web_assert) {
Chris@0 115 return empty($page->find('css', '.contextual .trigger.visually-hidden'));
Chris@0 116 });
Chris@0 117 $web_assert->elementContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is no longer constrained by the Contextual module.');
Chris@0 118 $web_assert->elementNotContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is constrained to a set of');
Chris@0 119 }
Chris@0 120
Chris@14 121 /**
Chris@14 122 * Gets the number of elements that are tabbable.
Chris@14 123 *
Chris@14 124 * @return int
Chris@14 125 * The number of tabbable elements.
Chris@14 126 */
Chris@14 127 protected function getTabbableElementsCount() {
Chris@14 128 // Mark all tabbable elements.
Chris@14 129 $this->getSession()->executeScript("jQuery(':tabbable').attr('data-marked', '');");
Chris@14 130 // Count all marked elements.
Chris@14 131 $count = count($this->getSession()->getPage()->findAll('css', "[data-marked]"));
Chris@14 132 // Remove set attributes.
Chris@14 133 $this->getSession()->executeScript("jQuery('[data-marked]').removeAttr('data-marked');");
Chris@14 134 return $count;
Chris@14 135 }
Chris@14 136
Chris@0 137 }