3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Drupal\Component\Utility\NestedArray;
8 * Test for multi_step_display selection display.
10 * @group entity_browser
12 * @package Drupal\Tests\entity_browser\FunctionalJavascript
14 class MultiStepSelectionDisplayTest extends EntityBrowserJavascriptTestBase {
17 * Open iframe entity browser and change scope to iframe.
19 protected function openEntityBrowser() {
20 $this->getSession()->getPage()->clickLink('Select entities');
22 ->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
23 $this->waitForAjaxToFinish();
27 * Close iframe entity browser and change scope to base page.
29 protected function closeEntityBrowser() {
30 $this->clickXpathSelector('//*[@data-drupal-selector="edit-use-selected"]');
31 $this->getSession()->switchToIFrame();
32 $this->waitForAjaxToFinish();
36 * Click on entity in view to be selected.
38 * @param string $entityId
39 * Entity ID that will be selected. Format: "file:1".
41 protected function clickViewEntity($entityId) {
42 $xpathViewRow = '//*[./*[contains(@class, "views-field-entity-browser-select") and .//input[@name="entity_browser_select[' . $entityId . ']"]]]';
44 $this->clickXpathSelector($xpathViewRow, FALSE);
48 * Wait for Ajax Commands to finish.
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
55 * It's related to: Drupal.entityBrowserCommandQueue.executeCommands
57 protected function waitSelectionDisplayAjaxCommands() {
58 $this->waitForAjaxToFinish();
59 $this->getSession()->wait(200);
60 $this->waitForAjaxToFinish();
64 * Change selection mode for article reference field form display widget.
66 * @param array $configuration
67 * Configuration that will be used for field form display.
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');
75 $form_display->setComponent(
77 NestedArray::mergeDeep($form_display->getComponent('field_reference'), $configuration)
82 * Check that selection state in entity browser Inline Entity Form.
84 public function testAjaxCommands() {
86 $this->createFile('test_file1');
87 $this->createFile('test_file2');
88 $this->createFile('test_file3');
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' => [
95 'view' => 'files_entity_browser_html',
96 'auto_select' => TRUE,
100 $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
101 $this->drupalGet('node/add/article');
102 $this->openEntityBrowser();
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"]');
110 $this->clickViewEntity('file:1');
111 $this->waitSelectionDisplayAjaxCommands();
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"]');
119 // Click on first entity Remove button.
120 $this->clickXpathSelector('//input[@data-row-id="0"]');
121 $this->waitSelectionDisplayAjaxCommands();
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"]');
129 $this->clickViewEntity('file:1');
130 $this->waitSelectionDisplayAjaxCommands();
131 $this->closeEntityBrowser();
133 // Testing quick adding and removing of entities with usage of Table
135 $widget_configurations = [
136 // View widget configuration.
137 '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
139 'view' => 'files_entity_browser',
140 'auto_select' => TRUE,
144 $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
145 $this->drupalGet('node/add/article');
146 $this->openEntityBrowser();
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);
153 $this->waitSelectionDisplayAjaxCommands();
155 // Check that there are 5 entities in selection display list.
156 $this->assertSession()
157 ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 5);
159 // Quickly remove all 5 entities.
160 foreach (array_keys($entitiesToAdd) as $entityIndex) {
161 $this->clickXpathSelector('//input[@data-row-id="' . $entityIndex . '"]');
163 $this->waitSelectionDisplayAjaxCommands();
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"]');
171 $this->clickViewEntity('file:1');
172 $this->waitSelectionDisplayAjaxCommands();
173 $this->closeEntityBrowser();
175 // Testing adding with preselection with usage of Grid view.
176 $widget_configurations = [
177 // View widget configuration.
178 '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
180 'view' => 'files_entity_browser_grid',
181 'auto_select' => TRUE,
185 $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
187 // Change selection mode to 'Edit', to test adding/removing inside EB.
188 $this->changeFieldFormDisplayConfig([
190 'selection_mode' => 'selection_edit',
194 $this->drupalGet('node/add/article');
195 $this->openEntityBrowser();
197 $this->clickViewEntity('file:1');
198 $this->waitSelectionDisplayAjaxCommands();
199 $this->closeEntityBrowser();
201 $this->openEntityBrowser();
203 $this->clickViewEntity('file:2');
204 $this->waitSelectionDisplayAjaxCommands();
205 $this->closeEntityBrowser();
207 $this->assertSession()
208 ->elementsCount('xpath', '//div[contains(@class, "entities-list")]/*', 2);
210 // Testing removing with preselection with usage of Unformatted view.
211 $widget_configurations = [
212 // View widget configuration.
213 '774798f1-5ec5-4b63-84bd-124cd51ec07d' => [
215 'view' => 'files_entity_browser_unformatted',
216 'auto_select' => TRUE,
220 $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], [], $widget_configurations);
222 $this->drupalGet('node/add/article');
223 $this->openEntityBrowser();
225 // Select 3 entities.
226 $entitiesToAdd = ['file:1', 'file:2', 'file:3'];
227 foreach ($entitiesToAdd as $entityId) {
228 $this->clickViewEntity($entityId);
230 // For some reason PhantomJS crashes here on quick clicking. That's why
231 // waiting is added. Selenium works fine.
232 $this->waitSelectionDisplayAjaxCommands();
234 $this->closeEntityBrowser();
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);
240 $this->openEntityBrowser();
242 // Click on first entity Remove button.
243 $this->clickXpathSelector('//input[@data-row-id="0"]');
244 $this->waitSelectionDisplayAjaxCommands();
246 $this->closeEntityBrowser();
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);