BlockUiTest.php 12 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\block\Tests;

5
use Drupal\Component\Utility\Html;
6
7
8
use Drupal\simpletest\WebTestBase;

/**
9
10
11
 * Tests that the block configuration UI exists and stores data correctly.
 *
 * @group block
12
13
14
15
 */
class BlockUiTest extends WebTestBase {

  /**
16
   * Modules to install.
17
18
19
   *
   * @var array
   */
20
  public static $modules = array('block', 'block_test', 'help');
21
22
23

  protected $regions;

24
25
26
27
28
29
30
31
32
33
34
35
36
37
  /**
   * The submitted block values used by this test.
   *
   * @var array
   */
  protected $blockValues;

  /**
   * The block entities used by this test.
   *
   * @var \Drupal\block\BlockInterface[]
   */
  protected $blocks;

38
39
40
41
42
  /**
   * An administrative user to configure the test environment.
   */
  protected $adminUser;

43
  protected function setUp() {
44
45
46
47
48
49
50
    parent::setUp();
    // Create and log in an administrative user.
    $this->adminUser = $this->drupalCreateUser(array(
      'administer blocks',
      'access administration pages',
    ));
    $this->drupalLogin($this->adminUser);
51
52

    // Enable some test blocks.
53
    $this->blockValues = array(
54
55
56
      array(
        'label' => 'Tools',
        'tr' => '5',
57
        'plugin_id' => 'system_menu_block:tools',
58
        'settings' => array('region' => 'sidebar_second', 'id' => 'tools'),
59
60
61
62
        'test_weight' => '-1',
      ),
      array(
        'label' => 'Powered by Drupal',
63
        'tr' => '16',
64
        'plugin_id' => 'system_powered_by_block',
65
        'settings' => array('region' => 'footer', 'id' => 'powered'),
66
67
68
        'test_weight' => '0',
      ),
    );
69
70
71
    $this->blocks = array();
    foreach ($this->blockValues as $values) {
      $this->blocks[] = $this->drupalPlaceBlock($values['plugin_id'], $values['settings']);
72
    }
73
74
  }

75
76
77
78
  /**
   * Test block demo page exists and functions correctly.
   */
  public function testBlockDemoUiPage() {
79
    $this->drupalPlaceBlock('help_block', array('region' => 'help'));
80
    $this->drupalGet('admin/structure/block');
81
    $this->clickLink(t('Demonstrate block regions (@theme)', array('@theme' => 'Classy')));
82
83
    $elements = $this->xpath('//div[contains(@class, "region-highlighted")]/div[contains(@class, "block-region") and contains(text(), :title)]', array(':title' => 'Highlighted'));
    $this->assertTrue(!empty($elements), 'Block demo regions are shown.');
84
85
86
87

    \Drupal::service('theme_handler')->install(array('test_theme'));
    $this->drupalGet('admin/structure/block/demo/test_theme');
    $this->assertEscaped('<strong>Test theme</strong>');
88
89
90
91

    \Drupal::service('theme_handler')->install(['stable']);
    $this->drupalGet('admin/structure/block/demo/stable');
    $this->assertResponse(404, 'Hidden themes that are not the default theme are not supported by the block demo screen');
92
93
  }

94
  /**
95
   * Test block admin page exists and functions correctly.
96
   */
97
98
99
100
101
102
103
  function testBlockAdminUiPage() {
    // Visit the blocks admin ui.
    $this->drupalGet('admin/structure/block');
    // Look for the blocks table.
    $blocks_table = $this->xpath("//table[@id='blocks']");
    $this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
    // Look for test blocks in the table.
104
105
106
    foreach ($this->blockValues as $delta => $values) {
      $block = $this->blocks[$delta];
      $label = $block->label();
107
      $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
108
      $this->assertTrue((string) $element[0] == $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
109
      // Look for a test block region select form element.
110
      $this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
111
      // Move the test block to the header region.
112
      $edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
113
      // Look for a test block weight select form element.
114
      $this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
115
      // Change the test block's weight.
116
      $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
117
    }
118
    $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
119
    foreach ($this->blockValues as $values) {
120
121
      // Check if the region and weight settings changes have persisted.
      $this->assertOptionSelected(
122
        'edit-blocks-' . $values['settings']['id'] . '-region',
123
        'header',
124
        'The block "' . $label . '" has the correct region assignment (header).'
125
126
      );
      $this->assertOptionSelected(
127
        'edit-blocks-' . $values['settings']['id'] . '-weight',
128
        $values['test_weight'],
129
        'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').'
130
131
      );
    }
132
133
134
135
136
137

    // Add a block with a machine name the same as a region name.
    $this->drupalPlaceBlock('system_powered_by_block', ['region' => 'header', 'id' => 'header']);
    $this->drupalGet('admin/structure/block');
    $element = $this->xpath('//tr[contains(@class, :class)]', [':class' => 'region-title-header']);
    $this->assertTrue(!empty($element));
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    // Ensure hidden themes do not appear in the UI. Enable another non base
    // theme and place the local tasks block.
    $this->assertTrue(\Drupal::service('theme_handler')->themeExists('classy'), 'The classy base theme is enabled');
    $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']);
    \Drupal::service('theme_installer')->install(['stable', 'stark']);
    $this->drupalGet('admin/structure/block');
    $theme_handler = \Drupal::service('theme_handler');
    $this->assertLink($theme_handler->getName('classy'));
    $this->assertLink($theme_handler->getName('stark'));
    $this->assertNoLink($theme_handler->getName('stable'));

    $this->drupalGet('admin/structure/block/list/stable');
    $this->assertResponse(404, 'Placing blocks through UI is not possible for a hidden base theme.');

    \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save();
    \Drupal::service('router.builder')->rebuildIfNeeded();
    $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']);
    $this->drupalGet('admin/structure/block');
    $this->assertLink($theme_handler->getName('stable'));
    $this->drupalGet('admin/structure/block/list/stable');
    $this->assertResponse(200, 'Placing blocks through UI is possible for a hidden base theme that is the admin theme.');
160
  }
161
162

  /**
163
   * Tests the block categories on the listing page.
164
   */
165
166
  public function testCandidateBlockList() {
    $arguments = array(
167
168
      ':title' => 'Display message',
      ':category' => 'Block test',
169
      ':href' => 'admin/structure/block/add/test_block_instantiation/classy',
170
    );
171
    $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
172
173

    $this->drupalGet('admin/structure/block');
174
175
    $this->clickLinkPartialName('Place block');
    $elements = $this->xpath($pattern, $arguments);
176
177
178
179
180
181
182
    $this->assertTrue(!empty($elements), 'The test block appears in the category for its module.');

    // Trigger the custom category addition in block_test_block_alter().
    $this->container->get('state')->set('block_test_info_alter', TRUE);
    $this->container->get('plugin.manager.block')->clearCachedDefinitions();

    $this->drupalGet('admin/structure/block');
183
184
185
    $this->clickLinkPartialName('Place block');
    $arguments[':category'] = 'Custom category';
    $elements = $this->xpath($pattern, $arguments);
186
    $this->assertTrue(!empty($elements), 'The test block appears in a custom category controlled by block_test_block_alter().');
187
  }
188

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  /**
   * Tests the behavior of unsatisfied context-aware blocks.
   */
  public function testContextAwareUnsatisfiedBlocks() {
    $arguments = array(
      ':category' => 'Block test',
      ':href' => 'admin/structure/block/add/test_context_aware_unsatisfied/classy',
      ':text' => 'Test context-aware unsatisfied block',
    );

    $this->drupalGet('admin/structure/block');
    $this->clickLinkPartialName('Place block');
    $elements = $this->xpath('//tr[.//td/div[text()=:text] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]', $arguments);
    $this->assertTrue(empty($elements), 'The context-aware test block does not appear.');

    $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware_unsatisfied');
    $this->assertTrue(!empty($definition), 'The context-aware test block does not exist.');
  }

208
209
210
211
  /**
   * Tests the behavior of context-aware blocks.
   */
  public function testContextAwareBlocks() {
212
213
214
215
216
217
    $expected_text = '<div id="test_context_aware--username">' . \Drupal::currentUser()->getUsername() . '</div>';
    $this->drupalGet('');
    $this->assertNoText('Test context-aware block');
    $this->assertNoRaw($expected_text);

    $block_url = 'admin/structure/block/add/test_context_aware/classy';
218
    $arguments = array(
219
220
221
      ':title' => 'Test context-aware block',
      ':category' => 'Block test',
      ':href' => $block_url,
222
    );
223
    $pattern = '//tr[.//td/div[text()=:title] and .//td[text()=:category] and .//td//a[contains(@href, :href)]]';
224
225

    $this->drupalGet('admin/structure/block');
226
227
228
    $this->clickLinkPartialName('Place block');
    $elements = $this->xpath($pattern, $arguments);
    $this->assertTrue(!empty($elements), 'The context-aware test block appears.');
229
230
    $definition = \Drupal::service('plugin.manager.block')->getDefinition('test_context_aware');
    $this->assertTrue(!empty($definition), 'The context-aware test block exists.');
231
232
233
234
235
236
237
238
    $edit = [
      'region' => 'content',
      'settings[context_mapping][user]' => '@block_test.multiple_static_context:user2',
    ];
    $this->drupalPostForm($block_url, $edit, 'Save block');

    $this->drupalGet('');
    $this->assertText('Test context-aware block');
239
    $this->assertText('User context found.');
240
    $this->assertRaw($expected_text);
241
242
243
244
245
246
247
248
249

    // Test context mapping allows empty selection for optional contexts.
    $this->drupalGet('admin/structure/block/manage/testcontextawareblock');
    $edit = [
      'settings[context_mapping][user]' => '',
    ];
    $this->drupalPostForm(NULL, $edit, 'Save block');
    $this->drupalGet('');
    $this->assertText('No context mapping selected.');
250
    $this->assertNoText('User context found.');
251
252
  }

253
  /**
254
   * Tests that the BlockForm populates machine name correctly.
255
256
   */
  public function testMachineNameSuggestion() {
257
    $url = 'admin/structure/block/add/test_block_instantiation/classy';
258
    $this->drupalGet($url);
259
    $this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
260
    $this->drupalPostForm($url, array(), 'Save block');
261
262
263

    // Now, check to make sure the form starts by autoincrementing correctly.
    $this->drupalGet($url);
264
    $this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
265
    $this->drupalPostForm($url, array(), 'Save block');
266
267
268

    // And verify that it continues working beyond just the first two.
    $this->drupalGet($url);
269
    $this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
270
271
  }

272
273
274
275
276
277
  /**
   * Tests the block placement indicator.
   */
  public function testBlockPlacementIndicator() {
    // Select the 'Powered by Drupal' block to be placed.
    $block = array();
278
    $block['id'] = strtolower($this->randomMachineName());
279
    $block['theme'] = 'classy';
280
281
282
283
    $block['region'] = 'content';

    // After adding a block, it will indicate which block was just added.
    $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
284
    $this->assertUrl('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
285
286
287

    // Resaving the block page will remove the block indicator.
    $this->drupalPostForm(NULL, array(), t('Save blocks'));
288
    $this->assertUrl('admin/structure/block/list/classy');
289
290
  }

291
}