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[] = [
85 'key' => ['default' => 'value2'],
91 // Set a storage and a option value, so the option value should be kept.
92 $test_parameters[] = [
100 'key' => ['default' => 'value3'],
107 // Set no storage but an options value, so the options value should be kept.
108 $test_parameters[] = [
114 'key' => ['default' => 'value2'],
120 // Set additional options, which aren't part of the definition, so they
121 // should be ignored if all is set.
122 $test_parameters[] = [
129 'key' => ['default' => 'value2'],
135 $test_parameters[] = [
142 'key' => ['default' => 'value2'],
150 // Provide multiple options with their corresponding definition.
151 $test_parameters[] = [
158 'key' => ['default' => 'value2'],
159 'key2' => ['default' => 'value3'],
166 // Set a complex definition structure with a zero and a one level structure.
167 $test_parameters[] = [
171 'key1' => ['key1:1' => 'value1', 'key1:2' => 'value2'],
174 'key0' => ['default' => 'value0'],
175 'key1' => ['contains' => [
176 'key1:1' => ['default' => 'value1:1'],
181 'key1' => ['key1:1' => 'value1'],
184 // Setup a two level structure.
185 $test_parameters[] = [
190 'key2:1:1' => 'value0',
192 'key2:1:2:1' => 'value1',
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'],
210 'key2:1:1' => 'value0',
212 'key2:1:2:1' => 'value1',
219 return $test_parameters;
223 * Data provider for testSetOptionDefault().
227 public function providerTestSetOptionDefault() {
228 $test_parameters = [];
229 // No definition should change anything on the storage.
230 $test_parameters[] = [
235 // Set a single definition, which should be picked up.
236 $test_parameters[] = [
239 'key' => ['default' => 'value'],
245 // Set multiple keys, all should be picked up.
246 $test_parameters[] = [
249 'key' => ['default' => 'value'],
250 'key2' => ['default' => 'value2'],
251 'key3' => ['default' => 'value3'],
259 // Setup a definition with multiple levels.
260 $test_parameters[] = [
263 'key' => ['default' => 'value'],
264 'key2' => ['contains' => [
265 'key2:1' => ['default' => 'value2:1'],
266 'key2:2' => ['default' => 'value2:2'],
272 'key2:1' => 'value2:1',
273 'key2:2' => 'value2:2',
278 return $test_parameters;
282 * @dataProvider providerTestFilterByDefinedOptions
283 * @covers ::filterByDefinedOptions
285 public function testFilterByDefinedOptions($storage, $options, $expected_storage) {
286 $this->testHelperPlugin->setDefinedOptions($options);
287 $this->testHelperPlugin->filterByDefinedOptions($storage);
288 $this->assertEquals($expected_storage, $storage);
291 public function providerTestFilterByDefinedOptions() {
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];
303 // Multiple options, just one defined.
304 $data[] = [$values_2, $options_1, $values_1];
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]];
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]];