3 namespace Drupal\Tests\settings_tray\FunctionalJavascript;
5 use Drupal\block\Entity\Block;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\user\Entity\Role;
10 * Tests handling of configuration overrides.
12 * @group settings_tray
14 class OverriddenConfigurationTest extends SettingsTrayTestBase {
19 public static $modules = [
20 'settings_tray_override_test',
28 protected function setUp() {
31 $user = $this->createUser([
33 'access contextual links',
36 $this->drupalLogin($user);
40 * Test blocks with overridden related configuration removed when overridden.
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']);
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'));
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>'],
73 $menu_link_content->save();
74 // Confirm the menu block does include menu section when the menu is not
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');
83 // Confirm the menu block does not include menu section when the menu is
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');
104 * Tests that blocks with configuration overrides are disabled.
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.',
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);
123 // Confirm the block Settings Tray functionality is disabled when block is
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();
130 $this->drupalGet('user');
131 $this->assertOverriddenBlockDisabled($overridden_block, 'Now this will be the label.');
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',
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);
148 * Asserts that an overridden block has Settings Tray disabled.
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.
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'));
166 // Confirm the block is not marked as Settings Tray editable.
167 $this->assertFalse($page->find('css', $block_selector)
168 ->hasAttribute('data-drupal-settingstray'));
170 // Confirm the text is actually overridden.
171 $web_assert->elementContains('css', $this->getBlockSelector($overridden_block), $override_text);