3 namespace Drupal\Tests\views\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Tests\TestHelperPlugin;
9 * @coversDefaultClass \Drupal\views\Plugin\views\PluginBase
12 class PluginBaseTest extends UnitTestCase {
15 * The test helper plugin to use for the tests.
17 * @var \Drupal\views\Tests\TestHelperPlugin
19 protected $testHelperPlugin;
24 protected function setUp() {
27 $this->testHelperPlugin = new TestHelperPlugin([], 'default', []);
31 * Tests the unpackOptions method.
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
42 * Whether to unpack all options.
44 * @dataProvider providerTestUnpackOptions
45 * @covers ::unpackOptions
47 public function testUnpackOptions($storage, $options, $definition, $expected, $all = FALSE) {
48 $this->testHelperPlugin->unpackOptions($storage, $options, $definition, $all);
49 $this->assertEquals($storage, $expected);
53 * Tests the setOptionDefault method.
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
62 * @dataProvider providerTestSetOptionDefault
63 * @covers ::setOptionDefaults
65 public function testSetOptionDefault($storage, $definition, $expected) {
66 $this->testHelperPlugin->testSetOptionDefaults($storage, $definition);
67 $this->assertEquals($storage, $expected);
71 * Data provider for testUnpackOptions().
75 public function providerTestUnpackOptions() {
76 $test_parameters = [];
77 // Set a storage but no value, so the storage value should be kept.
78 $test_parameters[] = [
84 'key' => ['default' => 'value2'],
90 // Set a storage and a option value, so the option value should be kept.
91 $test_parameters[] = [
99 'key' => ['default' => 'value3'],
106 // Set no storage but an options value, so the options value should be kept.
107 $test_parameters[] = [
113 'key' => ['default' => 'value2'],
119 // Set additional options, which aren't part of the definition, so they
120 // should be ignored if all is set.
121 $test_parameters[] = [
128 'key' => ['default' => 'value2'],
134 $test_parameters[] = [
141 'key' => ['default' => 'value2'],
149 // Provide multiple options with their corresponding definition.
150 $test_parameters[] = [
157 'key' => ['default' => 'value2'],
158 'key2' => ['default' => 'value3'],
165 // Set a complex definition structure with a zero and a one level structure.
166 $test_parameters[] = [
170 'key1' => ['key1:1' => 'value1', 'key1:2' => 'value2'],
173 'key0' => ['default' => 'value0'],
174 'key1' => ['contains' => ['key1:1' => ['default' => 'value1:1']]],
178 'key1' => ['key1:1' => 'value1'],
181 // Setup a two level structure.
182 $test_parameters[] = [
187 'key2:1:1' => 'value0',
188 'key2:1:2' => ['key2:1:2:1' => 'value1'],
197 'key2:1:1' => ['default' => 'value2:1:2:1'],
199 'contains' => ['key2:1:2:1' => ['default' => 'value2:1:2:1']],
209 'key2:1:1' => 'value0',
210 'key2:1:2' => ['key2:1:2:1' => 'value1'],
216 return $test_parameters;
220 * Data provider for testSetOptionDefault().
224 public function providerTestSetOptionDefault() {
225 $test_parameters = [];
226 // No definition should change anything on the storage.
227 $test_parameters[] = [
232 // Set a single definition, which should be picked up.
233 $test_parameters[] = [
236 'key' => ['default' => 'value'],
242 // Set multiple keys, all should be picked up.
243 $test_parameters[] = [
246 'key' => ['default' => 'value'],
247 'key2' => ['default' => 'value2'],
248 'key3' => ['default' => 'value3'],
256 // Setup a definition with multiple levels.
257 $test_parameters[] = [
260 'key' => ['default' => 'value'],
263 'key2:1' => ['default' => 'value2:1'],
264 'key2:2' => ['default' => 'value2:2'],
271 'key2:1' => 'value2:1',
272 'key2:2' => 'value2:2',
277 return $test_parameters;
281 * @dataProvider providerTestFilterByDefinedOptions
282 * @covers ::filterByDefinedOptions
284 public function testFilterByDefinedOptions($storage, $options, $expected_storage) {
285 $this->testHelperPlugin->setDefinedOptions($options);
286 $this->testHelperPlugin->filterByDefinedOptions($storage);
287 $this->assertEquals($expected_storage, $storage);
290 public function providerTestFilterByDefinedOptions() {
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];
302 // Multiple options, just one defined.
303 $data[] = [$values_2, $options_1, $values_1];
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]];
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]];