views_exposed_form.test

Definition of ViewsExposedFormTest.

File

tests/views_exposed_form.test
View source
  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 the admin interface of exposed filter and sort items.
  52. */
  53. function testExposedAdminUi() {
  54. $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
  55. $this->drupalLogin($admin_user);
  56. menu_rebuild();
  57. $edit = array();
  58. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  59. // Be sure that the button is called exposed.
  60. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter'));
  61. // The first time the filter UI is displayed, the operator and the
  62. // value forms should be shown.
  63. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
  64. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
  65. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  66. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
  67. // Click the Expose filter button.
  68. $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter'));
  69. // Check the label of the expose button.
  70. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter'));
  71. // Check the label of the grouped exposed button
  72. $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters'));
  73. // After Expose the filter, Operator and Value should be still here
  74. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
  75. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
  76. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  77. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
  78. // Check the validations of the filter handler.
  79. $edit = array();
  80. $edit['options[expose][identifier]'] = '';
  81. $this->drupalPost(NULL, $edit, t('Apply'));
  82. $this->assertText(t('The identifier is required if the filter is exposed.'));
  83. $edit = array();
  84. $edit['options[expose][identifier]'] = 'value';
  85. $this->drupalPost(NULL, $edit, t('Apply'));
  86. $this->assertText(t('This identifier is not allowed.'));
  87. // Now check the sort criteria.
  88. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created');
  89. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort'));
  90. $this->assertNoFieldById('edit-options-expose-label', '', t('Make sure no label field is shown'));
  91. // Click the Grouped Filters button.
  92. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  93. $this->drupalPost(NULL, array(), t('Grouped filters'));
  94. // After click on 'Grouped Filters' standard operator and value should not be displayed
  95. $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists');
  96. $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists');
  97. $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists');
  98. $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists');
  99. // Check that after click on 'Grouped Filters', a new button is shown to
  100. // add more items to the list.
  101. $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item'));
  102. // Create a grouped filter
  103. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  104. $edit = array();
  105. $edit["options[group_info][group_items][1][title]"] = 'Is Article';
  106. $edit["options[group_info][group_items][1][value][article]"] = 'article';
  107. $edit["options[group_info][group_items][2][title]"] = 'Is Page';
  108. $edit["options[group_info][group_items][2][value][page]"] = TRUE;
  109. $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
  110. $edit["options[group_info][group_items][3][value][article]"] = TRUE;
  111. $edit["options[group_info][group_items][3][value][page]"] = TRUE;
  112. $this->drupalPost(NULL, $edit, t('Apply'));
  113. // Validate that all the titles are defined for each group
  114. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  115. $edit = array();
  116. $edit["options[group_info][group_items][1][title]"] = 'Is Article';
  117. $edit["options[group_info][group_items][1][value][article]"] = TRUE;
  118. // This should trigger an error
  119. $edit["options[group_info][group_items][2][title]"] = '';
  120. $edit["options[group_info][group_items][2][value][page]"] = TRUE;
  121. $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
  122. $edit["options[group_info][group_items][3][value][article]"] = TRUE;
  123. $edit["options[group_info][group_items][3][value][page]"] = TRUE;
  124. $this->drupalPost(NULL, $edit, t('Apply'));
  125. $this->assertRaw(t('The title is required if value for this item is defined.'), t('Group items should have a title'));
  126. // Un-Expose the filter
  127. $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
  128. $this->drupalPost(NULL, array(), t('Hide filter'));
  129. // After Un-Expose the filter, Operator and Value should be shown again
  130. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter');
  131. $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter');
  132. $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter');
  133. $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter');
  134. // Click the Expose sort button.
  135. $edit = array();
  136. $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort'));
  137. // Check the label of the expose button.
  138. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort'));
  139. $this->assertFieldById('edit-options-expose-label', '', t('Make sure a label field is shown'));
  140. }
  141. }