Version 1
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / InlineEntityFormTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 use Behat\Mink\Element\NodeElement;
6
7 /**
8  * Test for integration of entity browser and inline entity form.
9  *
10  * @group entity_browser
11  *
12  * @package Drupal\Tests\entity_browser\FunctionalJavascript
13  */
14 class InlineEntityFormTest extends EntityBrowserJavascriptTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = [
20     'ctools',
21     'views',
22     'block',
23     'node',
24     'file',
25     'image',
26     'field_ui',
27     'views_ui',
28     'system',
29     'node',
30     'inline_entity_form',
31     'entity',
32     'entity_browser_ief_test',
33   ];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected static $userPermissions = [
39     'create media',
40     'update media',
41     'access ief_entity_browser_file entity browser pages',
42     'access ief_entity_browser_file_modal entity browser pages',
43     'access content',
44     'create ief_content content',
45     'edit any ief_content content',
46   ];
47
48   /**
49    * Drag element in document with defined offset position.
50    *
51    * @param \Behat\Mink\Element\NodeElement $element
52    *   Element that will be dragged.
53    * @param int $offsetX
54    *   Vertical offset for element drag in pixels.
55    * @param int $offsetY
56    *   Horizontal offset for element drag in pixels.
57    */
58   protected function dragDropElement(NodeElement $element, $offsetX, $offsetY) {
59     $elemXpath = $element->getXpath();
60
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); };";
65
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});" .
76       "})();";
77
78     $this->getSession()->executeScript($jsCode);
79   }
80
81   /**
82    * Check that selection state in entity browser Inline Entity Form.
83    */
84   public function testEntityBrowserInsideInlineEntityForm() {
85
86     $this->createFile('test_file1');
87     $this->createFile('test_file2');
88     $this->createFile('test_file3');
89
90     $this->drupalGet('node/add/ief_content');
91     $page = $this->getSession()->getPage();
92
93     $page->fillField('Title', 'Test IEF Title');
94     $page->pressButton('Add new Test File Media');
95
96     $this->assertSession()->assertWaitOnAjaxRequest();
97
98     $page->fillField('Media name', 'Test Bundle Media');
99     $page->clickLink('Select entities');
100
101     $this->getSession()
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]');
105
106     $page->pressButton('Select entities');
107     $this->assertSession()->assertWaitOnAjaxRequest();
108
109     $page->pressButton('Use selected');
110     $this->getSession()->switchToIFrame();
111     $this->assertSession()->assertWaitOnAjaxRequest();
112
113     $page->pressButton('Create Test File Media');
114     $this->assertSession()->assertWaitOnAjaxRequest();
115
116     $page->pressButton('Save');
117
118     $this->drupalGet('node/1/edit');
119     $page = $this->getSession()->getPage();
120
121     $page->pressButton('Edit');
122     $this->assertSession()->assertWaitOnAjaxRequest();
123
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();
128
129     $page->pressButton('Update Test File Media');
130     $this->assertSession()->assertWaitOnAjaxRequest();
131
132     $page->pressButton('Edit');
133     $this->assertSession()->assertWaitOnAjaxRequest();
134
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.');
139     }
140     $this->assertSame('file:1', $secondElement->getAttribute('data-entity-id'));
141
142     // Test remove of element.
143     $this->click('input[name*="ief_media_bundle_file_field_remove_1_1"]');
144     $this->assertSession()->assertWaitOnAjaxRequest();
145
146     $page->pressButton('Update Test File Media');
147     $this->assertSession()->assertWaitOnAjaxRequest();
148
149     $page->pressButton('Edit');
150     $this->assertSession()->assertWaitOnAjaxRequest();
151
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"]');
155
156     // Test add inside Entity Browser.
157     $page->clickLink('Select entities');
158
159     $this->getSession()
160       ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
161     $page->checkField('entity_browser_select[file:3]');
162
163     $page->pressButton('Select entities');
164     $this->assertSession()->assertWaitOnAjaxRequest();
165
166     $page->pressButton('Use selected');
167     $this->getSession()->switchToIFrame();
168     $this->assertSession()->assertWaitOnAjaxRequest();
169
170     $page->pressButton('Update Test File Media');
171     $this->assertSession()->assertWaitOnAjaxRequest();
172
173     $page->pressButton('Edit');
174     $this->assertSession()->assertWaitOnAjaxRequest();
175
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.');
180     }
181     $this->assertSame('file:3', $secondElement->getAttribute('data-entity-id'));
182
183     // Test reorder inside Entity Browser.
184     $page->clickLink('Select entities');
185
186     $this->getSession()
187       ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
188
189     $dragged = $this->xpath("//div[@data-drupal-selector='edit-selected-items-2-0']")[0];
190     $this->dragDropElement($dragged, 150, 0);
191     $this->assertSession()->assertWaitOnAjaxRequest();
192
193     $page->pressButton('Use selected');
194     $this->getSession()->switchToIFrame();
195     $this->assertSession()->assertWaitOnAjaxRequest();
196
197     $page->pressButton('Update Test File Media');
198     $this->assertSession()->assertWaitOnAjaxRequest();
199
200     $page->pressButton('Edit');
201     $this->assertSession()->assertWaitOnAjaxRequest();
202
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.');
207     }
208     $this->assertSame('file:2', $secondElement->getAttribute('data-entity-id'));
209
210     // Test remove inside entity browser.
211     $page->clickLink('Select entities');
212
213     $this->getSession()
214       ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file');
215     $this->assertSession()->assertWaitOnAjaxRequest();
216
217     $page->pressButton('remove_3_0');
218     $this->assertSession()->assertWaitOnAjaxRequest();
219
220     $page->pressButton('Use selected');
221     $this->getSession()->switchToIFrame();
222     $this->assertSession()->assertWaitOnAjaxRequest();
223
224     $page->pressButton('Update Test File Media');
225     $this->assertSession()->assertWaitOnAjaxRequest();
226
227     $page->pressButton('Edit');
228     $this->assertSession()->assertWaitOnAjaxRequest();
229
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.');
234     }
235     $this->assertSame('file:2', $secondElement->getAttribute('data-entity-id'));
236   }
237
238   /**
239    * Checks auto_open functionality for modals.
240    */
241   public function testModalAutoOpenInsideInlineEntityForm() {
242
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')
245       ->save();
246
247     $this->drupalGet('node/add/ief_content');
248     $page = $this->getSession()->getPage();
249
250     $page->fillField('Title', 'Test IEF Title');
251     $page->pressButton('Add existing Test File Media');
252
253     $this->assertSession()->assertWaitOnAjaxRequest();
254
255     $this->getSession()
256       ->switchToIFrame('entity_browser_iframe_ief_entity_browser_file_modal');
257
258     $this->assertSession()->pageTextContains('Test entity browser file modal');
259   }
260 }