annotate core/modules/block/tests/src/Functional/BlockTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
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\block\Entity\Block;
Chris@0 7 use Drupal\Core\Url;
Chris@0 8 use Drupal\user\Entity\Role;
Chris@0 9 use Drupal\user\RoleInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests basic block functionality.
Chris@0 13 *
Chris@0 14 * @group block
Chris@0 15 */
Chris@0 16 class BlockTest extends BlockTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Tests block visibility.
Chris@0 20 */
Chris@0 21 public function testBlockVisibility() {
Chris@0 22 $block_name = 'system_powered_by_block';
Chris@0 23 // Create a random title for the block.
Chris@0 24 $title = $this->randomMachineName(8);
Chris@0 25 // Enable a standard block.
Chris@0 26 $default_theme = $this->config('system.theme')->get('default');
Chris@0 27 $edit = [
Chris@0 28 'id' => strtolower($this->randomMachineName(8)),
Chris@0 29 'region' => 'sidebar_first',
Chris@0 30 'settings[label]' => $title,
Chris@0 31 'settings[label_display]' => TRUE,
Chris@0 32 ];
Chris@0 33 // Set the block to be hidden on any user path, and to be shown only to
Chris@0 34 // authenticated users.
Chris@0 35 $edit['visibility[request_path][pages]'] = '/user*';
Chris@0 36 $edit['visibility[request_path][negate]'] = TRUE;
Chris@0 37 $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
Chris@0 38 $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
Chris@0 39 $this->assertFieldChecked('edit-visibility-request-path-negate-0');
Chris@0 40
Chris@0 41 $this->drupalPostForm(NULL, $edit, t('Save block'));
Chris@0 42 $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0 43
Chris@0 44 $this->clickLink('Configure');
Chris@0 45 $this->assertFieldChecked('edit-visibility-request-path-negate-1');
Chris@0 46
Chris@0 47 $this->drupalGet('');
Chris@0 48 $this->assertText($title, 'Block was displayed on the front page.');
Chris@0 49
Chris@0 50 $this->drupalGet('user');
Chris@0 51 $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
Chris@0 52
Chris@0 53 // Confirm that the block is not displayed to anonymous users.
Chris@0 54 $this->drupalLogout();
Chris@0 55 $this->drupalGet('');
Chris@0 56 $this->assertNoText($title, 'Block was not displayed to anonymous users.');
Chris@0 57
Chris@0 58 // Confirm that an empty block is not displayed.
Chris@0 59 $this->assertNoText('Powered by Drupal', 'Empty block not displayed.');
Chris@0 60 $this->assertNoRaw('sidebar-first', 'Empty sidebar-first region is not displayed.');
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Tests that visibility can be properly toggled.
Chris@0 65 */
Chris@0 66 public function testBlockToggleVisibility() {
Chris@0 67 $block_name = 'system_powered_by_block';
Chris@0 68 // Create a random title for the block.
Chris@0 69 $title = $this->randomMachineName(8);
Chris@0 70 // Enable a standard block.
Chris@0 71 $default_theme = $this->config('system.theme')->get('default');
Chris@0 72 $edit = [
Chris@0 73 'id' => strtolower($this->randomMachineName(8)),
Chris@0 74 'region' => 'sidebar_first',
Chris@0 75 'settings[label]' => $title,
Chris@0 76 ];
Chris@0 77 $block_id = $edit['id'];
Chris@0 78 // Set the block to be shown only to authenticated users.
Chris@0 79 $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
Chris@0 80 $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0 81 $this->clickLink('Configure');
Chris@0 82 $this->assertFieldChecked('edit-visibility-user-role-roles-authenticated');
Chris@0 83
Chris@0 84 $edit = [
Chris@0 85 'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
Chris@0 86 ];
Chris@0 87 $this->drupalPostForm(NULL, $edit, 'Save block');
Chris@0 88 $this->clickLink('Configure');
Chris@0 89 $this->assertNoFieldChecked('edit-visibility-user-role-roles-authenticated');
Chris@0 90
Chris@0 91 // Ensure that no visibility is configured.
Chris@0 92 /** @var \Drupal\block\BlockInterface $block */
Chris@0 93 $block = Block::load($block_id);
Chris@0 94 $visibility_config = $block->getVisibilityConditions()->getConfiguration();
Chris@0 95 $this->assertIdentical([], $visibility_config);
Chris@0 96 $this->assertIdentical([], $block->get('visibility'));
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Test block visibility when leaving "pages" textarea empty.
Chris@0 101 */
Chris@0 102 public function testBlockVisibilityListedEmpty() {
Chris@0 103 $block_name = 'system_powered_by_block';
Chris@0 104 // Create a random title for the block.
Chris@0 105 $title = $this->randomMachineName(8);
Chris@0 106 // Enable a standard block.
Chris@0 107 $default_theme = $this->config('system.theme')->get('default');
Chris@0 108 $edit = [
Chris@0 109 'id' => strtolower($this->randomMachineName(8)),
Chris@0 110 'region' => 'sidebar_first',
Chris@0 111 'settings[label]' => $title,
Chris@0 112 'visibility[request_path][negate]' => TRUE,
Chris@0 113 ];
Chris@0 114 // Set the block to be hidden on any user path, and to be shown only to
Chris@0 115 // authenticated users.
Chris@0 116 $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0 117 $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0 118
Chris@0 119 $this->drupalGet('user');
Chris@0 120 $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
Chris@0 121
Chris@0 122 $this->drupalGet('USER');
Chris@0 123 $this->assertNoText($title, 'Block was not displayed according to block visibility rules regardless of path case.');
Chris@0 124
Chris@0 125 // Confirm that the block is not displayed to anonymous users.
Chris@0 126 $this->drupalLogout();
Chris@0 127 $this->drupalGet('');
Chris@0 128 $this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.');
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Tests adding a block from the library page with a weight query string.
Chris@0 133 */
Chris@0 134 public function testAddBlockFromLibraryWithWeight() {
Chris@0 135 $default_theme = $this->config('system.theme')->get('default');
Chris@0 136 // Test one positive, zero, and one negative weight.
Chris@0 137 foreach (['7', '0', '-9'] as $weight) {
Chris@0 138 $options = [
Chris@0 139 'query' => [
Chris@0 140 'region' => 'sidebar_first',
Chris@0 141 'weight' => $weight,
Chris@0 142 ],
Chris@0 143 ];
Chris@0 144 $this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
Chris@0 145
Chris@0 146 $block_name = 'system_powered_by_block';
Chris@0 147 $add_url = Url::fromRoute('block.admin_add', [
Chris@0 148 'plugin_id' => $block_name,
Chris@0 149 'theme' => $default_theme
Chris@0 150 ]);
Chris@0 151 $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
Chris@0 152 $this->assertEqual(1, count($links), 'Found one matching link.');
Chris@0 153 $this->assertEqual(t('Place block'), $links[0]->getText(), 'Found the expected link text.');
Chris@0 154
Chris@0 155 list($path, $query_string) = explode('?', $links[0]->getAttribute('href'), 2);
Chris@0 156 parse_str($query_string, $query_parts);
Chris@0 157 $this->assertEqual($weight, $query_parts['weight'], 'Found the expected weight query string.');
Chris@0 158
Chris@0 159 // Create a random title for the block.
Chris@0 160 $title = $this->randomMachineName(8);
Chris@0 161 $block_id = strtolower($this->randomMachineName(8));
Chris@0 162 $edit = [
Chris@0 163 'id' => $block_id,
Chris@0 164 'settings[label]' => $title,
Chris@0 165 ];
Chris@0 166 // Create the block using the link parsed from the library page.
Chris@0 167 $this->drupalPostForm($this->getAbsoluteUrl($links[0]->getAttribute('href')), $edit, t('Save block'));
Chris@0 168
Chris@0 169 // Ensure that the block was created with the expected weight.
Chris@0 170 /** @var \Drupal\block\BlockInterface $block */
Chris@0 171 $block = Block::load($block_id);
Chris@0 172 $this->assertEqual($weight, $block->getWeight(), 'Found the block with expected weight.');
Chris@0 173 }
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Test configuring and moving a module-define block to specific regions.
Chris@0 178 */
Chris@0 179 public function testBlock() {
Chris@0 180 // Place page title block to test error messages.
Chris@0 181 $this->drupalPlaceBlock('page_title_block');
Chris@0 182
Chris@0 183 // Disable the block.
Chris@0 184 $this->drupalGet('admin/structure/block');
Chris@0 185 $this->clickLink('Disable');
Chris@0 186
Chris@0 187 // Select the 'Powered by Drupal' block to be configured and moved.
Chris@0 188 $block = [];
Chris@0 189 $block['id'] = 'system_powered_by_block';
Chris@0 190 $block['settings[label]'] = $this->randomMachineName(8);
Chris@0 191 $block['settings[label_display]'] = TRUE;
Chris@0 192 $block['theme'] = $this->config('system.theme')->get('default');
Chris@0 193 $block['region'] = 'header';
Chris@0 194
Chris@0 195 // Set block title to confirm that interface works and override any custom titles.
Chris@0 196 $this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], ['settings[label]' => $block['settings[label]'], 'settings[label_display]' => $block['settings[label_display]'], 'id' => $block['id'], 'region' => $block['region']], t('Save block'));
Chris@0 197 $this->assertText(t('The block configuration has been saved.'), 'Block title set.');
Chris@0 198 // Check to see if the block was created by checking its configuration.
Chris@0 199 $instance = Block::load($block['id']);
Chris@0 200
Chris@0 201 $this->assertEqual($instance->label(), $block['settings[label]'], 'Stored block title found.');
Chris@0 202
Chris@0 203 // Check whether the block can be moved to all available regions.
Chris@0 204 foreach ($this->regions as $region) {
Chris@0 205 $this->moveBlockToRegion($block, $region);
Chris@0 206 }
Chris@0 207
Chris@0 208 // Disable the block.
Chris@0 209 $this->drupalGet('admin/structure/block');
Chris@0 210 $this->clickLink('Disable');
Chris@0 211
Chris@0 212 // Confirm that the block is now listed as disabled.
Chris@0 213 $this->assertText(t('The block settings have been updated.'), 'Block successfully moved to disabled region.');
Chris@0 214
Chris@0 215 // Confirm that the block instance title and markup are not displayed.
Chris@0 216 $this->drupalGet('node');
Chris@0 217 $this->assertNoText(t($block['settings[label]']));
Chris@0 218 // Check for <div id="block-my-block-instance-name"> if the machine name
Chris@0 219 // is my_block_instance_name.
Chris@0 220 $xpath = $this->buildXPathQuery('//div[@id=:id]/*', [':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))]);
Chris@0 221 $this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
Chris@0 222
Chris@0 223 // Test deleting the block from the edit form.
Chris@0 224 $this->drupalGet('admin/structure/block/manage/' . $block['id']);
Chris@0 225 $this->clickLink(t('Remove block'));
Chris@0 226 $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block['settings[label]']]));
Chris@0 227 $this->drupalPostForm(NULL, [], t('Remove'));
Chris@0 228 $this->assertRaw(t('The block %name has been removed.', ['%name' => $block['settings[label]']]));
Chris@0 229
Chris@0 230 // Test deleting a block via "Configure block" link.
Chris@0 231 $block = $this->drupalPlaceBlock('system_powered_by_block');
Chris@0 232 $this->drupalGet('admin/structure/block/manage/' . $block->id(), ['query' => ['destination' => 'admin']]);
Chris@0 233 $this->clickLink(t('Remove block'));
Chris@0 234 $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block->label()]));
Chris@0 235 $this->drupalPostForm(NULL, [], t('Remove'));
Chris@0 236 $this->assertRaw(t('The block %name has been removed.', ['%name' => $block->label()]));
Chris@0 237 $this->assertUrl('admin');
Chris@0 238 $this->assertNoRaw($block->id());
Chris@0 239 }
Chris@0 240
Chris@0 241 /**
Chris@0 242 * Tests that the block form has a theme selector when not passed via the URL.
Chris@0 243 */
Chris@0 244 public function testBlockThemeSelector() {
Chris@0 245 // Install all themes.
Chris@0 246 \Drupal::service('theme_handler')->install(['bartik', 'seven', 'stark']);
Chris@0 247 $theme_settings = $this->config('system.theme');
Chris@0 248 foreach (['bartik', 'seven', 'stark'] as $theme) {
Chris@0 249 $this->drupalGet('admin/structure/block/list/' . $theme);
Chris@0 250 $this->assertTitle(t('Block layout') . ' | Drupal');
Chris@0 251 // Select the 'Powered by Drupal' block to be placed.
Chris@0 252 $block = [];
Chris@0 253 $block['id'] = strtolower($this->randomMachineName());
Chris@0 254 $block['theme'] = $theme;
Chris@0 255 $block['region'] = 'content';
Chris@0 256 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
Chris@0 257 $this->assertText(t('The block configuration has been saved.'));
Chris@0 258 $this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . Html::getClass($block['id']));
Chris@0 259
Chris@0 260 // Set the default theme and ensure the block is placed.
Chris@0 261 $theme_settings->set('default', $theme)->save();
Chris@0 262 $this->drupalGet('');
Chris@0 263 $elements = $this->xpath('//div[@id = :id]', [':id' => Html::getUniqueId('block-' . $block['id'])]);
Chris@0 264 $this->assertTrue(!empty($elements), 'The block was found.');
Chris@0 265 }
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@0 269 * Test block display of theme titles.
Chris@0 270 */
Chris@0 271 public function testThemeName() {
Chris@0 272 // Enable the help block.
Chris@0 273 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
Chris@0 274 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 275 // Explicitly set the default and admin themes.
Chris@0 276 $theme = 'block_test_specialchars_theme';
Chris@0 277 \Drupal::service('theme_handler')->install([$theme]);
Chris@0 278 \Drupal::service('router.builder')->rebuild();
Chris@0 279 $this->drupalGet('admin/structure/block');
Chris@0 280 $this->assertEscaped('<"Cat" & \'Mouse\'>');
Chris@0 281 $this->drupalGet('admin/structure/block/list/block_test_specialchars_theme');
Chris@0 282 $this->assertEscaped('Demonstrate block regions (<"Cat" & \'Mouse\'>)');
Chris@0 283 }
Chris@0 284
Chris@0 285 /**
Chris@0 286 * Test block title display settings.
Chris@0 287 */
Chris@0 288 public function testHideBlockTitle() {
Chris@0 289 $block_name = 'system_powered_by_block';
Chris@0 290 // Create a random title for the block.
Chris@0 291 $title = $this->randomMachineName(8);
Chris@0 292 $id = strtolower($this->randomMachineName(8));
Chris@0 293 // Enable a standard block.
Chris@0 294 $default_theme = $this->config('system.theme')->get('default');
Chris@0 295 $edit = [
Chris@0 296 'id' => $id,
Chris@0 297 'region' => 'sidebar_first',
Chris@0 298 'settings[label]' => $title,
Chris@0 299 ];
Chris@0 300 $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0 301 $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0 302
Chris@0 303 $this->drupalGet('user');
Chris@0 304 $this->assertNoText($title, 'Block title was not displayed by default.');
Chris@0 305
Chris@0 306 $edit = [
Chris@0 307 'settings[label_display]' => TRUE,
Chris@0 308 ];
Chris@0 309 $this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block'));
Chris@0 310 $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0 311
Chris@0 312 $this->drupalGet('admin/structure/block/manage/' . $id);
Chris@0 313 $this->assertFieldChecked('edit-settings-label-display', 'The display_block option has the correct default value on the configuration form.');
Chris@0 314
Chris@0 315 $this->drupalGet('user');
Chris@0 316 $this->assertText($title, 'Block title was displayed when enabled.');
Chris@0 317 }
Chris@0 318
Chris@0 319 /**
Chris@0 320 * Moves a block to a given region via the UI and confirms the result.
Chris@0 321 *
Chris@0 322 * @param array $block
Chris@0 323 * An array of information about the block, including the following keys:
Chris@0 324 * - module: The module providing the block.
Chris@0 325 * - title: The title of the block.
Chris@0 326 * - delta: The block's delta key.
Chris@0 327 * @param string $region
Chris@0 328 * The machine name of the theme region to move the block to, for example
Chris@0 329 * 'header' or 'sidebar_first'.
Chris@0 330 */
Chris@0 331 public function moveBlockToRegion(array $block, $region) {
Chris@0 332 // Set the created block to a specific region.
Chris@0 333 $block += ['theme' => $this->config('system.theme')->get('default')];
Chris@0 334 $edit = [];
Chris@0 335 $edit['blocks[' . $block['id'] . '][region]'] = $region;
Chris@0 336 $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
Chris@0 337
Chris@0 338 // Confirm that the block was moved to the proper region.
Chris@0 339 $this->assertText(t('The block settings have been updated.'), format_string('Block successfully moved to %region_name region.', ['%region_name' => $region]));
Chris@0 340
Chris@0 341 // Confirm that the block is being displayed.
Chris@0 342 $this->drupalGet('');
Chris@0 343 $this->assertText(t($block['settings[label]']), 'Block successfully being displayed on the page.');
Chris@0 344
Chris@0 345 // Confirm that the custom block was found at the proper region.
Chris@0 346 $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', [
Chris@0 347 ':region-class' => 'region region-' . Html::getClass($region),
Chris@0 348 ':block-id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
Chris@0 349 ]);
Chris@0 350 $this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', ['%region_name' => Html::getClass($region)]));
Chris@0 351 }
Chris@0 352
Chris@0 353 /**
Chris@0 354 * Test that cache tags are properly set and bubbled up to the page cache.
Chris@0 355 *
Chris@0 356 * Verify that invalidation of these cache tags works:
Chris@0 357 * - "block:<block ID>"
Chris@0 358 * - "block_plugin:<block plugin ID>"
Chris@0 359 */
Chris@0 360 public function testBlockCacheTags() {
Chris@0 361 // The page cache only works for anonymous users.
Chris@0 362 $this->drupalLogout();
Chris@0 363
Chris@0 364 // Enable page caching.
Chris@0 365 $config = $this->config('system.performance');
Chris@0 366 $config->set('cache.page.max_age', 300);
Chris@0 367 $config->save();
Chris@0 368
Chris@0 369 // Place the "Powered by Drupal" block.
Chris@0 370 $block = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered']);
Chris@0 371
Chris@0 372 // Prime the page cache.
Chris@0 373 $this->drupalGet('<front>');
Chris@0 374 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0 375
Chris@0 376 // Verify a cache hit, but also the presence of the correct cache tags in
Chris@0 377 // both the page and block caches.
Chris@0 378 $this->drupalGet('<front>');
Chris@0 379 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0 380 $cid_parts = [\Drupal::url('<front>', [], ['absolute' => TRUE]), 'html'];
Chris@0 381 $cid = implode(':', $cid_parts);
Chris@0 382 $cache_entry = \Drupal::cache('page')->get($cid);
Chris@0 383 $expected_cache_tags = [
Chris@0 384 'config:block_list',
Chris@0 385 'block_view',
Chris@0 386 'config:block.block.powered',
Chris@0 387 'config:user.role.anonymous',
Chris@0 388 'http_response',
Chris@0 389 'rendered',
Chris@0 390 ];
Chris@0 391 sort($expected_cache_tags);
Chris@0 392 $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0 393 $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0 394 $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
Chris@0 395 $expected_cache_tags = [
Chris@0 396 'block_view',
Chris@0 397 'config:block.block.powered',
Chris@0 398 'rendered',
Chris@0 399 ];
Chris@0 400 sort($expected_cache_tags);
Chris@0 401 $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0 402
Chris@0 403 // The "Powered by Drupal" block is modified; verify a cache miss.
Chris@0 404 $block->setRegion('content');
Chris@0 405 $block->save();
Chris@0 406 $this->drupalGet('<front>');
Chris@0 407 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0 408
Chris@0 409 // Now we should have a cache hit again.
Chris@0 410 $this->drupalGet('<front>');
Chris@0 411 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0 412
Chris@0 413 // Place the "Powered by Drupal" block another time; verify a cache miss.
Chris@0 414 $block_2 = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered-2']);
Chris@0 415 $this->drupalGet('<front>');
Chris@0 416 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0 417
Chris@0 418 // Verify a cache hit, but also the presence of the correct cache tags.
Chris@0 419 $this->drupalGet('<front>');
Chris@0 420 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0 421 $cid_parts = [\Drupal::url('<front>', [], ['absolute' => TRUE]), 'html'];
Chris@0 422 $cid = implode(':', $cid_parts);
Chris@0 423 $cache_entry = \Drupal::cache('page')->get($cid);
Chris@0 424 $expected_cache_tags = [
Chris@0 425 'config:block_list',
Chris@0 426 'block_view',
Chris@0 427 'config:block.block.powered',
Chris@0 428 'config:block.block.powered-2',
Chris@0 429 'config:user.role.anonymous',
Chris@0 430 'http_response',
Chris@0 431 'rendered',
Chris@0 432 ];
Chris@0 433 sort($expected_cache_tags);
Chris@0 434 $this->assertEqual($cache_entry->tags, $expected_cache_tags);
Chris@0 435 $expected_cache_tags = [
Chris@0 436 'block_view',
Chris@0 437 'config:block.block.powered',
Chris@0 438 'rendered',
Chris@0 439 ];
Chris@0 440 sort($expected_cache_tags);
Chris@0 441 $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0 442 $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
Chris@0 443 $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0 444 $expected_cache_tags = [
Chris@0 445 'block_view',
Chris@0 446 'config:block.block.powered-2',
Chris@0 447 'rendered',
Chris@0 448 ];
Chris@0 449 sort($expected_cache_tags);
Chris@0 450 $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0 451 $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered-2:' . implode(':', $keys));
Chris@0 452 $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0 453
Chris@0 454 // Now we should have a cache hit again.
Chris@0 455 $this->drupalGet('<front>');
Chris@0 456 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0 457
Chris@0 458 // Delete the "Powered by Drupal" blocks; verify a cache miss.
Chris@0 459 entity_delete_multiple('block', ['powered', 'powered-2']);
Chris@0 460 $this->drupalGet('<front>');
Chris@0 461 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0 462 }
Chris@0 463
Chris@0 464 /**
Chris@0 465 * Tests that a link exists to block layout from the appearance form.
Chris@0 466 */
Chris@0 467 public function testThemeAdminLink() {
Chris@0 468 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
Chris@0 469 $theme_admin = $this->drupalCreateUser([
Chris@0 470 'administer blocks',
Chris@0 471 'administer themes',
Chris@0 472 'access administration pages',
Chris@0 473 ]);
Chris@0 474 $this->drupalLogin($theme_admin);
Chris@0 475 $this->drupalGet('admin/appearance');
Chris@0 476 $this->assertText('You can place blocks for each theme on the block layout page');
Chris@0 477 $this->assertLinkByHref('admin/structure/block');
Chris@0 478 }
Chris@0 479
Chris@0 480 /**
Chris@0 481 * Tests that uninstalling a theme removes its block configuration.
Chris@0 482 */
Chris@0 483 public function testUninstallTheme() {
Chris@0 484 /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
Chris@0 485 $theme_handler = \Drupal::service('theme_handler');
Chris@0 486
Chris@0 487 $theme_handler->install(['seven']);
Chris@0 488 $this->config('system.theme')->set('default', 'seven')->save();
Chris@0 489 $block = $this->drupalPlaceBlock('system_powered_by_block', ['theme' => 'seven', 'region' => 'help']);
Chris@0 490 $this->drupalGet('<front>');
Chris@0 491 $this->assertText('Powered by Drupal');
Chris@0 492
Chris@0 493 $this->config('system.theme')->set('default', 'classy')->save();
Chris@0 494 $theme_handler->uninstall(['seven']);
Chris@0 495
Chris@0 496 // Ensure that the block configuration does not exist anymore.
Chris@0 497 $this->assertIdentical(NULL, Block::load($block->id()));
Chris@0 498 }
Chris@0 499
Chris@0 500 /**
Chris@0 501 * Tests the block access.
Chris@0 502 */
Chris@0 503 public function testBlockAccess() {
Chris@0 504 $this->drupalPlaceBlock('test_access', ['region' => 'help']);
Chris@0 505
Chris@0 506 $this->drupalGet('<front>');
Chris@0 507 $this->assertNoText('Hello test world');
Chris@0 508
Chris@0 509 \Drupal::state()->set('test_block_access', TRUE);
Chris@0 510 $this->drupalGet('<front>');
Chris@0 511 $this->assertText('Hello test world');
Chris@0 512 }
Chris@0 513
Chris@0 514 /**
Chris@0 515 * Tests block_user_role_delete.
Chris@0 516 */
Chris@0 517 public function testBlockUserRoleDelete() {
Chris@0 518 $role1 = Role::create(['id' => 'test_role1', 'name' => $this->randomString()]);
Chris@0 519 $role1->save();
Chris@0 520
Chris@0 521 $role2 = Role::create(['id' => 'test_role2', 'name' => $this->randomString()]);
Chris@0 522 $role2->save();
Chris@0 523
Chris@0 524 $block = Block::create([
Chris@0 525 'id' => $this->randomMachineName(),
Chris@0 526 'plugin' => 'system_powered_by_block',
Chris@0 527 ]);
Chris@0 528
Chris@0 529 $block->setVisibilityConfig('user_role', [
Chris@0 530 'roles' => [
Chris@0 531 $role1->id() => $role1->id(),
Chris@0 532 $role2->id() => $role2->id(),
Chris@0 533 ],
Chris@0 534 ]);
Chris@0 535
Chris@0 536 $block->save();
Chris@0 537
Chris@0 538 $this->assertEqual($block->getVisibility()['user_role']['roles'], [
Chris@0 539 $role1->id() => $role1->id(),
Chris@0 540 $role2->id() => $role2->id()
Chris@0 541 ]);
Chris@0 542
Chris@0 543 $role1->delete();
Chris@0 544
Chris@0 545 $block = Block::load($block->id());
Chris@0 546 $this->assertEqual($block->getVisibility()['user_role']['roles'], [
Chris@0 547 $role2->id() => $role2->id()
Chris@0 548 ]);
Chris@0 549 }
Chris@0 550
Chris@0 551 }