3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
6 * Tests the entity_browser.
8 * @group entity_browser
10 class EntityBrowserTest extends EntityBrowserJavascriptTestBase {
13 * Tests single widget selector.
15 public function testSingleWidgetSelector() {
17 // Sets the single widget selector.
18 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
19 $browser = $this->container->get('entity_type.manager')
20 ->getStorage('entity_browser')
21 ->load('test_entity_browser_file');
23 $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'single', 'Widget selector is set to single.');
26 $image = $this->createFile('llama');
28 $this->drupalGet('node/add/article');
30 $this->assertSession()->linkExists('Select entities');
31 $this->getSession()->getPage()->clickLink('Select entities');
33 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
35 $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
36 $this->getSession()->getPage()->pressButton('Select entities');
38 // Switch back to the main page.
39 $this->getSession()->switchToIFrame();
40 $this->waitForAjaxToFinish();
41 // Test the Edit functionality.
42 $this->assertSession()->pageTextContains('llama.jpg');
43 $this->assertSession()->buttonExists('Edit');
44 // @TODO Test the edit button.
45 // Test the Delete functionality.
46 $this->assertSession()->buttonExists('Remove');
47 $this->getSession()->getPage()->pressButton('Remove');
48 $this->waitForAjaxToFinish();
49 $this->assertSession()->pageTextNotContains('llama.jpg');
50 $this->assertSession()->linkExists('Select entities');
54 * Tests tabs widget selector.
56 public function testTabsWidgetSelector() {
58 // Sets the tabs widget selector.
59 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
60 $browser = $this->container->get('entity_type.manager')
61 ->getStorage('entity_browser')
62 ->load('test_entity_browser_file')
63 ->setWidgetSelector('tabs');
66 $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'tabs', 'Widget selector is set to tabs.');
69 $image = $this->createFile('llama');
71 // Create a second file.
72 $image2 = $this->createFile('llama2');
74 $this->drupalGet('node/add/article');
76 $this->assertSession()->linkExists('Select entities');
77 $this->getSession()->getPage()->clickLink('Select entities');
79 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
81 $this->assertSession()->linkExists('view');
82 $this->assertSession()->linkExists('upload');
84 $this->assertEquals('is-active active', $this->getSession()->getPage()->findLink('view')->getAttribute('class'));
86 $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
87 $this->getSession()->getPage()->pressButton('Select entities');
88 $this->getSession()->switchToIFrame();
90 $this->waitForAjaxToFinish();
92 $this->assertSession()->pageTextContains('llama.jpg');
94 $this->getSession()->getPage()->clickLink('Select entities');
95 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
96 $this->getSession()->getPage()->clickLink('upload');
98 // This is producing an error. Still investigating
99 // InvalidStateError: DOM Exception 11: An attempt was made to use an object
100 // that is not, or is no longer, usable.
102 // 'files[upload][]' => $this->container->get('file_system')->realpath($image2->getFileUri()),
104 // $this->drupalPostForm(NULL, $edit, 'Select files');.
108 * Tests dropdown widget selector.
110 public function testDropdownWidgetSelector() {
112 // Sets the dropdown widget selector.
113 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
114 $browser = $this->container->get('entity_type.manager')
115 ->getStorage('entity_browser')
116 ->load('test_entity_browser_file')
117 ->setWidgetSelector('drop_down');
120 $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'drop_down', 'Widget selector is set to dropdown.');
123 $image = $this->createFile('llama');
125 $this->drupalGet('node/add/article');
127 $this->assertSession()->linkExists('Select entities');
128 $this->getSession()->getPage()->clickLink('Select entities');
130 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
132 $this->assertSession()->selectExists('widget');
133 // Selects the view widget.
134 $this->getSession()->getPage()->selectFieldOption('widget', '774798f1-5ec5-4b63-84bd-124cd51ec07d');
136 $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
137 $this->getSession()->getPage()->pressButton('Select entities');
138 $this->getSession()->switchToIFrame();
140 $this->waitForAjaxToFinish();
142 $this->assertSession()->pageTextContains('llama.jpg');
144 $this->getSession()->getPage()->clickLink('Select entities');
146 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
149 // Selects the upload widget.
150 // $this->getSession()->getPage()->selectFieldOption('widget', '2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');.
154 * Tests wievs selection display.
156 public function testViewsSelectionDisplayWidget() {
158 // Sets the dropdown widget selector.
159 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
160 $browser = $this->container->get('entity_type.manager')
161 ->getStorage('entity_browser')
162 ->load('test_entity_browser_file')
163 ->setSelectionDisplay('view');
166 $this->assertEquals($browser->getSelectionDisplay()->getPluginId(), 'view', 'Selection display is set to view.');
171 * Tests NoDisplay selection display plugin.
173 public function testNoDisplaySelectionDisplay() {
174 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
175 $form_display = $this->container->get('entity_type.manager')
176 ->getStorage('entity_form_display')
177 ->load('node.article.default');
179 $form_display->setComponent('field_reference', [
180 'type' => 'entity_browser_entity_reference',
182 'entity_browser' => 'multiple_submit_example',
183 'field_widget_display' => 'label',
188 $account = $this->drupalCreateUser([
189 'access multiple_submit_example entity browser pages',
190 'create article content',
193 $this->drupalLogin($account);
195 $this->drupalGet('node/add/article');
196 // Open the entity browser widget form.
197 $this->getSession()->getPage()->clickLink('Select entities');
198 $this->getSession()->switchToIFrame('entity_browser_iframe_multiple_submit_example');
200 // Click the second submit button to make sure the widget does not close.
201 $this->getSession()->getPage()->pressButton('Second submit button');
203 // Check that the entity browser widget is still open.
204 $this->getSession()->getPage()->hasButton('Second submit button');
206 // Click the primary submit button to close the widget.
207 $this->getSession()->getPage()->pressButton('Select entities');
209 // Check that the entity browser widget is closed.
210 $this->assertSession()->buttonNotExists('Second submit button');