Security update for Core, with self-updated composer
[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  */
16 class BlockContextMappingUpdateTest extends UpdatePathTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected static $modules = ['block_test', 'language'];
22
23   /**
24    * {@inheritdoc}
25    */
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',
32     ];
33   }
34
35   /**
36    * Tests that block context mapping is updated properly.
37    */
38   public function testUpdateHookN() {
39     $this->runUpdates();
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>');
41
42     // Disable maintenance mode.
43     \Drupal::state()->set('system.maintenance_mode', FALSE);
44
45     // We finished updating so we can log in the user now.
46     $this->drupalLogin($this->rootUser);
47
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';
52
53     // Create two nodes, a page and an article.
54     $page = Node::create([
55       'type' => 'page',
56       'title' => 'Page node',
57     ]);
58     $page->save();
59
60     $article = Node::create([
61       'type' => 'article',
62       'title' => 'Article node',
63     ]);
64     $article->save();
65
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.');
69
70     $this->drupalGet('node/' . $article->id());
71     $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
72
73     $this->drupalLogout();
74
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.');
78
79     $this->drupalGet('node/' . $article->id());
80     $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
81
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']);
88
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');
93
94     $this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney_spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
95
96     $disabled_block_visibility = $disabled_block->get('visibility');
97     $this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
98
99     $admin_user = $this->drupalCreateUser(['administer blocks']);
100     $this->drupalLogin($admin_user);
101
102     $this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
103     $this->assertResponse('200');
104   }
105
106 }