OutsideInBlockFormTest.php 15.7 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\outside_in\FunctionalJavascript;

5 6
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
7 8
use Drupal\user\Entity\Role;

9 10 11 12 13 14 15
/**
 * Testing opening and saving block forms in the off-canvas tray.
 *
 * @group outside_in
 */
class OutsideInBlockFormTest extends OutsideInJavascriptTestBase {

16 17
  const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';

18 19 20
  /**
   * {@inheritdoc}
   */
21 22 23 24 25 26 27 28 29 30
  public static $modules = [
    'node',
    'block',
    'system',
    'breakpoint',
    'toolbar',
    'contextual',
    'outside_in',
    'quickedit',
    'search',
31
    'block_content',
32 33 34
    // Add test module to override CSS pointer-events properties because they
    // cause test failures.
    'outside_in_test_css',
35
  ];
36 37 38 39 40 41

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
42 43 44 45

    $this->createBlockContentType('basic', TRUE);
    $block_content = $this->createBlockContent('Custom Block', 'basic', TRUE);

46 47 48 49 50 51 52
    // @todo Ensure that this test class works against bartik and stark:
    //   https://www.drupal.org/node/2784881.
    $this->enableTheme('bartik');
    $user = $this->createUser([
      'administer blocks',
      'access contextual links',
      'access toolbar',
53 54 55
      'administer nodes',
      'access in-place editing',
      'search content',
56 57 58
    ]);
    $this->drupalLogin($user);

59
    $this->placeBlock('block_content:' . $block_content->uuid(), ['id' => 'custom']);
60 61
    $this->placeBlock('system_powered_by_block', ['id' => 'powered']);
    $this->placeBlock('system_branding_block', ['id' => 'branding']);
62
    $this->placeBlock('search_form_block', ['id' => 'search']);
63 64 65
  }

  /**
66
   * Tests opening Offcanvas tray by click blocks and elements in the blocks.
67 68
   *
   * @dataProvider providerTestBlocks
69
   */
70
  public function testBlocks($block_id, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item) {
71
    $web_assert = $this->assertSession();
72
    $page = $this->getSession()->getPage();
73
    $block_selector = '#block-' . $block_id;
74
    $this->drupalGet('user');
75 76 77 78 79

    $link = $page->find('css', "$block_selector .contextual-links li a");
    $this->assertEquals('Quick edit', $link->getText(), "'Quick edit' is the first contextual link for the block.");
    $this->assertContains("/admin/structure/block/manage/$block_id/offcanvas?destination=user/2", $link->getAttribute('href'));

80 81 82 83 84 85 86 87 88
    if (isset($toolbar_item)) {
      // Check that you can open a toolbar tray and it will be closed after
      // entering edit mode.
      if ($element = $page->find('css', "#toolbar-administration a.is-active")) {
        // If a tray was open from page load close it.
        $element->click();
        $this->waitForNoElement("#toolbar-administration a.is-active");
      }
      $page->find('css', $toolbar_item)->click();
89
      $web_assert->waitForElementVisible('css', "{$toolbar_item}.is-active");
90
    }
91
    $this->enableEditMode();
92 93 94 95 96 97 98
    if (isset($toolbar_item)) {
      $this->waitForNoElement("{$toolbar_item}.is-active");
    }

    $this->openBlockForm($block_selector);

    switch ($block_id) {
99
      case 'powered':
100 101 102 103 104
        // Fill out form, save the form.
        $page->fillField('settings[label]', $new_page_text);
        $page->checkField('settings[label_display]');
        break;

105
      case 'branding':
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        // Fill out form, save the form.
        $page->fillField('settings[site_information][site_name]', $new_page_text);
        break;
    }

    if (isset($new_page_text)) {
      $page->pressButton($button_text);
      // Make sure the changes are present.
      // @todo Use a wait method that will take into account the form submitting
      //   and all JavaScript activity. https://www.drupal.org/node/2837676
      //   The use \Behat\Mink\WebAssert::pageTextContains to check text.
      $this->assertJsCondition('jQuery("' . $block_selector . ' ' . $label_selector . '").html() == "' . $new_page_text . '"');
    }

    $this->openBlockForm($block_selector);

122
    $this->disableEditMode();
123 124 125
    // Canvas should close when editing module is closed.
    $this->waitForOffCanvasToClose();

126
    $this->enableEditMode();
127 128 129 130 131 132 133

    $element_selector = "$block_selector {$element_selector}";
    // Open block form by clicking a element inside the block.
    // This confirms that default action for links and form elements is
    // suppressed.
    $this->openBlockForm($element_selector);

134 135
    // Exit edit mode using ESC.
    $web_assert->elementTextContains('css', '.contextual-toolbar-tab button', 'Editing');
136
    $web_assert->elementAttributeContains('css', '.dialog-offcanvas__main-canvas', 'class', 'js-outside-in-edit-mode');
137 138 139 140
    // Simulate press the Escape key.
    $this->getSession()->executeScript('jQuery("body").trigger(jQuery.Event("keyup", { keyCode: 27 }));');
    $this->waitForOffCanvasToClose();
    $this->getSession()->wait(100);
141
    $this->assertEditModeDisabled();
142 143
    $web_assert->elementTextContains('css', '#drupal-live-announce', 'Exited edit mode.');
    $web_assert->elementTextNotContains('css', '.contextual-toolbar-tab button', 'Editing');
144
    $web_assert->elementAttributeNotContains('css', '.dialog-offcanvas__main-canvas', 'class', 'js-outside-in-edit-mode');
145 146 147 148 149 150
  }

  /**
   * Dataprovider for testBlocks().
   */
  public function providerTestBlocks() {
151
    $blocks = [
152
      'block-powered' => [
153
        'id' => 'powered',
154 155
        'new_page_text' => 'Can you imagine anyone showing the label on this block?',
        'element_selector' => '.content a',
156
        'label_selector' => 'h2',
157
        'button_text' => 'Save Powered by Drupal',
158
        'toolbar_item' => '#toolbar-item-user',
159
      ],
160
      'block-branding' => [
161
        'id' => 'branding',
162 163
        'new_page_text' => 'The site that will live a very short life.',
        'element_selector' => 'a[rel="home"]:nth-child(2)',
164
        'label_selector' => '.site-branding__name a',
165
        'button_text' => 'Save Site branding',
166
        'toolbar_item' => '#toolbar-item-administration',
167
      ],
168
      'block-search' => [
169
        'id' => 'search',
170
        'new_page_text' => NULL,
171
        'element_selector' => '#edit-submit',
172
        'label_selector' => 'h2',
173
        'button_text' => 'Save Search form',
174
        'toolbar_item' => NULL,
175 176
      ],
    ];
177
    return $blocks;
178 179 180
  }

  /**
181
   * Enables edit mode by pressing edit button in the toolbar.
182
   */
183 184
  protected function enableEditMode() {
    $this->pressToolbarEditButton();
185
    $this->waitForToolbarToLoad();
186 187
    $this->assertEditModeEnabled();
  }
188

189 190 191 192 193
  /**
   * Disables edit mode by pressing edit button in the toolbar.
   */
  protected function disableEditMode() {
    $this->pressToolbarEditButton();
194
    $this->waitForToolbarToLoad();
195
    $this->assertEditModeDisabled();
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  }

  /**
   * Asserts that Off-Canvas block form is valid.
   */
  protected function assertOffCanvasBlockFormIsValid() {
    $web_assert = $this->assertSession();
    // Check that common block form elements exist.
    $web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label"]');
    $web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]');
    // Check that advanced block form elements do not exist.
    $web_assert->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]');
    $web_assert->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');
  }

211 212 213 214 215 216 217
  /**
   * Open block form by clicking the element found with a css selector.
   *
   * @param string $block_selector
   *   A css selector selects the block or an element within it.
   */
  protected function openBlockForm($block_selector) {
218
    $this->waitForToolbarToLoad();
219 220 221 222
    $this->click($block_selector);
    $this->waitForOffCanvasToOpen();
    $this->assertOffCanvasBlockFormIsValid();
  }
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  /**
   * Tests QuickEdit links behavior.
   */
  public function testQuickEditLinks() {
    $quick_edit_selector = '#quickedit-entity-toolbar';
    $body_selector = '.field--name-body p';
    $block_selector = '#block-powered';
    $web_assert = $this->assertSession();
    // Create a Content type and two test nodes.
    $this->createContentType(['type' => 'page']);
    $auth_role = Role::load(Role::AUTHENTICATED_ID);
    $this->grantPermissions($auth_role, [
      'edit any page content',
      'access content',
    ]);
    $node = $this->createNode(
      [
        'title' => 'Page One',
        'type' => 'page',
        'body' => [
          [
            'value' => 'Regular NODE body for the test.',
            'format' => 'plain_text',
          ],
        ],
      ]
    );
    $page = $this->getSession()->getPage();
    // Load the same page twice.
    foreach ([1, 2] as $page_load_times) {
      $this->drupalGet('node/' . $node->id());
255 256 257

      $this->waitForToolbarToLoad();

258 259
      // The 2nd page load we should already be in edit mode.
      if ($page_load_times == 1) {
260
        $this->enableEditMode();
261 262 263
      }
      // In Edit mode clicking field should open QuickEdit toolbar.
      $page->find('css', $body_selector)->click();
264
      $web_assert->waitForElementVisible('css', $quick_edit_selector);
265 266

      $this->disableEditMode();
267 268 269 270 271 272
      // Exiting Edit mode should close QuickEdit toolbar.
      $web_assert->elementNotExists('css', $quick_edit_selector);
      // When not in Edit mode QuickEdit toolbar should not open.
      $page->find('css', $body_selector)->click();
      $web_assert->elementNotExists('css', $quick_edit_selector);

273
      $this->enableEditMode();
274 275
      $this->openBlockForm($block_selector);
      $page->find('css', $body_selector)->click();
276
      $web_assert->waitForElementVisible('css', $quick_edit_selector);
277 278 279 280 281 282 283 284 285 286 287
      // Offcanvas should be closed when opening QuickEdit toolbar.
      $this->waitForOffCanvasToClose();

      $this->openBlockForm($block_selector);
      // QuickEdit toolbar should be closed when opening Offcanvas.
      $web_assert->elementNotExists('css', $quick_edit_selector);
    }

    // Check using contextual links to invoke QuickEdit and open the tray.
    $this->drupalGet('node/' . $node->id());
    $web_assert->assertWaitOnAjaxRequest();
288
    $this->disableEditMode();
289 290
    // Open QuickEdit toolbar before going into Edit mode.
    $this->clickContextualLink('.node', "Quick edit");
291
    $web_assert->waitForElementVisible('css', $quick_edit_selector);
292 293 294 295 296 297 298 299
    // Open off-canvas and enter Edit mode via contextual link.
    $this->clickContextualLink($block_selector, "Quick edit");
    $this->waitForOffCanvasToOpen();
    // QuickEdit toolbar should be closed when opening Offcanvas.
    $web_assert->elementNotExists('css', $quick_edit_selector);
    // Open QuickEdit toolbar via contextual link while in Edit mode.
    $this->clickContextualLink('.node', "Quick edit", FALSE);
    $this->waitForOffCanvasToClose();
300
    $web_assert->waitForElementVisible('css', $quick_edit_selector);
301 302
  }

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  /**
   * Tests enabling and disabling Edit Mode.
   */
  public function testEditModeEnableDisable() {
    foreach (['contextual_link', 'toolbar_link'] as $enable_option) {
      $this->drupalGet('user');
      $this->assertEditModeDisabled();
      switch ($enable_option) {
        // Enable Edit mode.
        case 'contextual_link':
          $this->clickContextualLink('#block-powered', "Quick edit");
          $this->waitForOffCanvasToOpen();
          $this->assertEditModeEnabled();
          break;

        case 'toolbar_link':
          $this->enableEditMode();
          break;
      }
      $this->disableEditMode();

      // Make another page request to ensure Edit mode is still disabled.
      $this->drupalGet('user');
      $this->assertEditModeDisabled();
      // Make sure on this page request it also re-enables and disables
      // correctly.
      $this->enableEditMode();
      $this->disableEditMode();
    }
  }

  /**
   * Assert that edit mode has been properly enabled.
   */
  protected function assertEditModeEnabled() {
    $web_assert = $this->assertSession();
    // No contextual triggers should be hidden.
    $web_assert->elementNotExists('css', '.contextual .trigger.visually-hidden');
    // The toolbar edit button should read "Editing".
    $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
    // The main canvas element should have the "js-outside-in-edit-mode" class.
    $web_assert->elementExists('css', '.dialog-offcanvas__main-canvas.js-outside-in-edit-mode');
  }

  /**
   * Assert that edit mode has been properly disabled.
   */
  protected function assertEditModeDisabled() {
    $web_assert = $this->assertSession();
    // Contextual triggers should be hidden.
    $web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
    // No contextual triggers should be not hidden.
    $web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
    // The toolbar edit button should read "Edit".
    $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
    // The main canvas element should NOT have the "js-outside-in-edit-mode"
    // class.
    $web_assert->elementNotExists('css', '.dialog-offcanvas__main-canvas.js-outside-in-edit-mode');
  }

  /**
   * Press the toolbar Edit button provided by the contextual module.
   */
  protected function pressToolbarEditButton() {
367
    $this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
368 369 370 371 372 373 374 375

    $edit_button = $this->getSession()
      ->getPage()
      ->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);

    $edit_button->press();
  }

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  /**
   * Creates a custom block.
   *
   * @param bool|string $title
   *   (optional) Title of block. When no value is given uses a random name.
   *   Defaults to FALSE.
   * @param string $bundle
   *   (optional) Bundle name. Defaults to 'basic'.
   * @param bool $save
   *   (optional) Whether to save the block. Defaults to TRUE.
   *
   * @return \Drupal\block_content\Entity\BlockContent
   *   Created custom block.
   */
  protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
    $title = $title ?: $this->randomName();
    $block_content = BlockContent::create([
      'info' => $title,
      'type' => $bundle,
      'langcode' => 'en',
      'body' => [
        'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
        'format' => 'plain_text',
      ],
    ]);
    if ($block_content && $save === TRUE) {
      $block_content->save();
    }
    return $block_content;
  }

  /**
   * Creates a custom block type (bundle).
   *
   * @param string $label
   *   The block type label.
   * @param bool $create_body
   *   Whether or not to create the body field.
   *
   * @return \Drupal\block_content\Entity\BlockContentType
   *   Created custom block type.
   */
  protected function createBlockContentType($label, $create_body = FALSE) {
    $bundle = BlockContentType::create([
      'id' => $label,
      'label' => $label,
      'revision' => FALSE,
    ]);
    $bundle->save();
    if ($create_body) {
      block_content_add_body_field($bundle->id());
    }
    return $bundle;
  }

  /**
   * Tests that contextual links in custom blocks are changed.
   *
   * "Quick edit" is quickedit.module link.
   * "Quick edit settings" is outside_in.module link.
   */
  public function testCustomBlockLinks() {
    $this->drupalGet('user');
    $page = $this->getSession()->getPage();
    $links = $page->findAll('css', "#block-custom .contextual-links li a");
    $link_labels = [];
    /** @var \Behat\Mink\Element\NodeElement $link */
    foreach ($links as $link) {
      $link_labels[$link->getAttribute('href')] = $link->getText();
    }
    $href = array_search('Quick edit', $link_labels);
    $this->assertEquals('', $href);
    $href = array_search('Quick edit settings', $link_labels);
    $this->assertTrue(strstr($href, '/admin/structure/block/manage/custom/offcanvas?destination=user/2') !== FALSE);
  }

452
}