ExposedFormTest.php 9.79 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\Plugin\ExposedFormTest.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\views\Tests\Plugin;
9

10
11
use Drupal\views\Tests\UI\UITestBase;

merlinofchaos's avatar
merlinofchaos committed
12
13
14
/**
 * Tests exposed forms.
 */
15
class ExposedFormTest extends UITestBase {
16

17
18
19
20
21
22
23
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_reset_button', 'test_exposed_admin_ui');

24
25
26
27
28
29
30
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('views_ui');

merlinofchaos's avatar
merlinofchaos committed
31
32
33
34
35
36
37
38
  public static function getInfo() {
    return array(
      'name' => 'Exposed forms',
      'description' => 'Test exposed forms functionality.',
      'group' => 'Views Plugins',
    );
  }

39
40
41
  protected function setUp() {
    parent::setUp();

42
43
    $this->drupalCreateContentType(array('type' => 'article'));
    $this->drupalCreateContentType(array('type' => 'page'));
44
45
46
47
48
49
50
51

    // Create some random nodes.
    for ($i = 0; $i < 5; $i++) {
      $this->drupalCreateNode();
    }
  }

  /**
52
   * Tests whether the reset button works on an exposed form.
53
54
55
56
57
58
59
60
61
62
63
   */
  public function testResetButton() {
    $this->drupalGet('test_reset_button', array('query' => array('type' => 'article')));
    // Test that the type has been set.
    $this->assertFieldById('edit-type', 'article', 'Article type filter set.');

    // Test the reset works.
    $this->drupalGet('test_reset_button', array('query' => array('op' => 'Reset')));
    $this->assertResponse(200);
    // Test the type has been reset.
    $this->assertFieldById('edit-type', 'All', 'Article type filter has been reset.');
64
65
  }

merlinofchaos's avatar
merlinofchaos committed
66
67
68
69
70
  /**
   * Tests, whether and how the reset button can be renamed.
   */
  public function testRenameResetButton() {
    // Look at the page and check the label "reset".
71
    $this->drupalGet('test_reset_button');
merlinofchaos's avatar
merlinofchaos committed
72
    // Rename the label of the reset button.
73
    $view = views_get_view('test_reset_button');
74
    $view->setDisplay();
merlinofchaos's avatar
merlinofchaos committed
75

76
    $exposed_form = $view->display_handler->getOption('exposed_form');
merlinofchaos's avatar
merlinofchaos committed
77
78
    $exposed_form['options']['reset_button_label'] = $expected_label = $this->randomName();
    $exposed_form['options']['reset_button'] = TRUE;
79
    $view->display_handler->setOption('exposed_form', $exposed_form);
merlinofchaos's avatar
merlinofchaos committed
80
81
82
83
84
    $view->save();

    views_invalidate_cache();

    // Look whether ther reset button label changed.
85
    $this->drupalGet('test_reset_button');
86
    $this->assertResponse(200);
merlinofchaos's avatar
merlinofchaos committed
87
88
89
90

    $this->helperButtonHasLabel('edit-reset', $expected_label);
  }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  /**
   * Tests the exposed form markup.
   */
  public function testExposedFormRender() {
    $view = views_get_view('test_reset_button');
    $this->executeView($view);
    $exposed_form = $view->display_handler->getPlugin('exposed_form');
    $this->drupalSetContent($exposed_form->render_exposed_form());

    $expected_id = drupal_clean_css_identifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display);
    $this->assertFieldByXpath('//form/@id', $expected_id, 'Expected form ID found.');

    $expected_action = url($view->display_handler->getUrl());
    $this->assertFieldByXPath('//form/@action', $expected_action, 'The expected value for the action attribute was found.');
  }

merlinofchaos's avatar
merlinofchaos committed
107
108
109
110
  /**
   * Tests the admin interface of exposed filter and sort items.
   */
  function testExposedAdminUi() {
111
    menu_router_rebuild();
merlinofchaos's avatar
merlinofchaos committed
112
113
114
    $edit = array();

    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
115
    // Be sure that the button is called exposed.
merlinofchaos's avatar
merlinofchaos committed
116
117
    $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter'));

118
119
120
121
122
123
124
    // The first time the filter UI is displayed, the operator and the
    // value forms should be shown.
    $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
    $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
    $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
    $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');

merlinofchaos's avatar
merlinofchaos committed
125
126
    // Click the Expose filter button.
    $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter'));
127
    // Check the label of the expose button.
merlinofchaos's avatar
merlinofchaos committed
128
    $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter'));
129
130
    // Check the label of the grouped exposed button
    $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters'));
merlinofchaos's avatar
merlinofchaos committed
131

132
133
134
135
136
137
    // After exposing the filter, Operator and Value should be still here.
    $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
    $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
    $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
    $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');

merlinofchaos's avatar
merlinofchaos committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    // Check the validations of the filter handler.
    $edit = array();
    $edit['options[expose][identifier]'] = '';
    $this->drupalPost(NULL, $edit, t('Apply'));
    $this->assertText(t('The identifier is required if the filter is exposed.'));

    $edit = array();
    $edit['options[expose][identifier]'] = 'value';
    $this->drupalPost(NULL, $edit, t('Apply'));
    $this->assertText(t('This identifier is not allowed.'));

    // Now check the sort criteria.
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created');
    $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort'));
    $this->assertNoFieldById('edit-options-expose-label', '', t('Make sure no label field is shown'));

154
155
156
    // Click the Grouped Filters button.
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
    $this->drupalPost(NULL, array(), t('Grouped filters'));
157
158
159
160
161
162
163
164

    // After click on 'Grouped Filters', the standard operator and value should
    // not be displayed.
    $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists');
    $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists');
    $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists');
    $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists');

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    // Check that after click on 'Grouped Filters', a new button is shown to
    // add more items to the list.
    $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item'));

    // Create a grouped filter
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
    $edit = array();
    $edit["options[group_info][group_items][1][title]"] = 'Is Article';
    $edit["options[group_info][group_items][1][value][article]"] = 'article';

    $edit["options[group_info][group_items][2][title]"] = 'Is Page';
    $edit["options[group_info][group_items][2][value][page]"] = TRUE;

    $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
    $edit["options[group_info][group_items][3][value][article]"] = TRUE;
    $edit["options[group_info][group_items][3][value][page]"] = TRUE;
    $this->drupalPost(NULL, $edit, t('Apply'));

183
184
185
186
187
188
189
190
191
192
193
    // Select the empty operator, so the empty value should not trigger a form
    // error.
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/body_value');
    $edit = array();
    $edit["options[group_info][group_items][1][title]"] = $this->randomName();
    $edit["options[group_info][group_items][1][operator]"] = 'empty';
    $this->drupalPost(NULL, $edit, t('Apply'));
    $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', array(), 'Validation did not run for the empty operator.');
    // Test the validation error message text is not shown.
    $this->assertNoText(t('The value is required if title for this item is defined.'));

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    // Validate that all the titles are defined for each group
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
    $edit = array();
    $edit["options[group_info][group_items][1][title]"] = 'Is Article';
    $edit["options[group_info][group_items][1][value][article]"] = TRUE;

    // This should trigger an error
    $edit["options[group_info][group_items][2][title]"] = '';
    $edit["options[group_info][group_items][2][value][page]"] = TRUE;

    $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
    $edit["options[group_info][group_items][3][value][article]"] = TRUE;
    $edit["options[group_info][group_items][3][value][page]"] = TRUE;
    $this->drupalPost(NULL, $edit, t('Apply'));
    $this->assertRaw(t('The title is required if value for this item is defined.'), t('Group items should have a title'));

210
211
212
213
214
215
216
217
218
219
    // Un-expose the filter.
    $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type');
    $this->drupalPost(NULL, array(), t('Hide filter'));

    // After Un-exposing the filter, Operator and Value should be shown again.
    $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter');
    $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter');
    $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter');
    $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter');

merlinofchaos's avatar
merlinofchaos committed
220
221
222
    // Click the Expose sort button.
    $edit = array();
    $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort'));
223
    // Check the label of the expose button.
merlinofchaos's avatar
merlinofchaos committed
224
225
226
    $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort'));
    $this->assertFieldById('edit-options-expose-label', '', t('Make sure a label field is shown'));
  }
227

merlinofchaos's avatar
merlinofchaos committed
228
}