3 namespace Drupal\Tests\Core\EntityReferenceSelection;
5 use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
6 use Drupal\Tests\UnitTestCase;
9 * Provides unit testing for selection handlers.
11 * @coversDefaultClass \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase
13 * @group entity_reference
16 class EntityReferenceSelectionUnitTest extends UnitTestCase {
19 * Tests invalid default configuration.
21 * @covers ::defaultConfiguration
22 * @covers ::resolveBackwardCompatibilityConfiguration
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(
29 ['class' => 'TestSelectionWithInvalidDefaultConfiguration']
34 * Tests the selection handler with malformed 'handler_settings' value.
36 * @covers ::setConfiguration
37 * @covers ::resolveBackwardCompatibilityConfiguration
39 public function testMalformedHandlerSettingsValue() {
40 $this->setExpectedException(\InvalidArgumentException::class, "The setting 'handler_settings' is reserved and cannot be used.");
42 // The deprecated 'handler_setting' should be an array.
43 ['handler_settings' => FALSE],
45 ['class' => 'TestSelectionWithInvalidDefaultConfiguration']
50 * Provides test data for ::testSetConfiguration()
54 * @see \Drupal\Tests\Core\EntityReferenceSelection\testSetConfiguration
56 public function providerTestSetConfiguration() {
69 'handler_settings' => [
81 'handler_settings' => [
96 'handler_settings' => [
97 // Same setting from root level takes precedence.
98 'setting2' => 'this will be overwritten',
106 * Tests selection handler plugin configuration set.
108 * @dataProvider providerTestSetConfiguration
109 * @covers ::setConfiguration
110 * @covers ::resolveBackwardCompatibilityConfiguration
111 * @covers ::ensureBackwardCompatibilityConfiguration
113 * @param array $options
114 * The configuration passed to the plugin.
116 public function testSetConfiguration($options) {
117 $selection = new TestSelection($options, 'test_selector', []);
120 'target_type' => NULL,
121 'handler' => 'test_selector',
125 'qux' => 'qux value',
126 'bar' => 'bar value',
127 'baz' => 'baz value',
129 'setting3' => 'foobar',
130 'handler_settings' => [
133 'qux' => 'qux value',
134 'bar' => 'bar value',
135 'baz' => 'baz value',
137 'setting3' => 'foobar',
141 $this->assertArrayEquals($expected, $selection->getConfiguration());
145 * Tests the selection handler plugin BC structure.
147 * @covers ::setConfiguration
148 * @covers ::resolveBackwardCompatibilityConfiguration
149 * @covers ::ensureBackwardCompatibilityConfiguration
151 public function testSetConfigurationBcLevel() {
153 'target_type' => 'some_entity_type_id',
154 'handler' => 'test_selector',
157 $selection = new TestSelection($config, 'test_selector', []);
160 'target_type' => 'some_entity_type_id',
161 'handler' => 'test_selector',
164 'setting2' => ['qux' => 'qux value'],
165 'setting3' => 'foobar',
166 'handler_settings' => [
168 'setting2' => ['qux' => 'qux value'],
169 'setting3' => 'foobar',
173 $this->assertArrayEquals($expected, $selection->getConfiguration());
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';
182 $this->assertArrayEquals($expected, $selection->getConfiguration());
186 * Tests deprecation error triggering.
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.
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);
203 * Provides a testing plugin.
205 class TestSelection extends SelectionPluginBase {
207 public function defaultConfiguration() {
210 'qux' => 'qux value',
212 'setting3' => 'foobar',
213 ] + parent::defaultConfiguration();
216 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {}
218 public function validateReferenceableEntities(array $ids) {}
220 public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {}
225 * Provides a testing plugin with invalid default configuration.
227 class TestSelectionWithInvalidDefaultConfiguration extends TestSelection {
229 public function defaultConfiguration() {
231 'handler_settings' => ['foo' => 'bar'],