3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\user\Entity\Role;
10 * Test Settings Tray and Quick Edit modules integration.
12 * @group settings_tray
14 class QuickEditIntegrationTest extends SettingsTrayTestBase {
19 public static $modules = [
28 protected function setUp() {
30 $user = $this->createUser([
32 'access contextual links',
35 'access in-place editing',
37 $this->drupalLogin($user);
42 * Tests QuickEdit links behavior.
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',
56 $node = $this->createNode(
58 'title' => 'Page One',
62 'value' => 'Regular NODE body for the test.',
63 'format' => 'plain_text',
68 $page = $this->getSession()->getPage();
69 $block_plugin = 'system_powered_by_block';
71 foreach ($this->getTestThemes() as $theme) {
73 $this->enableTheme($theme);
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();
84 // In Edit mode clicking field should open QuickEdit toolbar.
85 $page->find('css', $body_selector)->click();
86 $this->assertElementVisibleAfterWait('css', $quick_edit_selector);
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();
101 $this->openBlockForm($block_selector);
102 // QuickEdit toolbar should be closed when opening Off-canvas dialog.
103 $web_assert->elementNotExists('css', $quick_edit_selector);
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();
126 * Tests that contextual links in custom blocks are changed.
128 * "Quick edit" is quickedit.module link.
129 * "Quick edit settings" is settings_tray.module link.
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");
141 /** @var \Behat\Mink\Element\NodeElement $link */
142 foreach ($links as $link) {
143 $link_labels[$link->getAttribute('href')] = $link->getText();
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);
153 * Creates a custom block.
155 * @param bool|string $title
156 * (optional) Title of block. When no value is given uses a random name.
158 * @param string $bundle
159 * (optional) Bundle name. Defaults to 'basic'.
161 * (optional) Whether to save the block. Defaults to TRUE.
163 * @return \Drupal\block_content\Entity\BlockContent
164 * Created custom block.
166 protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
167 $title = $title ?: $this->randomName();
168 $block_content = BlockContent::create([
173 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
174 'format' => 'plain_text',
177 if ($block_content && $save === TRUE) {
178 $block_content->save();
180 return $block_content;
184 * Creates a custom block type (bundle).
186 * @param string $label
187 * The block type label.
188 * @param bool $create_body
189 * Whether or not to create the body field.
191 * @return \Drupal\block_content\Entity\BlockContentType
192 * Created custom block type.
194 protected function createBlockContentType($label, $create_body = FALSE) {
195 $bundle = BlockContentType::create([
202 block_content_add_body_field($bundle->id());