3 namespace Drupal\Tests\block\Functional\Update;
5 use Drupal\block\Entity\Block;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7 use Drupal\node\Entity\Node;
10 * Tests the upgrade path for block context mapping renames.
12 * @see https://www.drupal.org/node/2354889
16 class BlockContextMappingUpdateTest extends UpdatePathTestBase {
21 protected static $modules = ['block_test', 'language'];
26 protected function setDatabaseDumpFiles() {
27 $this->databaseDumpFiles = [
28 __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
29 __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.block-context-manager-2354889.php',
30 __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.language-enabled.php',
31 __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.block-test-enabled.php',
36 * Tests that block context mapping is updated properly.
38 public function testUpdateHookN() {
40 $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>');
42 // Disable maintenance mode.
43 \Drupal::state()->set('system.maintenance_mode', FALSE);
45 // We finished updating so we can log in the user now.
46 $this->drupalLogin($this->rootUser);
48 // The block that we are testing has the following visibility rules:
49 // - only visible on node pages
50 // - only visible to authenticated users.
51 $block_title = 'Test for 2354889';
53 // Create two nodes, a page and an article.
54 $page = Node::create([
56 'title' => 'Page node',
60 $article = Node::create([
62 'title' => 'Article node',
66 // Check that the block appears only on Page nodes for authenticated users.
67 $this->drupalGet('node/' . $page->id());
68 $this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.');
70 $this->drupalGet('node/' . $article->id());
71 $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
73 $this->drupalLogout();
75 // Check that the block does not appear on any page for anonymous users.
76 $this->drupalGet('node/' . $page->id());
77 $this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.');
79 $this->drupalGet('node/' . $article->id());
80 $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
82 // Ensure that all the context mappings got updated properly.
83 $block = Block::load('testfor2354889');
84 $visibility = $block->get('visibility');
85 $this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']);
86 $this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
87 $this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']);
89 // Check that a block with invalid context is being disabled and that it can
90 // still be edited afterward.
91 $disabled_block = Block::load('thirdtestfor2354889');
92 $this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled');
94 $this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney_spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
96 $disabled_block_visibility = $disabled_block->get('visibility');
97 $this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
99 $admin_user = $this->drupalCreateUser(['administer blocks']);
100 $this->drupalLogin($admin_user);
102 $this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
103 $this->assertResponse('200');