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