FilterTest.php 7.68 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\views\Functional\Plugin;
4

5
use Drupal\Component\Render\FormattableMarkup;
6
use Drupal\Tests\views\Functional\ViewTestBase;
7
use Drupal\views\Views;
8
9
10
11
12
use Drupal\views_test_data\Plugin\views\filter\FilterTest as FilterPlugin;

/**
 * Tests general filter plugin functionality.
 *
13
 * @group views
14
 * @see \Drupal\views\Plugin\views\filter\FilterPluginBase
15
 */
16
class FilterTest extends ViewTestBase {
17

18
19
20
21
22
  /**
   * Views used by this test.
   *
   * @var array
   */
23
  public static $testViews = ['test_filter', 'test_filter_in_operator_ui'];
24

25
26
27
28
29
  /**
   * Modules to enable.
   *
   * @var array
   */
30
  protected static $modules = ['views_ui', 'node'];
31

32
33
34
35
36
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

37
  protected function setUp($import_test_views = TRUE): void {
38
    parent::setUp($import_test_views);
39
40

    $this->enableViewsTestModule();
41

42
    $this->adminUser = $this->drupalCreateUser(['administer views']);
43
44
45
    $this->drupalLogin($this->adminUser);
    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
    $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
46
47
48
  }

  /**
49
   * {@inheritdoc}
50
51
52
   */
  protected function viewsData() {
    $data = parent::viewsData();
53
    $data['views_test_data']['name']['filter']['id'] = 'test_filter';
54
55
56
57
58
59
60
61
62

    return $data;
  }

  /**
   * Test query of the row plugin.
   */
  public function testFilterQuery() {
    // Check that we can find the test filter plugin.
63
    $plugin = $this->container->get('plugin.manager.views.filter')->createInstance('test_filter');
64
    $this->assertInstanceOf(FilterPlugin::class, $plugin);
65

66
    $view = Views::getView('test_filter');
67
68
69
    $view->initDisplay();

    // Change the filtering.
70
71
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'test_filter' => [
72
        'id' => 'test_filter',
73
        'table' => 'views_test_data',
74
75
76
77
        'field' => 'name',
        'operator' => '=',
        'value' => 'John',
        'group' => 0,
78
79
      ],
    ]);
80
81
82
83
84
85
86
87

    $this->executeView($view);

    // Make sure the query have where data.
    $this->assertTrue(!empty($view->query->where));

    // Check the data added.
    $where = $view->query->where;
88
    $this->assertIdentical($where[0]['conditions'][0]['field'], 'views_test_data.name', 'Where condition field matches');
89
90
91
92
93
94
95
96
97
    $this->assertIdentical($where[0]['conditions'][0]['value'], 'John', 'Where condition value matches');
    $this->assertIdentical($where[0]['conditions'][0]['operator'], '=', 'Where condition operator matches');

    $this->executeView($view);

    // Check that our operator and value match on the filter.
    $this->assertIdentical($view->filter['test_filter']->operator, '=');
    $this->assertIdentical($view->filter['test_filter']->value, 'John');

98
99
    // Check that we have a single element, as a result of applying the '= John'
    // filter.
100
    $this->assertCount(1, $view->result, new FormattableMarkup('Results were returned. @count results.', ['@count' => count($view->result)]));
101
102
103
104
105
106

    $view->destroy();

    $view->initDisplay();

    // Change the filtering.
107
108
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'test_filter' => [
109
        'id' => 'test_filter',
110
        'table' => 'views_test_data',
111
112
113
114
        'field' => 'name',
        'operator' => '<>',
        'value' => 'John',
        'group' => 0,
115
116
      ],
    ]);
117
118
119
120
121
122
123

    $this->executeView($view);

    // Check that our operator and value match on the filter.
    $this->assertIdentical($view->filter['test_filter']->operator, '<>');
    $this->assertIdentical($view->filter['test_filter']->value, 'John');

124
125
    // Check if we have the other elements in the dataset, as a result of
    // applying the '<> John' filter.
126
    $this->assertCount(4, $view->result, new FormattableMarkup('Results were returned. @count results.', ['@count' => count($view->result)]));
127
128
129
130
131
132

    $view->destroy();
    $view->initDisplay();

    // Set the test_enable option to FALSE. The 'where' clause should not be
    // added to the query.
133
134
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'test_filter' => [
135
        'id' => 'test_filter',
136
        'table' => 'views_test_data',
137
138
139
140
141
142
        'field' => 'name',
        'operator' => '<>',
        'value' => 'John',
        'group' => 0,
        // Disable this option, so nothing should be added to the query.
        'test_enable' => FALSE,
143
144
      ],
    ]);
145
146
147
148
149

    // Execute the view again.
    $this->executeView($view);

    // Check if we have all 5 results.
150
    $this->assertCount(5, $view->result, new FormattableMarkup('All @count results returned', ['@count' => count($view->displayHandlers)]));
151
152
  }

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  /**
   * Test no error message is displayed when all options are selected in an
   * exposed filter.
   */
  public function testInOperatorSelectAllOptions() {
    $view = Views::getView('test_filter_in_operator_ui');
    $row['row[type]'] = 'fields';
    $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_in_operator_ui/default/row', $row, t('Apply'));
    $field['name[node_field_data.nid]'] = TRUE;
    $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_filter_in_operator_ui/default/field', $field, t('Add and configure fields'));
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/field/nid', [], t('Apply'));
    $edit['options[value][all]'] = TRUE;
    $edit['options[value][article]'] = TRUE;
    $edit['options[value][page]'] = TRUE;
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/type', $edit, t('Apply'));
    $this->drupalPostForm('admin/structure/views/view/test_filter_in_operator_ui/edit/default', [], t('Save'));
    $this->drupalPostForm(NULL, [], t('Update preview'));
    $this->assertNoText('An illegal choice has been detected.');
  }
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  /**
   * Tests the limit of the expose operator functionality.
   */
  public function testLimitExposedOperators() {

    $this->drupalGet('test_filter_in_operator_ui');
    $this->assertResponse(200);
    $this->assertOption('edit-nid-op', '<');
    $this->assertOption('edit-nid-op', '<=');
    $this->assertOption('edit-nid-op', '=');
    $this->assertNoOption('edit-nid-op', '>');
    $this->assertNoOption('edit-nid-op', '>=');

    // Because there are not operators that use the min and max fields, those
    // fields should not be in the exposed form.
    $this->assertFieldById('edit-nid-value');
    $this->assertNoFieldById('edit-nid-min');
    $this->assertNoFieldById('edit-nid-max');

    $edit = [];
    $edit['options[operator]'] = '>';
    $edit['options[expose][operator_list][]'] = ['>', '>=', 'between'];
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/nid', $edit, t('Apply'));
    $this->drupalPostForm('admin/structure/views/view/test_filter_in_operator_ui/edit/default', [], t('Save'));

    $this->drupalGet('test_filter_in_operator_ui');
    $this->assertResponse(200);
    $this->assertNoOption('edit-nid-op', '<');
    $this->assertNoOption('edit-nid-op', '<=');
    $this->assertNoOption('edit-nid-op', '=');
    $this->assertOption('edit-nid-op', '>');
    $this->assertOption('edit-nid-op', '>=');

    $this->assertFieldById('edit-nid-value');
    $this->assertFieldById('edit-nid-min');
    $this->assertFieldById('edit-nid-max');

    // Set the default to an excluded operator.
    $edit = [];
    $edit['options[operator]'] = '=';
    $edit['options[expose][operator_list][]'] = ['<', '>'];
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/nid', $edit, t('Apply'));
    $this->assertText('You selected the "Is equal to" operator as the default value but is not included in the list of limited operators.');
  }

218
}