Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
6 use Drupal\views\Tests\TestHelperPlugin;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * @coversDefaultClass \Drupal\views\Plugin\views\PluginBase
|
Chris@0
|
10 * @group views
|
Chris@0
|
11 */
|
Chris@0
|
12 class PluginBaseTest extends UnitTestCase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The test helper plugin to use for the tests.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var \Drupal\views\Tests\TestHelperPlugin
|
Chris@0
|
18 */
|
Chris@0
|
19 protected $testHelperPlugin;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * {@inheritdoc}
|
Chris@0
|
23 */
|
Chris@0
|
24 protected function setUp() {
|
Chris@0
|
25 parent::setUp();
|
Chris@0
|
26
|
Chris@0
|
27 $this->testHelperPlugin = new TestHelperPlugin([], 'default', []);
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Tests the unpackOptions method.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param array $storage
|
Chris@0
|
34 * The storage array to unpack option into.
|
Chris@0
|
35 * @param array $options
|
Chris@0
|
36 * The array of options to unpack.
|
Chris@0
|
37 * @param array $definition
|
Chris@0
|
38 * The definition array, defining default options.
|
Chris@0
|
39 * @param array $expected
|
Chris@0
|
40 * The expected array after unpacking
|
Chris@0
|
41 * @param bool $all
|
Chris@0
|
42 * Whether to unpack all options.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @dataProvider providerTestUnpackOptions
|
Chris@0
|
45 * @covers ::unpackOptions
|
Chris@0
|
46 */
|
Chris@0
|
47 public function testUnpackOptions($storage, $options, $definition, $expected, $all = FALSE) {
|
Chris@0
|
48 $this->testHelperPlugin->unpackOptions($storage, $options, $definition, $all);
|
Chris@0
|
49 $this->assertEquals($storage, $expected);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Tests the setOptionDefault method.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param array $storage
|
Chris@0
|
56 * The storage array to unpack option into.
|
Chris@0
|
57 * @param array $definition
|
Chris@0
|
58 * The definition array, defining default options.
|
Chris@0
|
59 * @param array $expected
|
Chris@0
|
60 * The expected array after unpacking
|
Chris@0
|
61 *
|
Chris@0
|
62 * @dataProvider providerTestSetOptionDefault
|
Chris@0
|
63 * @covers ::setOptionDefaults
|
Chris@0
|
64 */
|
Chris@0
|
65 public function testSetOptionDefault($storage, $definition, $expected) {
|
Chris@0
|
66 $this->testHelperPlugin->testSetOptionDefaults($storage, $definition);
|
Chris@0
|
67 $this->assertEquals($storage, $expected);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Data provider for testUnpackOptions().
|
Chris@0
|
72 *
|
Chris@0
|
73 * @return array
|
Chris@0
|
74 */
|
Chris@0
|
75 public function providerTestUnpackOptions() {
|
Chris@0
|
76 $test_parameters = [];
|
Chris@0
|
77 // Set a storage but no value, so the storage value should be kept.
|
Chris@0
|
78 $test_parameters[] = [
|
Chris@0
|
79 'storage' => [
|
Chris@0
|
80 'key' => 'value',
|
Chris@0
|
81 ],
|
Chris@0
|
82 'options' => [],
|
Chris@0
|
83 'definition' => [
|
Chris@0
|
84 'key' => ['default' => 'value2'],
|
Chris@0
|
85 ],
|
Chris@0
|
86 'expected' => [
|
Chris@0
|
87 'key' => 'value',
|
Chris@0
|
88 ],
|
Chris@0
|
89 ];
|
Chris@0
|
90 // Set a storage and a option value, so the option value should be kept.
|
Chris@0
|
91 $test_parameters[] = [
|
Chris@0
|
92 'storage' => [
|
Chris@0
|
93 'key' => 'value',
|
Chris@0
|
94 ],
|
Chris@0
|
95 'options' => [
|
Chris@0
|
96 'key' => 'value2',
|
Chris@0
|
97 ],
|
Chris@0
|
98 'definition' => [
|
Chris@0
|
99 'key' => ['default' => 'value3'],
|
Chris@0
|
100 ],
|
Chris@0
|
101 'expected' => [
|
Chris@0
|
102 'key' => 'value2',
|
Chris@0
|
103 ],
|
Chris@17
|
104 '',
|
Chris@0
|
105 ];
|
Chris@0
|
106 // Set no storage but an options value, so the options value should be kept.
|
Chris@0
|
107 $test_parameters[] = [
|
Chris@0
|
108 'storage' => [],
|
Chris@0
|
109 'options' => [
|
Chris@0
|
110 'key' => 'value',
|
Chris@0
|
111 ],
|
Chris@0
|
112 'definition' => [
|
Chris@0
|
113 'key' => ['default' => 'value2'],
|
Chris@0
|
114 ],
|
Chris@0
|
115 'expected' => [
|
Chris@0
|
116 'key' => 'value',
|
Chris@0
|
117 ],
|
Chris@0
|
118 ];
|
Chris@0
|
119 // Set additional options, which aren't part of the definition, so they
|
Chris@0
|
120 // should be ignored if all is set.
|
Chris@0
|
121 $test_parameters[] = [
|
Chris@0
|
122 'storage' => [],
|
Chris@0
|
123 'options' => [
|
Chris@0
|
124 'key' => 'value',
|
Chris@0
|
125 'key2' => 'value2',
|
Chris@0
|
126 ],
|
Chris@0
|
127 'definition' => [
|
Chris@0
|
128 'key' => ['default' => 'value2'],
|
Chris@0
|
129 ],
|
Chris@0
|
130 'expected' => [
|
Chris@0
|
131 'key' => 'value',
|
Chris@0
|
132 ],
|
Chris@0
|
133 ];
|
Chris@0
|
134 $test_parameters[] = [
|
Chris@0
|
135 'storage' => [],
|
Chris@0
|
136 'options' => [
|
Chris@0
|
137 'key' => 'value',
|
Chris@0
|
138 'key2' => 'value2',
|
Chris@0
|
139 ],
|
Chris@0
|
140 'definition' => [
|
Chris@0
|
141 'key' => ['default' => 'value2'],
|
Chris@0
|
142 ],
|
Chris@0
|
143 'expected' => [
|
Chris@0
|
144 'key' => 'value',
|
Chris@0
|
145 'key2' => 'value2',
|
Chris@0
|
146 ],
|
Chris@0
|
147 'all' => TRUE,
|
Chris@0
|
148 ];
|
Chris@0
|
149 // Provide multiple options with their corresponding definition.
|
Chris@0
|
150 $test_parameters[] = [
|
Chris@0
|
151 'storage' => [],
|
Chris@0
|
152 'options' => [
|
Chris@0
|
153 'key' => 'value',
|
Chris@0
|
154 'key2' => 'value2',
|
Chris@0
|
155 ],
|
Chris@0
|
156 'definition' => [
|
Chris@0
|
157 'key' => ['default' => 'value2'],
|
Chris@0
|
158 'key2' => ['default' => 'value3'],
|
Chris@0
|
159 ],
|
Chris@0
|
160 'expected' => [
|
Chris@0
|
161 'key' => 'value',
|
Chris@0
|
162 'key2' => 'value2',
|
Chris@0
|
163 ],
|
Chris@0
|
164 ];
|
Chris@0
|
165 // Set a complex definition structure with a zero and a one level structure.
|
Chris@0
|
166 $test_parameters[] = [
|
Chris@0
|
167 'storage' => [],
|
Chris@0
|
168 'options' => [
|
Chris@0
|
169 'key0' => 'value',
|
Chris@0
|
170 'key1' => ['key1:1' => 'value1', 'key1:2' => 'value2'],
|
Chris@0
|
171 ],
|
Chris@0
|
172 'definition' => [
|
Chris@0
|
173 'key0' => ['default' => 'value0'],
|
Chris@0
|
174 'key1' => ['contains' => ['key1:1' => ['default' => 'value1:1']]],
|
Chris@0
|
175 ],
|
Chris@0
|
176 'expected' => [
|
Chris@0
|
177 'key0' => 'value',
|
Chris@0
|
178 'key1' => ['key1:1' => 'value1'],
|
Chris@0
|
179 ],
|
Chris@0
|
180 ];
|
Chris@0
|
181 // Setup a two level structure.
|
Chris@0
|
182 $test_parameters[] = [
|
Chris@0
|
183 'storage' => [],
|
Chris@0
|
184 'options' => [
|
Chris@0
|
185 'key2' => [
|
Chris@0
|
186 'key2:1' => [
|
Chris@0
|
187 'key2:1:1' => 'value0',
|
Chris@0
|
188 'key2:1:2' => ['key2:1:2:1' => 'value1'],
|
Chris@0
|
189 ],
|
Chris@0
|
190 ],
|
Chris@0
|
191 ],
|
Chris@0
|
192 'definition' => [
|
Chris@0
|
193 'key2' => [
|
Chris@0
|
194 'contains' => [
|
Chris@0
|
195 'key2:1' => [
|
Chris@0
|
196 'contains' => [
|
Chris@0
|
197 'key2:1:1' => ['default' => 'value2:1:2:1'],
|
Chris@0
|
198 'key2:1:2' => [
|
Chris@0
|
199 'contains' => ['key2:1:2:1' => ['default' => 'value2:1:2:1']],
|
Chris@0
|
200 ],
|
Chris@0
|
201 ],
|
Chris@0
|
202 ],
|
Chris@0
|
203 ],
|
Chris@0
|
204 ],
|
Chris@0
|
205 ],
|
Chris@0
|
206 'expected' => [
|
Chris@0
|
207 'key2' => [
|
Chris@0
|
208 'key2:1' => [
|
Chris@0
|
209 'key2:1:1' => 'value0',
|
Chris@0
|
210 'key2:1:2' => ['key2:1:2:1' => 'value1'],
|
Chris@0
|
211 ],
|
Chris@0
|
212 ],
|
Chris@0
|
213 ],
|
Chris@0
|
214 ];
|
Chris@0
|
215
|
Chris@0
|
216 return $test_parameters;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * Data provider for testSetOptionDefault().
|
Chris@0
|
221 *
|
Chris@0
|
222 * @return array
|
Chris@0
|
223 */
|
Chris@0
|
224 public function providerTestSetOptionDefault() {
|
Chris@0
|
225 $test_parameters = [];
|
Chris@0
|
226 // No definition should change anything on the storage.
|
Chris@0
|
227 $test_parameters[] = [
|
Chris@0
|
228 'storage' => [],
|
Chris@0
|
229 'definition' => [],
|
Chris@0
|
230 'expected' => [],
|
Chris@0
|
231 ];
|
Chris@0
|
232 // Set a single definition, which should be picked up.
|
Chris@0
|
233 $test_parameters[] = [
|
Chris@0
|
234 'storage' => [],
|
Chris@0
|
235 'definition' => [
|
Chris@0
|
236 'key' => ['default' => 'value'],
|
Chris@0
|
237 ],
|
Chris@0
|
238 'expected' => [
|
Chris@0
|
239 'key' => 'value',
|
Chris@0
|
240 ],
|
Chris@0
|
241 ];
|
Chris@0
|
242 // Set multiple keys, all should be picked up.
|
Chris@0
|
243 $test_parameters[] = [
|
Chris@0
|
244 'storage' => [],
|
Chris@0
|
245 'definition' => [
|
Chris@0
|
246 'key' => ['default' => 'value'],
|
Chris@0
|
247 'key2' => ['default' => 'value2'],
|
Chris@0
|
248 'key3' => ['default' => 'value3'],
|
Chris@0
|
249 ],
|
Chris@0
|
250 'expected' => [
|
Chris@0
|
251 'key' => 'value',
|
Chris@0
|
252 'key2' => 'value2',
|
Chris@0
|
253 'key3' => 'value3',
|
Chris@0
|
254 ],
|
Chris@0
|
255 ];
|
Chris@0
|
256 // Setup a definition with multiple levels.
|
Chris@0
|
257 $test_parameters[] = [
|
Chris@0
|
258 'storage' => [],
|
Chris@0
|
259 'definition' => [
|
Chris@0
|
260 'key' => ['default' => 'value'],
|
Chris@0
|
261 'key2' => [
|
Chris@0
|
262 'contains' => [
|
Chris@0
|
263 'key2:1' => ['default' => 'value2:1'],
|
Chris@0
|
264 'key2:2' => ['default' => 'value2:2'],
|
Chris@0
|
265 ],
|
Chris@0
|
266 ],
|
Chris@0
|
267 ],
|
Chris@0
|
268 'expected' => [
|
Chris@0
|
269 'key' => 'value',
|
Chris@0
|
270 'key2' => [
|
Chris@0
|
271 'key2:1' => 'value2:1',
|
Chris@0
|
272 'key2:2' => 'value2:2',
|
Chris@0
|
273 ],
|
Chris@0
|
274 ],
|
Chris@0
|
275 ];
|
Chris@0
|
276
|
Chris@0
|
277 return $test_parameters;
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@0
|
280 /**
|
Chris@0
|
281 * @dataProvider providerTestFilterByDefinedOptions
|
Chris@0
|
282 * @covers ::filterByDefinedOptions
|
Chris@0
|
283 */
|
Chris@0
|
284 public function testFilterByDefinedOptions($storage, $options, $expected_storage) {
|
Chris@0
|
285 $this->testHelperPlugin->setDefinedOptions($options);
|
Chris@0
|
286 $this->testHelperPlugin->filterByDefinedOptions($storage);
|
Chris@0
|
287 $this->assertEquals($expected_storage, $storage);
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 public function providerTestFilterByDefinedOptions() {
|
Chris@0
|
291 $data = [];
|
Chris@0
|
292
|
Chris@0
|
293 // A simple defined option.
|
Chris@0
|
294 $values_1 = ['key1' => 'value1'];
|
Chris@0
|
295 $options_1 = ['key1' => ['default' => '']];
|
Chris@0
|
296 $data[] = [$values_1, $options_1, $values_1];
|
Chris@0
|
297 // Multiple defined options .
|
Chris@0
|
298 $values_2 = ['key1' => 'value1', 'key2' => 'value2'];
|
Chris@0
|
299 $options_2 = ['key1' => ['default' => ''], 'key2' => ['default' => '']];
|
Chris@0
|
300 $data[] = [$values_2, $options_2, $values_2];
|
Chris@0
|
301
|
Chris@0
|
302 // Multiple options, just one defined.
|
Chris@0
|
303 $data[] = [$values_2, $options_1, $values_1];
|
Chris@0
|
304
|
Chris@0
|
305 // Nested options, all properly defined.
|
Chris@0
|
306 $data[] = [['sub1' => $values_2, 'sub2' => $values_2], ['sub1' => ['contains' => $options_2], 'sub2' => ['contains' => $options_2]], ['sub1' => $values_2, 'sub2' => $values_2]];
|
Chris@0
|
307
|
Chris@0
|
308 // Nested options, not all properly defined.
|
Chris@0
|
309 $data[] = [['sub1' => $values_2, 'sub2' => $values_2], ['sub1' => ['contains' => $options_2], 'sub2' => ['contains' => $options_1]], ['sub1' => $values_2, 'sub2' => $values_1]];
|
Chris@0
|
310
|
Chris@0
|
311 return $data;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 }
|