Pull merge.
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / Update / BlockContentReusableUpdateTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional\Update;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
7
8 /**
9  * Tests 'reusable' field related update functions for the Block Content module.
10  *
11  * @group Update
12  * @group block_content
13  * @group legacy
14  */
15 class BlockContentReusableUpdateTest extends UpdatePathTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setDatabaseDumpFiles() {
21     $this->databaseDumpFiles = [
22       __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
23       // Override the 'block_content' view with an extra display with overridden
24       // filters. This extra display should also have a filter added for
25       // 'reusable' field so that it does not expose non-reusable fields. This
26       // display also has a filter that only shows blocks that contain 'block2'
27       // in the 'info' field.
28       __DIR__ . '/../../../fixtures/update/drupal-8.views_block_content-2976334.php',
29     ];
30   }
31
32   /**
33    * Tests adding 'reusable' entity base field to the block content entity type.
34    *
35    * @see block_content_update_8600()
36    * @see block_content_post_update_add_views_reusable_filter()
37    */
38   public function testReusableFieldAddition() {
39     $assert_session = $this->assertSession();
40     $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
41
42     // Ensure that 'reusable' field is not present before updates.
43     $this->assertEmpty($entity_definition_update_manager->getFieldStorageDefinition('reusable', 'block_content'));
44
45     // Ensure that 'reusable' filter is not present before updates.
46     $view_config = \Drupal::configFactory()->get('views.view.block_content');
47     $this->assertFalse($view_config->isNew());
48     $this->assertEmpty($view_config->get('display.default.display_options.filters.reusable'));
49     $this->assertEmpty($view_config->get('display.page_2.display_options.filters.reusable'));
50     // Run updates.
51     $this->runUpdates();
52
53     // Ensure that 'reusable' filter is present after updates.
54     \Drupal::configFactory()->clearStaticCache();
55     $view_config = \Drupal::configFactory()->get('views.view.block_content');
56     $this->assertNotEmpty($view_config->get('display.default.display_options.filters.reusable'));
57     $this->assertNotEmpty($view_config->get('display.page_2.display_options.filters.reusable'));
58
59     // Check that the field exists and is configured correctly.
60     $reusable_field = $entity_definition_update_manager->getFieldStorageDefinition('reusable', 'block_content');
61     $this->assertEquals('Reusable', $reusable_field->getLabel());
62     $this->assertEquals('A boolean indicating whether this block is reusable.', $reusable_field->getDescription());
63     $this->assertEquals(FALSE, $reusable_field->isRevisionable());
64     $this->assertEquals(FALSE, $reusable_field->isTranslatable());
65
66     $after_block1 = BlockContent::create([
67       'info' => 'After update block1',
68       'type' => 'basic_block',
69     ]);
70     $after_block1->save();
71     // Add second block that will be shown with the 'info' filter on the
72     // additional view display.
73     $after_block2 = BlockContent::create([
74       'info' => 'After update block2',
75       'type' => 'basic_block',
76     ]);
77     $after_block2->save();
78
79     $this->assertTrue($after_block1->isReusable());
80     $this->assertTrue($after_block2->isReusable());
81
82     $admin_user = $this->drupalCreateUser(['administer blocks']);
83     $this->drupalLogin($admin_user);
84
85     $block_non_reusable = BlockContent::create([
86       'info' => 'block1 non reusable',
87       'type' => 'basic_block',
88       'reusable' => FALSE,
89     ]);
90     $block_non_reusable->save();
91     // Add second block that would be shown with the 'info' filter on the
92     // additional view display if the 'reusable' filter was not added.
93     $block2_non_reusable = BlockContent::create([
94       'info' => 'block2 non reusable',
95       'type' => 'basic_block',
96       'reusable' => FALSE,
97     ]);
98     $block2_non_reusable->save();
99     $this->assertFalse($block_non_reusable->isReusable());
100     $this->assertFalse($block2_non_reusable->isReusable());
101
102     // Ensure the Custom Block view shows the reusable blocks only.
103     $this->drupalGet('admin/structure/block/block-content');
104     $assert_session->statusCodeEquals('200');
105     $assert_session->responseContains('view-id-block_content');
106     $assert_session->pageTextContains($after_block1->label());
107     $assert_session->pageTextContains($after_block2->label());
108     $assert_session->pageTextNotContains($block_non_reusable->label());
109     $assert_session->pageTextNotContains($block2_non_reusable->label());
110
111     // Ensure the view's other display also only shows reusable blocks and still
112     // filters on the 'info' field.
113     $this->drupalGet('extra-view-display');
114     $assert_session->statusCodeEquals('200');
115     $assert_session->responseContains('view-id-block_content');
116     $assert_session->pageTextNotContains($after_block1->label());
117     $assert_session->pageTextContains($after_block2->label());
118     $assert_session->pageTextNotContains($block_non_reusable->label());
119     $assert_session->pageTextNotContains($block2_non_reusable->label());
120
121     // Ensure the Custom Block listing without Views installed shows the only
122     // reusable blocks.
123     $this->drupalGet('admin/structure/block/block-content');
124     $this->container->get('module_installer')->uninstall(['views_ui', 'views']);
125     $this->drupalGet('admin/structure/block/block-content');
126     $assert_session->statusCodeEquals('200');
127     $assert_session->responseNotContains('view-id-block_content');
128     $assert_session->pageTextContains($after_block1->label());
129     $assert_session->pageTextContains($after_block2->label());
130     $assert_session->pageTextNotContains($block_non_reusable->label());
131     $assert_session->pageTextNotContains($block2_non_reusable->label());
132
133     $this->drupalGet('block/' . $after_block1->id());
134     $assert_session->statusCodeEquals('200');
135
136     // Ensure the non-reusable block is not accessible in the form.
137     $this->drupalGet('block/' . $block_non_reusable->id());
138     $assert_session->statusCodeEquals('403');
139
140     $this->drupalLogout();
141
142     $this->drupalLogin($this->createUser([
143       'access user profiles',
144       'administer blocks',
145     ]));
146     $this->drupalGet('block/' . $after_block1->id());
147     $assert_session->statusCodeEquals('200');
148
149     $this->drupalGet('block/' . $block_non_reusable->id());
150     $assert_session->statusCodeEquals('403');
151   }
152
153 }