a543baf1262e7b061244b4e321bce65e12541498
[yaffs-website] / web / core / modules / settings_tray / tests / src / FunctionalJavascript / QuickEditIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\user\Entity\Role;
8
9 /**
10  * Test Settings Tray and Quick Edit modules integration.
11  *
12  * @group settings_tray
13  */
14 class QuickEditIntegrationTest extends SettingsTrayTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'node',
21     'block_content',
22     'quickedit',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30     $user = $this->createUser([
31       'administer blocks',
32       'access contextual links',
33       'access toolbar',
34       'administer nodes',
35       'access in-place editing',
36     ]);
37     $this->drupalLogin($user);
38
39   }
40
41   /**
42    * Tests QuickEdit links behavior.
43    */
44   public function testQuickEditLinks() {
45     $quick_edit_selector = '#quickedit-entity-toolbar';
46     $node_selector = '[data-quickedit-entity-id="node/1"]';
47     $body_selector = '[data-quickedit-field-id="node/1/body/en/full"]';
48     $web_assert = $this->assertSession();
49     // Create a Content type and two test nodes.
50     $this->createContentType(['type' => 'page']);
51     $auth_role = Role::load(Role::AUTHENTICATED_ID);
52     $this->grantPermissions($auth_role, [
53       'edit any page content',
54       'access content',
55     ]);
56     $node = $this->createNode(
57       [
58         'title' => 'Page One',
59         'type' => 'page',
60         'body' => [
61           [
62             'value' => 'Regular NODE body for the test.',
63             'format' => 'plain_text',
64           ],
65         ],
66       ]
67     );
68     $page = $this->getSession()->getPage();
69     $block_plugin = 'system_powered_by_block';
70
71     foreach ($this->getTestThemes() as $theme) {
72
73       $this->enableTheme($theme);
74
75       $block = $this->placeBlock($block_plugin);
76       $block_selector = $this->getBlockSelector($block);
77       // Load the same page twice.
78       foreach ([1, 2] as $page_load_times) {
79         $this->drupalGet('node/' . $node->id());
80         // The 2nd page load we should already be in edit mode.
81         if ($page_load_times == 1) {
82           $this->enableEditMode();
83         }
84         // In Edit mode clicking field should open QuickEdit toolbar.
85         $page->find('css', $body_selector)->click();
86         $this->assertElementVisibleAfterWait('css', $quick_edit_selector);
87
88         $this->disableEditMode();
89         // Exiting Edit mode should close QuickEdit toolbar.
90         $web_assert->elementNotExists('css', $quick_edit_selector);
91         // When not in Edit mode QuickEdit toolbar should not open.
92         $page->find('css', $body_selector)->click();
93         $web_assert->elementNotExists('css', $quick_edit_selector);
94         $this->enableEditMode();
95         $this->openBlockForm($block_selector);
96         $page->find('css', $body_selector)->click();
97         $this->assertElementVisibleAfterWait('css', $quick_edit_selector);
98         // Off-canvas dialog should be closed when opening QuickEdit toolbar.
99         $this->waitForOffCanvasToClose();
100
101         $this->openBlockForm($block_selector);
102         // QuickEdit toolbar should be closed when opening Off-canvas dialog.
103         $web_assert->elementNotExists('css', $quick_edit_selector);
104       }
105       // Check using contextual links to invoke QuickEdit and open the tray.
106       $this->drupalGet('node/' . $node->id());
107       $web_assert->assertWaitOnAjaxRequest();
108       $this->disableEditMode();
109       // Open QuickEdit toolbar before going into Edit mode.
110       $this->clickContextualLink($node_selector, "Quick edit");
111       $this->assertElementVisibleAfterWait('css', $quick_edit_selector);
112       // Open off-canvas and enter Edit mode via contextual link.
113       $this->clickContextualLink($block_selector, "Quick edit");
114       $this->waitForOffCanvasToOpen();
115       // QuickEdit toolbar should be closed when opening off-canvas dialog.
116       $web_assert->elementNotExists('css', $quick_edit_selector);
117       // Open QuickEdit toolbar via contextual link while in Edit mode.
118       $this->clickContextualLink($node_selector, "Quick edit", FALSE);
119       $this->waitForOffCanvasToClose();
120       $this->assertElementVisibleAfterWait('css', $quick_edit_selector);
121       $this->disableEditMode();
122     }
123   }
124
125   /**
126    * Tests that contextual links in custom blocks are changed.
127    *
128    * "Quick edit" is quickedit.module link.
129    * "Quick edit settings" is settings_tray.module link.
130    */
131   public function testCustomBlockLinks() {
132     $this->createBlockContentType('basic', TRUE);
133     $block_content = $this->createBlockContent('Custom Block', 'basic', TRUE);
134     $this->placeBlock('block_content:' . $block_content->uuid(), ['id' => 'custom']);
135     $this->drupalGet('user');
136     $page = $this->getSession()->getPage();
137     $this->toggleContextualTriggerVisibility('#block-custom');
138     $page->find('css', '#block-custom .contextual button')->press();
139     $links = $page->findAll('css', "#block-custom .contextual-links li a");
140     $link_labels = [];
141     /** @var \Behat\Mink\Element\NodeElement $link */
142     foreach ($links as $link) {
143       $link_labels[$link->getAttribute('href')] = $link->getText();
144     }
145     $href = array_search('Quick edit', $link_labels);
146     $this->assertEquals('', $href);
147     $href = array_search('Quick edit settings', $link_labels);
148     $destination = (string) $this->loggedInUser->toUrl()->toString();
149     $this->assertTrue(strstr($href, "/admin/structure/block/manage/custom/settings-tray?destination=$destination") !== FALSE);
150   }
151
152   /**
153    * Creates a custom block.
154    *
155    * @param bool|string $title
156    *   (optional) Title of block. When no value is given uses a random name.
157    *   Defaults to FALSE.
158    * @param string $bundle
159    *   (optional) Bundle name. Defaults to 'basic'.
160    * @param bool $save
161    *   (optional) Whether to save the block. Defaults to TRUE.
162    *
163    * @return \Drupal\block_content\Entity\BlockContent
164    *   Created custom block.
165    */
166   protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
167     $title = $title ?: $this->randomName();
168     $block_content = BlockContent::create([
169       'info' => $title,
170       'type' => $bundle,
171       'langcode' => 'en',
172       'body' => [
173         'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
174         'format' => 'plain_text',
175       ],
176     ]);
177     if ($block_content && $save === TRUE) {
178       $block_content->save();
179     }
180     return $block_content;
181   }
182
183   /**
184    * Creates a custom block type (bundle).
185    *
186    * @param string $label
187    *   The block type label.
188    * @param bool $create_body
189    *   Whether or not to create the body field.
190    *
191    * @return \Drupal\block_content\Entity\BlockContentType
192    *   Created custom block type.
193    */
194   protected function createBlockContentType($label, $create_body = FALSE) {
195     $bundle = BlockContentType::create([
196       'id' => $label,
197       'label' => $label,
198       'revision' => FALSE,
199     ]);
200     $bundle->save();
201     if ($create_body) {
202       block_content_add_body_field($bundle->id());
203     }
204     return $bundle;
205   }
206
207 }