BlockContextMappingUpdateTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\Tests\block\Functional\Update;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\FunctionalTests\Update\UpdatePathTestBase;
  5. use Drupal\node\Entity\Node;
  6. /**
  7. * Tests the upgrade path for block context mapping renames.
  8. *
  9. * @see https://www.drupal.org/node/2354889
  10. *
  11. * @group Update
  12. * @group legacy
  13. */
  14. class BlockContextMappingUpdateTest extends UpdatePathTestBase {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected static $modules = ['block_test', 'language'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setDatabaseDumpFiles() {
  23. $this->databaseDumpFiles = [
  24. __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
  25. __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.block-context-manager-2354889.php',
  26. __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.language-enabled.php',
  27. __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.block-test-enabled.php',
  28. ];
  29. }
  30. /**
  31. * Tests that block context mapping is updated properly.
  32. */
  33. public function testUpdateHookN() {
  34. $this->runUpdates();
  35. $this->assertRaw('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:<ul><li>User login (Visibility: Baloney spam)</li></ul>');
  36. // Disable maintenance mode.
  37. \Drupal::state()->set('system.maintenance_mode', FALSE);
  38. // We finished updating so we can log in the user now.
  39. $this->drupalLogin($this->rootUser);
  40. // The block that we are testing has the following visibility rules:
  41. // - only visible on node pages
  42. // - only visible to authenticated users.
  43. $block_title = 'Test for 2354889';
  44. // Create two nodes, a page and an article.
  45. $page = Node::create([
  46. 'type' => 'page',
  47. 'title' => 'Page node',
  48. ]);
  49. $page->save();
  50. $article = Node::create([
  51. 'type' => 'article',
  52. 'title' => 'Article node',
  53. ]);
  54. $article->save();
  55. // Check that the block appears only on Page nodes for authenticated users.
  56. $this->drupalGet('node/' . $page->id());
  57. $this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.');
  58. $this->drupalGet('node/' . $article->id());
  59. $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
  60. $this->drupalLogout();
  61. // Check that the block does not appear on any page for anonymous users.
  62. $this->drupalGet('node/' . $page->id());
  63. $this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.');
  64. $this->drupalGet('node/' . $article->id());
  65. $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
  66. // Ensure that all the context mappings got updated properly.
  67. $block = Block::load('testfor2354889');
  68. $visibility = $block->get('visibility');
  69. $this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']);
  70. $this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
  71. $this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']);
  72. // Check that a block with invalid context is being disabled and that it can
  73. // still be edited afterward.
  74. $disabled_block = Block::load('thirdtestfor2354889');
  75. $this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled');
  76. $this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney_spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
  77. $disabled_block_visibility = $disabled_block->get('visibility');
  78. $this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
  79. $admin_user = $this->drupalCreateUser(['administer blocks']);
  80. $this->drupalLogin($admin_user);
  81. $this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
  82. $this->assertResponse('200');
  83. }
  84. }