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