Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Form\FormState;
|
Chris@0
|
6 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
7 use Drupal\views\Plugin\views\wizard\WizardPluginBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * @coversDefaultClass \Drupal\views\Plugin\views\wizard\WizardPluginBase
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group views
|
Chris@0
|
13 */
|
Chris@0
|
14 class WizardPluginBaseTest extends UnitTestCase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * @covers ::getSelected
|
Chris@0
|
18 *
|
Chris@0
|
19 * @dataProvider providerTestGetSelected
|
Chris@0
|
20 */
|
Chris@0
|
21 public function testGetSelected($expected, $element = [], $parents = [], $user_input = [], $not_rebuilding_expected = NULL) {
|
Chris@0
|
22 $not_rebuilding_expected = $not_rebuilding_expected ?: $expected;
|
Chris@0
|
23 $form_state = new FormState();
|
Chris@0
|
24 $form_state->setUserInput($user_input);
|
Chris@0
|
25
|
Chris@0
|
26 $actual = WizardPluginBase::getSelected($form_state, $parents, 'the_default_value', $element);
|
Chris@0
|
27 $this->assertSame($not_rebuilding_expected, $actual);
|
Chris@0
|
28 $this->assertSame($user_input, $form_state->getUserInput());
|
Chris@0
|
29
|
Chris@0
|
30 $form_state->setRebuild();
|
Chris@0
|
31 $actual = WizardPluginBase::getSelected($form_state, $parents, 'the_default_value', $element);
|
Chris@0
|
32 $this->assertSame($expected, $actual);
|
Chris@0
|
33 $this->assertSame($user_input, $form_state->getUserInput());
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Provides test data for testGetSelected().
|
Chris@0
|
38 */
|
Chris@0
|
39 public function providerTestGetSelected() {
|
Chris@0
|
40 $data = [];
|
Chris@0
|
41 // A form element with an invalid #type.
|
Chris@0
|
42 $data['invalid_type'] = [
|
Chris@0
|
43 'the_default_value',
|
Chris@0
|
44 [
|
Chris@0
|
45 '#type' => 'checkbox',
|
Chris@0
|
46 ],
|
Chris@0
|
47 ];
|
Chris@0
|
48 // A form element with no #options.
|
Chris@0
|
49 $data['no_options'] = [
|
Chris@0
|
50 'the_default_value',
|
Chris@0
|
51 [
|
Chris@0
|
52 '#type' => 'select',
|
Chris@0
|
53 ],
|
Chris@0
|
54 ];
|
Chris@0
|
55 // A valid form element with no user input.
|
Chris@0
|
56 $data['no_user_input'] = [
|
Chris@0
|
57 'the_default_value',
|
Chris@0
|
58 [
|
Chris@0
|
59 '#type' => 'select',
|
Chris@0
|
60 '#options' => [
|
Chris@0
|
61 'option1' => 'Option 1',
|
Chris@0
|
62 ],
|
Chris@0
|
63 ],
|
Chris@0
|
64 ];
|
Chris@0
|
65 // A valid form element with user input that doesn't correspond to it.
|
Chris@0
|
66 $data['mismatched_input'] = [
|
Chris@0
|
67 'the_default_value',
|
Chris@0
|
68 [
|
Chris@0
|
69 '#type' => 'select',
|
Chris@0
|
70 '#options' => [
|
Chris@0
|
71 'option1' => 'Option 1',
|
Chris@0
|
72 ],
|
Chris@0
|
73 ],
|
Chris@0
|
74 ['foo', 'bar'],
|
Chris@0
|
75 ['foo' => ['foo' => 'value1']],
|
Chris@0
|
76 ];
|
Chris@0
|
77 // A valid form element with a valid dynamic value that matches the default
|
Chris@0
|
78 // value.
|
Chris@0
|
79 $data['matching_default'] = [
|
Chris@0
|
80 'the_default_value',
|
Chris@0
|
81 [
|
Chris@0
|
82 '#type' => 'select',
|
Chris@0
|
83 '#options' => [
|
Chris@0
|
84 'the_default_value' => 'Option 1',
|
Chris@0
|
85 ],
|
Chris@0
|
86 ],
|
Chris@0
|
87 ['foo', 'bar'],
|
Chris@0
|
88 ['foo' => ['bar' => 'the_default_value']],
|
Chris@0
|
89 ];
|
Chris@0
|
90 // A valid form element with a valid dynamic value that does not match the
|
Chris@0
|
91 // default value.
|
Chris@0
|
92 $data['mismatched_value'] = [
|
Chris@0
|
93 'option1',
|
Chris@0
|
94 [
|
Chris@0
|
95 '#type' => 'select',
|
Chris@0
|
96 '#options' => [
|
Chris@0
|
97 'option1' => 'Option 1',
|
Chris@0
|
98 ],
|
Chris@0
|
99 ],
|
Chris@0
|
100 ['foo', 'bar'],
|
Chris@0
|
101 ['foo' => ['bar' => 'option1']],
|
Chris@0
|
102 'the_default_value',
|
Chris@0
|
103 ];
|
Chris@0
|
104 return $data;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 }
|