08c5cfe862b02a1fde2cb6b30110acc623a1fe03
[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     // Test the form validation with invalid IDs.
68     $machine_name_edit_url = 'admin/structure/views/nojs/display/test_view/test_1/display_id';
69     $error_text = t('Display name must be letters, numbers, or underscores only.');
70
71     // Test that potential invalid display ID requests are detected
72     $this->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title');
73     $this->assertText('Invalid display id fake_display_name');
74
75     $edit = ['display_id' => 'test 1'];
76     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
77     $this->assertText($error_text);
78
79     $edit = ['display_id' => 'test_1#'];
80     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
81     $this->assertText($error_text);
82
83     // Test using an existing display ID.
84     $edit = ['display_id' => 'default'];
85     $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
86     $this->assertText(t('Display id should be unique.'));
87
88     // Test that the display ID has not been changed.
89     $this->drupalGet('admin/structure/views/view/test_view/edit/test_1');
90     $this->assertLink(t('test_1'));
91
92     // Test that validation does not run on cancel.
93     $this->drupalGet('admin/structure/views/view/test_view');
94     // Delete the field to cause an error on save.
95     $fields = [];
96     $fields['fields[age][removed]'] = 1;
97     $fields['fields[id][removed]'] = 1;
98     $fields['fields[name][removed]'] = 1;
99     $this->drupalPostForm('admin/structure/views/nojs/rearrange/test_view/default/field', $fields, t('Apply'));
100     $this->drupalPostForm(NULL, [], 'Save');
101     $this->drupalPostForm(NULL, [], t('Cancel'));
102     $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
103     $this->assertUrl('admin/structure/views', [], 'Redirected back to the view listing page..');
104   }
105
106   /**
107    * Tests the language options on the views edit form.
108    */
109   public function testEditFormLanguageOptions() {
110     // Language options should not exist without language module.
111     $test_views = [
112       'test_view' => 'default',
113       'test_display' => 'page_1',
114     ];
115     foreach ($test_views as $view_name => $display) {
116       $this->drupalGet('admin/structure/views/view/' . $view_name);
117       $this->assertResponse(200);
118       $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
119       $this->assertNoLinkByHref($langcode_url);
120       $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
121       $this->assertNoLink(t('Content language of view row'));
122     }
123
124     // Make the site multilingual and test the options again.
125     $this->container->get('module_installer')->install(['language', 'content_translation']);
126     ConfigurableLanguage::createFromLangcode('hu')->save();
127     $this->resetAll();
128     $this->rebuildContainer();
129
130     // Language options should now exist with entity language the default.
131     foreach ($test_views as $view_name => $display) {
132       $this->drupalGet('admin/structure/views/view/' . $view_name);
133       $this->assertResponse(200);
134       $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
135       if ($view_name == 'test_view') {
136         $this->assertNoLinkByHref($langcode_url);
137         $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
138         $this->assertNoLink(t('Content language of view row'));
139       }
140       else {
141         $this->assertLinkByHref($langcode_url);
142         $this->assertNoLink(t('@type language selected for page', ['@type' => t('Content')]));
143         $this->assertLink(t('Content language of view row'));
144       }
145
146       $this->drupalGet($langcode_url);
147       $this->assertResponse(200);
148       if ($view_name == 'test_view') {
149         $this->assertText(t('The view is not based on a translatable entity type or the site is not multilingual.'));
150       }
151       else {
152         $this->assertFieldByName('rendering_language', '***LANGUAGE_entity_translation***');
153         // Test that the order of the language list is similar to other language
154         // lists, such as in the content translation settings.
155         $expected_elements = [
156           '***LANGUAGE_entity_translation***',
157           '***LANGUAGE_entity_default***',
158           '***LANGUAGE_site_default***',
159           '***LANGUAGE_language_interface***',
160           'en',
161           'hu',
162         ];
163         $elements = $this->xpath('//select[@id="edit-rendering-language"]/option');
164         // Compare values inside the option elements with expected values.
165         for ($i = 0; $i < count($elements); $i++) {
166           $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]);
167         }
168
169         // Check that the selected values are respected even we they are not
170         // supposed to be listed.
171         // Give permission to edit languages to authenticated users.
172         $edit = [
173           'authenticated[administer languages]' => TRUE,
174         ];
175         $this->drupalPostForm('/admin/people/permissions', $edit, t('Save permissions'));
176         // Enable Content language negotiation so we have one more item
177         // to select.
178         $edit = [
179           'language_content[configurable]' => TRUE,
180         ];
181         $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
182
183         // Choose the new negotiation as the rendering language.
184         $edit = [
185           'rendering_language' => '***LANGUAGE_language_content***',
186         ];
187         $this->drupalPostForm('/admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language', $edit, t('Apply'));
188
189         // Disable language content negotiation.
190         $edit = [
191           'language_content[configurable]' => FALSE,
192         ];
193         $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
194
195         // Check that the previous selection is listed and selected.
196         $this->drupalGet($langcode_url);
197         $element = $this->xpath('//select[@id="edit-rendering-language"]/option[@value="***LANGUAGE_language_content***" and @selected="selected"]');
198         $this->assertFalse(empty($element), 'Current selection is not lost');
199
200         // Check the order for the langcode filter.
201         $langcode_url = 'admin/structure/views/nojs/handler/' . $view_name . '/' . $display . '/filter/langcode';
202         $this->drupalGet($langcode_url);
203         $this->assertResponse(200);
204
205         $expected_elements = [
206           'all',
207           '***LANGUAGE_site_default***',
208           '***LANGUAGE_language_interface***',
209           '***LANGUAGE_language_content***',
210           'en',
211           'hu',
212           'und',
213           'zxx',
214         ];
215         $elements = $this->xpath('//div[@id="edit-options-value"]//input');
216         // Compare values inside the option elements with expected values.
217         for ($i = 0; $i < count($elements); $i++) {
218           $this->assertEqual($elements[$i]->attributes()->{'value'}, $expected_elements[$i]);
219         }
220       }
221     }
222   }
223
224   /**
225    * Tests Representative Node for a Taxonomy Term.
226    */
227   public function testRelationRepresentativeNode() {
228     // Populate and submit the form.
229     $edit["name[taxonomy_term_field_data.tid_representative]"] = TRUE;
230     $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_groupwise_term_ui/default/relationship', $edit, 'Add and configure relationships');
231     // Apply changes.
232     $edit = [];
233     $this->drupalPostForm('admin/structure/views/nojs/handler/test_groupwise_term_ui/default/relationship/tid_representative', $edit, 'Apply');
234   }
235
236 }