Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / Tests / ConfigUITest.php
1 <?php
2
3 namespace Drupal\entity_browser\Tests;
4
5 use Drupal\entity_browser\Plugin\EntityBrowser\Display\IFrame;
6 use Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\NoDisplay;
7 use Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector\Tabs;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Tests the entity browser config UI.
12  *
13  * @group entity_browser
14  */
15 class ConfigUITest extends WebTestBase {
16
17   /**
18    * The test user.
19    *
20    * @var \Drupal\User\UserInterface
21    */
22   protected $adminUser;
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['entity_browser', 'ctools', 'block', 'views', 'entity_browser_entity_form'];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36     $this->drupalPlaceBlock('local_actions_block');
37     $this->adminUser = $this->drupalCreateUser([
38       'administer entity browsers',
39     ]);
40   }
41
42   /**
43    * Tests the entity browser config UI.
44    */
45   public function testConfigUI() {
46     // We need token module to test upload widget settings.
47     $this->container->get('module_installer')->install(['token']);
48
49     $this->drupalGet('/admin/config/content/entity_browser');
50     $this->assertResponse(403, "Anonymous user can't access entity browser listing page.");
51     $this->drupalGet('/admin/config/content/entity_browser/add');
52     $this->assertResponse(403, "Anonymous user can't access entity browser add form.");
53
54     // Listing is empty.
55     $this->drupalLogin($this->adminUser);
56     $this->drupalGet('/admin/config/content/entity_browser');
57     $this->assertResponse(200, 'Admin user is able to navigate to the entity browser listing page.');
58     $this->assertText('There are no entity browser entities yet.', 'Entity browsers table is empty.');
59
60     // Add page.
61     $this->clickLink('Add Entity browser');
62     $this->assertUrl('/admin/config/content/entity_browser/add');
63     $edit = [
64       'label' => 'Test entity browser',
65       'id' => 'test_entity_browser',
66       'display' => 'iframe',
67       'widget_selector' => 'tabs',
68       'selection_display' => 'no_display',
69     ];
70     $this->drupalPostForm(NULL, $edit, 'Next');
71
72     // Display configuration step.
73     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/display', ['query' => ['js' => 'nojs']]);
74     $edit = [
75       'width' => 100,
76       'height' => 100,
77       'link_text' => 'All animals are created equal',
78       'auto_open' => TRUE,
79     ];
80     $this->drupalPostForm(NULL, $edit, 'Next');
81
82     // Widget selector step.
83     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/widget_selector', ['query' => ['js' => 'nojs']]);
84     $this->assertText('This plugin has no configuration options.');
85     $this->drupalPostForm(NULL, [], 'Next');
86
87     // Selection display step.
88     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/selection_display', ['query' => ['js' => 'nojs']]);
89     $this->assertText('This plugin has no configuration options.');
90     $this->drupalPostForm(NULL, [], 'Previous');
91
92     // Widget selector step again.
93     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/widget_selector', ['query' => ['js' => 'nojs']]);
94     $this->assertText('This plugin has no configuration options.');
95     $this->drupalPostForm(NULL, [], 'Next');
96
97     // Selection display step.
98     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/selection_display', ['query' => ['js' => 'nojs']]);
99     $this->assertText('This plugin has no configuration options.');
100     $this->drupalPostForm(NULL, [], 'Next');
101
102     // Widgets step.
103     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/widgets', ['query' => ['js' => 'nojs']]);
104     $this->assertText('The available plugins are:');
105     $this->assertText("Upload: Adds an upload field browser's widget.");
106     $this->assertText("View: Uses a view to provide entity listing in a browser's widget.");
107     $this->assertText("Entity form: Provides entity form widget.");
108     $this->drupalPostAjaxForm(NULL, ['widget' => 'upload'], 'widget');
109     $this->assertText('Label (Upload)');
110     $this->assertText('You can use tokens in the upload location.');
111     $this->assertLink('Browse available tokens.');
112
113     // Make sure that removing of widgets works.
114     $this->drupalPostAjaxForm(NULL, ['widget' => 'view'], 'widget');
115     $this->assertText('Label (View)');
116     $this->assertText('View : View display', 'View selection dropdown label found.');
117     $this->assertRaw('- Select a view -', 'Empty option appears in the view selection dropdown.');
118     $this->assertText('Submit button text', 'Widget submit button text element found.');
119     $this->assertFieldByXPath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-submit-text")]', 'Select entities', 'Widget submit button text element found.');
120     $delete_buttons = $this->xpath("//input[@value='Delete']");
121     $delete_button_name = (string) $delete_buttons[1]->attributes()['name'];
122     $this->drupalPostAjaxForm(NULL, [], [$delete_button_name => 'Delete']);
123     $this->assertNoText('View : View display', 'View widget was removed.');
124     $this->assertNoRaw('- Select a view -', 'View widget was removed.');
125     $this->assertEqual(count($this->xpath("//input[@value='Delete']")), 1, 'Only one delete button appears on the page.');
126
127     // Make sure the "Entity form" widget has all available config elements.
128     $this->drupalPostAjaxForm(NULL, ['widget' => 'entity_form'], 'widget');
129     $this->assertText('Label (Entity form)');
130     $this->assertText('Entity type', 'Entity type select found on IEF widget.');
131     $this->assertText('Bundle', 'Bundle select found on IEF widget.');
132     $this->assertText('Form mode', 'Form mode select found on IEF widget.');
133     $this->assertFieldByXPath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-submit-text")]', 'Save entity', 'Widget submit button text element found.');
134     $entity_type_element = $this->xpath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-entity-type")]');
135     $entity_type_name = (string) $entity_type_element[0]['name'];
136     $edit = [
137       $entity_type_name => 'user',
138     ];
139     $commands = $this->drupalPostAjaxForm(NULL, $edit, $entity_type_name);
140     // WebTestBase::drupalProcessAjaxResponse() won't correctly execute our ajax
141     // commands so we have to do it manually. Code below is based on the logic
142     // in that function.
143     $content = $this->content;
144     $dom = new \DOMDocument();
145     @$dom->loadHTML($content);
146     $xpath = new \DOMXPath($dom);
147     foreach ($commands as $command) {
148       if ($command['command'] == 'insert' && $command['method'] == 'replaceWith') {
149         $wrapperNode = $xpath->query('//*[@id="' . ltrim($command['selector'], '#') . '"]')->item(0);
150         $newDom = new \DOMDocument();
151         @$newDom->loadHTML('<div>' . $command['data'] . '</div>');
152         $newNode = @$dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
153         $wrapperNode->parentNode->replaceChild($newNode, $wrapperNode);
154         $content = $dom->saveHTML();
155         $this->setRawContent($content);
156       }
157     }
158     $this->verbose($content);
159     // Assure the form_mode "Register" is one of the available options.
160     $form_mode_element = $this->xpath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-form-mode-form-select")]');
161     $form_mode_id = (string) $form_mode_element[0]['id'];
162     $form_mode_name = (string) $form_mode_element[0]['name'];
163     $this->assertOption($form_mode_id, 'register', 'A non-default form mode is correctly available to be chosen.');
164     $bundle_element = $this->xpath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-bundle-select")]');
165     $bundle_name = (string) $bundle_element[0]['name'];
166     $submit_text_element = $this->xpath('//*[starts-with(@data-drupal-selector, "edit-table-") and contains(@data-drupal-selector, "-form-submit-text")]');
167     $submit_text_name = (string) $submit_text_element[1]['name'];
168     $edit = [
169       $entity_type_name => 'user',
170       $bundle_name => 'user',
171       $form_mode_name => 'register',
172       $submit_text_name => 'But some are more equal than others',
173     ];
174     $this->drupalPostForm(NULL, $edit, 'Finish');
175
176     // Back on listing page.
177     $this->assertUrl('/admin/config/content/entity_browser');
178     $this->assertText('Test entity browser', 'Entity browser label found on the listing page');
179     $this->assertText('test_entity_browser', 'Entity browser ID found on the listing page.');
180
181     // Check structure of entity browser object.
182     /** @var \Drupal\entity_browser\EntityBrowserInterface $loaded_entity_browser */
183     $loaded_entity_browser = $this->container->get('entity_type.manager')
184       ->getStorage('entity_browser')
185       ->load('test_entity_browser');
186     $this->assertEqual('test_entity_browser', $loaded_entity_browser->id(), 'Entity browser ID was correctly saved.');
187     $this->assertEqual('Test entity browser', $loaded_entity_browser->label(), 'Entity browser label was correctly saved.');
188     $this->assertTrue($loaded_entity_browser->getDisplay() instanceof IFrame, 'Entity browser display was correctly saved.');
189     $expected = [
190       'width' => '100',
191       'height' => '100',
192       'link_text' => 'All animals are created equal',
193       'auto_open' => TRUE,
194     ];
195     $this->assertEqual($expected, $loaded_entity_browser->getDisplay()->getConfiguration(), 'Entity browser display configuration was correctly saved.');
196     $this->assertTrue($loaded_entity_browser->getSelectionDisplay() instanceof NoDisplay, 'Entity browser selection display was correctly saved.');
197     $this->assertEqual([], $loaded_entity_browser->getSelectionDisplay()->getConfiguration(), 'Entity browser selection display configuration was correctly saved.');
198     $this->assertEqual($loaded_entity_browser->getWidgetSelector() instanceof Tabs, 'Entity browser widget selector was correctly saved.');
199     $this->assertEqual([], $loaded_entity_browser->getWidgetSelector()->getConfiguration(), 'Entity browser widget selector configuration was correctly saved.');
200
201     $widgets = $loaded_entity_browser->getWidgets();
202     $instance_ids = $widgets->getInstanceIds();
203     $first_uuid = current($instance_ids);
204     $second_uuid = next($instance_ids);
205     /** @var \Drupal\entity_browser\WidgetInterface $widget */
206     $widget = $widgets->get($first_uuid);
207     $this->assertEqual('upload', $widget->id(), 'Entity browser widget was correctly saved.');
208     $this->assertEqual($first_uuid, $widget->uuid(), 'Entity browser widget uuid was correctly saved.');
209     $configuration = $widget->getConfiguration()['settings'];
210     $this->assertEqual([
211       'upload_location' => 'public://',
212       'multiple' => TRUE,
213       'submit_text' => 'Select files',
214       'extensions' => 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
215     ], $configuration, 'Entity browser widget configuration was correctly saved.');
216     $this->assertEqual(1, $widget->getWeight(), 'Entity browser widget weight was correctly saved.');
217     $widget = $widgets->get($second_uuid);
218     $this->assertEqual('entity_form', $widget->id(), 'Entity browser widget was correctly saved.');
219     $this->assertEqual($second_uuid, $widget->uuid(), 'Entity browser widget uuid was correctly saved.');
220     $configuration = $widget->getConfiguration()['settings'];
221     $this->assertEqual([
222       'entity_type' => 'user',
223       'bundle' => 'user',
224       'form_mode' => 'register',
225       'submit_text' => 'But some are more equal than others',
226     ], $configuration, 'Entity browser widget configuration was correctly saved.');
227     $this->assertEqual(2, $widget->getWeight(), 'Entity browser widget weight was correctly saved.');
228
229     // Navigate to edit.
230     $this->clickLink('Edit');
231     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser');
232     $this->assertFieldById('edit-label', 'Test entity browser', 'Correct label found.');
233     $this->assertText('test_entity_browser', 'Correct id found.');
234     $this->assertOptionSelected('edit-display', 'iframe', 'Correct display selected.');
235     $this->assertOptionSelected('edit-widget-selector', 'tabs', 'Correct widget selector selected.');
236     $this->assertOptionSelected('edit-selection-display', 'no_display', 'Correct selection display selected.');
237
238     $this->drupalPostForm(NULL, [], 'Next');
239     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/display', ['query' => ['js' => 'nojs']]);
240     $this->assertFieldById('edit-width', '100', 'Correct value for width found.');
241     $this->assertFieldById('edit-height', '100', 'Correct value for height found.');
242     $this->assertFieldById('edit-link-text', 'All animals are created equal', 'Correct value for link text found.');
243     $this->assertFieldChecked('edit-auto-open', 'Auto open is enabled.');
244
245     $this->drupalPostForm(NULL, [], 'Next');
246     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/widget_selector', ['query' => ['js' => 'nojs']]);
247
248     $this->drupalPostForm(NULL, [], 'Next');
249     $this->assertUrl('/admin/config/content/entity_browser/test_entity_browser/selection_display', ['query' => ['js' => 'nojs']]);
250
251     $this->drupalPostForm(NULL, [], 'Next');
252     $this->assertFieldById('edit-table-' . $first_uuid . '-label', 'upload', 'Correct value for widget label found.');
253     $this->assertFieldChecked('edit-table-' . $first_uuid . '-form-multiple', 'Accept multiple files option is enabled by default.');
254     $this->assertText('Multiple uploads will only be accepted if the source field allows more than one value.');
255     $this->assertFieldById('edit-table-' . $first_uuid . '-form-upload-location', 'public://', 'Correct value for upload location found.');
256     $this->assertFieldByXPath("//input[@data-drupal-selector='edit-table-" . $first_uuid . "-form-submit-text']", 'Select files', 'Correct value for submit text found.');
257     $this->assertFieldById('edit-table-' . $second_uuid . '-label', 'entity_form', 'Correct value for widget label found.');
258     $this->assertOptionSelectedWithDrupalSelector('edit-table-' . $second_uuid . '-form-entity-type', 'user', 'Correct value for entity type found.');
259     $this->assertOptionSelectedWithDrupalSelector('edit-table-' . $second_uuid . '-form-bundle-select', 'user', 'Correct value for bundle found.');
260     $this->assertOptionSelectedWithDrupalSelector('edit-table-' . $second_uuid . '-form-form-mode-form-select', 'register', 'Correct value for form modes found.');
261     $this->assertFieldByXPath("//input[@data-drupal-selector='edit-table-" . $second_uuid . "-form-submit-text']", 'But some are more equal than others', 'Correct value for submit text found.');
262
263     $this->drupalPostForm(NULL, ['table[' . $first_uuid . '][form][multiple]' => FALSE], 'Finish');
264     $this->drupalGet('/admin/config/content/entity_browser/test_entity_browser/widgets');
265     $this->assertNoFieldChecked('edit-table-' . $first_uuid . '-form-multiple', 'Accept multiple files option is disabled.');
266
267     $this->drupalLogout();
268     $this->drupalGet('/admin/config/content/entity_browser/test_entity_browser');
269     $this->assertResponse(403, "Anonymous user can't access entity browser edit form.");
270
271     $this->drupalLogin($this->adminUser);
272     $this->drupalGet('/admin/config/content/entity_browser');
273     $this->clickLink('Delete');
274     $this->assertText('This action cannot be undone.', 'Delete question found.');
275     $this->drupalPostForm(NULL, [], 'Delete Entity Browser');
276
277     $this->assertText('Entity browser Test entity browser was deleted.', 'Confirmation message found.');
278     $this->assertText('There are no entity browser entities yet.', 'Entity browsers table is empty.');
279     $this->drupalLogout();
280   }
281
282 }