class NewDefaultThemeBlocksTest
Same name and namespace in other branches
- 11.x core/modules/block/tests/src/Kernel/NewDefaultThemeBlocksTest.php \Drupal\Tests\block\Kernel\NewDefaultThemeBlocksTest
- 10 core/modules/block/tests/src/Kernel/NewDefaultThemeBlocksTest.php \Drupal\Tests\block\Kernel\NewDefaultThemeBlocksTest
- 8.9.x core/modules/block/tests/src/Kernel/NewDefaultThemeBlocksTest.php \Drupal\Tests\block\Kernel\NewDefaultThemeBlocksTest
Tests that a new default theme gets blocks.
@group block
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\block\Kernel\NewDefaultThemeBlocksTest uses \Drupal\Tests\block\Traits\BlockCreationTrait extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of NewDefaultThemeBlocksTest
File
-
core/
modules/ block/ tests/ src/ Kernel/ NewDefaultThemeBlocksTest.php, line 13
Namespace
Drupal\Tests\block\KernelView source
class NewDefaultThemeBlocksTest extends KernelTestBase {
use BlockCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'system',
];
/**
* Check the blocks are correctly copied by block_themes_installed().
*/
public function testNewDefaultThemeBlocks() {
$this->installConfig([
'system',
]);
/** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
$theme_installer = $this->container
->get('theme_installer');
$default_theme = $this->config('system.theme')
->get('default');
// Add two instances of the user login block.
$this->placeBlock('user_login_block', [
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
]);
$this->placeBlock('user_login_block', [
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
]);
// Add an instance of a different block.
$this->placeBlock('system_powered_by_block', [
'id' => $default_theme . '_' . strtolower($this->randomMachineName(8)),
]);
// Install a different theme that does not have blocks.
$new_theme = 'test_theme';
// The new theme is different from the previous default theme.
$this->assertNotEquals($new_theme, $default_theme);
$theme_installer->install([
$new_theme,
]);
$this->config('system.theme')
->set('default', $new_theme)
->save();
$block_storage = $this->container
->get('entity_type.manager')
->getStorage('block');
// Ensure that the new default theme has the same blocks as the previous
// default theme.
$default_block_names = $block_storage->getQuery()
->condition('theme', $default_theme)
->execute();
$new_blocks = $block_storage->getQuery()
->condition('theme', $new_theme)
->execute();
$this->assertSameSize($default_block_names, $new_blocks);
foreach ($default_block_names as $default_block_name) {
// Remove the matching block from the list of blocks in the new theme.
// E.g., if the old theme has block.block.stark_admin,
// unset block.block.olivero_admin.
unset($new_blocks[str_replace($default_theme . '_', $new_theme . '_', $default_block_name)]);
}
$this->assertEmpty($new_blocks);
// Install a hidden base theme and ensure blocks are not copied.
$base_theme = 'test_basetheme';
$theme_installer->install([
$base_theme,
]);
$new_blocks = $block_storage->getQuery()
->condition('theme', $base_theme)
->execute();
// Installing a hidden base theme does not copy the blocks from the default
// theme.
$this->assertEmpty($new_blocks);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.