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