afa849f8c8c04f72e1f4cab354d598e9434e025c
[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       'definition' => [
84         'key' => ['default' => 'value2'],
85       ],
86       'expected' => [
87         'key' => 'value',
88       ],
89     ];
90     // Set a storage and a option value, so the option value should be kept.
91     $test_parameters[] = [
92       'storage' => [
93         'key' => 'value',
94       ],
95       'options' => [
96         'key' => 'value2',
97       ],
98       'definition' => [
99         'key' => ['default' => 'value3'],
100       ],
101       'expected' => [
102         'key' => 'value2',
103       ],
104       '',
105     ];
106     // Set no storage but an options value, so the options value should be kept.
107     $test_parameters[] = [
108       'storage' => [],
109       'options' => [
110         'key' => 'value',
111       ],
112       'definition' => [
113         'key' => ['default' => 'value2'],
114       ],
115       'expected' => [
116         'key' => 'value',
117       ],
118     ];
119     // Set additional options, which aren't part of the definition, so they
120     // should be ignored if all is set.
121     $test_parameters[] = [
122       'storage' => [],
123       'options' => [
124         'key' => 'value',
125         'key2' => 'value2',
126       ],
127       'definition' => [
128         'key' => ['default' => 'value2'],
129       ],
130       'expected' => [
131         'key' => 'value',
132       ],
133     ];
134     $test_parameters[] = [
135       'storage' => [],
136       'options' => [
137         'key' => 'value',
138         'key2' => 'value2',
139       ],
140       'definition' => [
141         'key' => ['default' => 'value2'],
142       ],
143       'expected' => [
144         'key' => 'value',
145         'key2' => 'value2',
146       ],
147       'all' => TRUE,
148     ];
149     // Provide multiple options with their corresponding definition.
150     $test_parameters[] = [
151       'storage' => [],
152       'options' => [
153         'key' => 'value',
154         'key2' => 'value2',
155       ],
156       'definition' => [
157         'key' => ['default' => 'value2'],
158         'key2' => ['default' => 'value3'],
159       ],
160       'expected' => [
161         'key' => 'value',
162         'key2' => 'value2',
163       ],
164     ];
165     // Set a complex definition structure with a zero and a one level structure.
166     $test_parameters[] = [
167       'storage' => [],
168       'options' => [
169         'key0' => 'value',
170         'key1' => ['key1:1' => 'value1', 'key1:2' => 'value2'],
171       ],
172       'definition' => [
173         'key0' => ['default' => 'value0'],
174         'key1' => ['contains' => ['key1:1' => ['default' => 'value1:1']]],
175       ],
176       'expected' => [
177         'key0' => 'value',
178         'key1' => ['key1:1' => 'value1'],
179       ],
180     ];
181     // Setup a two level structure.
182     $test_parameters[] = [
183       'storage' => [],
184       'options' => [
185         'key2' => [
186           'key2:1' => [
187             'key2:1:1' => 'value0',
188             'key2:1:2' => ['key2:1:2:1' => 'value1'],
189           ],
190         ],
191       ],
192       'definition' => [
193         'key2' => [
194           'contains' => [
195             'key2:1' => [
196               'contains' => [
197                 'key2:1:1' => ['default' => 'value2:1:2:1'],
198                 'key2:1:2' => [
199                   'contains' => ['key2:1:2:1' => ['default' => 'value2:1:2:1']],
200                 ],
201               ],
202             ],
203           ],
204         ],
205       ],
206       'expected' => [
207         'key2' => [
208           'key2:1' => [
209             'key2:1:1' => 'value0',
210             'key2:1:2' => ['key2:1:2:1' => 'value1'],
211           ],
212         ],
213       ],
214     ];
215
216     return $test_parameters;
217   }
218
219   /**
220    * Data provider for testSetOptionDefault().
221    *
222    * @return array
223    */
224   public function providerTestSetOptionDefault() {
225     $test_parameters = [];
226     // No definition should change anything on the storage.
227     $test_parameters[] = [
228       'storage' => [],
229       'definition' => [],
230       'expected' => [],
231     ];
232     // Set a single definition, which should be picked up.
233     $test_parameters[] = [
234       'storage' => [],
235       'definition' => [
236         'key' => ['default' => 'value'],
237       ],
238       'expected' => [
239         'key' => 'value',
240       ],
241     ];
242     // Set multiple keys, all should be picked up.
243     $test_parameters[] = [
244       'storage' => [],
245       'definition' => [
246         'key' => ['default' => 'value'],
247         'key2' => ['default' => 'value2'],
248         'key3' => ['default' => 'value3'],
249       ],
250       'expected' => [
251         'key' => 'value',
252         'key2' => 'value2',
253         'key3' => 'value3',
254       ],
255     ];
256     // Setup a definition with multiple levels.
257     $test_parameters[] = [
258       'storage' => [],
259       'definition' => [
260         'key' => ['default' => 'value'],
261         'key2' => [
262           'contains' => [
263             'key2:1' => ['default' => 'value2:1'],
264             'key2:2' => ['default' => 'value2:2'],
265           ],
266         ],
267       ],
268       'expected' => [
269         'key' => 'value',
270         'key2' => [
271           'key2:1' => 'value2:1',
272           'key2:2' => 'value2:2',
273         ],
274       ],
275     ];
276
277     return $test_parameters;
278   }
279
280   /**
281    * @dataProvider providerTestFilterByDefinedOptions
282    * @covers ::filterByDefinedOptions
283    */
284   public function testFilterByDefinedOptions($storage, $options, $expected_storage) {
285     $this->testHelperPlugin->setDefinedOptions($options);
286     $this->testHelperPlugin->filterByDefinedOptions($storage);
287     $this->assertEquals($expected_storage, $storage);
288   }
289
290   public function providerTestFilterByDefinedOptions() {
291     $data = [];
292
293     // A simple defined option.
294     $values_1 = ['key1' => 'value1'];
295     $options_1 = ['key1' => ['default' => '']];
296     $data[] = [$values_1, $options_1, $values_1];
297     // Multiple defined options .
298     $values_2 = ['key1' => 'value1', 'key2' => 'value2'];
299     $options_2 = ['key1' => ['default' => ''], 'key2' => ['default' => '']];
300     $data[] = [$values_2, $options_2, $values_2];
301
302     // Multiple options, just one defined.
303     $data[] = [$values_2, $options_1, $values_1];
304
305     // Nested options, all properly defined.
306     $data[] = [['sub1' => $values_2, 'sub2' => $values_2], ['sub1' => ['contains' => $options_2], 'sub2' => ['contains' => $options_2]], ['sub1' => $values_2, 'sub2' => $values_2]];
307
308     // Nested options, not all properly defined.
309     $data[] = [['sub1' => $values_2, 'sub2' => $values_2], ['sub1' => ['contains' => $options_2], 'sub2' => ['contains' => $options_1]], ['sub1' => $values_2, 'sub2' => $values_1]];
310
311     return $data;
312   }
313
314 }