3 namespace Drupal\Tests\settings_tray\Functional;
5 use Drupal\block\Entity\Block;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests opening and saving block forms in the off-canvas dialog.
11 * @group settings_tray
13 class SettingsTrayTest extends BrowserTestBase {
18 public static $modules = [
24 * Gets the block CSS selector.
26 * @param \Drupal\block\Entity\Block $block
32 protected function getBlockSelector(Block $block) {
33 return '#block-' . $block->id();
37 * Tests the 3 possible forms[settings_tray] annotations: class, FALSE, none.
39 * There is also functional JS test coverage to ensure that the two blocks
40 * that support Settings Tray (the "class" and "none" cases) do work
43 * @see SettingsTrayBlockFormTest::testBlocks()
45 public function testPossibleAnnotations() {
46 $test_block_plugin_ids = [
47 // Block that explicitly provides an "settings_tray" form class.
48 'settings_tray_test_class',
49 // Block that explicitly provides no "settings_tray" form, thus opting out.
50 'settings_tray_test_false',
51 // Block that does nothing explicit for Settings Tray.
52 'settings_tray_test_none',
56 foreach ($test_block_plugin_ids as $plugin_id) {
57 $placed_blocks[$plugin_id] = $this->placeBlock($plugin_id);
61 $web_assert = $this->assertSession();
62 foreach ($placed_blocks as $plugin_id => $placed_block) {
63 $block_selector = $this->getBlockSelector($placed_block);
65 // All blocks are rendered.
66 $web_assert->elementExists('css', $block_selector);
68 // All blocks except 'settings_tray_test_false' are editable. For more
69 // detailed test coverage, which requires JS execution, see
70 // \Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::testBlocks().
71 if ($plugin_id === 'settings_tray_test_false') {
72 $web_assert->elementNotExists('css', "{$block_selector}[data-drupal-settingstray=\"editable\"]");
75 $web_assert->elementExists('css', "{$block_selector}[data-drupal-settingstray=\"editable\"]");
81 * Tests that certain blocks opt out from Settings Tray.
83 public function testOptOut() {
84 $web_assert = $this->assertSession();
86 $non_excluded_block = $this->placeBlock('system_powered_by_block');
87 $excluded_block_plugin_ids = ['page_title_block', 'system_main_block', 'settings_tray_test_false'];
88 $block_selectors = [];
89 // Place blocks that should be excluded.
90 foreach ($excluded_block_plugin_ids as $excluded_block_plugin_id) {
91 // The block HTML 'id' attribute will be "block-[block_id]".
92 $block_selectors[] = $this->getBlockSelector($this->placeBlock($excluded_block_plugin_id));
95 // Assert that block has been marked as "editable" and contextual that
97 $web_assert->elementExists('css', $this->getBlockSelector($non_excluded_block) . "[data-drupal-settingstray=\"editable\"]");
98 // Assert that each block that has a "forms[settings_tray] = FALSE" annotation:
99 // - is still rendered on the page
100 // - but is not marked as "editable" by settings_tray_preprocess_block()
101 // - and does not have the Settings Tray contextual link.
102 foreach ($block_selectors as $block_selector) {
103 $web_assert->elementExists('css', $block_selector);
104 $web_assert->elementNotExists('css', "{$block_selector}[data-drupal-settingstray=\"editable\"]");
105 $web_assert->elementNotExists('css', "$block_selector [data-settings-tray-edit]");