Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / EntityReferenceSelection / EntityReferenceSelectionUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\EntityReferenceSelection;
4
5 use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Provides unit testing for selection handlers.
10  *
11  * @coversDefaultClass \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase
12  *
13  * @group entity_reference
14  * @group legacy
15  */
16 class EntityReferenceSelectionUnitTest extends UnitTestCase {
17
18   /**
19    * Tests invalid default configuration.
20    *
21    * @covers ::defaultConfiguration
22    * @covers ::resolveBackwardCompatibilityConfiguration
23    */
24   public function testInvalidDefaultConfiguration() {
25     $this->setExpectedException(\InvalidArgumentException::class, "TestSelectionWithInvalidDefaultConfiguration::defaultConfiguration() should not contain a 'handler_settings' key. All settings should be placed in the root level.");
26     new TestSelectionWithInvalidDefaultConfiguration(
27       [],
28       'test_selector',
29       ['class' => 'TestSelectionWithInvalidDefaultConfiguration']
30     );
31   }
32
33   /**
34    * Tests the selection handler with malformed 'handler_settings' value.
35    *
36    * @covers ::setConfiguration
37    * @covers ::resolveBackwardCompatibilityConfiguration
38    */
39   public function testMalformedHandlerSettingsValue() {
40     $this->setExpectedException(\InvalidArgumentException::class, "The setting 'handler_settings' is reserved and cannot be used.");
41     new TestSelection(
42       // The deprecated 'handler_setting' should be an array.
43       ['handler_settings' => FALSE],
44       'test_selector',
45       ['class' => 'TestSelectionWithInvalidDefaultConfiguration']
46     );
47   }
48
49   /**
50    * Provides test data for ::testSetConfiguration()
51    *
52    * @return array
53    *
54    * @see \Drupal\Tests\Core\EntityReferenceSelection\testSetConfiguration
55    */
56   public function providerTestSetConfiguration() {
57     return [
58       [
59         [
60           'setting1' => 'foo',
61           'setting2' => [
62             'bar' => 'bar value',
63             'baz' => 'baz value',
64           ],
65         ],
66       ],
67       [
68         [
69           'handler_settings' => [
70             'setting1' => 'foo',
71             'setting2' => [
72               'bar' => 'bar value',
73               'baz' => 'baz value',
74             ],
75           ],
76         ],
77       ],
78       [
79         [
80           'setting1' => 'foo',
81           'handler_settings' => [
82             'setting2' => [
83               'bar' => 'bar value',
84               'baz' => 'baz value',
85             ],
86           ]
87         ],
88       ],
89       [
90         [
91           'setting1' => 'foo',
92           'setting2' => [
93             'bar' => 'bar value',
94             'baz' => 'baz value',
95           ],
96           'handler_settings' => [
97             // Same setting from root level takes precedence.
98             'setting2' => 'this will be overwritten',
99           ]
100         ],
101       ],
102     ];
103   }
104
105   /**
106    * Tests selection handler plugin configuration set.
107    *
108    * @dataProvider providerTestSetConfiguration
109    * @covers ::setConfiguration
110    * @covers ::resolveBackwardCompatibilityConfiguration
111    * @covers ::ensureBackwardCompatibilityConfiguration
112    *
113    * @param array $options
114    *   The configuration passed to the plugin.
115    */
116   public function testSetConfiguration($options) {
117     $selection = new TestSelection($options, 'test_selector', []);
118
119     $expected = [
120       'target_type' => NULL,
121       'handler' => 'test_selector',
122       'entity' => NULL,
123       'setting1' => 'foo',
124       'setting2' => [
125         'qux' => 'qux value',
126         'bar' => 'bar value',
127         'baz' => 'baz value',
128       ],
129       'setting3' => 'foobar',
130       'handler_settings' => [
131         'setting1' => 'foo',
132         'setting2' => [
133           'qux' => 'qux value',
134           'bar' => 'bar value',
135           'baz' => 'baz value',
136         ],
137         'setting3' => 'foobar',
138       ],
139     ];
140
141     $this->assertArrayEquals($expected, $selection->getConfiguration());
142   }
143
144   /**
145    * Tests the selection handler plugin BC structure.
146    *
147    * @covers ::setConfiguration
148    * @covers ::resolveBackwardCompatibilityConfiguration
149    * @covers ::ensureBackwardCompatibilityConfiguration
150    */
151   public function testSetConfigurationBcLevel() {
152     $config = [
153       'target_type' => 'some_entity_type_id',
154       'handler' => 'test_selector',
155       'setting1' => 'foo',
156     ];
157     $selection = new TestSelection($config, 'test_selector', []);
158
159     $expected = [
160       'target_type' => 'some_entity_type_id',
161       'handler' => 'test_selector',
162       'entity' => NULL,
163       'setting1' => 'foo',
164       'setting2' => ['qux' => 'qux value'],
165       'setting3' => 'foobar',
166       'handler_settings' => [
167         'setting1' => 'foo',
168         'setting2' => ['qux' => 'qux value'],
169         'setting3' => 'foobar',
170       ],
171     ];
172
173     $this->assertArrayEquals($expected, $selection->getConfiguration());
174
175     // Read the stored values and override a setting.
176     $config = $selection->getConfiguration();
177     $config['setting1'] = 'bar';
178     $selection->setConfiguration($config);
179     $expected['setting1'] = 'bar';
180     $expected['handler_settings']['setting1'] = 'bar';
181
182     $this->assertArrayEquals($expected, $selection->getConfiguration());
183   }
184
185   /**
186    * Tests deprecation error triggering.
187    *
188    * @covers ::setConfiguration
189    * @covers ::resolveBackwardCompatibilityConfiguration
190    * @expectedDeprecation Providing settings under 'handler_settings' is deprecated and will be removed before 9.0.0. Move the settings in the root of the configuration array. See https://www.drupal.org/node/2870971.
191    */
192   public function testDeprecationErrorTriggering() {
193     // Configuration with BC level.
194     $config = ['handler_settings' => ['setting1' => TRUE]];
195     new TestSelection($config, 'test_selector', []);
196     // Ensure at least one assertion.
197     $this->assertTrue(TRUE);
198   }
199
200 }
201
202 /**
203  * Provides a testing plugin.
204  */
205 class TestSelection extends SelectionPluginBase {
206
207   public function defaultConfiguration() {
208     return [
209       'setting2' => [
210         'qux' => 'qux value',
211       ],
212       'setting3' => 'foobar',
213     ] + parent::defaultConfiguration();
214   }
215
216   public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {}
217
218   public function validateReferenceableEntities(array $ids) {}
219
220   public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {}
221
222 }
223
224 /**
225  * Provides a testing plugin with invalid default configuration.
226  */
227 class TestSelectionWithInvalidDefaultConfiguration extends TestSelection {
228
229   public function defaultConfiguration() {
230     return [
231       'handler_settings' => ['foo' => 'bar'],
232     ];
233   }
234
235 }