PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views_ui/src/Tests/ViewEditTest.php

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