BlockTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\block\Entity\Block;
  5. use Drupal\Core\Url;
  6. use Drupal\user\Entity\Role;
  7. use Drupal\user\RoleInterface;
  8. /**
  9. * Tests basic block functionality.
  10. *
  11. * @group block
  12. */
  13. class BlockTest extends BlockTestBase {
  14. /**
  15. * Tests block visibility.
  16. */
  17. public function testBlockVisibility() {
  18. $block_name = 'system_powered_by_block';
  19. // Create a random title for the block.
  20. $title = $this->randomMachineName(8);
  21. // Enable a standard block.
  22. $default_theme = $this->config('system.theme')->get('default');
  23. $edit = [
  24. 'id' => strtolower($this->randomMachineName(8)),
  25. 'region' => 'sidebar_first',
  26. 'settings[label]' => $title,
  27. 'settings[label_display]' => TRUE,
  28. ];
  29. // Set the block to be hidden on any user path, and to be shown only to
  30. // authenticated users.
  31. $edit['visibility[request_path][pages]'] = '/user*';
  32. $edit['visibility[request_path][negate]'] = TRUE;
  33. $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
  34. $this->drupalGet('admin/structure/block/add/' . $block_name . '/' . $default_theme);
  35. $this->assertFieldChecked('edit-visibility-request-path-negate-0');
  36. $this->drupalPostForm(NULL, $edit, t('Save block'));
  37. $this->assertText('The block configuration has been saved.', 'Block was saved');
  38. $this->clickLink('Configure');
  39. $this->assertFieldChecked('edit-visibility-request-path-negate-1');
  40. $this->drupalGet('');
  41. $this->assertText($title, 'Block was displayed on the front page.');
  42. $this->drupalGet('user');
  43. $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
  44. // Confirm that the block is not displayed to anonymous users.
  45. $this->drupalLogout();
  46. $this->drupalGet('');
  47. $this->assertNoText($title, 'Block was not displayed to anonymous users.');
  48. // Confirm that an empty block is not displayed.
  49. $this->assertNoText('Powered by Drupal', 'Empty block not displayed.');
  50. $this->assertNoRaw('sidebar-first', 'Empty sidebar-first region is not displayed.');
  51. }
  52. /**
  53. * Tests that visibility can be properly toggled.
  54. */
  55. public function testBlockToggleVisibility() {
  56. $block_name = 'system_powered_by_block';
  57. // Create a random title for the block.
  58. $title = $this->randomMachineName(8);
  59. // Enable a standard block.
  60. $default_theme = $this->config('system.theme')->get('default');
  61. $edit = [
  62. 'id' => strtolower($this->randomMachineName(8)),
  63. 'region' => 'sidebar_first',
  64. 'settings[label]' => $title,
  65. ];
  66. $block_id = $edit['id'];
  67. // Set the block to be shown only to authenticated users.
  68. $edit['visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']'] = TRUE;
  69. $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
  70. $this->clickLink('Configure');
  71. $this->assertFieldChecked('edit-visibility-user-role-roles-authenticated');
  72. $edit = [
  73. 'visibility[user_role][roles][' . RoleInterface::AUTHENTICATED_ID . ']' => FALSE,
  74. ];
  75. $this->drupalPostForm(NULL, $edit, 'Save block');
  76. $this->clickLink('Configure');
  77. $this->assertNoFieldChecked('edit-visibility-user-role-roles-authenticated');
  78. // Ensure that no visibility is configured.
  79. /** @var \Drupal\block\BlockInterface $block */
  80. $block = Block::load($block_id);
  81. $visibility_config = $block->getVisibilityConditions()->getConfiguration();
  82. $this->assertIdentical([], $visibility_config);
  83. $this->assertIdentical([], $block->get('visibility'));
  84. }
  85. /**
  86. * Test block visibility when leaving "pages" textarea empty.
  87. */
  88. public function testBlockVisibilityListedEmpty() {
  89. $block_name = 'system_powered_by_block';
  90. // Create a random title for the block.
  91. $title = $this->randomMachineName(8);
  92. // Enable a standard block.
  93. $default_theme = $this->config('system.theme')->get('default');
  94. $edit = [
  95. 'id' => strtolower($this->randomMachineName(8)),
  96. 'region' => 'sidebar_first',
  97. 'settings[label]' => $title,
  98. 'visibility[request_path][negate]' => TRUE,
  99. ];
  100. // Set the block to be hidden on any user path, and to be shown only to
  101. // authenticated users.
  102. $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
  103. $this->assertText('The block configuration has been saved.', 'Block was saved');
  104. $this->drupalGet('user');
  105. $this->assertNoText($title, 'Block was not displayed according to block visibility rules.');
  106. $this->drupalGet('USER');
  107. $this->assertNoText($title, 'Block was not displayed according to block visibility rules regardless of path case.');
  108. // Confirm that the block is not displayed to anonymous users.
  109. $this->drupalLogout();
  110. $this->drupalGet('');
  111. $this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.');
  112. }
  113. /**
  114. * Tests adding a block from the library page with a weight query string.
  115. */
  116. public function testAddBlockFromLibraryWithWeight() {
  117. $default_theme = $this->config('system.theme')->get('default');
  118. // Test one positive, zero, and one negative weight.
  119. foreach (['7', '0', '-9'] as $weight) {
  120. $options = [
  121. 'query' => [
  122. 'region' => 'sidebar_first',
  123. 'weight' => $weight,
  124. ],
  125. ];
  126. $this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
  127. $block_name = 'system_powered_by_block';
  128. $add_url = Url::fromRoute('block.admin_add', [
  129. 'plugin_id' => $block_name,
  130. 'theme' => $default_theme,
  131. ]);
  132. $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
  133. $this->assertEqual(1, count($links), 'Found one matching link.');
  134. $this->assertEqual(t('Place block'), $links[0]->getText(), 'Found the expected link text.');
  135. list($path, $query_string) = explode('?', $links[0]->getAttribute('href'), 2);
  136. parse_str($query_string, $query_parts);
  137. $this->assertEqual($weight, $query_parts['weight'], 'Found the expected weight query string.');
  138. // Create a random title for the block.
  139. $title = $this->randomMachineName(8);
  140. $block_id = strtolower($this->randomMachineName(8));
  141. $edit = [
  142. 'id' => $block_id,
  143. 'settings[label]' => $title,
  144. ];
  145. // Create the block using the link parsed from the library page.
  146. $this->drupalPostForm($this->getAbsoluteUrl($links[0]->getAttribute('href')), $edit, t('Save block'));
  147. // Ensure that the block was created with the expected weight.
  148. /** @var \Drupal\block\BlockInterface $block */
  149. $block = Block::load($block_id);
  150. $this->assertEqual($weight, $block->getWeight(), 'Found the block with expected weight.');
  151. }
  152. }
  153. /**
  154. * Test configuring and moving a module-define block to specific regions.
  155. */
  156. public function testBlock() {
  157. // Place page title block to test error messages.
  158. $this->drupalPlaceBlock('page_title_block');
  159. // Disable the block.
  160. $this->drupalGet('admin/structure/block');
  161. $this->clickLink('Disable');
  162. // Select the 'Powered by Drupal' block to be configured and moved.
  163. $block = [];
  164. $block['id'] = 'system_powered_by_block';
  165. $block['settings[label]'] = $this->randomMachineName(8);
  166. $block['settings[label_display]'] = TRUE;
  167. $block['theme'] = $this->config('system.theme')->get('default');
  168. $block['region'] = 'header';
  169. // Set block title to confirm that interface works and override any custom titles.
  170. $this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], ['settings[label]' => $block['settings[label]'], 'settings[label_display]' => $block['settings[label_display]'], 'id' => $block['id'], 'region' => $block['region']], t('Save block'));
  171. $this->assertText(t('The block configuration has been saved.'), 'Block title set.');
  172. // Check to see if the block was created by checking its configuration.
  173. $instance = Block::load($block['id']);
  174. $this->assertEqual($instance->label(), $block['settings[label]'], 'Stored block title found.');
  175. // Check whether the block can be moved to all available regions.
  176. foreach ($this->regions as $region) {
  177. $this->moveBlockToRegion($block, $region);
  178. }
  179. // Disable the block.
  180. $this->drupalGet('admin/structure/block');
  181. $this->clickLink('Disable');
  182. // Confirm that the block is now listed as disabled.
  183. $this->assertText(t('The block settings have been updated.'), 'Block successfully moved to disabled region.');
  184. // Confirm that the block instance title and markup are not displayed.
  185. $this->drupalGet('node');
  186. $this->assertNoText(t($block['settings[label]']));
  187. // Check for <div id="block-my-block-instance-name"> if the machine name
  188. // is my_block_instance_name.
  189. $xpath = $this->buildXPathQuery('//div[@id=:id]/*', [':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))]);
  190. $this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
  191. // Test deleting the block from the edit form.
  192. $this->drupalGet('admin/structure/block/manage/' . $block['id']);
  193. $this->clickLink(t('Remove block'));
  194. $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block['settings[label]']]));
  195. $this->drupalPostForm(NULL, [], t('Remove'));
  196. $this->assertRaw(t('The block %name has been removed.', ['%name' => $block['settings[label]']]));
  197. // Test deleting a block via "Configure block" link.
  198. $block = $this->drupalPlaceBlock('system_powered_by_block');
  199. $this->drupalGet('admin/structure/block/manage/' . $block->id(), ['query' => ['destination' => 'admin']]);
  200. $this->clickLink(t('Remove block'));
  201. $this->assertRaw(t('Are you sure you want to remove the block @name?', ['@name' => $block->label()]));
  202. $this->drupalPostForm(NULL, [], t('Remove'));
  203. $this->assertRaw(t('The block %name has been removed.', ['%name' => $block->label()]));
  204. $this->assertUrl('admin');
  205. $this->assertNoRaw($block->id());
  206. }
  207. /**
  208. * Tests that the block form has a theme selector when not passed via the URL.
  209. */
  210. public function testBlockThemeSelector() {
  211. // Install all themes.
  212. \Drupal::service('theme_handler')->install(['bartik', 'seven', 'stark']);
  213. $theme_settings = $this->config('system.theme');
  214. foreach (['bartik', 'seven', 'stark'] as $theme) {
  215. $this->drupalGet('admin/structure/block/list/' . $theme);
  216. $this->assertTitle(t('Block layout') . ' | Drupal');
  217. // Select the 'Powered by Drupal' block to be placed.
  218. $block = [];
  219. $block['id'] = strtolower($this->randomMachineName());
  220. $block['theme'] = $theme;
  221. $block['region'] = 'content';
  222. $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
  223. $this->assertText(t('The block configuration has been saved.'));
  224. $this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . Html::getClass($block['id']));
  225. // Set the default theme and ensure the block is placed.
  226. $theme_settings->set('default', $theme)->save();
  227. $this->drupalGet('');
  228. $elements = $this->xpath('//div[@id = :id]', [':id' => Html::getUniqueId('block-' . $block['id'])]);
  229. $this->assertTrue(!empty($elements), 'The block was found.');
  230. }
  231. }
  232. /**
  233. * Test block display of theme titles.
  234. */
  235. public function testThemeName() {
  236. // Enable the help block.
  237. $this->drupalPlaceBlock('help_block', ['region' => 'help']);
  238. $this->drupalPlaceBlock('local_tasks_block');
  239. // Explicitly set the default and admin themes.
  240. $theme = 'block_test_specialchars_theme';
  241. \Drupal::service('theme_handler')->install([$theme]);
  242. \Drupal::service('router.builder')->rebuild();
  243. $this->drupalGet('admin/structure/block');
  244. $this->assertEscaped('<"Cat" & \'Mouse\'>');
  245. $this->drupalGet('admin/structure/block/list/block_test_specialchars_theme');
  246. $this->assertEscaped('Demonstrate block regions (<"Cat" & \'Mouse\'>)');
  247. }
  248. /**
  249. * Test block title display settings.
  250. */
  251. public function testHideBlockTitle() {
  252. $block_name = 'system_powered_by_block';
  253. // Create a random title for the block.
  254. $title = $this->randomMachineName(8);
  255. $id = strtolower($this->randomMachineName(8));
  256. // Enable a standard block.
  257. $default_theme = $this->config('system.theme')->get('default');
  258. $edit = [
  259. 'id' => $id,
  260. 'region' => 'sidebar_first',
  261. 'settings[label]' => $title,
  262. ];
  263. $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
  264. $this->assertText('The block configuration has been saved.', 'Block was saved');
  265. $this->drupalGet('user');
  266. $this->assertNoText($title, 'Block title was not displayed by default.');
  267. $edit = [
  268. 'settings[label_display]' => TRUE,
  269. ];
  270. $this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block'));
  271. $this->assertText('The block configuration has been saved.', 'Block was saved');
  272. $this->drupalGet('admin/structure/block/manage/' . $id);
  273. $this->assertFieldChecked('edit-settings-label-display', 'The display_block option has the correct default value on the configuration form.');
  274. $this->drupalGet('user');
  275. $this->assertText($title, 'Block title was displayed when enabled.');
  276. }
  277. /**
  278. * Moves a block to a given region via the UI and confirms the result.
  279. *
  280. * @param array $block
  281. * An array of information about the block, including the following keys:
  282. * - module: The module providing the block.
  283. * - title: The title of the block.
  284. * - delta: The block's delta key.
  285. * @param string $region
  286. * The machine name of the theme region to move the block to, for example
  287. * 'header' or 'sidebar_first'.
  288. */
  289. public function moveBlockToRegion(array $block, $region) {
  290. // Set the created block to a specific region.
  291. $block += ['theme' => $this->config('system.theme')->get('default')];
  292. $edit = [];
  293. $edit['blocks[' . $block['id'] . '][region]'] = $region;
  294. $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
  295. // Confirm that the block was moved to the proper region.
  296. $this->assertText(t('The block settings have been updated.'), format_string('Block successfully moved to %region_name region.', ['%region_name' => $region]));
  297. // Confirm that the block is being displayed.
  298. $this->drupalGet('');
  299. $this->assertText(t($block['settings[label]']), 'Block successfully being displayed on the page.');
  300. // Confirm that the custom block was found at the proper region.
  301. $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', [
  302. ':region-class' => 'region region-' . Html::getClass($region),
  303. ':block-id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
  304. ]);
  305. $this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', ['%region_name' => Html::getClass($region)]));
  306. }
  307. /**
  308. * Test that cache tags are properly set and bubbled up to the page cache.
  309. *
  310. * Verify that invalidation of these cache tags works:
  311. * - "block:<block ID>"
  312. * - "block_plugin:<block plugin ID>"
  313. */
  314. public function testBlockCacheTags() {
  315. // The page cache only works for anonymous users.
  316. $this->drupalLogout();
  317. // Enable page caching.
  318. $config = $this->config('system.performance');
  319. $config->set('cache.page.max_age', 300);
  320. $config->save();
  321. // Place the "Powered by Drupal" block.
  322. $block = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered']);
  323. // Prime the page cache.
  324. $this->drupalGet('<front>');
  325. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  326. // Verify a cache hit, but also the presence of the correct cache tags in
  327. // both the page and block caches.
  328. $this->drupalGet('<front>');
  329. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
  330. $cid_parts = [\Drupal::url('<front>', [], ['absolute' => TRUE]), 'html'];
  331. $cid = implode(':', $cid_parts);
  332. $cache_entry = \Drupal::cache('page')->get($cid);
  333. $expected_cache_tags = [
  334. 'config:block_list',
  335. 'block_view',
  336. 'config:block.block.powered',
  337. 'config:user.role.anonymous',
  338. 'http_response',
  339. 'rendered',
  340. ];
  341. sort($expected_cache_tags);
  342. $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
  343. $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
  344. $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
  345. $expected_cache_tags = [
  346. 'block_view',
  347. 'config:block.block.powered',
  348. 'rendered',
  349. ];
  350. sort($expected_cache_tags);
  351. $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
  352. // The "Powered by Drupal" block is modified; verify a cache miss.
  353. $block->setRegion('content');
  354. $block->save();
  355. $this->drupalGet('<front>');
  356. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  357. // Now we should have a cache hit again.
  358. $this->drupalGet('<front>');
  359. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
  360. // Place the "Powered by Drupal" block another time; verify a cache miss.
  361. $block_2 = $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered-2']);
  362. $this->drupalGet('<front>');
  363. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  364. // Verify a cache hit, but also the presence of the correct cache tags.
  365. $this->drupalGet('<front>');
  366. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
  367. $cid_parts = [\Drupal::url('<front>', [], ['absolute' => TRUE]), 'html'];
  368. $cid = implode(':', $cid_parts);
  369. $cache_entry = \Drupal::cache('page')->get($cid);
  370. $expected_cache_tags = [
  371. 'config:block_list',
  372. 'block_view',
  373. 'config:block.block.powered',
  374. 'config:block.block.powered-2',
  375. 'config:user.role.anonymous',
  376. 'http_response',
  377. 'rendered',
  378. ];
  379. sort($expected_cache_tags);
  380. $this->assertEqual($cache_entry->tags, $expected_cache_tags);
  381. $expected_cache_tags = [
  382. 'block_view',
  383. 'config:block.block.powered',
  384. 'rendered',
  385. ];
  386. sort($expected_cache_tags);
  387. $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
  388. $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
  389. $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
  390. $expected_cache_tags = [
  391. 'block_view',
  392. 'config:block.block.powered-2',
  393. 'rendered',
  394. ];
  395. sort($expected_cache_tags);
  396. $keys = \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:language_interface', 'theme', 'user.permissions'])->getKeys();
  397. $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered-2:' . implode(':', $keys));
  398. $this->assertIdentical($cache_entry->tags, $expected_cache_tags);
  399. // Now we should have a cache hit again.
  400. $this->drupalGet('<front>');
  401. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
  402. // Delete the "Powered by Drupal" blocks; verify a cache miss.
  403. entity_delete_multiple('block', ['powered', 'powered-2']);
  404. $this->drupalGet('<front>');
  405. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  406. }
  407. /**
  408. * Tests that a link exists to block layout from the appearance form.
  409. */
  410. public function testThemeAdminLink() {
  411. $this->drupalPlaceBlock('help_block', ['region' => 'help']);
  412. $theme_admin = $this->drupalCreateUser([
  413. 'administer blocks',
  414. 'administer themes',
  415. 'access administration pages',
  416. ]);
  417. $this->drupalLogin($theme_admin);
  418. $this->drupalGet('admin/appearance');
  419. $this->assertText('You can place blocks for each theme on the block layout page');
  420. $this->assertLinkByHref('admin/structure/block');
  421. }
  422. /**
  423. * Tests that uninstalling a theme removes its block configuration.
  424. */
  425. public function testUninstallTheme() {
  426. /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  427. $theme_handler = \Drupal::service('theme_handler');
  428. $theme_handler->install(['seven']);
  429. $this->config('system.theme')->set('default', 'seven')->save();
  430. $block = $this->drupalPlaceBlock('system_powered_by_block', ['theme' => 'seven', 'region' => 'help']);
  431. $this->drupalGet('<front>');
  432. $this->assertText('Powered by Drupal');
  433. $this->config('system.theme')->set('default', 'classy')->save();
  434. $theme_handler->uninstall(['seven']);
  435. // Ensure that the block configuration does not exist anymore.
  436. $this->assertIdentical(NULL, Block::load($block->id()));
  437. }
  438. /**
  439. * Tests the block access.
  440. */
  441. public function testBlockAccess() {
  442. $this->drupalPlaceBlock('test_access', ['region' => 'help']);
  443. $this->drupalGet('<front>');
  444. $this->assertNoText('Hello test world');
  445. \Drupal::state()->set('test_block_access', TRUE);
  446. $this->drupalGet('<front>');
  447. $this->assertText('Hello test world');
  448. }
  449. /**
  450. * Tests block_user_role_delete.
  451. */
  452. public function testBlockUserRoleDelete() {
  453. $role1 = Role::create(['id' => 'test_role1', 'name' => $this->randomString()]);
  454. $role1->save();
  455. $role2 = Role::create(['id' => 'test_role2', 'name' => $this->randomString()]);
  456. $role2->save();
  457. $block = Block::create([
  458. 'id' => $this->randomMachineName(),
  459. 'plugin' => 'system_powered_by_block',
  460. ]);
  461. $block->setVisibilityConfig('user_role', [
  462. 'roles' => [
  463. $role1->id() => $role1->id(),
  464. $role2->id() => $role2->id(),
  465. ],
  466. ]);
  467. $block->save();
  468. $this->assertEqual($block->getVisibility()['user_role']['roles'], [
  469. $role1->id() => $role1->id(),
  470. $role2->id() => $role2->id(),
  471. ]);
  472. $role1->delete();
  473. $block = Block::load($block->id());
  474. $this->assertEqual($block->getVisibility()['user_role']['roles'], [
  475. $role2->id() => $role2->id(),
  476. ]);
  477. }
  478. }