View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Database\Database;
class BlockContentCreationTest extends BlockContentTestBase {
protected static $modules = [
'block_content_test',
'dblog',
'field_ui',
];
protected $defaultTheme = 'classy';
protected $permissions = [
'administer blocks',
'administer block_content display',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->adminUser);
}
public function testBlockContentCreation() {
$this
->drupalLogin($this->adminUser);
$edit = [];
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$this
->assertSession()
->fieldNotExists('settings[view_mode]');
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties([
'info' => $edit['info[0][value]'],
]);
$block = reset($blocks);
$this
->assertNotEmpty($block, 'Custom Block found in database.');
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('A custom block with block description ' . $edit['info[0][value]'] . ' already exists.');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testBlockContentCreationMultipleViewModes() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer display modes',
]));
$this
->drupalGet('admin/structure/display-modes/view/add/block_content');
$edit = [
'id' => 'test_view_mode',
'label' => 'Test View Mode',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Saved the ' . $edit['label'] . ' view mode.');
$this
->drupalLogin($this->adminUser);
$edit = [];
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$this
->submitForm([
'region' => 'content',
], 'Save block');
$this
->drupalGet('admin/structure/block/block-content');
$this
->drupalGet('admin/structure/block/block-content/types');
$this
->clickLink('Manage display');
$this
->drupalGet('admin/structure/block/block-content/manage/basic/display');
$custom_view_mode = [
'display_modes_custom[test_view_mode]' => 1,
];
$this
->submitForm($custom_view_mode, 'Save');
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertSession()
->optionExists('edit-settings-view-mode', 'default');
$this
->assertSession()
->optionExists('edit-settings-view-mode', 'test_view_mode');
$view_mode['settings[view_mode]'] = 'test_view_mode';
$this
->submitForm($view_mode, 'Save block');
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertSession()
->fieldExists('settings[view_mode]');
$view_mode['region'] = 'content';
$view_mode['settings[view_mode]'] = 'test_view_mode';
$this
->submitForm($view_mode, 'Save block');
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertSession()
->fieldValueEquals('settings[view_mode]', 'test_view_mode');
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties([
'info' => $edit['info[0][value]'],
]);
$block = reset($blocks);
$this
->assertNotEmpty($block, 'Custom Block found in database.');
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('A custom block with block description ' . $edit['info[0][value]'] . ' already exists.');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testDefaultBlockContentCreation() {
$edit = [];
$edit['info[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalGet('block/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties([
'info' => $edit['info[0][value]'],
]);
$block = reset($blocks);
$this
->assertNotEmpty($block, 'Default Custom Block found in database.');
}
public function testFailedBlockCreation() {
try {
$this
->createBlockContent('fail_creation');
$this
->fail('Expected exception has not been thrown.');
} catch (\Exception $e) {
}
$connection = Database::getConnection();
$id = $connection
->select('block_content_field_data', 'b')
->fields('b', [
'id',
])
->condition('info', 'fail_creation')
->execute()
->fetchField();
$this
->assertFalse($id);
}
public function testBlockDelete() {
$edit = [];
$edit['info[0][value]'] = $this
->randomMachineName(8);
$body = $this
->randomMachineName(16);
$edit['body[0][value]'] = $body;
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit, 'Save');
$instance = [
'id' => mb_strtolower($edit['info[0][value]']),
'settings[label]' => $edit['info[0][value]'],
'region' => 'sidebar_first',
];
$block = BlockContent::load(1);
$url = 'admin/structure/block/add/block_content:' . $block
->uuid() . '/' . $this
->config('system.theme')
->get('default');
$this
->drupalGet($url);
$this
->submitForm($instance, 'Save block');
$block = BlockContent::load(1);
$this
->assertCount(1, $block
->getInstances());
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($body);
$this
->drupalGet('block/1/delete');
$this
->assertSession()
->pageTextContains('This will also remove 1 placed block instance.');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The custom block ' . $edit['info[0][value]'] . ' has been deleted.');
$edit2 = [];
$edit2['info[0][value]'] = $this
->randomMachineName(8);
$body2 = $this
->randomMachineName(16);
$edit2['body[0][value]'] = $body2;
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit2, 'Save');
$this
->assertSession()
->responseNotContains('Error message');
$edit3 = [];
$edit3['info[0][value]'] = $this
->randomMachineName(8);
$body = $this
->randomMachineName(16);
$edit3['body[0][value]'] = $body;
$this
->drupalGet('block/add/basic');
$this
->submitForm($edit3, 'Save');
$this
->drupalGet('block/3/delete');
$this
->assertSession()
->pageTextNotContains('This will also remove');
}
public function testConfigDependencies() {
$block = $this
->createBlockContent();
$block_placement_id = mb_strtolower($block
->label());
$instance = [
'id' => $block_placement_id,
'settings[label]' => $block
->label(),
'region' => 'sidebar_first',
];
$block = BlockContent::load(1);
$url = 'admin/structure/block/add/block_content:' . $block
->uuid() . '/' . $this
->config('system.theme')
->get('default');
$this
->drupalGet($url);
$this
->submitForm($instance, 'Save block');
$dependencies = \Drupal::service('config.manager')
->findConfigEntityDependenciesAsEntities('content', [
$block
->getConfigDependencyName(),
]);
$block_placement = reset($dependencies);
$this
->assertEquals($block_placement_id, $block_placement
->id(), "The block placement config entity has a dependency on the block content entity.");
}
}