views_exposed_form.test 9.0 KB

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