ContextualLinksTest.php 4.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
<?php

namespace Drupal\Tests\layout_builder\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;

/**
 * Test contextual links compatibility with the Layout Builder.
 *
 * @group layout_builder
 */
class ContextualLinksTest extends WebDriverTestBase {

  use AssertPageCacheContextsAndTagsTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'views',
    'views_ui',
    'layout_builder',
    'layout_builder_views_test',
    'layout_test',
    'block',
    'node',
    'contextual',
  ];

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

    $user = $this->drupalCreateUser([
      'configure any layout',
      'administer node display',
      'administer node fields',
      'access contextual links',
      'administer nodes',
      'bypass node access',
      'administer views',
    ]);
    $user->save();
    $this->drupalLogin($user);
    $this->createContentType(['type' => 'bundle_with_section_field']);

    $this->createNode([
      'type' => 'bundle_with_section_field',
      'body' => [
        [
          'value' => 'The node body',
        ],
      ],
    ]);
  }

  /**
   * Tests that the contextual links inside Layout Builder are removed.
   */
  public function testContextualLinks() {
    $page = $this->getSession()->getPage();

    $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';

    // Enable Layout Builder and overrides.
    $this->drupalPostForm(
      "$field_ui_prefix/display/default",
      ['layout[enabled]' => TRUE, 'layout[allow_custom]' => TRUE],
      'Save'
    );

    $this->drupalGet('node/1/layout');

    // Add a block that includes an entity contextual link.
    $this->addBlock('Test Block View: Teaser block');

    // Add a block that includes a views contextual link.
    $this->addBlock('Recent content');

    // Ensure the contextual links are correct before the layout is saved.
    $this->assertCorrectContextualLinksInUi();

    // Ensure the contextual links are correct when the Layout Builder is loaded
    // after being saved.
    $page->hasButton('Save layout');
    $page->pressButton('Save layout');
    $this->drupalGet('node/1/layout');
    $this->assertCorrectContextualLinksInUi();

    $this->drupalGet('node/1');
    $this->assertCorrectContextualLinksInNode();
  }

  /**
   * Adds block to the layout via Layout Builder's UI.
   *
   * @param string $block_name
101
   *   The block name as it appears in the Add block form.
102
103
104
105
106
   */
  protected function addBlock($block_name) {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

107
108
    $assert_session->linkExists('Add block');
    $page->clickLink('Add block');
109
110
111
112
    $assert_session->assertWaitOnAjaxRequest();
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', "#drupal-off-canvas a:contains('$block_name')"));
    $page->clickLink($block_name);
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '[data-drupal-selector=\'edit-actions-submit\']'));
113

114
    $page->pressButton('Add block');
115
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    $assert_session->assertWaitOnAjaxRequest();
  }

  /**
   * Asserts the contextual links are correct in Layout Builder UI.
   */
  protected function assertCorrectContextualLinksInUi() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.block-views-blocktest-block-view-block-2'));

    $layout_builder_specific_contextual_links = $page->findAll('css', '[data-contextual-id*=\'layout_builder_block:\']');
    $this->assertNotEmpty($layout_builder_specific_contextual_links);

    // Confirms Layout Builder contextual links are the only contextual links
    // inside the Layout Builder UI.
    $this->assertSameSize($layout_builder_specific_contextual_links, $page->findAll('css', '#layout-builder [data-contextual-id]'));
  }

  /**
   * Asserts the contextual links are correct on the canonical entity route.
   */
  protected function assertCorrectContextualLinksInNode() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '[data-contextual-id]'));

    // Ensure that no Layout Builder contextual links are visible on node view.
    $this->assertEmpty($page->findAll('css', '[data-contextual-id*=\'layout_builder_block:\']'));

    // Ensure that the contextual links that are hidden in Layout Builder UI
    // are visible on node view.
    $this->assertNotEmpty($page->findAll('css', '.layout-content [data-contextual-id]'));
  }

}