3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Database\Database;
10 * Create a block and test saving it.
12 * @group block_content
14 class BlockContentCreationTest extends BlockContentTestBase {
19 * Enable dummy module that implements hook_block_insert() for exceptions and
20 * field_ui to edit display settings.
24 public static $modules = ['block_content_test', 'dblog', 'field_ui'];
27 * Permissions to grant admin user.
31 protected $permissions = [
33 'administer block_content display'
39 protected function setUp() {
41 $this->drupalLogin($this->adminUser);
45 * Creates a "Basic page" block and verifies its consistency in the database.
47 public function testBlockContentCreation() {
48 $this->drupalLogin($this->adminUser);
52 $edit['info[0][value]'] = 'Test Block';
53 $edit['body[0][value]'] = $this->randomMachineName(16);
54 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
56 // Check that the Basic block has been created.
57 $this->assertRaw(format_string('@block %name has been created.', [
59 '%name' => $edit['info[0][value]']
60 ]), 'Basic block created.');
62 // Check that the view mode setting is hidden because only one exists.
63 $this->assertNoFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting hidden because only one exists');
65 // Check that the block exists in the database.
66 $blocks = \Drupal::entityTypeManager()
67 ->getStorage('block_content')
68 ->loadByProperties(['info' => $edit['info[0][value]']]);
69 $block = reset($blocks);
70 $this->assertTrue($block, 'Custom Block found in database.');
72 // Check that attempting to create another block with the same value for
73 // 'info' returns an error.
74 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
76 // Check that the Basic block has been created.
77 $this->assertRaw(format_string('A custom block with block description %value already exists.', [
78 '%value' => $edit['info[0][value]']
80 $this->assertResponse(200);
84 * Creates a "Basic page" block with multiple view modes.
86 public function testBlockContentCreationMultipleViewModes() {
87 // Add a new view mode and verify if it is selected as expected.
88 $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
89 $this->drupalGet('admin/structure/display-modes/view/add/block_content');
91 'id' => 'test_view_mode',
92 'label' => 'Test View Mode',
94 $this->drupalPostForm(NULL, $edit, t('Save'));
95 $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
97 $this->drupalLogin($this->adminUser);
101 $edit['info[0][value]'] = 'Test Block';
102 $edit['body[0][value]'] = $this->randomMachineName(16);
103 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
105 // Check that the Basic block has been created.
106 $this->assertRaw(format_string('@block %name has been created.', [
108 '%name' => $edit['info[0][value]']
109 ]), 'Basic block created.');
111 // Save our block permanently
112 $this->drupalPostForm(NULL, ['region' => 'content'], t('Save block'));
114 // Set test_view_mode as a custom display to be available on the list.
115 $this->drupalGet('admin/structure/block/block-content');
116 $this->drupalGet('admin/structure/block/block-content/types');
117 $this->clickLink(t('Manage display'));
118 $this->drupalGet('admin/structure/block/block-content/manage/basic/display');
119 $custom_view_mode = [
120 'display_modes_custom[test_view_mode]' => 1,
122 $this->drupalPostForm(NULL, $custom_view_mode, t('Save'));
124 // Go to the configure page and change the view mode.
125 $this->drupalGet('admin/structure/block/manage/testblock');
127 // Test the available view mode options.
128 $this->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
129 $this->assertOption('edit-settings-view-mode', 'test_view_mode', 'The test view mode is available.');
131 $view_mode['settings[view_mode]'] = 'test_view_mode';
132 $this->drupalPostForm(NULL, $view_mode, t('Save block'));
134 // Check that the view mode setting is shown because more than one exists.
135 $this->drupalGet('admin/structure/block/manage/testblock');
136 $this->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist');
138 // Change the view mode.
139 $view_mode['region'] = 'content';
140 $view_mode['settings[view_mode]'] = 'test_view_mode';
141 $this->drupalPostForm(NULL, $view_mode, t('Save block'));
143 // Go to the configure page and verify the view mode has changed.
144 $this->drupalGet('admin/structure/block/manage/testblock');
145 $this->assertFieldByXPath('//select[@name="settings[view_mode]"]/option[@selected="selected"]', 'test_view_mode', 'View mode changed to Test View Mode');
147 // Check that the block exists in the database.
148 $blocks = \Drupal::entityTypeManager()
149 ->getStorage('block_content')
150 ->loadByProperties(['info' => $edit['info[0][value]']]);
151 $block = reset($blocks);
152 $this->assertTrue($block, 'Custom Block found in database.');
154 // Check that attempting to create another block with the same value for
155 // 'info' returns an error.
156 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
158 // Check that the Basic block has been created.
159 $this->assertRaw(format_string('A custom block with block description %value already exists.', [
160 '%value' => $edit['info[0][value]']
162 $this->assertResponse(200);
166 * Create a default custom block.
168 * Creates a custom block from defaults and ensures that the 'basic block'
169 * type is being used.
171 public function testDefaultBlockContentCreation() {
173 $edit['info[0][value]'] = $this->randomMachineName(8);
174 $edit['body[0][value]'] = $this->randomMachineName(16);
175 // Don't pass the custom block type in the url so the default is forced.
176 $this->drupalPostForm('block/add', $edit, t('Save'));
178 // Check that the block has been created and that it is a basic block.
179 $this->assertRaw(format_string('@block %name has been created.', [
181 '%name' => $edit['info[0][value]'],
182 ]), 'Basic block created.');
184 // Check that the block exists in the database.
185 $blocks = \Drupal::entityTypeManager()
186 ->getStorage('block_content')
187 ->loadByProperties(['info' => $edit['info[0][value]']]);
188 $block = reset($blocks);
189 $this->assertTrue($block, 'Default Custom Block found in database.');
193 * Verifies that a transaction rolls back the failed creation.
195 public function testFailedBlockCreation() {
198 $this->createBlockContent('fail_creation');
199 $this->fail('Expected exception has not been thrown.');
201 catch (\Exception $e) {
202 $this->pass('Expected exception has been thrown.');
205 if (Database::getConnection()->supportsTransactions()) {
206 // Check that the block does not exist in the database.
207 $id = db_select('block_content_field_data', 'b')
208 ->fields('b', ['id'])
209 ->condition('info', 'fail_creation')
212 $this->assertFalse($id, 'Transactions supported, and block not found in database.');
215 // Check that the block exists in the database.
216 $id = db_select('block_content_field_data', 'b')
217 ->fields('b', ['id'])
218 ->condition('info', 'fail_creation')
221 $this->assertTrue($id, 'Transactions not supported, and block found in database.');
223 // Check that the failed rollback was logged.
224 $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
225 $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
230 * Test deleting a block.
232 public function testBlockDelete() {
235 $edit['info[0][value]'] = $this->randomMachineName(8);
236 $body = $this->randomMachineName(16);
237 $edit['body[0][value]'] = $body;
238 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
242 'id' => Unicode::strtolower($edit['info[0][value]']),
243 'settings[label]' => $edit['info[0][value]'],
244 'region' => 'sidebar_first',
246 $block = BlockContent::load(1);
247 $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
248 $this->drupalPostForm($url, $instance, t('Save block'));
250 $block = BlockContent::load(1);
252 // Test getInstances method.
253 $this->assertEqual(1, count($block->getInstances()));
255 // Navigate to home page.
256 $this->drupalGet('');
257 $this->assertText($body);
260 $this->drupalGet('block/1/delete');
261 $this->assertText(\Drupal::translation()->formatPlural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
263 $this->drupalPostForm(NULL, [], 'Delete');
264 $this->assertRaw(t('The custom block %name has been deleted.', ['%name' => $edit['info[0][value]']]));
266 // Create another block and force the plugin cache to flush.
268 $edit2['info[0][value]'] = $this->randomMachineName(8);
269 $body2 = $this->randomMachineName(16);
270 $edit2['body[0][value]'] = $body2;
271 $this->drupalPostForm('block/add/basic', $edit2, t('Save'));
273 $this->assertNoRaw('Error message');
275 // Create another block with no instances, and test we don't get a
276 // confirmation message about deleting instances.
278 $edit3['info[0][value]'] = $this->randomMachineName(8);
279 $body = $this->randomMachineName(16);
280 $edit3['body[0][value]'] = $body;
281 $this->drupalPostForm('block/add/basic', $edit3, t('Save'));
283 // Show the delete confirm form.
284 $this->drupalGet('block/3/delete');
285 $this->assertNoText('This will also remove');
289 * Test that placed content blocks create a dependency in the block placement.
291 public function testConfigDependencies() {
292 $block = $this->createBlockContent();
294 $block_placement_id = Unicode::strtolower($block->label());
296 'id' => $block_placement_id,
297 'settings[label]' => $block->label(),
298 'region' => 'sidebar_first',
300 $block = BlockContent::load(1);
301 $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
302 $this->drupalPostForm($url, $instance, t('Save block'));
304 $dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('content', [$block->getConfigDependencyName()]);
305 $block_placement = reset($dependencies);
306 $this->assertEqual($block_placement_id, $block_placement->id(), "The block placement config entity has a dependency on the block content entity.");