Version 1
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / PluginsTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 /**
6  * Tests the entity_browser plugins.
7  *
8  * @group entity_browser
9  */
10 class PluginsTest extends EntityBrowserJavascriptTestBase {
11
12   /**
13    * Tests the Entity browser iframe display plugin.
14    */
15   public function testIframeDisplayPlugin() {
16     $browser = $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'no_display');
17
18     $image = $this->createFile('lama');
19
20     // Tests view widget on single display.
21     $this->drupalGet('node/add/article');
22     $this->assertSession()->linkExists('Select entities');
23     $this->getSession()->getPage()->clickLink('Select entities');
24     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
25
26     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
27     $this->getSession()->getPage()->pressButton('Select entities');
28     $this->getSession()->switchToIFrame();
29     $this->waitUntilVisible('.field--type-entity-reference .button');
30     $this->assertSession()->pageTextContains('lama.jpg');
31
32     // Tests upload widget on single display. Gets the upload widget and sets
33     // the weight so we can test the view widget.
34     $upload_widget = $browser->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
35     $upload_widget->setWeight(0);
36     $browser->save();
37
38     $this->drupalGet('node/add/article');
39     $this->assertSession()->linkExists('Select entities');
40     $this->getSession()->getPage()->clickLink('Select entities');
41     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
42
43     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
44     $this->getSession()->getPage()->pressButton('Select entities');
45     $this->getSession()->switchToIFrame();
46     $this->waitUntilVisible('.field--type-entity-reference .button');
47     $this->assertSession()->pageTextContains('lama.jpg');
48
49     // Tests view tab with tabs widget selector.
50     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'no_display');
51
52     $this->drupalGet('node/add/article');
53     $this->assertSession()->linkExists('Select entities');
54     $this->getSession()->getPage()->clickLink('Select entities');
55     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
56     $this->assertSession()->linkExists('view');
57     $this->assertSession()->linkExists('upload');
58
59     $this->clickLink('view');
60     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
61     $this->getSession()->getPage()->pressButton('Select entities');
62     $this->getSession()->switchToIFrame();
63     $this->waitUntilVisible('.field--type-entity-reference .button');
64     $this->assertSession()->pageTextContains('lama.jpg');
65
66     // Tests upload tab with tabs widget selector.
67     $this->drupalGet('node/add/article');
68     $this->getSession()->getPage()->clickLink('Select entities');
69     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
70     $this->clickLink('upload');
71     $this->getSession()->getPage()->attachFileToField('edit-upload-upload', $this->container->get('file_system')->realpath($image->getFileUri()));
72     $this->waitForAjaxToFinish();
73     $this->getSession()->getPage()->checkField('upload[file_2][selected]');
74     $this->getSession()->getPage()->pressButton('Select files');
75     $this->getSession()->switchToIFrame();
76     $this->waitForAjaxToFinish();
77     $this->assertSession()->pageTextContains('lama.jpg');
78     // Tests view widget with drop down widget selector.
79     $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'drop_down', 'no_display');
80
81     // DropDown widget selector does not work with exposed view filter. This is
82     // a known bug and we need to remove exposed filters from the view until
83     // that is fixed.
84     /** @var \Drupal\views\Entity\View $view */
85     $view = $this->container->get('entity_type.manager')->getStorage('view')->load('files_entity_browser');
86     $display = &$view->getDisplay('default');
87     $display['display_options']['filters'] = [];
88     $view->save();
89
90     $this->drupalGet('node/add/article');
91     $this->getSession()->getPage()->clickLink('Select entities');
92     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
93     $this->assertSession()->selectExists('edit-widget');
94     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
95     $this->getSession()->getPage()->pressButton('Select entities');
96     $this->getSession()->switchToIFrame();
97     $this->waitUntilVisible('.field--type-entity-reference .button');
98     $this->assertSession()->pageTextContains('lama.jpg');
99
100     // Tests upload vidget with drop down widget selector.
101     $this->drupalGet('node/add/article');
102
103     $this->getSession()->getPage()->clickLink('Select entities');
104     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
105     $this->getSession()->getPage()->selectFieldOption('edit-widget', '2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
106     $this->waitForAjaxToFinish();
107     $this->getSession()->getPage()->attachFileToField('files[upload][]', $this->container->get('file_system')->realpath($image->getFileUri()));
108     $this->waitForAjaxToFinish();
109     $this->getSession()->getPage()->checkField('upload[file_3][selected]');
110     $this->getSession()->getPage()->pressButton('Select files');
111     $this->getSession()->switchToIFrame();
112     $this->waitForAjaxToFinish();
113     // In iframe I get page not found, so this fails.
114     $this->assertSession()->pageTextContains('lama.jpg');
115     // Tests view selection display.
116     $view_configuration = [
117       'view' => 'test_selection_display_view',
118       'view_display' => 'entity_browser_1',
119     ];
120     $browser = $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'view', [], [], $view_configuration);
121
122     $this->drupalGet('node/add/article');
123     $this->getSession()->getPage()->clickLink('Select entities');
124     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
125
126     // Tests multistep selection display.
127     $image1 = $this->createFile('first_file');
128     $image2 = $this->createFile('second_file');
129
130     $upload_widget = $browser->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
131     $upload_widget->setWeight(-9);
132     $browser->save();
133
134     $multistep_configuration = [
135       'entity_type' => 'file',
136       'display' => 'label',
137       'display_settings' => [],
138       'select_text' => 'Use selected',
139       'selection_hidden' => 0,
140     ];
141
142     $browser = $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], $multistep_configuration);
143     $upload_widget = $browser->getWidget('774798f1-5ec5-4b63-84bd-124cd51ec07d');
144     $upload_widget->setWeight(0);
145     $browser->save();
146
147     $this->drupalGet('node/add/article');
148     $this->getSession()->getPage()->clickLink('Select entities');
149     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
150
151     $this->getSession()->getPage()->attachFileToField('files[upload][]', $this->container->get('file_system')->realpath($image1->getFileUri()));
152     $this->waitForAjaxToFinish();
153     $this->getSession()->getPage()->pressButton('Select files');
154
155     $this->assertSession()->pageTextContains('first_file.jpg');
156     $this->assertSession()->pageTextNotContains('second_file.jpg');
157
158     $this->getSession()->getPage()->clickLink('view');
159     // This shows page not found, which is caused by https://www.drupal.org/node/2771547
160     // Uncomment this hunk when a fix for that problem lands.
161     //$this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image2->id() . ']');
162     //$this->getSession()->getPage()->pressButton('Select entities');
163     //$this->assertSession()->responseContains('edit-selected-items-2-1-remove-button');
164     //$this->assertSession()->responseContains('edit-selected-items-1-0-remove-button');
165     //$this->getSession()->getPage()->pressButton('Use selected');
166     //$this->getSession()->switchToIFrame();
167     //$this->waitForAjaxToFinish();
168     //$this->assertSession()->pageTextContains('first_file.jpg');
169     //$this->assertSession()->pageTextContains('second_file.jpg');
170   }
171
172   /**
173    * Tests Entity browser modal display plugin.
174    */
175   public function testModalDisplay() {
176     $modal_display_config = [
177       'width' => '650',
178       'height' => '500',
179       'link_text' => 'Select entities',
180     ];
181     $this->getEntityBrowser('test_entity_browser_file', 'modal', 'single', 'no_display', $modal_display_config);
182
183     $image = $this->createFile('lama');
184
185     $this->drupalGet('node/add/article');
186     $this->assertSession()->buttonExists('Select entities');
187     $this->getSession()->getPage()->pressButton('Select entities');
188     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
189
190     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
191     $this->getSession()->getPage()->pressButton('Select entities');
192     $this->getSession()->switchToIFrame();
193     $this->waitForAjaxToFinish();
194
195     $this->assertSession()->pageTextContains('lama.jpg');
196   }
197
198   /**
199    * Tests Entity browser standalone display plugin.
200    */
201   public function testStandaloneDisplay() {
202     $image = $this->createFile('lama');
203     $standalone_configuration = [
204       'entity_browser_id' => 'test_entity_browser_file',
205       'path' => 'test',
206     ];
207     $this->getEntityBrowser('test_entity_browser_file', 'standalone', 'single', 'no_display', $standalone_configuration);
208
209     $this->drupalGet('test');
210
211     $this->assertSession()->buttonExists('Select entities');
212     $this->getSession()->getPage()->pressButton('Select entities');
213
214     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
215     $this->getSession()->getPage()->pressButton('Select entities');
216
217     // TODO test if entities were selected. Will most likely need a custom event
218     // subscriber that displays a message or something along those lines.
219   }
220
221 }