View source
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block\Entity\Block;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
class OverriddenConfigurationTest extends SettingsTrayTestBase {
protected static $modules = [
'settings_tray_override_test',
'menu_ui',
'menu_link_content',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$user = $this
->createUser([
'administer blocks',
'access contextual links',
'access toolbar',
]);
$this
->drupalLogin($user);
}
public function testOverriddenConfigurationRemoved() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->grantPermissions(Role::load(Role::AUTHENTICATED_ID), [
'administer site configuration',
'administer menu',
]);
$branding_block = $this
->placeBlock('system_branding_block');
$this
->drupalGet('user');
$this
->enableEditMode();
$this
->openBlockForm($this
->getBlockSelector($branding_block));
$web_assert
->fieldExists('settings[site_information][site_name]');
$this->container
->get('state')
->set('settings_tray_override_test.site_name', TRUE);
$this
->drupalGet('user');
$this
->openBlockForm($this
->getBlockSelector($branding_block));
$web_assert
->fieldNotExists('settings[site_information][site_name]');
$page
->pressButton('Save Site branding');
$this
->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
$web_assert
->assertWaitOnAjaxRequest();
$this
->assertEquals('Llama Fan Club', \Drupal::configFactory()
->get('system.site')
->get('name'));
$this
->assertEquals('Drupal', \Drupal::configFactory()
->getEditable('system.site')
->get('name'));
$menu_link_content = MenuLinkContent::create([
'title' => 'This is on the menu',
'menu_name' => 'main',
'link' => [
'uri' => 'route:<front>',
],
]);
$menu_link_content
->save();
$menu_block = $this
->placeBlock('system_menu_block:main');
$web_assert
->assertWaitOnAjaxRequest();
$this
->drupalGet('user');
$web_assert
->pageTextContains('This is on the menu');
$this
->openBlockForm($this
->getBlockSelector($menu_block));
$web_assert
->elementExists('css', '#menu-overview');
$this->container
->get('state')
->set('settings_tray_override_test.menu', TRUE);
$this
->drupalGet('user');
$web_assert
->pageTextContains('This is on the menu');
$menu_with_overrides = \Drupal::configFactory()
->get('system.menu.main')
->get();
$menu_without_overrides = \Drupal::configFactory()
->getEditable('system.menu.main')
->get();
$this
->openBlockForm($this
->getBlockSelector($menu_block));
$web_assert
->elementNotExists('css', '#menu-overview');
$page
->pressButton('Save Main navigation');
$this
->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
$web_assert
->assertWaitOnAjaxRequest();
$this
->assertEquals('Labely label', \Drupal::configFactory()
->get('system.menu.main')
->get('label'));
$this
->assertEquals('Main navigation', \Drupal::configFactory()
->getEditable('system.menu.main')
->get('label'));
$this
->assertEquals($menu_with_overrides, \Drupal::configFactory()
->get('system.menu.main')
->get());
$this
->assertEquals($menu_without_overrides, \Drupal::configFactory()
->getEditable('system.menu.main')
->get());
$web_assert
->pageTextContains('This is on the menu');
}
public function testOverriddenBlock() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$overridden_block = $this
->placeBlock('system_powered_by_block', [
'id' => 'overridden_block',
'label_display' => 1,
'label' => 'This will be overridden.',
]);
$this
->drupalGet('user');
$block_selector = $this
->getBlockSelector($overridden_block);
$this
->assertEquals('editable', $page
->find('css', $block_selector)
->getAttribute('data-drupal-settingstray'));
$web_assert
->elementContains('css', $block_selector, 'This will be overridden.');
$this
->enableEditMode();
$this
->openBlockForm($block_selector);
$this->container
->get('state')
->set('settings_tray_override_test.block', TRUE);
$overridden_block
->save();
$block_config = \Drupal::configFactory()
->getEditable('block.block.overridden_block');
$block_config
->set('settings', $block_config
->get('settings'))
->save();
$this
->drupalGet('user');
$this
->assertOverriddenBlockDisabled($overridden_block, 'Now this will be the label.');
$block = $this
->placeBlock('system_powered_by_block', [
'label_display' => 1,
'label' => 'Labely label',
]);
$this
->drupalGet('user');
$block_selector = $this
->getBlockSelector($block);
$this
->assertEquals('editable', $page
->find('css', $block_selector)
->getAttribute('data-drupal-settingstray'));
$web_assert
->elementContains('css', $block_selector, 'Labely label');
$this
->openBlockForm($block_selector);
}
protected function assertOverriddenBlockDisabled(Block $overridden_block, string $override_text) : void {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$block_selector = $this
->getBlockSelector($overridden_block);
$block_id = $overridden_block
->id();
$contextual_links = $page
->findAll('css', "{$block_selector} .contextual-links li a");
$this
->assertNotEmpty($contextual_links);
foreach ($contextual_links as $link) {
$this
->assertStringNotContainsString("/admin/structure/block/manage/{$block_id}/off-canvas", $link
->getAttribute('href'));
}
$this
->assertFalse($page
->find('css', $block_selector)
->hasAttribute('data-drupal-settingstray'));
$web_assert
->elementContains('css', $this
->getBlockSelector($overridden_block), $override_text);
}
}