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 = entity_load_multiple_by_properties('block_content', ['info' => $edit['info[0][value]']]);
67 $block = reset($blocks);
68 $this->assertTrue($block, 'Custom Block found in database.');
70 // Check that attempting to create another block with the same value for
71 // 'info' returns an error.
72 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
74 // Check that the Basic block has been created.
75 $this->assertRaw(format_string('A custom block with block description %value already exists.', [
76 '%value' => $edit['info[0][value]']
78 $this->assertResponse(200);
82 * Creates a "Basic page" block with multiple view modes.
84 public function testBlockContentCreationMultipleViewModes() {
85 // Add a new view mode and verify if it is selected as expected.
86 $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
87 $this->drupalGet('admin/structure/display-modes/view/add/block_content');
89 'id' => 'test_view_mode',
90 'label' => 'Test View Mode',
92 $this->drupalPostForm(NULL, $edit, t('Save'));
93 $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
95 $this->drupalLogin($this->adminUser);
99 $edit['info[0][value]'] = 'Test Block';
100 $edit['body[0][value]'] = $this->randomMachineName(16);
101 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
103 // Check that the Basic block has been created.
104 $this->assertRaw(format_string('@block %name has been created.', [
106 '%name' => $edit['info[0][value]']
107 ]), 'Basic block created.');
109 // Save our block permanently
110 $this->drupalPostForm(NULL, ['region' => 'content'], t('Save block'));
112 // Set test_view_mode as a custom display to be available on the list.
113 $this->drupalGet('admin/structure/block/block-content');
114 $this->drupalGet('admin/structure/block/block-content/types');
115 $this->clickLink(t('Manage display'));
116 $this->drupalGet('admin/structure/block/block-content/manage/basic/display');
117 $custom_view_mode = [
118 'display_modes_custom[test_view_mode]' => 1,
120 $this->drupalPostForm(NULL, $custom_view_mode, t('Save'));
122 // Go to the configure page and change the view mode.
123 $this->drupalGet('admin/structure/block/manage/testblock');
125 // Test the available view mode options.
126 $this->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
127 $this->assertOption('edit-settings-view-mode', 'test_view_mode', 'The test view mode is available.');
129 $view_mode['settings[view_mode]'] = 'test_view_mode';
130 $this->drupalPostForm(NULL, $view_mode, t('Save block'));
132 // Check that the view mode setting is shown because more than one exists.
133 $this->drupalGet('admin/structure/block/manage/testblock');
134 $this->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist');
136 // Change the view mode.
137 $view_mode['region'] = 'content';
138 $view_mode['settings[view_mode]'] = 'test_view_mode';
139 $this->drupalPostForm(NULL, $view_mode, t('Save block'));
141 // Go to the configure page and verify the view mode has changed.
142 $this->drupalGet('admin/structure/block/manage/testblock');
143 $this->assertFieldByXPath('//select[@name="settings[view_mode]"]/option[@selected="selected"]', 'test_view_mode', 'View mode changed to Test View Mode');
145 // Check that the block exists in the database.
146 $blocks = entity_load_multiple_by_properties('block_content', ['info' => $edit['info[0][value]']]);
147 $block = reset($blocks);
148 $this->assertTrue($block, 'Custom Block found in database.');
150 // Check that attempting to create another block with the same value for
151 // 'info' returns an error.
152 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
154 // Check that the Basic block has been created.
155 $this->assertRaw(format_string('A custom block with block description %value already exists.', [
156 '%value' => $edit['info[0][value]']
158 $this->assertResponse(200);
162 * Create a default custom block.
164 * Creates a custom block from defaults and ensures that the 'basic block'
165 * type is being used.
167 public function testDefaultBlockContentCreation() {
169 $edit['info[0][value]'] = $this->randomMachineName(8);
170 $edit['body[0][value]'] = $this->randomMachineName(16);
171 // Don't pass the custom block type in the url so the default is forced.
172 $this->drupalPostForm('block/add', $edit, t('Save'));
174 // Check that the block has been created and that it is a basic block.
175 $this->assertRaw(format_string('@block %name has been created.', [
177 '%name' => $edit['info[0][value]'],
178 ]), 'Basic block created.');
180 // Check that the block exists in the database.
181 $blocks = entity_load_multiple_by_properties('block_content', ['info' => $edit['info[0][value]']]);
182 $block = reset($blocks);
183 $this->assertTrue($block, 'Default Custom Block found in database.');
187 * Verifies that a transaction rolls back the failed creation.
189 public function testFailedBlockCreation() {
192 $this->createBlockContent('fail_creation');
193 $this->fail('Expected exception has not been thrown.');
195 catch (\Exception $e) {
196 $this->pass('Expected exception has been thrown.');
199 if (Database::getConnection()->supportsTransactions()) {
200 // Check that the block does not exist in the database.
201 $id = db_select('block_content_field_data', 'b')
202 ->fields('b', ['id'])
203 ->condition('info', 'fail_creation')
206 $this->assertFalse($id, 'Transactions supported, and block not found in database.');
209 // Check that the block exists in the database.
210 $id = db_select('block_content_field_data', 'b')
211 ->fields('b', ['id'])
212 ->condition('info', 'fail_creation')
215 $this->assertTrue($id, 'Transactions not supported, and block found in database.');
217 // Check that the failed rollback was logged.
218 $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
219 $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
224 * Test deleting a block.
226 public function testBlockDelete() {
229 $edit['info[0][value]'] = $this->randomMachineName(8);
230 $body = $this->randomMachineName(16);
231 $edit['body[0][value]'] = $body;
232 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
236 'id' => Unicode::strtolower($edit['info[0][value]']),
237 'settings[label]' => $edit['info[0][value]'],
238 'region' => 'sidebar_first',
240 $block = BlockContent::load(1);
241 $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
242 $this->drupalPostForm($url, $instance, t('Save block'));
244 $block = BlockContent::load(1);
246 // Test getInstances method.
247 $this->assertEqual(1, count($block->getInstances()));
249 // Navigate to home page.
250 $this->drupalGet('');
251 $this->assertText($body);
254 $this->drupalGet('block/1/delete');
255 $this->assertText(\Drupal::translation()->formatPlural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
257 $this->drupalPostForm(NULL, [], 'Delete');
258 $this->assertRaw(t('The custom block %name has been deleted.', ['%name' => $edit['info[0][value]']]));
260 // Create another block and force the plugin cache to flush.
262 $edit2['info[0][value]'] = $this->randomMachineName(8);
263 $body2 = $this->randomMachineName(16);
264 $edit2['body[0][value]'] = $body2;
265 $this->drupalPostForm('block/add/basic', $edit2, t('Save'));
267 $this->assertNoRaw('Error message');
269 // Create another block with no instances, and test we don't get a
270 // confirmation message about deleting instances.
272 $edit3['info[0][value]'] = $this->randomMachineName(8);
273 $body = $this->randomMachineName(16);
274 $edit3['body[0][value]'] = $body;
275 $this->drupalPostForm('block/add/basic', $edit3, t('Save'));
277 // Show the delete confirm form.
278 $this->drupalGet('block/3/delete');
279 $this->assertNoText('This will also remove');
283 * Test that placed content blocks create a dependency in the block placement.
285 public function testConfigDependencies() {
286 $block = $this->createBlockContent();
288 $block_placement_id = Unicode::strtolower($block->label());
290 'id' => $block_placement_id,
291 'settings[label]' => $block->label(),
292 'region' => 'sidebar_first',
294 $block = BlockContent::load(1);
295 $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
296 $this->drupalPostForm($url, $instance, t('Save block'));
298 $dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('content', [$block->getConfigDependencyName()]);
299 $block_placement = reset($dependencies);
300 $this->assertEqual($block_placement_id, $block_placement->id(), "The block placement config entity has a dependency on the block content entity.");