OutsideInBlockFormTest.php 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace Drupal\Tests\outside_in\FunctionalJavascript;

/**
 * Testing opening and saving block forms in the off-canvas tray.
 *
 * @group outside_in
 */
class OutsideInBlockFormTest extends OutsideInJavascriptTestBase {

  /**
   * {@inheritdoc}
   */
15 16 17 18 19 20 21 22 23 24 25
  public static $modules = [
    'node',
    'block',
    'system',
    'breakpoint',
    'toolbar',
    'contextual',
    'outside_in',
    'quickedit',
    'search',
  ];
26 27 28 29 30 31 32 33 34 35 36 37 38

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    // @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',
39 40 41
      'administer nodes',
      'access in-place editing',
      'search content',
42 43 44 45 46
    ]);
    $this->drupalLogin($user);

    $this->placeBlock('system_powered_by_block', ['id' => 'powered']);
    $this->placeBlock('system_branding_block', ['id' => 'branding']);
47
    $this->placeBlock('search_form_block', ['id' => 'search']);
48 49 50
  }

  /**
51
   * Tests opening Offcanvas tray by click blocks and elements in the blocks.
52 53
   *
   * @dataProvider providerTestBlocks
54
   */
55
  public function testBlocks($block_id, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item) {
56
    $web_assert = $this->assertSession();
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    $page = $this->getSession()->getPage();
    $block_selector = '#' . $block_id;
    $this->drupalGet('user');
    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();
      $this->waitForElement("{$toolbar_item}.is-active");
    }
    $this->toggleEditingMode();
    if (isset($toolbar_item)) {
      $this->waitForNoElement("{$toolbar_item}.is-active");
    }

    $this->openBlockForm($block_selector);

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

      case 'block-branding':
        // 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);

    $this->toggleEditingMode();
    // Canvas should close when editing module is closed.
    $this->waitForOffCanvasToClose();

    // Go into Edit mode again.
    $this->toggleEditingMode();

    $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);

    // Exit edit mode.
    $this->toggleEditingMode();
  }

  /**
   * Dataprovider for testBlocks().
   */
  public function providerTestBlocks() {
123
    $blocks = [
124
      'block-powered' => [
125 126 127
        'id' => 'block-powered',
        'new_page_text' => 'Can you imagine anyone showing the label on this block?',
        'element_selector' => '.content a',
128
        'label_selector' => 'h2',
129
        'button_text' => 'Save Powered by Drupal',
130
        'toolbar_item' => '#toolbar-item-user',
131
      ],
132
      'block-branding' => [
133 134 135
        'id' => 'block-branding',
        'new_page_text' => 'The site that will live a very short life.',
        'element_selector' => 'a[rel="home"]:nth-child(2)',
136
        'label_selector' => '.site-branding__name a',
137
        'button_text' => 'Save Site branding',
138
        'toolbar_item' => '#toolbar-item-administration',
139
      ],
140
      'block-search' => [
141
        'id' => 'block-search',
142
        'new_page_text' => NULL,
143
        'element_selector' => '#edit-submit',
144
        'label_selector' => 'h2',
145
        'button_text' => 'Save Search form',
146
        'toolbar_item' => NULL,
147 148
      ],
    ];
149
    return $blocks;
150 151 152 153 154
  }

  /**
   * Enables Editing mode by pressing "Edit" button in the toolbar.
   */
155
  protected function toggleEditingMode() {
156 157
    $this->waitForElement('div[data-contextual-id="block:block=powered:langcode=en|outside_in::langcode=en"] .contextual-links a');

158
    $this->waitForElement('#toolbar-bar');
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    $edit_button = $this->getSession()->getPage()->find('css', '#toolbar-bar div.contextual-toolbar-tab button');

    $edit_button->press();
  }

  /**
   * 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"]');
  }

178 179 180 181 182 183 184 185 186 187 188
  /**
   * 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) {
    $this->click($block_selector);
    $this->waitForOffCanvasToOpen();
    $this->assertOffCanvasBlockFormIsValid();
  }
189

190
}