3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
6 * Tests the entity_browser plugins.
8 * @group entity_browser
10 class PluginsTest extends EntityBrowserJavascriptTestBase {
13 * Tests the Entity browser iframe display plugin.
15 public function testIframeDisplayPlugin() {
16 $browser = $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'no_display');
18 $image = $this->createFile('lama');
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');
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');
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);
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');
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');
49 // Tests view tab with tabs widget selector.
50 $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'no_display');
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');
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');
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');
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
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'] = [];
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');
100 // Tests upload vidget with drop down widget selector.
101 $this->drupalGet('node/add/article');
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',
120 $browser = $this->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'view', [], [], $view_configuration);
122 $this->drupalGet('node/add/article');
123 $this->getSession()->getPage()->clickLink('Select entities');
124 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
126 // Tests multistep selection display.
127 $image1 = $this->createFile('first_file');
128 $image2 = $this->createFile('second_file');
130 $upload_widget = $browser->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
131 $upload_widget->setWeight(-9);
134 $multistep_configuration = [
135 'entity_type' => 'file',
136 'display' => 'label',
137 'display_settings' => [],
138 'select_text' => 'Use selected',
139 'selection_hidden' => 0,
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);
147 $this->drupalGet('node/add/article');
148 $this->getSession()->getPage()->clickLink('Select entities');
149 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
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');
155 $this->assertSession()->pageTextContains('first_file.jpg');
156 $this->assertSession()->pageTextNotContains('second_file.jpg');
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');
173 * Tests Entity browser modal display plugin.
175 public function testModalDisplay() {
176 $modal_display_config = [
179 'link_text' => 'Select entities',
181 $this->getEntityBrowser('test_entity_browser_file', 'modal', 'single', 'no_display', $modal_display_config);
183 $image = $this->createFile('lama');
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');
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();
195 $this->assertSession()->pageTextContains('lama.jpg');
199 * Tests Entity browser standalone display plugin.
201 public function testStandaloneDisplay() {
202 $image = $this->createFile('lama');
203 $standalone_configuration = [
204 'entity_browser_id' => 'test_entity_browser_file',
207 $this->getEntityBrowser('test_entity_browser_file', 'standalone', 'single', 'no_display', $standalone_configuration);
209 $this->drupalGet('test');
211 $this->assertSession()->buttonExists('Select entities');
212 $this->getSession()->getPage()->pressButton('Select entities');
214 $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
215 $this->getSession()->getPage()->pressButton('Select entities');
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.