updated core to 8.6.1 via composer

This commit is contained in:
2018-09-12 13:58:26 +02:00
parent a9a219f2ed
commit ea56b9fba3
4443 changed files with 112098 additions and 40708 deletions
@@ -8,8 +8,10 @@
(($, Drupal) => {
const blockConfigureSelector = '[data-settings-tray-edit]';
const toggleEditSelector = '[data-drupal-settingstray="toggle"]';
const itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button';
const contextualItemsSelector = '[data-contextual-id] a, [data-contextual-id] button';
const itemsToToggleSelector =
'[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button';
const contextualItemsSelector =
'[data-contextual-id] a, [data-contextual-id] button';
const quickEditItemSelector = '[data-quickedit-entity-id]';
/**
@@ -67,7 +69,9 @@
*/
function setEditModeState(editMode) {
if (!document.querySelector('[data-off-canvas-main-canvas]')) {
throw new Error('data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig');
throw new Error(
'data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig',
);
}
editMode = !!editMode;
const $editButton = $(toggleEditSelector);
@@ -77,47 +81,74 @@
$editButton.text(Drupal.t('Editing'));
closeToolbarTrays();
$editables = $('[data-drupal-settingstray="editable"]').once('settingstray');
$editables = $('[data-drupal-settingstray="editable"]').once(
'settingstray',
);
if ($editables.length) {
// Use event capture to prevent clicks on links.
document.querySelector('[data-off-canvas-main-canvas]').addEventListener('click', preventClick, true);
document
.querySelector('[data-off-canvas-main-canvas]')
.addEventListener('click', preventClick, true);
/**
* When a click occurs try and find the settings-tray edit link
* and click it.
*/
$editables
.not(contextualItemsSelector)
.on('click.settingstray', (e) => {
// Contextual links are allowed to function in Edit mode.
if ($(e.target).closest('.contextual').length || !localStorage.getItem('Drupal.contextualToolbar.isViewing')) {
return;
}
$(e.currentTarget).find(blockConfigureSelector).trigger('click');
disableQuickEdit();
});
$editables.not(contextualItemsSelector).on('click.settingstray', e => {
// Contextual links are allowed to function in Edit mode.
if (
$(e.target).closest('.contextual').length ||
!localStorage.getItem('Drupal.contextualToolbar.isViewing')
) {
return;
}
$(e.currentTarget)
.find(blockConfigureSelector)
.trigger('click');
disableQuickEdit();
});
$(quickEditItemSelector)
.not(contextualItemsSelector)
.on('click.settingstray', (e) => {
.on('click.settingstray', e => {
/**
* For all non-contextual links or the contextual QuickEdit link
* close the off-canvas dialog.
*/
if (!$(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
if (
!$(e.target)
.parent()
.hasClass('contextual') ||
$(e.target)
.parent()
.hasClass('quickedit')
) {
closeOffCanvas();
}
// Do not trigger if target is quick edit link to avoid loop.
if ($(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
if (
$(e.target)
.parent()
.hasClass('contextual') ||
$(e.target)
.parent()
.hasClass('quickedit')
) {
return;
}
$(e.currentTarget).find('li.quickedit a').trigger('click');
$(e.currentTarget)
.find('li.quickedit a')
.trigger('click');
});
}
}
// Disable edit mode.
else {
$editables = $('[data-drupal-settingstray="editable"]').removeOnce('settingstray');
$editables = $('[data-drupal-settingstray="editable"]').removeOnce(
'settingstray',
);
if ($editables.length) {
document.querySelector('[data-off-canvas-main-canvas]').removeEventListener('click', preventClick, true);
document
.querySelector('[data-off-canvas-main-canvas]')
.removeEventListener('click', preventClick, true);
$editables.off('.settingstray');
$(quickEditItemSelector).off('.settingstray');
}
@@ -159,17 +190,25 @@
* If there is an element and the renderer is 'off_canvas' then we want
* to add our changes.
*/
.filter(instance => instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas')
.filter(
instance =>
instance &&
$(instance.element).attr('data-dialog-renderer') === 'off_canvas',
)
/**
* Loop through all Ajax instances that use the 'off_canvas' renderer to
* set active editable ID.
*/
.forEach((instance) => {
.forEach(instance => {
// Check to make sure existing dialogOptions aren't overridden.
if (!instance.options.data.hasOwnProperty('dialogOptions')) {
instance.options.data.dialogOptions = {};
}
instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id');
instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(
instance.element,
)
.parents('.settings-tray-editable')
.attr('id');
instance.progress = { type: 'fullscreen' };
});
}
@@ -192,36 +231,39 @@
prepareAjaxLinks();
// When the first contextual link is added to the page set Edit Mode.
$('body').once('settings_tray.edit_mode_init').each(() => {
const editMode = localStorage.getItem('Drupal.contextualToolbar.isViewing') === 'false';
if (editMode) {
setEditModeState(true);
}
});
$('body')
.once('settings_tray.edit_mode_init')
.each(() => {
const editMode =
localStorage.getItem('Drupal.contextualToolbar.isViewing') ===
'false';
if (editMode) {
setEditModeState(true);
}
});
/**
* Bind a listener to all 'Quick edit' links for blocks. Click "Edit"
* button in toolbar to force Contextual Edit which starts Settings Tray
* edit mode also.
*/
data.$el.find(blockConfigureSelector)
.on('click.settingstray', () => {
if (!isInEditMode()) {
$(toggleEditSelector).trigger('click').trigger('click.settings_tray');
}
/**
* Always disable QuickEdit regardless of whether "EditMode" was just
* enabled.
*/
disableQuickEdit();
});
data.$el.find(blockConfigureSelector).on('click.settingstray', () => {
if (!isInEditMode()) {
$(toggleEditSelector)
.trigger('click')
.trigger('click.settings_tray');
}
/**
* Always disable QuickEdit regardless of whether "EditMode" was just
* enabled.
*/
disableQuickEdit();
});
});
$(document).on('keyup.settingstray', (e) => {
$(document).on('keyup.settingstray', e => {
if (isInEditMode() && e.keyCode === 27) {
Drupal.announce(
Drupal.t('Exited edit mode.'),
);
Drupal.announce(Drupal.t('Exited edit mode.'));
toggleEditMode();
}
});
@@ -237,7 +279,9 @@
*/
Drupal.behaviors.toggleEditMode = {
attach() {
$(toggleEditSelector).once('settingstray').on('click.settingstray', toggleEditMode);
$(toggleEditSelector)
.once('settingstray')
.on('click.settingstray', toggleEditMode);
},
};
@@ -245,7 +289,9 @@
$(window).on({
'dialog:beforecreate': (event, dialog, $element, settings) => {
if ($element.is('#drupal-off-canvas')) {
$('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');
$('body .settings-tray-active-editable').removeClass(
'settings-tray-active-editable',
);
const $activeElement = $(`#${settings.settingsTrayActiveEditableId}`);
if ($activeElement.length) {
$activeElement.addClass('settings-tray-active-editable');
@@ -254,7 +300,9 @@
},
'dialog:beforeclose': (event, dialog, $element) => {
if ($element.is('#drupal-off-canvas')) {
$('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');
$('body .settings-tray-active-editable').removeClass(
'settings-tray-active-editable',
);
}
},
});
@@ -5,6 +5,6 @@ package: Core
version: VERSION
core: 8.x
dependencies:
- block
- toolbar
- contextual
- drupal:block
- drupal:toolbar
- drupal:contextual
@@ -5,9 +5,9 @@ namespace Drupal\settings_tray\Block;
use Drupal\block\BlockForm;
use Drupal\block\BlockInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
@@ -23,6 +23,8 @@ use Drupal\Core\Url;
*/
class BlockEntitySettingTrayForm extends BlockForm {
use AjaxFormHelperTrait;
/**
* Provides a title callback to get the block's admin label.
*
@@ -122,17 +124,17 @@ class BlockEntitySettingTrayForm extends BlockForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => '::submitFormDialog',
'callback' => '::ajaxSubmit',
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
// static::submitFormDialog() requires data-drupal-selector to be the same
// between the various Ajax requests. A bug in
// \Drupal\Core\Form\FormBuilder prevents that from happening unless
// $form['#id'] is also the same. Normally, #id is set to a unique HTML ID
// via Html::getUniqueId(), but here we bypass that in order to work around
// the data-drupal-selector bug. This is okay so long as we assume that this
// form only ever occurs once on a page.
// static::ajaxSubmit() requires data-drupal-selector to be the same between
// the various Ajax requests. A bug in \Drupal\Core\Form\FormBuilder
// prevents that from happening unless $form['#id'] is also the same.
// Normally, #id is set to a unique HTML ID via Html::getUniqueId(), but
// here we bypass that in order to work around the data-drupal-selector bug.
// This is okay so long as we assume that this form only ever occurs once on
// a page.
// @todo Remove this workaround once https://www.drupal.org/node/2897377 is
// fixed.
$form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
@@ -141,38 +143,17 @@ class BlockEntitySettingTrayForm extends BlockForm {
}
/**
* Submit form dialog #ajax callback.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AJAX response that display validation error messages or redirects
* to a URL
*
* @todo Repalce this callback with generic trait in
* https://www.drupal.org/node/2896535.
* {@inheritdoc}
*/
public function submitFormDialog(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state->hasAnyErrors()) {
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -1000,
];
$command = new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form);
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
if ($redirect_url = $this->getRedirectUrl()) {
$command = new RedirectCommand($redirect_url->setAbsolute()->toString());
}
else {
if ($redirect_url = $this->getRedirectUrl()) {
$command = new RedirectCommand($redirect_url->setAbsolute()->toString());
}
else {
// Settings Tray always provides a destination.
throw new \Exception("No destination provided by Settings Tray form");
}
// Settings Tray always provides a destination.
throw new \Exception("No destination provided by Settings Tray form");
}
$response = new AjaxResponse();
return $response->addCommand($command);
}
@@ -4,4 +4,4 @@ package: Testing
version: VERSION
core: 8.x
dependencies:
- settings_tray
- drupal:settings_tray
@@ -5,5 +5,5 @@ package: Testing
version: VERSION
core: 8.x
dependencies:
- block
- settings_tray
- drupal:block
- drupal:settings_tray
@@ -5,4 +5,4 @@ package: Testing
version: VERSION
core: 8.x
dependencies:
- settings_tray
- drupal:settings_tray
@@ -0,0 +1,103 @@
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
/**
* Tests handling of configuration overrides.
*
* @group settings_tray
*/
class ConfigAccessTest extends SettingsTrayTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'menu_link_content',
'menu_ui',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$user = $this->createUser([
'administer blocks',
'access contextual links',
'access toolbar',
]);
$this->drupalLogin($user);
}
/**
* Tests access to block forms with related configuration is correct.
*/
public function testBlockConfigAccess() {
$page = $this->getSession()->getPage();
$web_assert = $this->assertSession();
// Confirm that System Branding block does not expose Site Name field
// without permission.
$block = $this->placeBlock('system_branding_block');
$this->drupalGet('user');
$this->enableEditMode();
$this->openBlockForm($this->getBlockSelector($block));
// The site name field should not appear because the user doesn't have
// permission.
$web_assert->fieldNotExists('settings[site_information][site_name]');
$page->pressButton('Save Site branding');
$this->waitForOffCanvasToClose();
$this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
$web_assert->assertWaitOnAjaxRequest();
// Confirm we did not save changes to the configuration.
$this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration']);
$this->drupalGet('user');
$this->openBlockForm($this->getBlockSelector($block));
// The site name field should appear because the user does have permission.
$web_assert->fieldExists('settings[site_information][site_name]');
// Confirm that the Menu block does not expose menu configuration without
// permission.
// Add a link or the menu will not render.
$menu_link_content = MenuLinkContent::create([
'title' => 'This is on the menu',
'menu_name' => 'main',
'link' => ['uri' => 'route:<front>'],
]);
$menu_link_content->save();
$this->assertNotEmpty($menu_link_content->isEnabled());
$menu_without_overrides = \Drupal::configFactory()->getEditable('system.menu.main')->get();
$block = $this->placeBlock('system_menu_block:main');
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->openBlockForm($this->getBlockSelector($block));
// Edit menu form should not appear because the user doesn't have
// permission.
$web_assert->pageTextNotContains('Edit menu');
$page->pressButton('Save Main navigation');
$this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
$web_assert->assertWaitOnAjaxRequest();
// Confirm we did not save changes to the menu or the menu link.
$this->assertEquals($menu_without_overrides, \Drupal::configFactory()->getEditable('system.menu.main')->get());
$menu_link_content = MenuLinkContent::load($menu_link_content->id());
$this->assertNotEmpty($menu_link_content->isEnabled());
// Confirm menu is still on the page.
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer menu']);
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->openBlockForm($this->getBlockSelector($block));
// Edit menu form should appear because the user does have permission.
$web_assert->pageTextContains('Edit menu');
}
}
@@ -0,0 +1,174 @@
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block\Entity\Block;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\Entity\Role;
/**
* Tests handling of configuration overrides.
*
* @group settings_tray
*/
class OverriddenConfigurationTest extends SettingsTrayTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'settings_tray_override_test',
'menu_ui',
'menu_link_content',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$user = $this->createUser([
'administer blocks',
'access contextual links',
'access toolbar',
]);
$this->drupalLogin($user);
}
/**
* Test blocks with overridden related configuration removed when overridden.
*/
public function testOverriddenConfigurationRemoved() {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration', 'administer menu']);
// Confirm the branding block does include 'site_information' section when
// the site name is not overridden.
$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]');
// Confirm the branding block does not include 'site_information' section
// when the site name is overridden.
$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();
// Confirm we did not save changes to the configuration.
$this->assertEquals('Llama Fan Club', \Drupal::configFactory()->get('system.site')->get('name'));
$this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
// Add a link or the menu will not render.
$menu_link_content = MenuLinkContent::create([
'title' => 'This is on the menu',
'menu_name' => 'main',
'link' => ['uri' => 'route:<front>'],
]);
$menu_link_content->save();
// Confirm the menu block does include menu section when the menu is not
// overridden.
$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');
// Confirm the menu block does not include menu section when the menu is
// overridden.
$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();
// Confirm we did not save changes to the configuration.
$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');
}
/**
* Tests that blocks with configuration overrides are disabled.
*/
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);
// Confirm the block is marked as Settings Tray editable.
$this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
// Confirm the label is not overridden.
$web_assert->elementContains('css', $block_selector, 'This will be overridden.');
$this->enableEditMode();
$this->openBlockForm($block_selector);
// Confirm the block Settings Tray functionality is disabled when block is
// overridden.
$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.');
// Test a non-overridden block does show the form in the off-canvas dialog.
$block = $this->placeBlock('system_powered_by_block', [
'label_display' => 1,
'label' => 'Labely label',
]);
$this->drupalGet('user');
$block_selector = $this->getBlockSelector($block);
// Confirm the block is marked as Settings Tray editable.
$this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
// Confirm the label is not overridden.
$web_assert->elementContains('css', $block_selector, 'Labely label');
$this->openBlockForm($block_selector);
}
/**
* Asserts that an overridden block has Settings Tray disabled.
*
* @param \Drupal\block\Entity\Block $overridden_block
* The overridden block.
* @param string $override_text
* The override text that should appear in the block.
*/
protected function assertOverriddenBlockDisabled(Block $overridden_block, $override_text) {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$block_selector = $this->getBlockSelector($overridden_block);
$block_id = $overridden_block->id();
// Confirm the block does not have a quick edit link.
$contextual_links = $page->findAll('css', "$block_selector .contextual-links li a");
$this->assertNotEmpty($contextual_links);
foreach ($contextual_links as $link) {
$this->assertNotContains("/admin/structure/block/manage/$block_id/off-canvas", $link->getAttribute('href'));
}
// Confirm the block is not marked as Settings Tray editable.
$this->assertFalse($page->find('css', $block_selector)
->hasAttribute('data-drupal-settingstray'));
// Confirm the text is actually overridden.
$web_assert->elementContains('css', $this->getBlockSelector($overridden_block), $override_text);
}
}
@@ -0,0 +1,206 @@
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\user\Entity\Role;
/**
* Test Settings Tray and Quick Edit modules integration.
*
* @group settings_tray
*/
class QuickEditIntegrationTest extends SettingsTrayTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'node',
'block_content',
'quickedit',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$user = $this->createUser([
'administer blocks',
'access contextual links',
'access toolbar',
'administer nodes',
'access in-place editing',
]);
$this->drupalLogin($user);
}
/**
* Tests QuickEdit links behavior.
*/
public function testQuickEditLinks() {
$quick_edit_selector = '#quickedit-entity-toolbar';
$node_selector = '[data-quickedit-entity-id="node/1"]';
$body_selector = '[data-quickedit-field-id="node/1/body/en/full"]';
$web_assert = $this->assertSession();
// Create a Content type and two test nodes.
$this->createContentType(['type' => 'page']);
$auth_role = Role::load(Role::AUTHENTICATED_ID);
$this->grantPermissions($auth_role, [
'edit any page content',
'access content',
]);
$node = $this->createNode(
[
'title' => 'Page One',
'type' => 'page',
'body' => [
[
'value' => 'Regular NODE body for the test.',
'format' => 'plain_text',
],
],
]
);
$page = $this->getSession()->getPage();
$block_plugin = 'system_powered_by_block';
foreach ($this->getTestThemes() as $theme) {
$this->enableTheme($theme);
$block = $this->placeBlock($block_plugin);
$block_selector = $this->getBlockSelector($block);
// Load the same page twice.
foreach ([1, 2] as $page_load_times) {
$this->drupalGet('node/' . $node->id());
// The 2nd page load we should already be in edit mode.
if ($page_load_times == 1) {
$this->enableEditMode();
}
// In Edit mode clicking field should open QuickEdit toolbar.
$page->find('css', $body_selector)->click();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this->disableEditMode();
// Exiting Edit mode should close QuickEdit toolbar.
$web_assert->elementNotExists('css', $quick_edit_selector);
// When not in Edit mode QuickEdit toolbar should not open.
$page->find('css', $body_selector)->click();
$web_assert->elementNotExists('css', $quick_edit_selector);
$this->enableEditMode();
$this->openBlockForm($block_selector);
$page->find('css', $body_selector)->click();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
// Off-canvas dialog should be closed when opening QuickEdit toolbar.
$this->waitForOffCanvasToClose();
$this->openBlockForm($block_selector);
// QuickEdit toolbar should be closed when opening Off-canvas dialog.
$web_assert->elementNotExists('css', $quick_edit_selector);
}
// Check using contextual links to invoke QuickEdit and open the tray.
$this->drupalGet('node/' . $node->id());
$web_assert->assertWaitOnAjaxRequest();
$this->disableEditMode();
// Open QuickEdit toolbar before going into Edit mode.
$this->clickContextualLink($node_selector, "Quick edit");
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
// Open off-canvas and enter Edit mode via contextual link.
$this->clickContextualLink($block_selector, "Quick edit");
$this->waitForOffCanvasToOpen();
// QuickEdit toolbar should be closed when opening off-canvas dialog.
$web_assert->elementNotExists('css', $quick_edit_selector);
// Open QuickEdit toolbar via contextual link while in Edit mode.
$this->clickContextualLink($node_selector, "Quick edit", FALSE);
$this->waitForOffCanvasToClose();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this->disableEditMode();
}
}
/**
* Tests that contextual links in custom blocks are changed.
*
* "Quick edit" is quickedit.module link.
* "Quick edit settings" is settings_tray.module link.
*/
public function testCustomBlockLinks() {
$this->createBlockContentType('basic', TRUE);
$block_content = $this->createBlockContent('Custom Block', 'basic', TRUE);
$this->placeBlock('block_content:' . $block_content->uuid(), ['id' => 'custom']);
$this->drupalGet('user');
$page = $this->getSession()->getPage();
$this->toggleContextualTriggerVisibility('#block-custom');
$page->find('css', '#block-custom .contextual button')->press();
$links = $page->findAll('css', "#block-custom .contextual-links li a");
$link_labels = [];
/** @var \Behat\Mink\Element\NodeElement $link */
foreach ($links as $link) {
$link_labels[$link->getAttribute('href')] = $link->getText();
}
$href = array_search('Quick edit', $link_labels);
$this->assertEquals('', $href);
$href = array_search('Quick edit settings', $link_labels);
$this->assertTrue(strstr($href, '/admin/structure/block/manage/custom/settings-tray?destination=user/2') !== FALSE);
}
/**
* Creates a custom block.
*
* @param bool|string $title
* (optional) Title of block. When no value is given uses a random name.
* Defaults to FALSE.
* @param string $bundle
* (optional) Bundle name. Defaults to 'basic'.
* @param bool $save
* (optional) Whether to save the block. Defaults to TRUE.
*
* @return \Drupal\block_content\Entity\BlockContent
* Created custom block.
*/
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = $title ?: $this->randomName();
$block_content = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
]);
if ($block_content && $save === TRUE) {
$block_content->save();
}
return $block_content;
}
/**
* Creates a custom block type (bundle).
*
* @param string $label
* The block type label.
* @param bool $create_body
* Whether or not to create the body field.
*
* @return \Drupal\block_content\Entity\BlockContentType
* Created custom block type.
*/
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE,
]);
$bundle->save();
if ($create_body) {
block_content_add_body_field($bundle->id());
}
return $bundle;
}
}
@@ -2,51 +2,24 @@
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\settings_tray_test\Plugin\Block\SettingsTrayFormAnnotationIsClassBlock;
use Drupal\settings_tray_test\Plugin\Block\SettingsTrayFormAnnotationNoneBlock;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Testing opening and saving block forms in the off-canvas dialog.
*
* @group settings_tray
*/
class SettingsTrayBlockFormTest extends OffCanvasTestBase {
use ContextualLinkClickTrait;
const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';
const LABEL_INPUT_SELECTOR = 'input[data-drupal-selector="edit-settings-label"]';
class SettingsTrayBlockFormTest extends SettingsTrayTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'node',
'block',
'system',
'breakpoint',
'toolbar',
'contextual',
'settings_tray',
'search',
'block_content',
'settings_tray_test',
// Add test module to override CSS pointer-events properties because they
// cause test failures.
'settings_tray_test_css',
'settings_tray_test',
'settings_tray_override_test',
'menu_ui',
'menu_link_content',
];
/**
@@ -55,8 +28,6 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
protected function setUp() {
parent::setUp();
$this->createBlockContentType('basic', TRUE);
$block_content = $this->createBlockContent('Custom Block', 'basic', TRUE);
$user = $this->createUser([
'administer blocks',
'access contextual links',
@@ -65,19 +36,29 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
'search content',
]);
$this->drupalLogin($user);
$this->placeBlock('block_content:' . $block_content->uuid(), ['id' => 'custom']);
}
/**
* Tests opening off-canvas dialog by click blocks and elements in the blocks.
*
* @dataProvider providerTestBlocks
*/
public function testBlocks($theme, $block_plugin, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item, $permissions) {
public function testBlocks() {
foreach ($this->getBlockTests() as $test) {
call_user_func_array([$this, 'doTestBlocks'], $test);
}
}
/**
* Tests opening off-canvas dialog by click blocks and elements in the blocks.
*/
protected function doTestBlocks($theme, $block_plugin, $new_page_text, $element_selector, $label_selector, $button_text, $toolbar_item, $permissions) {
if ($permissions) {
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), $permissions);
}
if ($new_page_text) {
// Some asserts can be based on this value, so it should not be the same
// for different blocks, because it can be saved in the site config.
$new_page_text = $new_page_text . ' ' . $theme . ' ' . $block_plugin;
}
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$this->enableTheme($theme);
@@ -86,8 +67,8 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
$block_id = $block->id();
$this->drupalGet('user');
$link = $page->find('css', "$block_selector .contextual-links li a");
$this->assertEquals('Quick edit', $link->getText(), "'Quick edit' is the first contextual link for the block.");
$link = $web_assert->waitForElement('css', "$block_selector .contextual-links li a");
$this->assertEquals('Quick edit', $link->getHtml(), "'Quick edit' is the first contextual link for the block.");
$this->assertContains("/admin/structure/block/manage/$block_id/settings-tray?destination=user/2", $link->getAttribute('href'));
if (isset($toolbar_item)) {
@@ -157,16 +138,24 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
$this->getSession()->executeScript('jQuery("body").trigger(jQuery.Event("keyup", { keyCode: 27 }));');
$this->waitForOffCanvasToClose();
$this->getSession()->wait(100);
$this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
$this->getSession()->getPage()->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
$this->assertEditModeDisabled();
$web_assert->elementTextContains('css', '#drupal-live-announce', 'Exited edit mode.');
$web_assert->elementTextNotContains('css', '.contextual-toolbar-tab button', 'Editing');
$this->assertNotEmpty($web_assert->waitForElement('css', '#drupal-live-announce:contains(Exited edit mode)'));
$this->waitForNoElement('.contextual-toolbar-tab button:contains(Editing)');
$web_assert->elementAttributeNotContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
// Clean up test data so each test does not impact the next.
$block->delete();
if ($permissions) {
user_role_revoke_permissions(Role::AUTHENTICATED_ID, $permissions);
}
}
/**
* Dataprovider for testBlocks().
* Creates tests for ::testBlocks().
*/
public function providerTestBlocks() {
public function getBlockTests() {
$blocks = [];
foreach ($this->getTestThemes() as $theme) {
$blocks += [
@@ -230,160 +219,6 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
return $blocks;
}
/**
* Enables edit mode by pressing edit button in the toolbar.
*/
protected function enableEditMode() {
$this->pressToolbarEditButton();
$this->assertEditModeEnabled();
}
/**
* Disables edit mode by pressing edit button in the toolbar.
*/
protected function disableEditMode() {
$this->assertSession()->assertWaitOnAjaxRequest();
$this->pressToolbarEditButton();
$this->assertEditModeDisabled();
}
/**
* Asserts that Off-Canvas block form is valid.
*/
protected function assertOffCanvasBlockFormIsValid() {
$web_assert = $this->assertSession();
// Confirm that Block title display label has been changed.
$web_assert->elementTextContains('css', '.form-item-settings-label-display label', 'Display block title');
// Confirm Block title label is shown if checkbox is checked.
if ($this->getSession()->getPage()->find('css', 'input[name="settings[label_display]"]')->isChecked()) {
$this->assertEquals($this->isLabelInputVisible(), TRUE, 'Label is visible');
$web_assert->elementTextContains('css', '.form-item-settings-label label', 'Block title');
}
else {
$this->assertEquals($this->isLabelInputVisible(), FALSE, 'Label is not visible');
}
// Check that common block form elements exist.
$web_assert->elementExists('css', static::LABEL_INPUT_SELECTOR);
$web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]');
// Check that advanced block form elements do not exist.
$web_assert->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]');
$web_assert->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');
}
/**
* Open block form by clicking the element found with a css selector.
*
* @param string $block_selector
* A css selector selects the block or an element within it.
* @param string $contextual_link_container
* The element that contains the contextual links. If none provide the
* $block_selector will be used.
*/
protected function openBlockForm($block_selector, $contextual_link_container = '') {
if (!$contextual_link_container) {
$contextual_link_container = $block_selector;
}
// Ensure that contextual link element is present because this is required
// to open the off-canvas dialog in edit mode.
$contextual_link = $this->assertSession()->waitForElement('css', "$contextual_link_container .contextual-links a");
$this->assertNotEmpty($contextual_link);
// When page first loads Edit Mode is not triggered until first contextual
// link is added.
$this->assertElementVisibleAfterWait('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
// Ensure that all other Ajax activity is completed.
$this->assertSession()->assertWaitOnAjaxRequest();
$this->click($block_selector);
$this->waitForOffCanvasToOpen();
$this->assertOffCanvasBlockFormIsValid();
}
/**
* Tests QuickEdit links behavior.
*/
public function testQuickEditLinks() {
$this->container->get('module_installer')->install(['quickedit']);
$this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), ['access in-place editing']);
$quick_edit_selector = '#quickedit-entity-toolbar';
$node_selector = '[data-quickedit-entity-id="node/1"]';
$body_selector = '[data-quickedit-field-id="node/1/body/en/full"]';
$web_assert = $this->assertSession();
// Create a Content type and two test nodes.
$this->createContentType(['type' => 'page']);
$auth_role = Role::load(Role::AUTHENTICATED_ID);
$this->grantPermissions($auth_role, [
'edit any page content',
'access content',
]);
$node = $this->createNode(
[
'title' => 'Page One',
'type' => 'page',
'body' => [
[
'value' => 'Regular NODE body for the test.',
'format' => 'plain_text',
],
],
]
);
$page = $this->getSession()->getPage();
$block_plugin = 'system_powered_by_block';
foreach ($this->getTestThemes() as $theme) {
$this->enableTheme($theme);
$block = $this->placeBlock($block_plugin);
$block_selector = $this->getBlockSelector($block);
// Load the same page twice.
foreach ([1, 2] as $page_load_times) {
$this->drupalGet('node/' . $node->id());
// The 2nd page load we should already be in edit mode.
if ($page_load_times == 1) {
$this->enableEditMode();
}
// In Edit mode clicking field should open QuickEdit toolbar.
$page->find('css', $body_selector)->click();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this->disableEditMode();
// Exiting Edit mode should close QuickEdit toolbar.
$web_assert->elementNotExists('css', $quick_edit_selector);
// When not in Edit mode QuickEdit toolbar should not open.
$page->find('css', $body_selector)->click();
$web_assert->elementNotExists('css', $quick_edit_selector);
$this->enableEditMode();
$this->openBlockForm($block_selector);
$page->find('css', $body_selector)->click();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
// Off-canvas dialog should be closed when opening QuickEdit toolbar.
$this->waitForOffCanvasToClose();
$this->openBlockForm($block_selector);
// QuickEdit toolbar should be closed when opening Off-canvas dialog.
$web_assert->elementNotExists('css', $quick_edit_selector);
}
// Check using contextual links to invoke QuickEdit and open the tray.
$this->drupalGet('node/' . $node->id());
$web_assert->assertWaitOnAjaxRequest();
$this->disableEditMode();
// Open QuickEdit toolbar before going into Edit mode.
$this->clickContextualLink($node_selector, "Quick edit");
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
// Open off-canvas and enter Edit mode via contextual link.
$this->clickContextualLink($block_selector, "Quick edit");
$this->waitForOffCanvasToOpen();
// QuickEdit toolbar should be closed when opening off-canvas dialog.
$web_assert->elementNotExists('css', $quick_edit_selector);
// Open QuickEdit toolbar via contextual link while in Edit mode.
$this->clickContextualLink($node_selector, "Quick edit", FALSE);
$this->waitForOffCanvasToClose();
$this->assertElementVisibleAfterWait('css', $quick_edit_selector);
$this->disableEditMode();
}
}
/**
* Tests enabling and disabling Edit Mode.
*/
@@ -419,212 +254,6 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
}
}
/**
* Assert that edit mode has been properly enabled.
*/
protected function assertEditModeEnabled() {
$web_assert = $this->assertSession();
// No contextual triggers should be hidden.
$web_assert->elementNotExists('css', '.contextual .trigger.visually-hidden');
// The toolbar edit button should read "Editing".
$web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
// The main canvas element should have the "js-settings-tray-edit-mode" class.
$web_assert->elementExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
/**
* Assert that edit mode has been properly disabled.
*/
protected function assertEditModeDisabled() {
$web_assert = $this->assertSession();
// Contextual triggers should be hidden.
$web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
// No contextual triggers should be not hidden.
$web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
// The toolbar edit button should read "Edit".
$web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
// The main canvas element should NOT have the "js-settings-tray-edit-mode"
// class.
$web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
/**
* Press the toolbar Edit button provided by the contextual module.
*/
protected function pressToolbarEditButton() {
$this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
$edit_button = $this->getSession()
->getPage()
->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);
$edit_button->press();
}
/**
* Creates a custom block.
*
* @param bool|string $title
* (optional) Title of block. When no value is given uses a random name.
* Defaults to FALSE.
* @param string $bundle
* (optional) Bundle name. Defaults to 'basic'.
* @param bool $save
* (optional) Whether to save the block. Defaults to TRUE.
*
* @return \Drupal\block_content\Entity\BlockContent
* Created custom block.
*/
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = $title ?: $this->randomName();
$block_content = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
'body' => [
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text',
],
]);
if ($block_content && $save === TRUE) {
$block_content->save();
}
return $block_content;
}
/**
* Creates a custom block type (bundle).
*
* @param string $label
* The block type label.
* @param bool $create_body
* Whether or not to create the body field.
*
* @return \Drupal\block_content\Entity\BlockContentType
* Created custom block type.
*/
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE,
]);
$bundle->save();
if ($create_body) {
block_content_add_body_field($bundle->id());
}
return $bundle;
}
/**
* Tests that contextual links in custom blocks are changed.
*
* "Quick edit" is quickedit.module link.
* "Quick edit settings" is settings_tray.module link.
*/
public function testCustomBlockLinks() {
$this->container->get('module_installer')->install(['quickedit']);
$this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), ['access in-place editing']);
$this->drupalGet('user');
$page = $this->getSession()->getPage();
$links = $page->findAll('css', "#block-custom .contextual-links li a");
$link_labels = [];
/** @var \Behat\Mink\Element\NodeElement $link */
foreach ($links as $link) {
$link_labels[$link->getAttribute('href')] = $link->getText();
}
$href = array_search('Quick edit', $link_labels);
$this->assertEquals('', $href);
$href = array_search('Quick edit settings', $link_labels);
$this->assertTrue(strstr($href, '/admin/structure/block/manage/custom/settings-tray?destination=user/2') !== FALSE);
}
/**
* Gets the block CSS selector.
*
* @param \Drupal\block\Entity\Block $block
* The block.
*
* @return string
* The CSS selector.
*/
public function getBlockSelector(Block $block) {
return '#block-' . str_replace('_', '-', $block->id());
}
/**
* Determines if the label input is visible.
*
* @return bool
* TRUE if the label is visible, FALSE if it is not.
*/
protected function isLabelInputVisible() {
return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible();
}
/**
* Tests access to block forms with related configuration is correct.
*/
public function testBlockConfigAccess() {
$page = $this->getSession()->getPage();
$web_assert = $this->assertSession();
// Confirm that System Branding block does not expose Site Name field
// without permission.
$block = $this->placeBlock('system_branding_block');
$this->drupalGet('user');
$this->enableEditMode();
$this->openBlockForm($this->getBlockSelector($block));
// The site name field should not appear because the user doesn't have
// permission.
$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();
// Confirm we did not save changes to the configuration.
$this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration']);
$this->drupalGet('user');
$this->openBlockForm($this->getBlockSelector($block));
// The site name field should appear because the user does have permission.
$web_assert->fieldExists('settings[site_information][site_name]');
// Confirm that the Menu block does not expose menu configuration without
// permission.
// Add a link or the menu will not render.
$menu_link_content = MenuLinkContent::create([
'title' => 'This is on the menu',
'menu_name' => 'main',
'link' => ['uri' => 'route:<front>'],
]);
$menu_link_content->save();
$this->assertNotEmpty($menu_link_content->isEnabled());
$menu_without_overrides = \Drupal::configFactory()->getEditable('system.menu.main')->get();
$block = $this->placeBlock('system_menu_block:main');
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->openBlockForm($this->getBlockSelector($block));
// Edit menu form should not appear because the user doesn't have
// permission.
$web_assert->pageTextNotContains('Edit menu');
$page->pressButton('Save Main navigation');
$this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
$web_assert->assertWaitOnAjaxRequest();
// Confirm we did not save changes to the menu or the menu link.
$this->assertEquals($menu_without_overrides, \Drupal::configFactory()->getEditable('system.menu.main')->get());
$menu_link_content = MenuLinkContent::load($menu_link_content->id());
$this->assertNotEmpty($menu_link_content->isEnabled());
// Confirm menu is still on the page.
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer menu']);
$this->drupalGet('user');
$web_assert->pageTextContains('This is on the menu');
$this->openBlockForm($this->getBlockSelector($block));
// Edit menu form should appear because the user does have permission.
$web_assert->pageTextContains('Edit menu');
}
/**
* Test that validation errors appear in the off-canvas dialog.
*/
@@ -658,138 +287,4 @@ class SettingsTrayBlockFormTest extends OffCanvasTestBase {
});
}
/**
* Tests that blocks with configuration overrides are disabled.
*/
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);
// Confirm the block is marked as Settings Tray editable.
$this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
// Confirm the label is not overridden.
$web_assert->elementContains('css', $block_selector, 'This will be overridden.');
$this->enableEditMode();
$this->openBlockForm($block_selector);
// Confirm the block Settings Tray functionality is disabled when block is
// overridden.
$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.');
// Test a non-overridden block does show the form in the off-canvas dialog.
$block = $this->placeBlock('system_powered_by_block', [
'label_display' => 1,
'label' => 'Labely label',
]);
$this->drupalGet('user');
$block_selector = $this->getBlockSelector($block);
// Confirm the block is marked as Settings Tray editable.
$this->assertEquals('editable', $page->find('css', $block_selector)->getAttribute('data-drupal-settingstray'));
// Confirm the label is not overridden.
$web_assert->elementContains('css', $block_selector, 'Labely label');
$this->openBlockForm($block_selector);
}
/**
* Test blocks with overridden related configuration removed when overridden.
*/
public function testOverriddenConfigurationRemoved() {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['administer site configuration', 'administer menu']);
// Confirm the branding block does include 'site_information' section when
// the site name is not overridden.
$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]');
// Confirm the branding block does not include 'site_information' section
// when the site name is overridden.
$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();
// Confirm we did not save changes to the configuration.
$this->assertEquals('Llama Fan Club', \Drupal::configFactory()->get('system.site')->get('name'));
$this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));
// Add a link or the menu will not render.
$menu_link_content = MenuLinkContent::create([
'title' => 'This is on the menu',
'menu_name' => 'main',
'link' => ['uri' => 'route:<front>'],
]);
$menu_link_content->save();
// Confirm the menu block does include menu section when the menu is not
// overridden.
$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');
// Confirm the menu block does not include menu section when the menu is
// overridden.
$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();
// Confirm we did not save changes to the configuration.
$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');
}
/**
* Asserts that an overridden block has Settings Tray disabled.
*
* @param \Drupal\block\Entity\Block $overridden_block
* The overridden block.
* @param string $override_text
* The override text that should appear in the block.
*/
protected function assertOverriddenBlockDisabled(Block $overridden_block, $override_text) {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$block_selector = $this->getBlockSelector($overridden_block);
$block_id = $overridden_block->id();
// Confirm the block does not have a quick edit link.
$contextual_links = $page->findAll('css', "$block_selector .contextual-links li a");
$this->assertNotEmpty($contextual_links);
foreach ($contextual_links as $link) {
$this->assertNotContains("/admin/structure/block/manage/$block_id/off-canvas", $link->getAttribute('href'));
}
// Confirm the block is not marked as Settings Tray editable.
$this->assertFalse($page->find('css', $block_selector)
->hasAttribute('data-drupal-settingstray'));
// Confirm the text is actually overridden.
$web_assert->elementContains('css', $this->getBlockSelector($overridden_block), $override_text);
}
}
@@ -0,0 +1,189 @@
<?php
namespace Drupal\Tests\settings_tray\FunctionalJavascript;
use Drupal\block\Entity\Block;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
/**
* Base class for Settings Tray tests.
*/
class SettingsTrayTestBase extends OffCanvasTestBase {
use ContextualLinkClickTrait;
/**
* {@inheritdoc}
*/
public static $modules = [
'settings_tray',
// Add test module to override CSS pointer-events properties because they
// cause test failures.
'settings_tray_test_css',
];
const TOOLBAR_EDIT_LINK_SELECTOR = '#toolbar-bar div.contextual-toolbar-tab button';
const LABEL_INPUT_SELECTOR = 'input[data-drupal-selector="edit-settings-label"]';
/**
* Open block form by clicking the element found with a css selector.
*
* @param string $block_selector
* A css selector selects the block or an element within it.
* @param string $contextual_link_container
* The element that contains the contextual links. If none provide the
* $block_selector will be used.
*/
protected function openBlockForm($block_selector, $contextual_link_container = '') {
if (!$contextual_link_container) {
$contextual_link_container = $block_selector;
}
// Ensure that contextual link element is present because this is required
// to open the off-canvas dialog in edit mode.
$contextual_link = $this->assertSession()->waitForElement('css', "$contextual_link_container .contextual-links a");
$this->assertNotEmpty($contextual_link);
// When page first loads Edit Mode is not triggered until first contextual
// link is added.
$this->assertElementVisibleAfterWait('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
// Ensure that all other Ajax activity is completed.
$this->assertSession()->assertWaitOnAjaxRequest();
$block = $this->getSession()->getPage()->find('css', $block_selector);
$block->mouseOver();
$block->click();
$this->waitForOffCanvasToOpen();
$this->assertOffCanvasBlockFormIsValid();
}
/**
* Enables edit mode by pressing edit button in the toolbar.
*/
protected function enableEditMode() {
$this->pressToolbarEditButton();
$this->assertEditModeEnabled();
}
/**
* Disables edit mode by pressing edit button in the toolbar.
*/
protected function disableEditMode() {
$this->assertSession()->assertWaitOnAjaxRequest();
$this->pressToolbarEditButton();
$this->assertEditModeDisabled();
}
/**
* Press the toolbar Edit button provided by the contextual module.
*/
protected function pressToolbarEditButton() {
$this->assertSession()->waitForElement('css', '[data-contextual-id] .contextual-links a');
$edit_button = $this->getSession()
->getPage()
->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR);
$this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
$edit_button->mouseOver();
$edit_button->press();
}
/**
* Assert that edit mode has been properly disabled.
*/
protected function assertEditModeDisabled() {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
$this->getSession()->executeScript("jQuery('[data-quickedit-entity-id]').trigger('mouseleave')");
$page->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
$this->assertTrue($page->waitFor(10, function ($page) {
return !$page->find('css', '.contextual .trigger:not(.visually-hidden)');
}));
// Contextual triggers should be hidden.
$web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
// No contextual triggers should be not hidden.
$web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
// The toolbar edit button should read "Edit".
$web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
// The main canvas element should NOT have the "js-settings-tray-edit-mode"
// class.
$web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
/**
* Assert that edit mode has been properly enabled.
*/
protected function assertEditModeEnabled() {
$web_assert = $this->assertSession();
$page = $this->getSession()->getPage();
// Move the mouse over the toolbar button so that isn't over a contextual
// links area which cause the contextual link to be shown.
$page->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
$this->assertTrue($page->waitFor(10, function ($page) {
return !$page->find('css', '.contextual .trigger.visually-hidden');
}));
// No contextual triggers should be hidden.
$web_assert->elementNotExists('css', '.contextual .trigger.visually-hidden');
// The toolbar edit button should read "Editing".
$web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Editing');
// The main canvas element should have the "js-settings-tray-edit-mode" class.
$web_assert->elementExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
}
/**
* Asserts that Off-Canvas block form is valid.
*/
protected function assertOffCanvasBlockFormIsValid() {
$web_assert = $this->assertSession();
// Confirm that Block title display label has been changed.
$web_assert->elementTextContains('css', '.form-item-settings-label-display label', 'Display block title');
// Confirm Block title label is shown if checkbox is checked.
if ($this->getSession()->getPage()->find('css', 'input[name="settings[label_display]"]')->isChecked()) {
$this->assertEquals($this->isLabelInputVisible(), TRUE, 'Label is visible');
$web_assert->elementTextContains('css', '.form-item-settings-label label', 'Block title');
}
else {
$this->assertEquals($this->isLabelInputVisible(), FALSE, 'Label is not visible');
}
// Check that common block form elements exist.
$web_assert->elementExists('css', static::LABEL_INPUT_SELECTOR);
$web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]');
// Check that advanced block form elements do not exist.
$web_assert->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]');
$web_assert->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');
}
/**
* {@inheritdoc}
*/
protected function getTestThemes() {
// Remove 'seven' theme. Settings Tray "Edit Mode" will not work with
// 'seven' because it removes all contextual links.
return array_filter(parent::getTestThemes(), function ($theme) {
return $theme !== 'seven';
});
}
/**
* Gets the block CSS selector.
*
* @param \Drupal\block\Entity\Block $block
* The block.
*
* @return string
* The CSS selector.
*/
public function getBlockSelector(Block $block) {
return '#block-' . str_replace('_', '-', $block->id());
}
/**
* Determines if the label input is visible.
*
* @return bool
* TRUE if the label is visible, FALSE if it is not.
*/
protected function isLabelInputVisible() {
return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible();
}
}