Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Handler / FilterBooleanOperatorStringTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Handler;
4
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\views\Views;
7
8 /**
9  * Tests the core Drupal\views\Plugin\views\filter\BooleanOperatorString
10  * handler.
11  *
12  * @group views
13  * @see \Drupal\views\Plugin\views\filter\BooleanOperatorString
14  */
15 class FilterBooleanOperatorStringTest extends ViewsKernelTestBase {
16
17   /**
18    * The modules to enable for this test.
19    *
20    * @var array
21    */
22   public static $modules = ['system'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_view'];
30
31   /**
32    * Map column names.
33    *
34    * @var array
35    */
36   protected $columnMap = [
37     'views_test_data_id' => 'id',
38   ];
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function schemaDefinition() {
44     $schema = parent::schemaDefinition();
45
46     $schema['views_test_data']['fields']['status'] = [
47       'description' => 'The status of this record',
48       'type' => 'varchar',
49       'length' => 255,
50       'not null' => TRUE,
51       'default' => '',
52     ];
53
54     return $schema;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function viewsData() {
61     $views_data = parent::viewsData();
62
63     $views_data['views_test_data']['status']['filter']['id'] = 'boolean_string';
64
65     return $views_data;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function dataSet() {
72     $data = parent::dataSet();
73
74     foreach ($data as &$row) {
75       if ($row['status']) {
76         $row['status'] = 'Enabled';
77       }
78       else {
79         $row['status'] = '';
80       }
81     }
82
83     return $data;
84   }
85
86   /**
87    * Tests the BooleanOperatorString filter.
88    */
89   public function testFilterBooleanOperatorString() {
90     $view = Views::getView('test_view');
91     $view->setDisplay();
92
93     // Add a the status boolean filter.
94     $view->displayHandlers->get('default')->overrideOption('filters', [
95       'status' => [
96         'id' => 'status',
97         'field' => 'status',
98         'table' => 'views_test_data',
99         'value' => 0,
100       ],
101     ]);
102     $this->executeView($view);
103
104     $expected_result = [
105       ['id' => 2],
106       ['id' => 4],
107     ];
108
109     $this->assertEqual(2, count($view->result));
110     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
111
112     $view->destroy();
113     $view->setDisplay();
114
115     // Add the status boolean filter.
116     $view->displayHandlers->get('default')->overrideOption('filters', [
117       'status' => [
118         'id' => 'status',
119         'field' => 'status',
120         'table' => 'views_test_data',
121         'value' => 1,
122       ],
123     ]);
124     $this->executeView($view);
125
126     $expected_result = [
127       ['id' => 1],
128       ['id' => 3],
129       ['id' => 5],
130     ];
131
132     $this->assertEqual(3, count($view->result));
133     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
134   }
135
136   /**
137    * Tests the Boolean filter with grouped exposed form enabled.
138    */
139   public function testFilterGroupedExposed() {
140     $filters = $this->getGroupedExposedFilters();
141     $view = Views::getView('test_view');
142
143     $view->setExposedInput(['status' => 1]);
144     $view->setDisplay();
145     $view->displayHandlers->get('default')->overrideOption('filters', $filters);
146
147     $this->executeView($view);
148
149     $expected_result = [
150       ['id' => 1],
151       ['id' => 3],
152       ['id' => 5],
153     ];
154
155     $this->assertEqual(3, count($view->result));
156     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
157     $view->destroy();
158
159     $view->setExposedInput(['status' => 2]);
160     $view->setDisplay();
161     $view->displayHandlers->get('default')->overrideOption('filters', $filters);
162
163     $this->executeView($view);
164
165     $expected_result = [
166       ['id' => 2],
167       ['id' => 4],
168     ];
169
170     $this->assertEqual(2, count($view->result));
171     $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
172   }
173
174   /**
175    * Provides grouped exposed filter configuration.
176    *
177    * @return array
178    *   Returns the filter configuration for exposed filters.
179    */
180   protected function getGroupedExposedFilters() {
181     $filters = [
182       'status' => [
183         'id' => 'status',
184         'table' => 'views_test_data',
185         'field' => 'status',
186         'relationship' => 'none',
187         'exposed' => TRUE,
188         'expose' => [
189           'operator' => 'status_op',
190           'label' => 'status',
191           'identifier' => 'status',
192         ],
193         'is_grouped' => TRUE,
194         'group_info' => [
195           'label' => 'status',
196           'identifier' => 'status',
197           'default_group' => 'All',
198           'group_items' => [
199             1 => [
200               'title' => 'Active',
201               'operator' => '=',
202               'value' => '1',
203             ],
204             2 => [
205               'title' => 'Blocked',
206               'operator' => '=',
207               'value' => '0',
208             ],
209           ],
210         ],
211       ],
212     ];
213     return $filters;
214   }
215
216 }