Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\block\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests that the block configuration UI exists and stores data correctly.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group block
|
Chris@0
|
12 */
|
Chris@0
|
13 class BlockUiTest extends BrowserTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Modules to install.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var array
|
Chris@0
|
19 */
|
Chris@0
|
20 public static $modules = ['block', 'block_test', 'help', 'condition_test'];
|
Chris@0
|
21
|
Chris@0
|
22 protected $regions;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The submitted block values used by this test.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $blockValues;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The block entities used by this test.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Drupal\block\BlockInterface[]
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $blocks;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * An administrative user to configure the test environment.
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $adminUser;
|
Chris@0
|
42
|
Chris@0
|
43 protected function setUp() {
|
Chris@0
|
44 parent::setUp();
|
Chris@0
|
45 // Create and log in an administrative user.
|
Chris@0
|
46 $this->adminUser = $this->drupalCreateUser([
|
Chris@0
|
47 'administer blocks',
|
Chris@0
|
48 'access administration pages',
|
Chris@0
|
49 ]);
|
Chris@0
|
50 $this->drupalLogin($this->adminUser);
|
Chris@0
|
51
|
Chris@0
|
52 // Enable some test blocks.
|
Chris@0
|
53 $this->blockValues = [
|
Chris@0
|
54 [
|
Chris@0
|
55 'label' => 'Tools',
|
Chris@0
|
56 'tr' => '5',
|
Chris@0
|
57 'plugin_id' => 'system_menu_block:tools',
|
Chris@0
|
58 'settings' => ['region' => 'sidebar_second', 'id' => 'tools'],
|
Chris@0
|
59 'test_weight' => '-1',
|
Chris@0
|
60 ],
|
Chris@0
|
61 [
|
Chris@0
|
62 'label' => 'Powered by Drupal',
|
Chris@0
|
63 'tr' => '16',
|
Chris@0
|
64 'plugin_id' => 'system_powered_by_block',
|
Chris@0
|
65 'settings' => ['region' => 'footer', 'id' => 'powered'],
|
Chris@0
|
66 'test_weight' => '0',
|
Chris@0
|
67 ],
|
Chris@0
|
68 ];
|
Chris@0
|
69 $this->blocks = [];
|
Chris@0
|
70 foreach ($this->blockValues as $values) {
|
Chris@0
|
71 $this->blocks[] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']);
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * Test block demo page exists and functions correctly.
|
Chris@0
|
77 */
|
Chris@0
|
78 public function testBlockDemoUiPage() {
|
Chris@0
|
79 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
|
Chris@0
|
80 $this->drupalGet('admin/structure/block');
|
Chris@0
|
81 $this->clickLink(t('Demonstrate block regions (@theme)', ['@theme' => 'Classy']));
|
Chris@0
|
82 $elements = $this->xpath('//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), :title)]', [':title' => 'Highlighted']);
|
Chris@0
|
83 $this->assertTrue(!empty($elements), 'Block demo regions are shown.');
|
Chris@0
|
84
|
Chris@0
|
85 \Drupal::service('theme_handler')->install(['test_theme']);
|
Chris@0
|
86 $this->drupalGet('admin/structure/block/demo/test_theme');
|
Chris@0
|
87 $this->assertEscaped('<strong>Test theme</strong>');
|
Chris@0
|
88
|
Chris@0
|
89 \Drupal::service('theme_handler')->install(['stable']);
|
Chris@0
|
90 $this->drupalGet('admin/structure/block/demo/stable');
|
Chris@0
|
91 $this->assertResponse(404, 'Hidden themes that are not the default theme are not supported by the block demo screen');
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Test block admin page exists and functions correctly.
|
Chris@0
|
96 */
|
Chris@0
|
97 public function testBlockAdminUiPage() {
|
Chris@0
|
98 // Visit the blocks admin ui.
|
Chris@0
|
99 $this->drupalGet('admin/structure/block');
|
Chris@0
|
100 // Look for the blocks table.
|
Chris@0
|
101 $blocks_table = $this->xpath("//table[@id='blocks']");
|
Chris@0
|
102 $this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
|
Chris@0
|
103 // Look for test blocks in the table.
|
Chris@0
|
104 foreach ($this->blockValues as $delta => $values) {
|
Chris@0
|
105 $block = $this->blocks[$delta];
|
Chris@0
|
106 $label = $block->label();
|
Chris@0
|
107 $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
|
Chris@0
|
108 $this->assertEquals($element[0]->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
|
Chris@0
|
109 // Look for a test block region select form element.
|
Chris@0
|
110 $this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
|
Chris@0
|
111 // Move the test block to the header region.
|
Chris@0
|
112 $edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
|
Chris@0
|
113 // Look for a test block weight select form element.
|
Chris@0
|
114 $this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
|
Chris@0
|
115 // Change the test block's weight.
|
Chris@0
|
116 $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
|
Chris@0
|
117 }
|
Chris@0
|
118 $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
|
Chris@0
|
119 foreach ($this->blockValues as $values) {
|
Chris@0
|
120 // Check if the region and weight settings changes have persisted.
|
Chris@0
|
121 $this->assertOptionSelected(
|
Chris@0
|
122 'edit-blocks-' . $values['settings']['id'] . '-region',
|
Chris@0
|
123 'header',
|
Chris@0
|
124 'The block "' . $label . '" has the correct region assignment (header).'
|
Chris@0
|
125 );
|
Chris@0
|
126 $this->assertOptionSelected(
|
Chris@0
|
127 'edit-blocks-' . $values['settings']['id'] . '-weight',
|
Chris@0
|
128 $values['test_weight'],
|
Chris@0
|
129 'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').'
|
Chris@0
|
130 );
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 // Add a block with a machine name the same as a region name.
|
Chris@0
|
134 $this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']);
|
Chris@0
|
135 $this->drupalGet('admin/structure/block');
|
Chris@0
|
136 $element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']);
|
Chris@0
|
137 $this->assertTrue(!empty($element));
|
Chris@0
|
138
|
Chris@0
|
139 // Ensure hidden themes do not appear in the UI. Enable another non base
|
Chris@0
|
140 // theme and place the local tasks block.
|
Chris@0
|
141 $this->assertTrue(\Drupal::service('theme_handler')->themeExists('classy'), 'The classy base theme is enabled');
|
Chris@0
|
142 $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']);
|
Chris@0
|
143 \Drupal::service('theme_installer')->install(['stable', 'stark']);
|
Chris@0
|
144 $this->drupalGet('admin/structure/block');
|
Chris@0
|
145 $theme_handler = \Drupal::service('theme_handler');
|
Chris@0
|
146 $this->assertLink($theme_handler->getName('classy'));
|
Chris@0
|
147 $this->assertLink($theme_handler->getName('stark'));
|
Chris@0
|
148 $this->assertNoLink($theme_handler->getName('stable'));
|
Chris@0
|
149
|
Chris@0
|
150 $this->drupalGet('admin/structure/block/list/stable');
|
Chris@0
|
151 $this->assertResponse(404, 'Placing blocks through UI is not possible for a hidden base theme.');
|
Chris@0
|
152
|
Chris@0
|
153 \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save();
|
Chris@0
|
154 \Drupal::service('router.builder')->rebuildIfNeeded();
|
Chris@0
|
155 $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']);
|
Chris@0
|
156 $this->drupalGet('admin/structure/block');
|
Chris@0
|
157 $this->assertLink($theme_handler->getName('stable'));
|
Chris@0
|
158 $this->drupalGet('admin/structure/block/list/stable');
|
Chris@0
|
159 $this->assertResponse(200, 'Placing blocks through UI is possible for a hidden base theme that is the admin theme.');
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * Tests the block categories on the listing page.
|
Chris@0
|
164 */
|
Chris@0
|
165 public function testCandidateBlockList() {
|
Chris@0
|
166 $arguments = [
|
Chris@0
|
167 ':title' => 'Display message',
|
Chris@0
|
168 ':category' => 'Block test',
|
Chris@0
|
169 ':href' => 'admin/structure/block/add/test_block_instantiation/classy',
|
Chris@0
|
170 ];
|
Chris@0
|
171 $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
|
Chris@0
|
172
|
Chris@0
|
173 $this->drupalGet('admin/structure/block');
|
Chris@0
|
174 $this->clickLink('Place block');
|
Chris@0
|
175 $elements = $this->xpath($pattern, $arguments);
|
Chris@0
|
176 $this->assertTrue(!empty($elements), 'The test block appears in the category for its module.');
|
Chris@0
|
177
|
Chris@0
|
178 // Trigger the custom category addition in block_test_block_alter().
|
Chris@0
|
179 $this->container->get('state')->set('block_test_info_alter', TRUE);
|
Chris@0
|
180 $this->container->get('plugin.manager.block')->clearCachedDefinitions();
|
Chris@0
|
181
|
Chris@0
|
182 $this->drupalGet('admin/structure/block');
|
Chris@0
|
183 $this->clickLink('Place block');
|
Chris@0
|
184 $arguments[':category'] = 'Custom category';
|
Chris@0
|
185 $elements = $this->xpath($pattern, $arguments);
|
Chris@0
|
186 $this->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().');
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Tests the behavior of unsatisfied context-aware blocks.
|
Chris@0
|
191 */
|
Chris@0
|
192 public function testContextAwareUnsatisfiedBlocks() {
|
Chris@0
|
193 $arguments = [
|
Chris@0
|
194 ':category' => 'Block test',
|
Chris@0
|
195 ':href' => 'admin/structure/block/add/test_context_aware_unsatisfied/classy',
|
Chris@0
|
196 ':text' => 'Test context-aware unsatisfied block',
|
Chris@0
|
197 ];
|
Chris@0
|
198
|
Chris@0
|
199 $this->drupalGet('admin/structure/block');
|
Chris@0
|
200 $this->clickLink('Place block');
|
Chris@0
|
201 $elements = $this->xpath('//tr[.//td/div[text()=:text] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]', $arguments);
|
Chris@0
|
202 $this->assertTrue(empty($elements), 'The context-aware test block does not appear.');
|
Chris@0
|
203
|
Chris@0
|
204 $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware_unsatisfied');
|
Chris@0
|
205 $this->assertTrue(!empty($definition), 'The context-aware test block does not exist.');
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * Tests the behavior of context-aware blocks.
|
Chris@0
|
210 */
|
Chris@0
|
211 public function testContextAwareBlocks() {
|
Chris@0
|
212 $expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()->getUsername() . '</div>';
|
Chris@0
|
213 $this->drupalGet('');
|
Chris@0
|
214 $this->assertNoText('Test context-aware block');
|
Chris@0
|
215 $this->assertNoRaw($expected_text);
|
Chris@0
|
216
|
Chris@0
|
217 $block_url = 'admin/structure/block/add/test_context_aware/classy';
|
Chris@0
|
218 $arguments = [
|
Chris@0
|
219 ':title' => 'Test context-aware block',
|
Chris@0
|
220 ':category' => 'Block test',
|
Chris@0
|
221 ':href' => $block_url,
|
Chris@0
|
222 ];
|
Chris@0
|
223 $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
|
Chris@0
|
224
|
Chris@0
|
225 $this->drupalGet('admin/structure/block');
|
Chris@0
|
226 $this->clickLink('Place block');
|
Chris@0
|
227 $elements = $this->xpath($pattern, $arguments);
|
Chris@0
|
228 $this->assertTrue(!empty($elements), 'The context-aware test block appears.');
|
Chris@0
|
229 $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
|
Chris@0
|
230 $this->assertTrue(!empty($definition), 'The context-aware test block exists.');
|
Chris@0
|
231 $edit = [
|
Chris@0
|
232 'region' => 'content',
|
Chris@0
|
233 'settings[context_mapping][user]' => '@block_test.multiple_static_context:userB',
|
Chris@0
|
234 ];
|
Chris@0
|
235 $this->drupalPostForm($block_url, $edit, 'Save block');
|
Chris@0
|
236
|
Chris@0
|
237 $this->drupalGet('');
|
Chris@0
|
238 $this->assertText('Test context-aware block');
|
Chris@0
|
239 $this->assertText('User context found.');
|
Chris@0
|
240 $this->assertRaw($expected_text);
|
Chris@0
|
241
|
Chris@0
|
242 // Test context mapping allows empty selection for optional contexts.
|
Chris@0
|
243 $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
|
Chris@0
|
244 $edit = [
|
Chris@0
|
245 'settings[context_mapping][user]' => '',
|
Chris@0
|
246 ];
|
Chris@0
|
247 $this->drupalPostForm(NULL, $edit, 'Save block');
|
Chris@0
|
248 $this->drupalGet('');
|
Chris@0
|
249 $this->assertText('No context mapping selected.');
|
Chris@0
|
250 $this->assertNoText('User context found.');
|
Chris@0
|
251
|
Chris@0
|
252 // Tests that conditions with missing context are not displayed.
|
Chris@0
|
253 $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
|
Chris@0
|
254 $this->assertNoRaw('No existing type');
|
Chris@0
|
255 $this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]');
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * Tests that the BlockForm populates machine name correctly.
|
Chris@0
|
260 */
|
Chris@0
|
261 public function testMachineNameSuggestion() {
|
Chris@0
|
262 $url = 'admin/structure/block/add/test_block_instantiation/classy';
|
Chris@0
|
263 $this->drupalGet($url);
|
Chris@0
|
264 $this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
|
Chris@0
|
265 $edit = ['region' => 'content'];
|
Chris@0
|
266 $this->drupalPostForm($url, $edit, 'Save block');
|
Chris@0
|
267 $this->assertText('The block configuration has been saved.');
|
Chris@0
|
268
|
Chris@0
|
269 // Now, check to make sure the form starts by autoincrementing correctly.
|
Chris@0
|
270 $this->drupalGet($url);
|
Chris@0
|
271 $this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
|
Chris@0
|
272 $this->drupalPostForm($url, $edit, 'Save block');
|
Chris@0
|
273 $this->assertText('The block configuration has been saved.');
|
Chris@0
|
274
|
Chris@0
|
275 // And verify that it continues working beyond just the first two.
|
Chris@0
|
276 $this->drupalGet($url);
|
Chris@0
|
277 $this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@0
|
280 /**
|
Chris@0
|
281 * Tests the block placement indicator.
|
Chris@0
|
282 */
|
Chris@0
|
283 public function testBlockPlacementIndicator() {
|
Chris@0
|
284 // Select the 'Powered by Drupal' block to be placed.
|
Chris@0
|
285 $block = [];
|
Chris@0
|
286 $block['id'] = strtolower($this->randomMachineName());
|
Chris@0
|
287 $block['theme'] = 'classy';
|
Chris@0
|
288 $block['region'] = 'content';
|
Chris@0
|
289
|
Chris@0
|
290 // After adding a block, it will indicate which block was just added.
|
Chris@0
|
291 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
|
Chris@0
|
292 $this->assertUrl('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
|
Chris@0
|
293
|
Chris@0
|
294 // Resaving the block page will remove the block indicator.
|
Chris@0
|
295 $this->drupalPostForm(NULL, [], t('Save blocks'));
|
Chris@0
|
296 $this->assertUrl('admin/structure/block/list/classy');
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 /**
|
Chris@0
|
300 * Tests if validation errors are passed plugin form to the parent form.
|
Chris@0
|
301 */
|
Chris@0
|
302 public function testBlockValidateErrors() {
|
Chris@0
|
303 $this->drupalPostForm('admin/structure/block/add/test_settings_validation/classy', ['region' => 'content', 'settings[digits]' => 'abc'], t('Save block'));
|
Chris@0
|
304
|
Chris@0
|
305 $arguments = [':message' => 'Only digits are allowed'];
|
Chris@0
|
306 $pattern = '//div[contains(@class,"messages messages--error")]/div[contains(text()[2],:message)]';
|
Chris@0
|
307 $elements = $this->xpath($pattern, $arguments);
|
Chris@0
|
308 $this->assertTrue($elements, 'Plugin error message found in parent form.');
|
Chris@0
|
309
|
Chris@0
|
310 $error_class_pattern = '//div[contains(@class,"form-item-settings-digits")]/input[contains(@class,"error")]';
|
Chris@0
|
311 $error_class = $this->xpath($error_class_pattern);
|
Chris@0
|
312 $this->assertTrue($error_class, 'Plugin error class found in parent form.');
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 /**
|
Chris@0
|
316 * Tests that the enable/disable routes are protected from CSRF.
|
Chris@0
|
317 */
|
Chris@0
|
318 public function testRouteProtection() {
|
Chris@0
|
319 // Get the first block generated in our setUp method.
|
Chris@0
|
320 /** @var \Drupal\block\BlockInterface $block */
|
Chris@0
|
321 $block = reset($this->blocks);
|
Chris@0
|
322 // Ensure that the enable and disable routes are protected.
|
Chris@0
|
323 $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/disable');
|
Chris@0
|
324 $this->assertResponse(403);
|
Chris@0
|
325 $this->drupalGet('admin/structure/block/manage/' . $block->id() . '/enable');
|
Chris@0
|
326 $this->assertResponse(403);
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 }
|