Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / src / Tests / ViewEditTest.php
1 <?php
2
3 namespace Drupal\views_ui\Tests;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\views\Entity\View;
7
8 /**
9  * Tests some general functionality of editing views, like deleting a view.
10  *
11  * @group views_ui
12  */
13 class ViewEditTest extends UITestBase {
14
15   /**
16    * Views used by this test.
17    *
18    * @var array
19    */
20   public static $testViews = ['test_view', 'test_display', 'test_groupwise_term_ui'];
21
22   /**
23    * Tests the delete link on a views UI.
24    */
25   public function testDeleteLink() {
26     $this->drupalGet('admin/structure/views/view/test_view');
27     $this->assertLink(t('Delete view'), 0, 'Ensure that the view delete link appears');
28
29     $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view');
30     $this->assertTrue($view instanceof View);
31     $this->clickLink(t('Delete view'));
32     $this->assertUrl('admin/structure/views/view/test_view/delete');
33     $this->drupalPostForm(NULL, [], t('Delete'));
34     $this->assertRaw(t('The view %name has been deleted.', ['%name' => $view->label()]));
35
36     $this->assertUrl('admin/structure/views');
37     $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view');
38     $this->assertFalse($view instanceof View);
39   }
40
41   /**
42    * Tests the machine name and administrative comment forms.
43    */
44   public function testOtherOptions() {
45     $this->drupalGet('admin/structure/views/view/test_view');
46     // Add a new attachment display.
47     $this->drupalPostForm(NULL, [], 'Add Attachment');
48
49     // Test that a long administrative comment is truncated.
50     $edit = ['display_comment' => 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen'];
51     $this->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_comment', $edit, 'Apply');
52     $this->assertText('one two three four five six seven eight nine ten eleven twelve thirteen fourteen...');
53
54     // Change the machine name for the display from page_1 to test_1.
55     $edit = ['display_id' => 'test_1'];
56     $this->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_id', $edit, 'Apply');
57     $this->assertLink(t('test_1'));
58
59     // Save the view, and test the new ID has been saved.
60     $this->drupalPostForm(NULL, [], 'Save');
61     $view = \Drupal::entityManager()->getStorage('view')->load('test_view');
62     $displays = $view->get('display');
63     $this->assertTrue(!empty($displays['test_1']), 'Display data found for new display ID key.');
64     $this->assertIdentical($displays['test_1']['id'], 'test_1', 'New display ID matches the display ID key.');
65     $this->assertFalse(array_key_exists('attachment_1', $displays), 'Old display ID not found.');
66
67     // Set to the same machine name and save the View.
68     $edit = ['display_id' => 'test_1'];
69     $this->drupalPostForm('admin/structure/views/nojs/display/test_view/test_1/display_id', $edit, 'Apply');
70     $this->drupalPostForm(NULL, [], 'Save');
71     $this->assertLink(t('test_1'));
72
73     // Test the form validation with invalid IDs.
74     $machine_name_edit_url = 'admin/structure/views/nojs/display/test_view/test_1/display_id';
75     $error_text = t('Display name must be letters, numbers, or underscores only.');
76
77     // Test that potential invalid display ID requests are detected
78     $this->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title');
79     $this->assertText('Invalid display id fake_display_name');
80
81     $edit = ['display_id' => 'test 1'];
82     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
83     $this->assertText($error_text);
84
85     $edit = ['display_id' => 'test_1#'];
86     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
87     $this->assertText($error_text);
88
89     // Test using an existing display ID.
90     $edit = ['display_id' => 'default'];
91     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
92     $this->assertText(t('Display id should be unique.'));
93
94     // Test that the display ID has not been changed.
95     $this->drupalGet('admin/structure/views/view/test_view/edit/test_1');
96     $this->assertLink(t('test_1'));
97
98     // Test that validation does not run on cancel.
99     $this->drupalGet('admin/structure/views/view/test_view');
100     // Delete the field to cause an error on save.
101     $fields = [];
102     $fields['fields[age][removed]'] = 1;
103     $fields['fields[id][removed]'] = 1;
104     $fields['fields[name][removed]'] = 1;
105     $this->drupalPostForm('admin/structure/views/nojs/rearrange/test_view/default/field', $fields, t('Apply'));
106     $this->drupalPostForm(NULL, [], 'Save');
107     $this->drupalPostForm(NULL, [], t('Cancel'));
108     $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
109     $this->assertUrl('admin/structure/views', [], 'Redirected back to the view listing page..');
110   }
111
112   /**
113    * Tests the language options on the views edit form.
114    */
115   public function testEditFormLanguageOptions() {
116     // Language options should not exist without language module.
117     $test_views = [
118       'test_view' => 'default',
119       'test_display' => 'page_1',
120     ];
121     foreach ($test_views as $view_name => $display) {
122       $this->drupalGet('admin/structure/views/view/' . $view_name);
123       $this->assertResponse(200);
124       $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
125       $this->assertNoLinkByHref($langcode_url);
126       $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
127       $this->assertNoLink(t('Content language of view row'));
128     }
129
130     // Make the site multilingual and test the options again.
131     $this->container->get('module_installer')->install(['language', 'content_translation']);
132     ConfigurableLanguage::createFromLangcode('hu')->save();
133     $this->resetAll();
134     $this->rebuildContainer();
135
136     // Language options should now exist with entity language the default.
137     foreach ($test_views as $view_name => $display) {
138       $this->drupalGet('admin/structure/views/view/' . $view_name);
139       $this->assertResponse(200);
140       $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
141       if ($view_name == 'test_view') {
142         $this->assertNoLinkByHref($langcode_url);
143         $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
144         $this->assertNoLink(t('Content language of view row'));
145       }
146       else {
147         $this->assertLinkByHref($langcode_url);
148         $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
149         $this->assertLink(t('Content language of view row'));
150       }
151
152       $this->drupalGet($langcode_url);
153       $this->assertResponse(200);
154       if ($view_name == 'test_view') {
155         $this->assertText(t('The view is not based on a translatable entity type or the site is not multilingual.'));
156       }
157       else {
158         $this->assertFieldByName('rendering_language', '***LANGUAGE_entity_translation***');
159         // Test that the order of the language list is similar to other language
160         // lists, such as in the content translation settings.
161         $expected_elements = [
162           '***LANGUAGE_entity_translation***',
163           '***LANGUAGE_entity_default***',
164           '***LANGUAGE_site_default***',
165           '***LANGUAGE_language_interface***',
166           'en',
167           'hu',
168         ];
169         $elements = $this->xpath('//select[@id="edit-rendering-language"]/option');
170         // Compare values inside the option elements with expected values.
171         for ($i = 0; $i < count($elements); $i++) {
172           $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]);
173         }
174
175         // Check that the selected values are respected even we they are not
176         // supposed to be listed.
177         // Give permission to edit languages to authenticated users.
178         $edit = [
179           'authenticated[administer languages]' => TRUE,
180         ];
181         $this->drupalPostForm('/admin/people/permissions', $edit, t('Save permissions'));
182         // Enable Content language negotiation so we have one more item
183         // to select.
184         $edit = [
185           'language_content[configurable]' => TRUE,
186         ];
187         $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
188
189         // Choose the new negotiation as the rendering language.
190         $edit = [
191           'rendering_language' => '***LANGUAGE_language_content***',
192         ];
193         $this->drupalPostForm('/admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language', $edit, t('Apply'));
194
195         // Disable language content negotiation.
196         $edit = [
197           'language_content[configurable]' => FALSE,
198         ];
199         $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
200
201         // Check that the previous selection is listed and selected.
202         $this->drupalGet($langcode_url);
203         $element = $this->xpath('//select[@id="edit-rendering-language"]/option[@value="***LANGUAGE_language_content***" and @selected="selected"]');
204         $this->assertFalse(empty($element), 'Current selection is not lost');
205
206         // Check the order for the langcode filter.
207         $langcode_url = 'admin/structure/views/nojs/handler/' . $view_name . '/' . $display . '/filter/langcode';
208         $this->drupalGet($langcode_url);
209         $this->assertResponse(200);
210
211         $expected_elements = [
212           'all',
213           '***LANGUAGE_site_default***',
214           '***LANGUAGE_language_interface***',
215           '***LANGUAGE_language_content***',
216           'en',
217           'hu',
218           'und',
219           'zxx',
220         ];
221         $elements = $this->xpath('//div[@id="edit-options-value"]//input');
222         // Compare values inside the option elements with expected values.
223         for ($i = 0; $i < count($elements); $i++) {
224           $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]);
225         }
226       }
227     }
228   }
229
230   /**
231    * Tests Representative Node for a Taxonomy Term.
232    */
233   public function testRelationRepresentativeNode() {
234     // Populate and submit the form.
235     $edit["name[taxonomy_term_field_data.tid_representative]"] = TRUE;
236     $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_groupwise_term_ui/default/relationship', $edit, 'Add and configure relationships');
237     // Apply changes.
238     $edit = [];
239     $this->drupalPostForm('admin/structure/views/nojs/handler/test_groupwise_term_ui/default/relationship/tid_representative', $edit, 'Apply');
240   }
241
242 }