Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / settings_tray / tests / src / Functional / SettingsTrayTest.php
1 <?php
2
3 namespace Drupal\Tests\settings_tray\Functional;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests opening and saving block forms in the off-canvas dialog.
10  *
11  * @group settings_tray
12  */
13 class SettingsTrayTest extends BrowserTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = [
19     'settings_tray',
20     'settings_tray_test',
21   ];
22
23   /**
24    * Gets the block CSS selector.
25    *
26    * @param \Drupal\block\Entity\Block $block
27    *   The block.
28    *
29    * @return string
30    *   The CSS selector.
31    */
32   protected  function getBlockSelector(Block $block) {
33     return '#block-' . $block->id();
34   }
35
36   /**
37    * Tests the 3 possible forms[settings_tray] annotations: class, FALSE, none.
38    *
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
41    * correctly.
42    *
43    * @see SettingsTrayBlockFormTest::testBlocks()
44    */
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',
53     ];
54
55     $placed_blocks = [];
56     foreach ($test_block_plugin_ids as $plugin_id) {
57       $placed_blocks[$plugin_id] = $this->placeBlock($plugin_id);
58     }
59
60     $this->drupalGet('');
61     $web_assert = $this->assertSession();
62     foreach ($placed_blocks as $plugin_id => $placed_block) {
63       $block_selector = $this->getBlockSelector($placed_block);
64
65       // All blocks are rendered.
66       $web_assert->elementExists('css', $block_selector);
67
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\"]");
73       }
74       else {
75         $web_assert->elementExists('css', "{$block_selector}[data-drupal-settingstray=\"editable\"]");
76       }
77     }
78   }
79
80   /**
81    * Tests that certain blocks opt out from Settings Tray.
82    */
83   public function testOptOut() {
84     $web_assert = $this->assertSession();
85
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));
93     }
94     $this->drupalGet('');
95     // Assert that block has been marked as "editable" and contextual that
96     // should exist does.
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]");
106     }
107   }
108
109 }