Chris@0: randomMachineName(8);
Chris@0: // Enable a standard block.
Chris@0: $default_theme = $this->config('system.theme')->get('default');
Chris@0: $edit = [
Chris@0: 'id' => strtolower($this->randomMachineName(8)),
Chris@0: 'region' => 'sidebar_first',
Chris@0: 'settings[label]' => $title,
Chris@0: 'settings[label_display]' => TRUE,
Chris@0: ];
Chris@0: // Set the block to be hidden on any user path, and to be shown only to
Chris@0: // authenticated users.
Chris@0: $edit['visibility[request_path][pages]'] = '/user*';
Chris@0: $edit['visibility[request_path][negate]'] = TRUE;
Chris@0: $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
Chris@0: $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
Chris@0: $this->assertFieldChecked('edit-visibility-request-path-negate-0');
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save block'));
Chris@0: $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0:
Chris@0: $this->clickLink('Configure');
Chris@0: $this->assertFieldChecked('edit-visibility-request-path-negate-1');
Chris@0:
Chris@0: $this->drupalGet('');
Chris@0: $this->assertText($title, 'Block was displayed on the front page.');
Chris@0:
Chris@0: $this->drupalGet('user');
Chris@0: $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
Chris@0:
Chris@0: // Confirm that the block is not displayed to anonymous users.
Chris@0: $this->drupalLogout();
Chris@0: $this->drupalGet('');
Chris@0: $this->assertNoText($title, 'Block was not displayed to anonymous users.');
Chris@0:
Chris@0: // Confirm that an empty block is not displayed.
Chris@0: $this->assertNoText('Powered by Drupal', 'Empty block not displayed.');
Chris@0: $this->assertNoRaw('sidebar-first', 'Empty sidebar-first region is not displayed.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that visibility can be properly toggled.
Chris@0: */
Chris@0: public function testBlockToggleVisibility() {
Chris@0: $block_name = 'system_powered_by_block';
Chris@0: // Create a random title for the block.
Chris@0: $title = $this->randomMachineName(8);
Chris@0: // Enable a standard block.
Chris@0: $default_theme = $this->config('system.theme')->get('default');
Chris@0: $edit = [
Chris@0: 'id' => strtolower($this->randomMachineName(8)),
Chris@0: 'region' => 'sidebar_first',
Chris@0: 'settings[label]' => $title,
Chris@0: ];
Chris@0: $block_id = $edit['id'];
Chris@0: // Set the block to be shown only to authenticated users.
Chris@0: $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
Chris@0: $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0: $this->clickLink('Configure');
Chris@0: $this->assertFieldChecked('edit-visibility-user-role-roles-authenticated');
Chris@0:
Chris@0: $edit = [
Chris@0: 'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, 'Save block');
Chris@0: $this->clickLink('Configure');
Chris@0: $this->assertNoFieldChecked('edit-visibility-user-role-roles-authenticated');
Chris@0:
Chris@0: // Ensure that no visibility is configured.
Chris@0: /** @var \Drupal\block\BlockInterface $block */
Chris@0: $block = Block::load($block_id);
Chris@0: $visibility_config = $block->getVisibilityConditions()->getConfiguration();
Chris@0: $this->assertIdentical([], $visibility_config);
Chris@0: $this->assertIdentical([], $block->get('visibility'));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test block visibility when leaving "pages" textarea empty.
Chris@0: */
Chris@0: public function testBlockVisibilityListedEmpty() {
Chris@0: $block_name = 'system_powered_by_block';
Chris@0: // Create a random title for the block.
Chris@0: $title = $this->randomMachineName(8);
Chris@0: // Enable a standard block.
Chris@0: $default_theme = $this->config('system.theme')->get('default');
Chris@0: $edit = [
Chris@0: 'id' => strtolower($this->randomMachineName(8)),
Chris@0: 'region' => 'sidebar_first',
Chris@0: 'settings[label]' => $title,
Chris@0: 'visibility[request_path][negate]' => TRUE,
Chris@0: ];
Chris@0: // Set the block to be hidden on any user path, and to be shown only to
Chris@0: // authenticated users.
Chris@0: $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0: $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0:
Chris@0: $this->drupalGet('user');
Chris@0: $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
Chris@0:
Chris@0: $this->drupalGet('USER');
Chris@0: $this->assertNoText($title, 'Block was not displayed according to block visibility rules regardless of path case.');
Chris@0:
Chris@0: // Confirm that the block is not displayed to anonymous users.
Chris@0: $this->drupalLogout();
Chris@0: $this->drupalGet('');
Chris@0: $this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests adding a block from the library page with a weight query string.
Chris@0: */
Chris@0: public function testAddBlockFromLibraryWithWeight() {
Chris@0: $default_theme = $this->config('system.theme')->get('default');
Chris@0: // Test one positive, zero, and one negative weight.
Chris@0: foreach (['7', '0', '-9'] as $weight) {
Chris@0: $options = [
Chris@0: 'query' => [
Chris@0: 'region' => 'sidebar_first',
Chris@0: 'weight' => $weight,
Chris@0: ],
Chris@0: ];
Chris@0: $this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
Chris@0:
Chris@0: $block_name = 'system_powered_by_block';
Chris@0: $add_url = Url::fromRoute('block.admin_add', [
Chris@0: 'plugin_id' => $block_name,
Chris@17: 'theme' => $default_theme,
Chris@0: ]);
Chris@0: $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
Chris@0: $this->assertEqual(1, count($links), 'Found one matching link.');
Chris@0: $this->assertEqual(t('Place block'), $links[0]->getText(), 'Found the expected link text.');
Chris@0:
Chris@0: list($path, $query_string) = explode('?', $links[0]->getAttribute('href'), 2);
Chris@0: parse_str($query_string, $query_parts);
Chris@0: $this->assertEqual($weight, $query_parts['weight'], 'Found the expected weight query string.');
Chris@0:
Chris@0: // Create a random title for the block.
Chris@0: $title = $this->randomMachineName(8);
Chris@0: $block_id = strtolower($this->randomMachineName(8));
Chris@0: $edit = [
Chris@0: 'id' => $block_id,
Chris@0: 'settings[label]' => $title,
Chris@0: ];
Chris@0: // Create the block using the link parsed from the library page.
Chris@0: $this->drupalPostForm($this->getAbsoluteUrl($links[0]->getAttribute('href')), $edit, t('Save block'));
Chris@0:
Chris@0: // Ensure that the block was created with the expected weight.
Chris@0: /** @var \Drupal\block\BlockInterface $block */
Chris@0: $block = Block::load($block_id);
Chris@0: $this->assertEqual($weight, $block->getWeight(), 'Found the block with expected weight.');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test configuring and moving a module-define block to specific regions.
Chris@0: */
Chris@0: public function testBlock() {
Chris@0: // Place page title block to test error messages.
Chris@0: $this->drupalPlaceBlock('page_title_block');
Chris@0:
Chris@0: // Disable the block.
Chris@0: $this->drupalGet('admin/structure/block');
Chris@0: $this->clickLink('Disable');
Chris@0:
Chris@0: // Select the 'Powered by Drupal' block to be configured and moved.
Chris@0: $block = [];
Chris@0: $block['id'] = 'system_powered_by_block';
Chris@0: $block['settings[label]'] = $this->randomMachineName(8);
Chris@0: $block['settings[label_display]'] = TRUE;
Chris@0: $block['theme'] = $this->config('system.theme')->get('default');
Chris@0: $block['region'] = 'header';
Chris@0:
Chris@0: // Set block title to confirm that interface works and override any custom titles.
Chris@0: $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: $this->assertText(t('The block configuration has been saved.'), 'Block title set.');
Chris@0: // Check to see if the block was created by checking its configuration.
Chris@0: $instance = Block::load($block['id']);
Chris@0:
Chris@0: $this->assertEqual($instance->label(), $block['settings[label]'], 'Stored block title found.');
Chris@0:
Chris@0: // Check whether the block can be moved to all available regions.
Chris@0: foreach ($this->regions as $region) {
Chris@0: $this->moveBlockToRegion($block, $region);
Chris@0: }
Chris@0:
Chris@0: // Disable the block.
Chris@0: $this->drupalGet('admin/structure/block');
Chris@0: $this->clickLink('Disable');
Chris@0:
Chris@0: // Confirm that the block is now listed as disabled.
Chris@0: $this->assertText(t('The block settings have been updated.'), 'Block successfully moved to disabled region.');
Chris@0:
Chris@0: // Confirm that the block instance title and markup are not displayed.
Chris@0: $this->drupalGet('node');
Chris@0: $this->assertNoText(t($block['settings[label]']));
Chris@0: // Check for
if the machine name
Chris@0: // is my_block_instance_name.
Chris@0: $xpath = $this->buildXPathQuery('//div[@id=:id]/*', [':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))]);
Chris@0: $this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
Chris@0:
Chris@0: // Test deleting the block from the edit form.
Chris@0: $this->drupalGet('admin/structure/block/manage/' . $block['id']);
Chris@0: $this->clickLink(t('Remove block'));
Chris@0: $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block['settings[label]']]));
Chris@0: $this->drupalPostForm(NULL, [], t('Remove'));
Chris@0: $this->assertRaw(t('The block %name has been removed.', ['%name' => $block['settings[label]']]));
Chris@0:
Chris@0: // Test deleting a block via "Configure block" link.
Chris@0: $block = $this->drupalPlaceBlock('system_powered_by_block');
Chris@0: $this->drupalGet('admin/structure/block/manage/' . $block->id(), ['query' => ['destination' => 'admin']]);
Chris@0: $this->clickLink(t('Remove block'));
Chris@0: $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block->label()]));
Chris@0: $this->drupalPostForm(NULL, [], t('Remove'));
Chris@0: $this->assertRaw(t('The block %name has been removed.', ['%name' => $block->label()]));
Chris@0: $this->assertUrl('admin');
Chris@0: $this->assertNoRaw($block->id());
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that the block form has a theme selector when not passed via the URL.
Chris@0: */
Chris@0: public function testBlockThemeSelector() {
Chris@0: // Install all themes.
Chris@0: \Drupal::service('theme_handler')->install(['bartik', 'seven', 'stark']);
Chris@0: $theme_settings = $this->config('system.theme');
Chris@0: foreach (['bartik', 'seven', 'stark'] as $theme) {
Chris@0: $this->drupalGet('admin/structure/block/list/' . $theme);
Chris@0: $this->assertTitle(t('Block layout') . ' | Drupal');
Chris@0: // Select the 'Powered by Drupal' block to be placed.
Chris@0: $block = [];
Chris@0: $block['id'] = strtolower($this->randomMachineName());
Chris@0: $block['theme'] = $theme;
Chris@0: $block['region'] = 'content';
Chris@0: $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
Chris@0: $this->assertText(t('The block configuration has been saved.'));
Chris@0: $this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . Html::getClass($block['id']));
Chris@0:
Chris@0: // Set the default theme and ensure the block is placed.
Chris@0: $theme_settings->set('default', $theme)->save();
Chris@0: $this->drupalGet('');
Chris@0: $elements = $this->xpath('//div[@id = :id]', [':id' => Html::getUniqueId('block-' . $block['id'])]);
Chris@0: $this->assertTrue(!empty($elements), 'The block was found.');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test block display of theme titles.
Chris@0: */
Chris@0: public function testThemeName() {
Chris@0: // Enable the help block.
Chris@0: $this->drupalPlaceBlock('help_block', ['region' => 'help']);
Chris@0: $this->drupalPlaceBlock('local_tasks_block');
Chris@0: // Explicitly set the default and admin themes.
Chris@0: $theme = 'block_test_specialchars_theme';
Chris@0: \Drupal::service('theme_handler')->install([$theme]);
Chris@0: \Drupal::service('router.builder')->rebuild();
Chris@0: $this->drupalGet('admin/structure/block');
Chris@0: $this->assertEscaped('<"Cat" & \'Mouse\'>');
Chris@0: $this->drupalGet('admin/structure/block/list/block_test_specialchars_theme');
Chris@0: $this->assertEscaped('Demonstrate block regions (<"Cat" & \'Mouse\'>)');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test block title display settings.
Chris@0: */
Chris@0: public function testHideBlockTitle() {
Chris@0: $block_name = 'system_powered_by_block';
Chris@0: // Create a random title for the block.
Chris@0: $title = $this->randomMachineName(8);
Chris@0: $id = strtolower($this->randomMachineName(8));
Chris@0: // Enable a standard block.
Chris@0: $default_theme = $this->config('system.theme')->get('default');
Chris@0: $edit = [
Chris@0: 'id' => $id,
Chris@0: 'region' => 'sidebar_first',
Chris@0: 'settings[label]' => $title,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
Chris@0: $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0:
Chris@0: $this->drupalGet('user');
Chris@0: $this->assertNoText($title, 'Block title was not displayed by default.');
Chris@0:
Chris@0: $edit = [
Chris@0: 'settings[label_display]' => TRUE,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block'));
Chris@0: $this->assertText('The block configuration has been saved.', 'Block was saved');
Chris@0:
Chris@0: $this->drupalGet('admin/structure/block/manage/' . $id);
Chris@0: $this->assertFieldChecked('edit-settings-label-display', 'The display_block option has the correct default value on the configuration form.');
Chris@0:
Chris@0: $this->drupalGet('user');
Chris@0: $this->assertText($title, 'Block title was displayed when enabled.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Moves a block to a given region via the UI and confirms the result.
Chris@0: *
Chris@0: * @param array $block
Chris@0: * An array of information about the block, including the following keys:
Chris@0: * - module: The module providing the block.
Chris@0: * - title: The title of the block.
Chris@0: * - delta: The block's delta key.
Chris@0: * @param string $region
Chris@0: * The machine name of the theme region to move the block to, for example
Chris@0: * 'header' or 'sidebar_first'.
Chris@0: */
Chris@0: public function moveBlockToRegion(array $block, $region) {
Chris@0: // Set the created block to a specific region.
Chris@0: $block += ['theme' => $this->config('system.theme')->get('default')];
Chris@0: $edit = [];
Chris@0: $edit['blocks[' . $block['id'] . '][region]'] = $region;
Chris@0: $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
Chris@0:
Chris@0: // Confirm that the block was moved to the proper region.
Chris@0: $this->assertText(t('The block settings have been updated.'), format_string('Block successfully moved to %region_name region.', ['%region_name' => $region]));
Chris@0:
Chris@0: // Confirm that the block is being displayed.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertText(t($block['settings[label]']), 'Block successfully being displayed on the page.');
Chris@0:
Chris@0: // Confirm that the custom block was found at the proper region.
Chris@0: $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', [
Chris@0: ':region-class' => 'region region-' . Html::getClass($region),
Chris@0: ':block-id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
Chris@0: ]);
Chris@0: $this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', ['%region_name' => Html::getClass($region)]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test that cache tags are properly set and bubbled up to the page cache.
Chris@0: *
Chris@0: * Verify that invalidation of these cache tags works:
Chris@0: * - "block:"
Chris@0: * - "block_plugin:"
Chris@0: */
Chris@0: public function testBlockCacheTags() {
Chris@0: // The page cache only works for anonymous users.
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Enable page caching.
Chris@0: $config = $this->config('system.performance');
Chris@0: $config->set('cache.page.max_age', 300);
Chris@0: $config->save();
Chris@0:
Chris@0: // Place the "Powered by Drupal" block.
Chris@0: $block = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered']);
Chris@0:
Chris@0: // Prime the page cache.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0:
Chris@0: // Verify a cache hit, but also the presence of the correct cache tags in
Chris@0: // both the page and block caches.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@18: $cid_parts = [Url::fromRoute('', [], ['absolute' => TRUE])->toString(), ''];
Chris@0: $cid = implode(':', $cid_parts);
Chris@0: $cache_entry = \Drupal::cache('page')->get($cid);
Chris@0: $expected_cache_tags = [
Chris@0: 'config:block_list',
Chris@0: 'block_view',
Chris@0: 'config:block.block.powered',
Chris@0: 'config:user.role.anonymous',
Chris@0: 'http_response',
Chris@0: 'rendered',
Chris@0: ];
Chris@0: sort($expected_cache_tags);
Chris@0: $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0: $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0: $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
Chris@0: $expected_cache_tags = [
Chris@0: 'block_view',
Chris@0: 'config:block.block.powered',
Chris@0: 'rendered',
Chris@0: ];
Chris@0: sort($expected_cache_tags);
Chris@0: $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0:
Chris@0: // The "Powered by Drupal" block is modified; verify a cache miss.
Chris@0: $block->setRegion('content');
Chris@0: $block->save();
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0:
Chris@0: // Now we should have a cache hit again.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0:
Chris@0: // Place the "Powered by Drupal" block another time; verify a cache miss.
Chris@0: $block_2 = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered-2']);
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0:
Chris@0: // Verify a cache hit, but also the presence of the correct cache tags.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@18: $cid_parts = [Url::fromRoute('', [], ['absolute' => TRUE])->toString(), ''];
Chris@0: $cid = implode(':', $cid_parts);
Chris@0: $cache_entry = \Drupal::cache('page')->get($cid);
Chris@0: $expected_cache_tags = [
Chris@0: 'config:block_list',
Chris@0: 'block_view',
Chris@0: 'config:block.block.powered',
Chris@0: 'config:block.block.powered-2',
Chris@0: 'config:user.role.anonymous',
Chris@0: 'http_response',
Chris@0: 'rendered',
Chris@0: ];
Chris@0: sort($expected_cache_tags);
Chris@0: $this->assertEqual($cache_entry->tags, $expected_cache_tags);
Chris@0: $expected_cache_tags = [
Chris@0: 'block_view',
Chris@0: 'config:block.block.powered',
Chris@0: 'rendered',
Chris@0: ];
Chris@0: sort($expected_cache_tags);
Chris@0: $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0: $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
Chris@0: $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0: $expected_cache_tags = [
Chris@0: 'block_view',
Chris@0: 'config:block.block.powered-2',
Chris@0: 'rendered',
Chris@0: ];
Chris@0: sort($expected_cache_tags);
Chris@0: $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
Chris@0: $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered-2:' . implode(':', $keys));
Chris@0: $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
Chris@0:
Chris@0: // Now we should have a cache hit again.
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Chris@0:
Chris@0: // Delete the "Powered by Drupal" blocks; verify a cache miss.
Chris@0: entity_delete_multiple('block', ['powered', 'powered-2']);
Chris@0: $this->drupalGet('');
Chris@0: $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that a link exists to block layout from the appearance form.
Chris@0: */
Chris@0: public function testThemeAdminLink() {
Chris@0: $this->drupalPlaceBlock('help_block', ['region' => 'help']);
Chris@0: $theme_admin = $this->drupalCreateUser([
Chris@0: 'administer blocks',
Chris@0: 'administer themes',
Chris@0: 'access administration pages',
Chris@0: ]);
Chris@0: $this->drupalLogin($theme_admin);
Chris@0: $this->drupalGet('admin/appearance');
Chris@0: $this->assertText('You can place blocks for each theme on the block layout page');
Chris@0: $this->assertLinkByHref('admin/structure/block');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that uninstalling a theme removes its block configuration.
Chris@0: */
Chris@0: public function testUninstallTheme() {
Chris@0: /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
Chris@0: $theme_handler = \Drupal::service('theme_handler');
Chris@0:
Chris@0: $theme_handler->install(['seven']);
Chris@0: $this->config('system.theme')->set('default', 'seven')->save();
Chris@0: $block = $this->drupalPlaceBlock('system_powered_by_block', ['theme' => 'seven', 'region' => 'help']);
Chris@0: $this->drupalGet('');
Chris@0: $this->assertText('Powered by Drupal');
Chris@0:
Chris@0: $this->config('system.theme')->set('default', 'classy')->save();
Chris@0: $theme_handler->uninstall(['seven']);
Chris@0:
Chris@0: // Ensure that the block configuration does not exist anymore.
Chris@0: $this->assertIdentical(NULL, Block::load($block->id()));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests the block access.
Chris@0: */
Chris@0: public function testBlockAccess() {
Chris@0: $this->drupalPlaceBlock('test_access', ['region' => 'help']);
Chris@0:
Chris@0: $this->drupalGet('');
Chris@0: $this->assertNoText('Hello test world');
Chris@0:
Chris@0: \Drupal::state()->set('test_block_access', TRUE);
Chris@0: $this->drupalGet('');
Chris@0: $this->assertText('Hello test world');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests block_user_role_delete.
Chris@0: */
Chris@0: public function testBlockUserRoleDelete() {
Chris@0: $role1 = Role::create(['id' => 'test_role1', 'name' => $this->randomString()]);
Chris@0: $role1->save();
Chris@0:
Chris@0: $role2 = Role::create(['id' => 'test_role2', 'name' => $this->randomString()]);
Chris@0: $role2->save();
Chris@0:
Chris@0: $block = Block::create([
Chris@0: 'id' => $this->randomMachineName(),
Chris@0: 'plugin' => 'system_powered_by_block',
Chris@0: ]);
Chris@0:
Chris@0: $block->setVisibilityConfig('user_role', [
Chris@0: 'roles' => [
Chris@0: $role1->id() => $role1->id(),
Chris@0: $role2->id() => $role2->id(),
Chris@0: ],
Chris@0: ]);
Chris@0:
Chris@0: $block->save();
Chris@0:
Chris@0: $this->assertEqual($block->getVisibility()['user_role']['roles'], [
Chris@0: $role1->id() => $role1->id(),
Chris@17: $role2->id() => $role2->id(),
Chris@0: ]);
Chris@0:
Chris@0: $role1->delete();
Chris@0:
Chris@0: $block = Block::load($block->id());
Chris@0: $this->assertEqual($block->getVisibility()['user_role']['roles'], [
Chris@17: $role2->id() => $role2->id(),
Chris@0: ]);
Chris@0: }
Chris@0:
Chris@0: }