SettingsTrayBlockFormTest.php 10.7 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
4

5
6
use Drupal\settings_tray_test\Plugin\Block\SettingsTrayFormAnnotationIsClassBlock;
use Drupal\settings_tray_test\Plugin\Block\SettingsTrayFormAnnotationNoneBlock;
7
8
use Drupal\user\Entity\Role;

9
/**
10
 * Testing opening and saving block forms in the off-canvas dialog.
11
 *
12
 * @group #slow
13
 * @group settings_tray
14
 */
15
class SettingsTrayBlockFormTest extends SettingsTrayTestBase {
16

17
18
19
  /**
   * {@inheritdoc}
   */
20
  public static $modules = [
21
22
    'node',
    'search',
23
    'settings_tray_test',
24
  ];
25
26
27
28
29
30

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
31

32
33
34
35
    $user = $this->createUser([
      'administer blocks',
      'access contextual links',
      'access toolbar',
36
37
      'administer nodes',
      'search content',
38
39
40
41
42
    ]);
    $this->drupalLogin($user);
  }

  /**
43
   * Tests opening off-canvas dialog by click blocks and elements in the blocks.
44
   */
45
46
47
48
49
50
51
52
53
54
  public function testBlocks() {
    foreach ($this->getBlockTests() as $test) {
      call_user_func_array([$this, 'doTestBlocks'], $test);
    }
  }

  /**
   * Tests opening off-canvas dialog by click blocks and elements in the blocks.
   */
  protected function doTestBlocks($theme, $block_plugin, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item, $permissions) {
55
56
57
    if ($permissions) {
      $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), $permissions);
    }
58
59
60
61
62
    if ($new_page_text) {
      // Some asserts can be based on this value, so it should not be the same
      // for different blocks, because it can be saved in the site config.
      $new_page_text = $new_page_text . ' ' . $theme . ' ' . $block_plugin;
    }
63
    $web_assert = $this->assertSession();
64
    $page = $this->getSession()->getPage();
65
66
    $this->enableTheme($theme);
    $block = $this->placeBlock($block_plugin);
67
    $block_selector = $this->getBlockSelector($block);
68
69
70
    $block_id = $block->id();
    $this->drupalGet('user');

71
72
    $link = $web_assert->waitForElement('css', "$block_selector .contextual-links li a");
    $this->assertEquals('Quick edit', $link->getHtml(), "'Quick edit' is the first contextual link for the block.");
73
74
    $destination = (string) $this->loggedInUser->toUrl()->toString();
    $this->assertContains("/admin/structure/block/manage/$block_id/settings-tray?destination=$destination", $link->getAttribute('href'));
75
76
77
78
79
80
81

    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();
82
        $web_assert->assertNoElementAfterWait('css', "#toolbar-administration a.is-active");
83
      }
84
85
86
87
88
      $page->find('css', $toolbar_item)->click();
      $this->assertElementVisibleAfterWait('css', "{$toolbar_item}.is-active");
    }
    $this->enableEditMode();
    if (isset($toolbar_item)) {
89
      $web_assert->assertNoElementAfterWait('css', "{$toolbar_item}.is-active");
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    }
    $this->openBlockForm($block_selector);
    switch ($block_plugin) {
      case 'system_powered_by_block':
        // Confirm "Display Title" is not checked.
        $web_assert->checkboxNotChecked('settings[label_display]');
        // Confirm Title is not visible.
        $this->assertEquals($this->isLabelInputVisible(), FALSE, 'Label is not visible');
        $page->checkField('settings[label_display]');
        $this->assertEquals($this->isLabelInputVisible(), TRUE, 'Label is visible');
        // Fill out form, save the form.
        $page->fillField('settings[label]', $new_page_text);

        break;

      case 'system_branding_block':
        // Fill out form, save the form.
        $page->fillField('settings[site_information][site_name]', $new_page_text);
        break;

      case 'settings_tray_test_class':
        $web_assert->elementExists('css', '[data-drupal-selector="edit-settings-some-setting"]');
        break;
    }
114

115
116
117
118
119
120
121
122
    if (isset($new_page_text)) {
      $page->pressButton($button_text);
      // Make sure the changes are present.
      $new_page_text_locator = "$block_selector $label_selector:contains($new_page_text)";
      $this->assertElementVisibleAfterWait('css', $new_page_text_locator);
      // The page is loaded with the new change but make sure page is
      // completely loaded.
      $this->assertPageLoadComplete();
123
    }
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    $this->openBlockForm($block_selector);

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

    $this->enableEditMode();

    // Open block form by clicking a element inside the block.
    // This confirms that default action for links and form elements is
    // suppressed.
    $this->openBlockForm("$block_selector {$element_selector}", $block_selector);
    $web_assert->elementTextContains('css', '.contextual-toolbar-tab button', 'Editing');
138
    $web_assert->elementAttributeContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
139
140
141
142
    // Simulate press the Escape key.
    $this->getSession()->executeScript('jQuery("body").trigger(jQuery.Event("keyup", { keyCode: 27 }));');
    $this->waitForOffCanvasToClose();
    $this->getSession()->wait(100);
143
144
    $this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
    $this->getSession()->getPage()->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
145
    $this->assertEditModeDisabled();
146
    $this->assertNotEmpty($web_assert->waitForElement('css', '#drupal-live-announce:contains(Exited edit mode)'));
147
    $web_assert->assertNoElementAfterWait('css', '.contextual-toolbar-tab button:contains(Editing)');
148
    $web_assert->elementAttributeNotContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
149
150
151
152
153
154

    // Clean up test data so each test does not impact the next.
    $block->delete();
    if ($permissions) {
      user_role_revoke_permissions(Role::AUTHENTICATED_ID, $permissions);
    }
155
156
157
  }

  /**
158
   * Creates tests for ::testBlocks().
159
   */
160
  public function getBlockTests() {
161
162
163
164
165
166
167
168
169
170
171
    $blocks = [];
    foreach ($this->getTestThemes() as $theme) {
      $blocks += [
        "$theme: block-powered" => [
          'theme' => $theme,
          'block_plugin' => 'system_powered_by_block',
          'new_page_text' => 'Can you imagine anyone showing the label on this block',
          'element_selector' => 'span a',
          'label_selector' => 'h2',
          'button_text' => 'Save Powered by Drupal',
          'toolbar_item' => '#toolbar-item-user',
172
          NULL,
173
174
175
176
177
178
179
180
181
        ],
        "$theme: block-branding" => [
          'theme' => $theme,
          'block_plugin' => 'system_branding_block',
          'new_page_text' => 'The site that will live a very short life',
          'element_selector' => "a[rel='home']:last-child",
          'label_selector' => "a[rel='home']:last-child",
          'button_text' => 'Save Site branding',
          'toolbar_item' => '#toolbar-item-administration',
182
          ['administer site configuration'],
183
184
185
186
187
        ],
        "$theme: block-search" => [
          'theme' => $theme,
          'block_plugin' => 'search_form_block',
          'new_page_text' => NULL,
188
          'element_selector' => '#edit-submit',
189
190
191
          'label_selector' => 'h2',
          'button_text' => 'Save Search form',
          'toolbar_item' => NULL,
192
          NULL,
193
194
195
196
197
198
199
200
201
202
203
        ],
        // This is the functional JS test coverage accompanying
        // \Drupal\Tests\settings_tray\Functional\SettingsTrayTest::testPossibleAnnotations().
        "$theme: " . SettingsTrayFormAnnotationIsClassBlock::class => [
          'theme' => $theme,
          'block_plugin' => 'settings_tray_test_class',
          'new_page_text' => NULL,
          'element_selector' => 'span',
          'label_selector' => NULL,
          'button_text' => NULL,
          'toolbar_item' => NULL,
204
          NULL,
205
206
207
208
209
210
211
212
213
214
215
        ],
        // This is the functional JS test coverage accompanying
        // \Drupal\Tests\settings_tray\Functional\SettingsTrayTest::testPossibleAnnotations().
        "$theme: " . SettingsTrayFormAnnotationNoneBlock::class => [
          'theme' => $theme,
          'block_plugin' => 'settings_tray_test_none',
          'new_page_text' => NULL,
          'element_selector' => 'span',
          'label_selector' => NULL,
          'button_text' => NULL,
          'toolbar_item' => NULL,
216
          NULL,
217
218
219
220
        ],
      ];
    }

221
    return $blocks;
222
223
  }

224
225
226
227
  /**
   * Tests enabling and disabling Edit Mode.
   */
  public function testEditModeEnableDisable() {
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
    foreach ($this->getTestThemes() as $theme) {
      $this->enableTheme($theme);
      $block = $this->placeBlock('system_powered_by_block');
      foreach (['contextual_link', 'toolbar_link'] as $enable_option) {
        $this->drupalGet('user');
        $this->assertEditModeDisabled();
        switch ($enable_option) {
          // Enable Edit mode.
          case 'contextual_link':
            $this->clickContextualLink($this->getBlockSelector($block), "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();
255
256
257
258
      }
    }
  }

259
260
261
262
263
264
265
266
  /**
   * Test that validation errors appear in the off-canvas dialog.
   */
  public function testValidationMessages() {
    $page = $this->getSession()->getPage();
    $web_assert = $this->assertSession();
    foreach ($this->getTestThemes() as $theme) {
      $this->enableTheme($theme);
267
      $block = $this->placeBlock('settings_tray_test_validation');
268
269
270
271
272
      $this->drupalGet('user');
      $this->enableEditMode();
      $this->openBlockForm($this->getBlockSelector($block));
      $page->pressButton('Save Block with validation error');
      $web_assert->assertWaitOnAjaxRequest();
273
274
      // The settings_tray_test_validation test plugin form always has a
      // validation error.
275
276
277
278
279
280
      $web_assert->elementContains('css', '#drupal-off-canvas', 'Sorry system error. Please save again');
      $this->disableEditMode();
      $block->delete();
    }
  }

281
282
283
284
285
286
287
288
289
290
291
  /**
   * {@inheritdoc}
   */
  protected function getTestThemes() {
    // Remove 'seven' theme. Setting Tray "Edit Mode" will not work with 'seven'
    // because it removes all contextual links the off-canvas dialog should.
    return array_filter(parent::getTestThemes(), function ($theme) {
      return $theme !== 'seven';
    });
  }

292
}