e8add2cefc7480b27e3426777593ea88b53483f7
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / EntityBrowserTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 /**
6  * Tests the entity_browser.
7  *
8  * @group entity_browser
9  */
10 class EntityBrowserTest extends EntityBrowserJavascriptTestBase {
11
12   /**
13    * Tests single widget selector.
14    */
15   public function testSingleWidgetSelector() {
16
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');
22
23     $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'single', 'Widget selector is set to single.');
24
25     // Create a file.
26     $image = $this->createFile('llama');
27
28     $this->drupalGet('node/add/article');
29
30     $this->assertSession()->linkExists('Select entities');
31     $this->getSession()->getPage()->clickLink('Select entities');
32
33     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
34
35     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
36     $this->getSession()->getPage()->pressButton('Select entities');
37
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');
51   }
52
53   /**
54    * Tests tabs widget selector.
55    */
56   public function testTabsWidgetSelector() {
57
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');
64     $browser->save();
65
66     $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'tabs', 'Widget selector is set to tabs.');
67
68     // Create a file.
69     $image = $this->createFile('llama');
70
71     // Create a second file.
72     $image2 = $this->createFile('llama2');
73
74     $this->drupalGet('node/add/article');
75
76     $this->assertSession()->linkExists('Select entities');
77     $this->getSession()->getPage()->clickLink('Select entities');
78
79     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
80
81     $this->assertSession()->linkExists('view');
82     $this->assertSession()->linkExists('upload');
83
84     $this->assertEquals('is-active active', $this->getSession()->getPage()->findLink('view')->getAttribute('class'));
85
86     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
87     $this->getSession()->getPage()->pressButton('Select entities');
88     $this->getSession()->switchToIFrame();
89
90     $this->waitForAjaxToFinish();
91
92     $this->assertSession()->pageTextContains('llama.jpg');
93
94     $this->getSession()->getPage()->clickLink('Select entities');
95     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
96     $this->getSession()->getPage()->clickLink('upload');
97
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.
101     //$edit = [
102     //  'files[upload][]' => $this->container->get('file_system')->realpath($image2->getFileUri()),
103     //];
104     // $this->drupalPostForm(NULL, $edit, 'Select files');.
105   }
106
107   /**
108    * Tests dropdown widget selector.
109    */
110   public function testDropdownWidgetSelector() {
111
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');
118     $browser->save();
119
120     $this->assertEquals($browser->getWidgetSelector()->getPluginId(), 'drop_down', 'Widget selector is set to dropdown.');
121
122     // Create a file.
123     $image = $this->createFile('llama');
124
125     $this->drupalGet('node/add/article');
126
127     $this->assertSession()->linkExists('Select entities');
128     $this->getSession()->getPage()->clickLink('Select entities');
129
130     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
131
132     $this->assertSession()->selectExists('widget');
133     // Selects the view widget.
134     $this->getSession()->getPage()->selectFieldOption('widget', '774798f1-5ec5-4b63-84bd-124cd51ec07d');
135
136     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $image->id() . ']');
137     $this->getSession()->getPage()->pressButton('Select entities');
138     $this->getSession()->switchToIFrame();
139
140     $this->waitForAjaxToFinish();
141
142     $this->assertSession()->pageTextContains('llama.jpg');
143
144     $this->getSession()->getPage()->clickLink('Select entities');
145
146     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
147
148     // Causes a fatal.
149     // Selects the upload widget.
150     // $this->getSession()->getPage()->selectFieldOption('widget', '2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');.
151   }
152
153   /**
154    * Tests wievs selection display.
155    */
156   public function testViewsSelectionDisplayWidget() {
157
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');
164     $browser->save();
165
166     $this->assertEquals($browser->getSelectionDisplay()->getPluginId(), 'view', 'Selection display is set to view.');
167
168   }
169
170   /**
171    * Tests NoDisplay selection display plugin.
172    */
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');
178
179     $form_display->setComponent('field_reference', [
180       'type' => 'entity_browser_entity_reference',
181       'settings' => [
182         'entity_browser' => 'multiple_submit_example',
183         'field_widget_display' => 'label',
184         'open' => TRUE,
185       ],
186     ])->save();
187
188     $account = $this->drupalCreateUser([
189       'access multiple_submit_example entity browser pages',
190       'create article content',
191       'access content',
192     ]);
193     $this->drupalLogin($account);
194
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');
199
200     // Click the second submit button to make sure the widget does not close.
201     $this->getSession()->getPage()->pressButton('Second submit button');
202
203     // Check that the entity browser widget is still open.
204     $this->getSession()->getPage()->hasButton('Second submit button');
205
206     // Click the primary submit button to close the widget.
207     $this->getSession()->getPage()->pressButton('Select entities');
208
209     // Check that the entity browser widget is closed.
210     $this->assertSession()->buttonNotExists('Second submit button');
211   }
212
213 }