3 namespace Drupal\views_ui\Tests;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\views\Entity\View;
9 * Tests some general functionality of editing views, like deleting a view.
13 class ViewEditTest extends UITestBase {
16 * Views used by this test.
20 public static $testViews = ['test_view', 'test_display', 'test_groupwise_term_ui'];
23 * Tests the delete link on a views UI.
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');
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()]));
36 $this->assertUrl('admin/structure/views');
37 $view = $this->container->get('entity.manager')->getStorage('view')->load('test_view');
38 $this->assertFalse($view instanceof View);
42 * Tests the machine name and administrative comment forms.
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');
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...');
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'));
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.');
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'));
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.');
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');
81 $edit = ['display_id' => 'test 1'];
82 $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
83 $this->assertText($error_text);
85 $edit = ['display_id' => 'test_1#'];
86 $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
87 $this->assertText($error_text);
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.'));
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'));
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.
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..');
113 * Tests the language options on the views edit form.
115 public function testEditFormLanguageOptions() {
116 // Language options should not exist without language module.
118 'test_view' => 'default',
119 'test_display' => 'page_1',
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'));
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();
134 $this->rebuildContainer();
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'));
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'));
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.'));
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***',
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]);
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.
179 'authenticated[administer languages]' => TRUE,
181 $this->drupalPostForm('/admin/people/permissions', $edit, t('Save permissions'));
182 // Enable Content language negotiation so we have one more item
185 'language_content[configurable]' => TRUE,
187 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
189 // Choose the new negotiation as the rendering language.
191 'rendering_language' => '***LANGUAGE_language_content***',
193 $this->drupalPostForm('/admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language', $edit, t('Apply'));
195 // Disable language content negotiation.
197 'language_content[configurable]' => FALSE,
199 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
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');
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);
211 $expected_elements = [
213 '***LANGUAGE_site_default***',
214 '***LANGUAGE_language_interface***',
215 '***LANGUAGE_language_content***',
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]);
231 * Tests Representative Node for a Taxonomy Term.
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');
239 $this->drupalPostForm('admin/structure/views/nojs/handler/test_groupwise_term_ui/default/relationship/tid_representative', $edit, 'Apply');