3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Behat\Mink\Element\NodeElement;
8 * Test for integration of entity browser and inline entity form.
10 * @group entity_browser
12 * @package Drupal\Tests\entity_browser\FunctionalJavascript
14 class InlineEntityFormTest extends EntityBrowserJavascriptTestBase {
19 public static $modules = [
32 'entity_browser_ief_test',
38 protected static $userPermissions = [
41 'access ief_entity_browser_file entity browser pages',
42 'access ief_entity_browser_file_modal entity browser pages',
44 'create ief_content content',
45 'edit any ief_content content',
49 * Drag element in document with defined offset position.
51 * @param \Behat\Mink\Element\NodeElement $element
52 * Element that will be dragged.
54 * Vertical offset for element drag in pixels.
56 * Horizontal offset for element drag in pixels.
58 protected function dragDropElement(NodeElement $element, $offsetX, $offsetY) {
59 $elemXpath = $element->getXpath();
61 $jsCode = "var fireMouseEvent = function (type, element, x, y) {"
62 . " var event = document.createEvent('MouseEvents');"
63 . " event.initMouseEvent(type, true, (type !== 'mousemove'), window, 0, 0, 0, x, y, false, false, false, false, 0, element);"
64 . " element.dispatchEvent(event); };";
66 // XPath provided by getXpath uses single quote (') to encapsulate strings,
67 // that's why xpath has to be quited with double quites in javascript code.
68 $jsCode .= "(function() {" .
69 " var dragElement = document.evaluate(\"{$elemXpath}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" .
70 " var pos = dragElement.getBoundingClientRect();" .
71 " var centerX = Math.floor((pos.left + pos.right) / 2);" .
72 " var centerY = Math.floor((pos.top + pos.bottom) / 2);" .
73 " fireMouseEvent('mousedown', dragElement, centerX, centerY);" .
74 " fireMouseEvent('mousemove', document, centerX + {$offsetX}, centerY + {$offsetY});" .
75 " fireMouseEvent('mouseup', dragElement, centerX + {$offsetX}, centerY + {$offsetY});" .
78 $this->getSession()->executeScript($jsCode);
82 * Check that selection state in entity browser Inline Entity Form.
84 public function testEntityBrowserInsideInlineEntityForm() {
86 $this->createFile('test_file1');
87 $this->createFile('test_file2');
88 $this->createFile('test_file3');
90 $this->drupalGet('node/add/ief_content');
91 $page = $this->getSession()->getPage();
93 $page->fillField('Title', 'Test IEF Title');
94 $page->pressButton('Add new Test File Media');
96 $this->assertSession()->assertWaitOnAjaxRequest();
98 $page->fillField('Media name', 'Test Bundle Media');
99 $page->clickLink('Select entities');
102 ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
103 $page->checkField('entity_browser_select[file:1]');
104 $page->checkField('entity_browser_select[file:2]');
106 $page->pressButton('Select entities');
107 $this->assertSession()->assertWaitOnAjaxRequest();
109 $page->pressButton('Use selected');
110 $this->getSession()->switchToIFrame();
111 $this->assertSession()->assertWaitOnAjaxRequest();
113 $page->pressButton('Create Test File Media');
114 $this->assertSession()->assertWaitOnAjaxRequest();
116 $page->pressButton('Save');
118 $this->drupalGet('node/1/edit');
119 $page = $this->getSession()->getPage();
121 $page->pressButton('Edit');
122 $this->assertSession()->assertWaitOnAjaxRequest();
124 // Test reorder of elements.
125 $dragged = $this->xpath("//div[@data-drupal-selector='edit-ief-media-field-form-inline-entity-form-entities-0-form-ief-media-bundle-file-field-current-items-0']")[0];
126 $this->dragDropElement($dragged, 150, 0);
127 $this->assertSession()->assertWaitOnAjaxRequest();
129 $page->pressButton('Update Test File Media');
130 $this->assertSession()->assertWaitOnAjaxRequest();
132 $page->pressButton('Edit');
133 $this->assertSession()->assertWaitOnAjaxRequest();
135 // Check that element on second position is test_file1 (file:1).
136 $secondElement = $page->find('xpath', '//div[@data-drupal-selector="edit-ief-media-field-form-inline-entity-form-entities-0-form-ief-media-bundle-file-field-current"]/div[2]');
137 if (empty($secondElement)) {
138 throw new \Exception('Element is not found.');
140 $this->assertSame('file:1', $secondElement->getAttribute('data-entity-id'));
142 // Test remove of element.
143 $this->click('input[name*="ief_media_bundle_file_field_remove_1_1"]');
144 $this->assertSession()->assertWaitOnAjaxRequest();
146 $page->pressButton('Update Test File Media');
147 $this->assertSession()->assertWaitOnAjaxRequest();
149 $page->pressButton('Edit');
150 $this->assertSession()->assertWaitOnAjaxRequest();
152 // Check that remote button does not exist for already removed entity.
153 $this->assertSession()
154 ->elementNotExists('css', '[name*="ief_media_bundle_file_field_remove_1_1"]');
156 // Test add inside Entity Browser.
157 $page->clickLink('Select entities');
160 ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
161 $page->checkField('entity_browser_select[file:3]');
163 $page->pressButton('Select entities');
164 $this->assertSession()->assertWaitOnAjaxRequest();
166 $page->pressButton('Use selected');
167 $this->getSession()->switchToIFrame();
168 $this->assertSession()->assertWaitOnAjaxRequest();
170 $page->pressButton('Update Test File Media');
171 $this->assertSession()->assertWaitOnAjaxRequest();
173 $page->pressButton('Edit');
174 $this->assertSession()->assertWaitOnAjaxRequest();
176 // Check that element on second position is test_file3 (file:3).
177 $secondElement = $page->find('xpath', '//div[@data-drupal-selector="edit-ief-media-field-form-inline-entity-form-entities-0-form-ief-media-bundle-file-field-current"]/div[2]');
178 if (empty($secondElement)) {
179 throw new \Exception('Element is not found.');
181 $this->assertSame('file:3', $secondElement->getAttribute('data-entity-id'));
183 // Test reorder inside Entity Browser.
184 $page->clickLink('Select entities');
187 ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
189 $dragged = $this->xpath("//div[@data-drupal-selector='edit-selected-items-2-0']")[0];
190 $this->dragDropElement($dragged, 150, 0);
191 $this->assertSession()->assertWaitOnAjaxRequest();
193 $page->pressButton('Use selected');
194 $this->getSession()->switchToIFrame();
195 $this->assertSession()->assertWaitOnAjaxRequest();
197 $page->pressButton('Update Test File Media');
198 $this->assertSession()->assertWaitOnAjaxRequest();
200 $page->pressButton('Edit');
201 $this->assertSession()->assertWaitOnAjaxRequest();
203 // Check that element on second position is test_file2 (file:2).
204 $secondElement = $page->find('xpath', '//div[@data-drupal-selector="edit-ief-media-field-form-inline-entity-form-entities-0-form-ief-media-bundle-file-field-current"]/div[2]');
205 if (empty($secondElement)) {
206 throw new \Exception('Element is not found.');
208 $this->assertSame('file:2', $secondElement->getAttribute('data-entity-id'));
210 // Test remove inside entity browser.
211 $page->clickLink('Select entities');
214 ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
215 $this->assertSession()->assertWaitOnAjaxRequest();
217 $page->pressButton('remove_3_0');
218 $this->assertSession()->assertWaitOnAjaxRequest();
220 $page->pressButton('Use selected');
221 $this->getSession()->switchToIFrame();
222 $this->assertSession()->assertWaitOnAjaxRequest();
224 $page->pressButton('Update Test File Media');
225 $this->assertSession()->assertWaitOnAjaxRequest();
227 $page->pressButton('Edit');
228 $this->assertSession()->assertWaitOnAjaxRequest();
230 // Check that element on first position is test_file2 (file:2).
231 $secondElement = $page->find('xpath', '//div[@data-drupal-selector="edit-ief-media-field-form-inline-entity-form-entities-0-form-ief-media-bundle-file-field-current"]/div[1]');
232 if (empty($secondElement)) {
233 throw new \Exception('Element is not found.');
235 $this->assertSame('file:2', $secondElement->getAttribute('data-entity-id'));
239 * Checks auto_open functionality for modals.
241 public function testModalAutoOpenInsideInlineEntityForm() {
243 $this->config('core.entity_form_display.node.ief_content.default')
244 ->set('content.ief_media_field.third_party_settings.entity_browser_entity_form.entity_browser_id', 'ief_entity_browser_file_modal')
247 $this->drupalGet('node/add/ief_content');
248 $page = $this->getSession()->getPage();
250 $page->fillField('Title', 'Test IEF Title');
251 $page->pressButton('Add existing Test File Media');
253 $this->assertSession()->assertWaitOnAjaxRequest();
256 ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file_modal');
258 $this->assertSession()->pageTextContains('Test entity browser file modal');