function BlockContentTypeTest::testsBlockContentAddTypes
Same name in other branches
- 9 core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()
- 10 core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()
- 11.x core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()
Tests that redirects work as expected when multiple block types exist.
File
-
core/
modules/ block_content/ tests/ src/ Functional/ BlockContentTypeTest.php, line 184
Class
- BlockContentTypeTest
- Ensures that custom block type functions work correctly.
Namespace
Drupal\Tests\block_content\FunctionalCode
public function testsBlockContentAddTypes() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
$this->drupalLogin($this->adminUser);
// Create two block types programmatically.
$type = $this->createBlockContentType('foo');
$type = $this->createBlockContentType('bar');
// Get the custom block storage.
$storage = $this->container
->get('entity_type.manager')
->getStorage('block_content');
// Install all themes.
\Drupal::service('theme_installer')->install([
'bartik',
'seven',
'stark',
]);
$theme_settings = $this->config('system.theme');
foreach ([
'bartik',
'seven',
'stark',
] as $default_theme) {
// Change the default theme.
$theme_settings->set('default', $default_theme)
->save();
$this->drupalPlaceBlock('local_actions_block');
\Drupal::service('router.builder')->rebuild();
// For each installed theme, go to its block page and test the redirects.
foreach ([
'bartik',
'seven',
'stark',
] as $theme) {
// Test that adding a block from the 'place blocks' form sends you to the
// block configure form.
$path = $theme == $default_theme ? 'admin/structure/block' : "admin/structure/block/list/{$theme}";
$this->drupalGet($path);
$this->clickLink('Place block');
$this->clickLink(t('Add custom block'));
// The seven theme has markup inside the link, we cannot use clickLink().
if ($default_theme == 'seven') {
$options = $theme != $default_theme ? [
'query' => [
'theme' => $theme,
],
] : [];
$this->assertLinkByHref(Url::fromRoute('block_content.add_form', [
'block_content_type' => 'foo',
], $options)->toString());
$this->drupalGet('block/add/foo', $options);
}
else {
$this->clickLink('foo');
}
// Create a new block.
$edit = [
'info[0][value]' => $this->randomMachineName(8),
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage->loadByProperties([
'info' => $edit['info[0][value]'],
]);
if (!empty($blocks)) {
$block = reset($blocks);
$this->assertUrl(Url::fromRoute('block.admin_add', [
'plugin_id' => 'block_content:' . $block->uuid(),
'theme' => $theme,
], [
'absolute' => TRUE,
])
->toString());
$this->drupalPostForm(NULL, [
'region' => 'content',
], t('Save block'));
$this->assertUrl(Url::fromRoute('block.admin_display_theme', [
'theme' => $theme,
], [
'absolute' => TRUE,
'query' => [
'block-placement' => Html::getClass($edit['info[0][value]']),
],
])->toString());
}
else {
$this->fail('Could not load created block.');
}
}
}
// Test that adding a block from the 'custom blocks list' doesn't send you
// to the block configure form.
$this->drupalGet('admin/structure/block/block-content');
$this->clickLink(t('Add custom block'));
$this->clickLink('foo');
$edit = [
'info[0][value]' => $this->randomMachineName(8),
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage->loadByProperties([
'info' => $edit['info[0][value]'],
]);
if (!empty($blocks)) {
$this->assertUrl(Url::fromRoute('entity.block_content.collection', [], [
'absolute' => TRUE,
])->toString());
}
else {
$this->fail('Could not load created block.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.