views_exposed_form.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsExposedFormTest.
  5. */
  6. /**
  7. * Tests exposed forms.
  8. */
  9. class ViewsExposedFormTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Exposed forms',
  13. 'description' => 'Test exposed forms functionality.',
  14. 'group' => 'Views Plugins',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp('views_ui');
  19. module_enable(array('views_ui'));
  20. // @TODO Figure out why it's required to clear the cache here.
  21. views_module_include('views_default', TRUE);
  22. views_get_all_views(TRUE);
  23. menu_rebuild();
  24. }
  25. /**
  26. * Tests, whether and how the reset button can be renamed.
  27. */
  28. public function testRenameResetButton() {
  29. $account = $this->drupalCreateUser();
  30. $this->drupalLogin($account);
  31. // Create some random nodes.
  32. for ($i = 0; $i < 5; $i++) {
  33. $this->drupalCreateNode();
  34. }
  35. // Look at the page and check the label "reset".
  36. $this->drupalGet('test_rename_reset_button');
  37. // Rename the label of the reset button.
  38. $view = views_get_view('test_rename_reset_button');
  39. $view->set_display('default');
  40. $exposed_form = $view->display_handler->get_option('exposed_form');
  41. $exposed_form['options']['reset_button_label'] = $expected_label = $this->randomName();
  42. $exposed_form['options']['reset_button'] = TRUE;
  43. $view->display_handler->set_option('exposed_form', $exposed_form);
  44. $view->save();
  45. views_invalidate_cache();
  46. // Look whether ther reset button label changed.
  47. $this->drupalGet('test_rename_reset_button');
  48. $this->helperButtonHasLabel('edit-reset', $expected_label);
  49. }
  50. /**
  51. * Tests that exposed values are correctly stored.
  52. */
  53. public function testRemember() {
  54. $account = $this->drupalCreateUser();
  55. $this->drupalLogin($account);
  56. // Create some random nodes.
  57. for ($i = 0; $i < 5; $i++) {
  58. $this->drupalCreateNode();
  59. }
  60. // Set the exposed filter.
  61. $this->drupalGet('test_exposed_remember', array('query' => array('type' => 'page')));
  62. $this->assertFieldByName('type', 'page');
  63. // Request the page again, should still be set.
  64. $this->drupalGet('test_exposed_remember');
  65. $this->assertFieldByName('type', 'page');
  66. // Request the page with an unrelated GET argument, filter should still be set.
  67. $this->drupalGet('test_exposed_remember', array('query' => array('argument' => 'value')));
  68. $this->assertFieldByName('type', 'page');
  69. // Change the remembered exposed value.
  70. $this->drupalGet('test_exposed_remember', array('query' => array('type' => 'article')));
  71. $this->assertFieldByName('type', 'article');
  72. // Request the page again, should have remembered the new value.
  73. $this->drupalGet('test_exposed_remember');
  74. $this->assertFieldByName('type', 'article');
  75. }
  76. /**
  77. * Tests the admin interface of exposed filter and sort items.
  78. */
  79. function testExposedAdminUi() {
  80. $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
  81. $this->drupalLogin($admin_user);
  82. menu_rebuild();
  83. $edit = array();
  84. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  85. // Be sure that the button is called exposed.
  86. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter'));
  87. // The first time the filter UI is displayed, the operator and the
  88. // value forms should be shown.
  89. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
  90. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
  91. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  92. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
  93. // Click the Expose filter button.
  94. $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter'));
  95. // Check the label of the expose button.
  96. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter'));
  97. // Check the label of the grouped exposed button
  98. $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters'));
  99. // After Expose the filter, Operator and Value should be still here
  100. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
  101. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
  102. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  103. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
  104. // Check the validations of the filter handler.
  105. $edit = array();
  106. $edit['options[expose][identifier]'] = '';
  107. $this->drupalPost(NULL, $edit, t('Apply'));
  108. $this->assertText(t('The identifier is required if the filter is exposed.'));
  109. $edit = array();
  110. $edit['options[expose][identifier]'] = 'value';
  111. $this->drupalPost(NULL, $edit, t('Apply'));
  112. $this->assertText(t('This identifier is not allowed.'));
  113. // Now check the sort criteria.
  114. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created');
  115. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort'));
  116. $this->assertNoFieldById('edit-options-expose-label', '', t('Make sure no label field is shown'));
  117. // Click the Grouped Filters button.
  118. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  119. $this->drupalPost(NULL, array(), t('Grouped filters'));
  120. // After click on 'Grouped Filters' standard operator and value should not be displayed
  121. $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists');
  122. $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists');
  123. $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists');
  124. $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists');
  125. // Check that after click on 'Grouped Filters', a new button is shown to
  126. // add more items to the list.
  127. $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item'));
  128. // Create a grouped filter
  129. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  130. $edit = array();
  131. $edit["options[group_info][group_items][1][title]"] = 'Is Article';
  132. $edit["options[group_info][group_items][1][value][article]"] = 'article';
  133. $edit["options[group_info][group_items][2][title]"] = 'Is Page';
  134. $edit["options[group_info][group_items][2][value][page]"] = TRUE;
  135. $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
  136. $edit["options[group_info][group_items][3][value][article]"] = TRUE;
  137. $edit["options[group_info][group_items][3][value][page]"] = TRUE;
  138. $this->drupalPost(NULL, $edit, t('Apply'));
  139. // Validate that all the titles are defined for each group
  140. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  141. $edit = array();
  142. $edit["options[group_info][group_items][1][title]"] = 'Is Article';
  143. $edit["options[group_info][group_items][1][value][article]"] = TRUE;
  144. // This should trigger an error
  145. $edit["options[group_info][group_items][2][title]"] = '';
  146. $edit["options[group_info][group_items][2][value][page]"] = TRUE;
  147. $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
  148. $edit["options[group_info][group_items][3][value][article]"] = TRUE;
  149. $edit["options[group_info][group_items][3][value][page]"] = TRUE;
  150. $this->drupalPost(NULL, $edit, t('Apply'));
  151. $this->assertRaw(t('The title is required if value for this item is defined.'), t('Group items should have a title'));
  152. // Un-Expose the filter
  153. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  154. $this->drupalPost(NULL, array(), t('Hide filter'));
  155. // After Un-Expose the filter, Operator and Value should be shown again
  156. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter');
  157. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter');
  158. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter');
  159. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter');
  160. // Click the Expose sort button.
  161. $edit = array();
  162. $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort'));
  163. // Check the label of the expose button.
  164. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort'));
  165. $this->assertFieldById('edit-options-expose-label', '', t('Make sure a label field is shown'));
  166. }
  167. }