97cd4db18f43e56973ad98c596b5ada84fbd1160
[yaffs-website] / web / core / modules / settings_tray / tests / src / FunctionalJavascript / OverriddenConfigurationTest.php
1 <?php
2
3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\user\Entity\Role;
8
9 /**
10  * Tests handling of configuration overrides.
11  *
12  * @group settings_tray
13  */
14 class OverriddenConfigurationTest extends SettingsTrayTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'settings_tray_override_test',
21     'menu_ui',
22     'menu_link_content',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $user = $this->createUser([
32       'administer blocks',
33       'access contextual links',
34       'access toolbar',
35     ]);
36     $this->drupalLogin($user);
37   }
38
39   /**
40    * Test  blocks with overridden related configuration removed when overridden.
41    */
42   public function testOverriddenConfigurationRemoved() {
43     $web_assert = $this->assertSession();
44     $page = $this->getSession()->getPage();
45     $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration', 'administer menu']);
46
47     // Confirm the branding block does include 'site_information' section when
48     // the site name is not overridden.
49     $branding_block = $this->placeBlock('system_branding_block');
50     $this->drupalGet('user');
51     $this->enableEditMode();
52     $this->openBlockForm($this->getBlockSelector($branding_block));
53     $web_assert->fieldExists('settings[site_information][site_name]');
54     // Confirm the branding block does not include 'site_information' section
55     // when the site name is overridden.
56     $this->container->get('state')->set('settings_tray_override_test.site_name', TRUE);
57     $this->drupalGet('user');
58     $this->openBlockForm($this->getBlockSelector($branding_block));
59     $web_assert->fieldNotExists('settings[site_information][site_name]');
60     $page->pressButton('Save Site branding');
61     $this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
62     $web_assert->assertWaitOnAjaxRequest();
63     // Confirm we did not save changes to the configuration.
64     $this->assertEquals('Llama Fan Club', \Drupal::configFactory()->get('system.site')->get('name'));
65     $this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
66
67     // Add a link or the menu will not render.
68     $menu_link_content = MenuLinkContent::create([
69       'title' => 'This is on the menu',
70       'menu_name' => 'main',
71       'link' => ['uri' => 'route:<front>'],
72     ]);
73     $menu_link_content->save();
74     // Confirm the menu block does include menu section when the menu is not
75     // overridden.
76     $menu_block = $this->placeBlock('system_menu_block:main');
77     $web_assert->assertWaitOnAjaxRequest();
78     $this->drupalGet('user');
79     $web_assert->pageTextContains('This is on the menu');
80     $this->openBlockForm($this->getBlockSelector($menu_block));
81     $web_assert->elementExists('css', '#menu-overview');
82
83     // Confirm the menu block does not include menu section when the menu is
84     // overridden.
85     $this->container->get('state')->set('settings_tray_override_test.menu', TRUE);
86     $this->drupalGet('user');
87     $web_assert->pageTextContains('This is on the menu');
88     $menu_with_overrides = \Drupal::configFactory()->get('system.menu.main')->get();
89     $menu_without_overrides = \Drupal::configFactory()->getEditable('system.menu.main')->get();
90     $this->openBlockForm($this->getBlockSelector($menu_block));
91     $web_assert->elementNotExists('css', '#menu-overview');
92     $page->pressButton('Save Main navigation');
93     $this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
94     $web_assert->assertWaitOnAjaxRequest();
95     // Confirm we did not save changes to the configuration.
96     $this->assertEquals('Labely label', \Drupal::configFactory()->get('system.menu.main')->get('label'));
97     $this->assertEquals('Main navigation', \Drupal::configFactory()->getEditable('system.menu.main')->get('label'));
98     $this->assertEquals($menu_with_overrides, \Drupal::configFactory()->get('system.menu.main')->get());
99     $this->assertEquals($menu_without_overrides, \Drupal::configFactory()->getEditable('system.menu.main')->get());
100     $web_assert->pageTextContains('This is on the menu');
101   }
102
103   /**
104    * Tests that blocks with configuration overrides are disabled.
105    */
106   public function testOverriddenBlock() {
107     $web_assert = $this->assertSession();
108     $page = $this->getSession()->getPage();
109     $overridden_block = $this->placeBlock('system_powered_by_block', [
110       'id' => 'overridden_block',
111       'label_display' => 1,
112       'label' => 'This will be overridden.',
113     ]);
114     $this->drupalGet('user');
115     $block_selector = $this->getBlockSelector($overridden_block);
116     // Confirm the block is marked as Settings Tray editable.
117     $this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
118     // Confirm the label is not overridden.
119     $web_assert->elementContains('css', $block_selector, 'This will be overridden.');
120     $this->enableEditMode();
121     $this->openBlockForm($block_selector);
122
123     // Confirm the block Settings Tray functionality is disabled when block is
124     // overridden.
125     $this->container->get('state')->set('settings_tray_override_test.block', TRUE);
126     $overridden_block->save();
127     $block_config = \Drupal::configFactory()->getEditable('block.block.overridden_block');
128     $block_config->set('settings', $block_config->get('settings'))->save();
129
130     $this->drupalGet('user');
131     $this->assertOverriddenBlockDisabled($overridden_block, 'Now this will be the label.');
132
133     // Test a non-overridden block does show the form in the off-canvas dialog.
134     $block = $this->placeBlock('system_powered_by_block', [
135       'label_display' => 1,
136       'label' => 'Labely label',
137     ]);
138     $this->drupalGet('user');
139     $block_selector = $this->getBlockSelector($block);
140     // Confirm the block is marked as Settings Tray editable.
141     $this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
142     // Confirm the label is not overridden.
143     $web_assert->elementContains('css', $block_selector, 'Labely label');
144     $this->openBlockForm($block_selector);
145   }
146
147   /**
148    * Asserts that an overridden block has Settings Tray disabled.
149    *
150    * @param \Drupal\block\Entity\Block $overridden_block
151    *   The overridden block.
152    * @param string $override_text
153    *   The override text that should appear in the block.
154    */
155   protected function assertOverriddenBlockDisabled(Block $overridden_block, $override_text) {
156     $web_assert = $this->assertSession();
157     $page = $this->getSession()->getPage();
158     $block_selector = $this->getBlockSelector($overridden_block);
159     $block_id = $overridden_block->id();
160     // Confirm the block does not have a quick edit link.
161     $contextual_links = $page->findAll('css', "$block_selector .contextual-links li a");
162     $this->assertNotEmpty($contextual_links);
163     foreach ($contextual_links as $link) {
164       $this->assertNotContains("/admin/structure/block/manage/$block_id/off-canvas", $link->getAttribute('href'));
165     }
166     // Confirm the block is not marked as Settings Tray editable.
167     $this->assertFalse($page->find('css', $block_selector)
168       ->hasAttribute('data-drupal-settingstray'));
169
170     // Confirm the text is actually overridden.
171     $web_assert->elementContains('css', $this->getBlockSelector($overridden_block), $override_text);
172   }
173
174 }