Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentCreationTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Database\Database;
8
9 /**
10  * Create a block and test saving it.
11  *
12  * @group block_content
13  */
14 class BlockContentCreationTest extends BlockContentTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * Enable dummy module that implements hook_block_insert() for exceptions and
20    * field_ui to edit display settings.
21    *
22    * @var array
23    */
24   public static $modules = ['block_content_test', 'dblog', 'field_ui'];
25
26   /**
27    * Permissions to grant admin user.
28    *
29    * @var array
30    */
31   protected $permissions = [
32     'administer blocks',
33     'administer block_content display'
34   ];
35
36   /**
37    * Sets the test up.
38    */
39   protected function setUp() {
40     parent::setUp();
41     $this->drupalLogin($this->adminUser);
42   }
43
44   /**
45    * Creates a "Basic page" block and verifies its consistency in the database.
46    */
47   public function testBlockContentCreation() {
48     $this->drupalLogin($this->adminUser);
49
50     // Create a block.
51     $edit = [];
52     $edit['info[0][value]'] = 'Test Block';
53     $edit['body[0][value]'] = $this->randomMachineName(16);
54     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
55
56     // Check that the Basic block has been created.
57     $this->assertRaw(format_string('@block %name has been created.', [
58       '@block' => 'basic',
59       '%name' => $edit['info[0][value]']
60     ]), 'Basic block created.');
61
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');
64
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.');
69
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'));
73
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]']
77     ]));
78     $this->assertResponse(200);
79   }
80
81   /**
82    * Creates a "Basic page" block with multiple view modes.
83    */
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');
88     $edit = [
89       'id' => 'test_view_mode',
90       'label' => 'Test View Mode',
91     ];
92     $this->drupalPostForm(NULL, $edit, t('Save'));
93     $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
94
95     $this->drupalLogin($this->adminUser);
96
97     // Create a block.
98     $edit = [];
99     $edit['info[0][value]'] = 'Test Block';
100     $edit['body[0][value]'] = $this->randomMachineName(16);
101     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
102
103     // Check that the Basic block has been created.
104     $this->assertRaw(format_string('@block %name has been created.', [
105       '@block' => 'basic',
106       '%name' => $edit['info[0][value]']
107     ]), 'Basic block created.');
108
109     // Save our block permanently
110     $this->drupalPostForm(NULL, ['region' => 'content'], t('Save block'));
111
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,
119     ];
120     $this->drupalPostForm(NULL, $custom_view_mode, t('Save'));
121
122     // Go to the configure page and change the view mode.
123     $this->drupalGet('admin/structure/block/manage/testblock');
124
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.');
128
129     $view_mode['settings[view_mode]'] = 'test_view_mode';
130     $this->drupalPostForm(NULL, $view_mode, t('Save block'));
131
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');
135
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'));
140
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');
144
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.');
149
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'));
153
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]']
157     ]));
158     $this->assertResponse(200);
159   }
160
161   /**
162    * Create a default custom block.
163    *
164    * Creates a custom block from defaults and ensures that the 'basic block'
165    * type is being used.
166    */
167   public function testDefaultBlockContentCreation() {
168     $edit = [];
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'));
173
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.', [
176       '@block' => 'basic',
177       '%name' => $edit['info[0][value]'],
178     ]), 'Basic block created.');
179
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.');
184   }
185
186   /**
187    * Verifies that a transaction rolls back the failed creation.
188    */
189   public function testFailedBlockCreation() {
190     // Create a block.
191     try {
192       $this->createBlockContent('fail_creation');
193       $this->fail('Expected exception has not been thrown.');
194     }
195     catch (\Exception $e) {
196       $this->pass('Expected exception has been thrown.');
197     }
198
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')
204         ->execute()
205         ->fetchField();
206       $this->assertFalse($id, 'Transactions supported, and block not found in database.');
207     }
208     else {
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')
213         ->execute()
214         ->fetchField();
215       $this->assertTrue($id, 'Transactions not supported, and block found in database.');
216
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.');
220     }
221   }
222
223   /**
224    * Test deleting a block.
225    */
226   public function testBlockDelete() {
227     // Create a block.
228     $edit = [];
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'));
233
234     // Place the block.
235     $instance = [
236       'id' => Unicode::strtolower($edit['info[0][value]']),
237       'settings[label]' => $edit['info[0][value]'],
238       'region' => 'sidebar_first',
239     ];
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'));
243
244     $block = BlockContent::load(1);
245
246     // Test getInstances method.
247     $this->assertEqual(1, count($block->getInstances()));
248
249     // Navigate to home page.
250     $this->drupalGet('');
251     $this->assertText($body);
252
253     // Delete the block.
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.'));
256
257     $this->drupalPostForm(NULL, [], 'Delete');
258     $this->assertRaw(t('The custom block %name has been deleted.', ['%name' => $edit['info[0][value]']]));
259
260     // Create another block and force the plugin cache to flush.
261     $edit2 = [];
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'));
266
267     $this->assertNoRaw('Error message');
268
269     // Create another block with no instances, and test we don't get a
270     // confirmation message about deleting instances.
271     $edit3 = [];
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'));
276
277     // Show the delete confirm form.
278     $this->drupalGet('block/3/delete');
279     $this->assertNoText('This will also remove');
280   }
281
282   /**
283    * Test that placed content blocks create a dependency in the block placement.
284    */
285   public function testConfigDependencies() {
286     $block = $this->createBlockContent();
287     // Place the block.
288     $block_placement_id = Unicode::strtolower($block->label());
289     $instance = [
290       'id' => $block_placement_id,
291       'settings[label]' => $block->label(),
292       'region' => 'sidebar_first',
293     ];
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'));
297
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.");
301   }
302
303 }