Version 1
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / MultiStepSelectionDisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 use Drupal\Component\Utility\NestedArray;
6
7 /**
8  * Test for multi_step_display selection display.
9  *
10  * @group entity_browser
11  *
12  * @package Drupal\Tests\entity_browser\FunctionalJavascript
13  */
14 class MultiStepSelectionDisplayTest extends EntityBrowserJavascriptTestBase {
15
16   /**
17    * Open iframe entity browser and change scope to iframe.
18    */
19   protected function openEntityBrowser() {
20     $this->getSession()->getPage()->clickLink('Select entities');
21     $this->getSession()
22       ->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
23     $this->waitForAjaxToFinish();
24   }
25
26   /**
27    * Close iframe entity browser and change scope to base page.
28    */
29   protected function closeEntityBrowser() {
30     $this->clickXpathSelector('//*[@data-drupal-selector="edit-use-selected"]');
31     $this->getSession()->switchToIFrame();
32     $this->waitForAjaxToFinish();
33   }
34
35   /**
36    * Click on entity in view to be selected.
37    *
38    * @param string $entityId
39    *   Entity ID that will be selected. Format: "file:1".
40    */
41   protected function clickViewEntity($entityId) {
42     $xpathViewRow = '//*[./*[contains(@class, "views-field-entity-browser-select") and .//input[@name="entity_browser_select[' . $entityId . ']"]]]';
43
44     $this->clickXpathSelector($xpathViewRow, FALSE);
45   }
46
47   /**
48    * Wait for Ajax Commands to finish.
49    *
50    * Since commands are executed in batches, it can occur that one command is
51    * still running and new one will be collected for next batch. To ensure all
52    * of commands are executed, we have to add additional 200ms wait, before next
53    * batch is triggered.
54    *
55    * It's related to: Drupal.entityBrowserCommandQueue.executeCommands
56    */
57   protected function waitSelectionDisplayAjaxCommands() {
58     $this->waitForAjaxToFinish();
59     $this->getSession()->wait(200);
60     $this->waitForAjaxToFinish();
61   }
62
63   /**
64    * Change selection mode for article reference field form display widget.
65    *
66    * @param array $configuration
67    *   Configuration that will be used for field form display.
68    */
69   protected function changeFieldFormDisplayConfig(array $configuration) {
70     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
71     $form_display = $this->container->get('entity_type.manager')
72       ->getStorage('entity_form_display')
73       ->load('node.article.default');
74
75     $form_display->setComponent(
76       'field_reference',
77       NestedArray::mergeDeep($form_display->getComponent('field_reference'), $configuration)
78     )->save();
79   }
80
81   /**
82    * Check that selection state in entity browser Inline Entity Form.
83    */
84   public function testAjaxCommands() {
85
86     $this->createFile('test_file1');
87     $this->createFile('test_file2');
88     $this->createFile('test_file3');
89
90     // Testing Action buttons (adding and removing) with usage of HTML View.
91     $widget_configurations = [
92       // View widget configuration.
93       '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
94         'settings' => [
95           'view' => 'files_entity_browser_html',
96           'auto_select' => TRUE,
97         ],
98       ],
99     ];
100     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
101     $this->drupalGet('node/add/article');
102     $this->openEntityBrowser();
103
104     // Check that action buttons are not there.
105     $this->assertSession()
106       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
107     $this->assertSession()
108       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
109
110     $this->clickViewEntity('file:1');
111     $this->waitSelectionDisplayAjaxCommands();
112
113     // Check that action buttons are there.
114     $this->assertSession()
115       ->elementExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
116     $this->assertSession()
117       ->elementExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
118
119     // Click on first entity Remove button.
120     $this->clickXpathSelector('//input[@data-row-id="0"]');
121     $this->waitSelectionDisplayAjaxCommands();
122
123     // Check that action buttons are not there.
124     $this->assertSession()
125       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
126     $this->assertSession()
127       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
128
129     $this->clickViewEntity('file:1');
130     $this->waitSelectionDisplayAjaxCommands();
131     $this->closeEntityBrowser();
132
133     // Testing quick adding and removing of entities with usage of Table
134     // (default) view.
135     $widget_configurations = [
136       // View widget configuration.
137       '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
138         'settings' => [
139           'view' => 'files_entity_browser',
140           'auto_select' => TRUE,
141         ],
142       ],
143     ];
144     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
145     $this->drupalGet('node/add/article');
146     $this->openEntityBrowser();
147
148     // Quickly add 5 entities.
149     $entitiesToAdd = ['file:1', 'file:2', 'file:3', 'file:1', 'file:2'];
150     foreach ($entitiesToAdd as $entityId) {
151       $this->clickViewEntity($entityId);
152     }
153     $this->waitSelectionDisplayAjaxCommands();
154
155     // Check that there are 5 entities in selection display list.
156     $this->assertSession()
157       ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 5);
158
159     // Quickly remove all 5 entities.
160     foreach (array_keys($entitiesToAdd) as $entityIndex) {
161       $this->clickXpathSelector('//input[@data-row-id="' . $entityIndex . '"]');
162     }
163     $this->waitSelectionDisplayAjaxCommands();
164
165     // Check that action buttons are not there.
166     $this->assertSession()
167       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-use-selected"]');
168     $this->assertSession()
169       ->elementNotExists('xpath', '//*[@data-drupal-selector="edit-show-selection"]');
170
171     $this->clickViewEntity('file:1');
172     $this->waitSelectionDisplayAjaxCommands();
173     $this->closeEntityBrowser();
174
175     // Testing adding with preselection with usage of Grid view.
176     $widget_configurations = [
177       // View widget configuration.
178       '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
179         'settings' => [
180           'view' => 'files_entity_browser_grid',
181           'auto_select' => TRUE,
182         ],
183       ],
184     ];
185     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
186
187     // Change selection mode to 'Edit', to test adding/removing inside EB.
188     $this->changeFieldFormDisplayConfig([
189       'settings' => [
190         'selection_mode' => 'selection_edit',
191       ],
192     ]);
193
194     $this->drupalGet('node/add/article');
195     $this->openEntityBrowser();
196
197     $this->clickViewEntity('file:1');
198     $this->waitSelectionDisplayAjaxCommands();
199     $this->closeEntityBrowser();
200
201     $this->openEntityBrowser();
202
203     $this->clickViewEntity('file:2');
204     $this->waitSelectionDisplayAjaxCommands();
205     $this->closeEntityBrowser();
206
207     $this->assertSession()
208       ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 2);
209
210     // Testing removing with preselection with usage of Unformatted view.
211     $widget_configurations = [
212       // View widget configuration.
213       '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
214         'settings' => [
215           'view' => 'files_entity_browser_unformatted',
216           'auto_select' => TRUE,
217         ],
218       ],
219     ];
220     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
221
222     $this->drupalGet('node/add/article');
223     $this->openEntityBrowser();
224
225     // Select 3 entities.
226     $entitiesToAdd = ['file:1', 'file:2', 'file:3'];
227     foreach ($entitiesToAdd as $entityId) {
228       $this->clickViewEntity($entityId);
229
230       // For some reason PhantomJS crashes here on quick clicking. That's why
231       // waiting is added. Selenium works fine.
232       $this->waitSelectionDisplayAjaxCommands();
233     }
234     $this->closeEntityBrowser();
235
236     // Check that there are 3 entities in selection list after closing of EB.
237     $this->assertSession()
238       ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 3);
239
240     $this->openEntityBrowser();
241
242     // Click on first entity Remove button.
243     $this->clickXpathSelector('//input[@data-row-id="0"]');
244     $this->waitSelectionDisplayAjaxCommands();
245
246     $this->closeEntityBrowser();
247
248     // Check that there are 2 entities in selection list after closing of EB.
249     $this->assertSession()
250       ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 2);
251   }
252
253 }