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 // 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.');
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');
75 $edit = ['display_id' => 'test 1'];
76 $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
77 $this->assertText($error_text);
79 $edit = ['display_id' => 'test_1#'];
80 $this->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
81 $this->assertText($error_text);
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.'));
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'));
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.
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..');
107 * Tests the language options on the views edit form.
109 public function testEditFormLanguageOptions() {
110 // Language options should not exist without language module.
112 'test_view' => 'default',
113 'test_display' => 'page_1',
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'));
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();
128 $this->rebuildContainer();
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'));
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'));
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.'));
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***',
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]);
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.
173 'authenticated[administer languages]' => TRUE,
175 $this->drupalPostForm('/admin/people/permissions', $edit, t('Save permissions'));
176 // Enable Content language negotiation so we have one more item
179 'language_content[configurable]' => TRUE,
181 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
183 // Choose the new negotiation as the rendering language.
185 'rendering_language' => '***LANGUAGE_language_content***',
187 $this->drupalPostForm('/admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language', $edit, t('Apply'));
189 // Disable language content negotiation.
191 'language_content[configurable]' => FALSE,
193 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
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');
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);
205 $expected_elements = [
207 '***LANGUAGE_site_default***',
208 '***LANGUAGE_language_interface***',
209 '***LANGUAGE_language_content***',
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]);
225 * Tests Representative Node for a Taxonomy Term.
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');
233 $this->drupalPostForm('admin/structure/views/nojs/handler/test_groupwise_term_ui/default/relationship/tid_representative', $edit, 'Apply');