View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Database\Database;
class BlockContentCreationTest extends BlockContentTestBase {
public static $modules = [
'block_content_test',
'dblog',
'field_ui',
];
protected $defaultTheme = 'classy';
protected $permissions = [
'administer blocks',
'administer block_content display',
];
protected function setUp() {
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
->drupalPostForm('block/add/basic', $edit, t('Save'));
$this
->assertRaw(new FormattableMarkup('@block %name has been created.', [
'@block' => 'basic',
'%name' => $edit['info[0][value]'],
]), 'Basic block created.');
$this
->assertNoFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting hidden because only one exists');
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties([
'info' => $edit['info[0][value]'],
]);
$block = reset($blocks);
$this
->assertNotEmpty($block, 'Custom Block found in database.');
$this
->drupalPostForm('block/add/basic', $edit, t('Save'));
$this
->assertRaw(new FormattableMarkup('A custom block with block description %value already exists.', [
'%value' => $edit['info[0][value]'],
]));
$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
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertRaw(t('Saved the %label view mode.', [
'%label' => $edit['label'],
]));
$this
->drupalLogin($this->adminUser);
$edit = [];
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('block/add/basic', $edit, t('Save'));
$this
->assertRaw(new FormattableMarkup('@block %name has been created.', [
'@block' => 'basic',
'%name' => $edit['info[0][value]'],
]), 'Basic block created.');
$this
->drupalPostForm(NULL, [
'region' => 'content',
], t('Save block'));
$this
->drupalGet('admin/structure/block/block-content');
$this
->drupalGet('admin/structure/block/block-content/types');
$this
->clickLink(t('Manage display'));
$this
->drupalGet('admin/structure/block/block-content/manage/basic/display');
$custom_view_mode = [
'display_modes_custom[test_view_mode]' => 1,
];
$this
->drupalPostForm(NULL, $custom_view_mode, t('Save'));
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
$this
->assertOption('edit-settings-view-mode', 'test_view_mode', 'The test view mode is available.');
$view_mode['settings[view_mode]'] = 'test_view_mode';
$this
->drupalPostForm(NULL, $view_mode, t('Save block'));
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist');
$view_mode['region'] = 'content';
$view_mode['settings[view_mode]'] = 'test_view_mode';
$this
->drupalPostForm(NULL, $view_mode, t('Save block'));
$this
->drupalGet('admin/structure/block/manage/testblock');
$this
->assertFieldByXPath('//select[@name="settings[view_mode]"]/option[@selected="selected"]', 'test_view_mode', 'View mode changed to 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
->drupalPostForm('block/add/basic', $edit, t('Save'));
$this
->assertRaw(new FormattableMarkup('A custom block with block description %value already exists.', [
'%value' => $edit['info[0][value]'],
]));
$this
->assertSession()
->statusCodeEquals(200);
}
public function testDefaultBlockContentCreation() {
$edit = [];
$edit['info[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('block/add', $edit, t('Save'));
$this
->assertRaw(new FormattableMarkup('@block %name has been created.', [
'@block' => 'basic',
'%name' => $edit['info[0][value]'],
]), 'Basic block 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();
if ($connection
->supportsTransactions()) {
$id = $connection
->select('block_content_field_data', 'b')
->fields('b', [
'id',
])
->condition('info', 'fail_creation')
->execute()
->fetchField();
$this
->assertFalse($id, 'Transactions supported, and block not found in database.');
}
else {
$id = $connection
->select('block_content_field_data', 'b')
->fields('b', [
'id',
])
->condition('info', 'fail_creation')
->execute()
->fetchField();
$this
->assertTrue($id, 'Transactions not supported, and block found in database.');
$records = $connection
->query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")
->fetchAll();
$this
->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
}
}
public function testBlockDelete() {
$edit = [];
$edit['info[0][value]'] = $this
->randomMachineName(8);
$body = $this
->randomMachineName(16);
$edit['body[0][value]'] = $body;
$this
->drupalPostForm('block/add/basic', $edit, t('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
->drupalPostForm($url, $instance, t('Save block'));
$block = BlockContent::load(1);
$this
->assertCount(1, $block
->getInstances());
$this
->drupalGet('');
$this
->assertText($body);
$this
->drupalGet('block/1/delete');
$this
->assertText(\Drupal::translation()
->formatPlural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw(t('The custom block %name has been deleted.', [
'%name' => $edit['info[0][value]'],
]));
$edit2 = [];
$edit2['info[0][value]'] = $this
->randomMachineName(8);
$body2 = $this
->randomMachineName(16);
$edit2['body[0][value]'] = $body2;
$this
->drupalPostForm('block/add/basic', $edit2, t('Save'));
$this
->assertNoRaw('Error message');
$edit3 = [];
$edit3['info[0][value]'] = $this
->randomMachineName(8);
$body = $this
->randomMachineName(16);
$edit3['body[0][value]'] = $body;
$this
->drupalPostForm('block/add/basic', $edit3, t('Save'));
$this
->drupalGet('block/3/delete');
$this
->assertNoText('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
->drupalPostForm($url, $instance, t('Save block'));
$dependencies = \Drupal::service('config.manager')
->findConfigEntityDependentsAsEntities('content', [
$block
->getConfigDependencyName(),
]);
$block_placement = reset($dependencies);
$this
->assertEqual($block_placement_id, $block_placement
->id(), "The block placement config entity has a dependency on the block content entity.");
}
}