View source
<?php
namespace Drupal\Tests\Core\EntityReferenceSelection;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
use Drupal\Tests\UnitTestCase;
class EntityReferenceSelectionUnitTest extends UnitTestCase {
public function testInvalidDefaultConfiguration() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("TestSelectionWithInvalidDefaultConfiguration::defaultConfiguration() should not contain a 'handler_settings' key. All settings should be placed in the root level.");
new TestSelectionWithInvalidDefaultConfiguration([], 'test_selector', [
'class' => 'TestSelectionWithInvalidDefaultConfiguration',
]);
}
public function testMalformedHandlerSettingsValue() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("The setting 'handler_settings' is reserved and cannot be used.");
new TestSelection([
'handler_settings' => FALSE,
], 'test_selector', [
'class' => 'TestSelectionWithInvalidDefaultConfiguration',
]);
}
public function providerTestSetConfiguration() {
return [
[
[
'setting1' => 'foo',
'setting2' => [
'bar' => 'bar value',
'baz' => 'baz value',
],
],
],
[
[
'handler_settings' => [
'setting1' => 'foo',
'setting2' => [
'bar' => 'bar value',
'baz' => 'baz value',
],
],
],
],
[
[
'setting1' => 'foo',
'handler_settings' => [
'setting2' => [
'bar' => 'bar value',
'baz' => 'baz value',
],
],
],
],
[
[
'setting1' => 'foo',
'setting2' => [
'bar' => 'bar value',
'baz' => 'baz value',
],
'handler_settings' => [
'setting2' => 'this will be overwritten',
],
],
],
];
}
public function testSetConfiguration($options) {
$selection = new TestSelection($options, 'test_selector', []);
$expected = [
'target_type' => NULL,
'handler' => 'test_selector',
'entity' => NULL,
'setting1' => 'foo',
'setting2' => [
'qux' => 'qux value',
'bar' => 'bar value',
'baz' => 'baz value',
],
'setting3' => 'foobar',
'handler_settings' => [
'setting1' => 'foo',
'setting2' => [
'qux' => 'qux value',
'bar' => 'bar value',
'baz' => 'baz value',
],
'setting3' => 'foobar',
],
];
$this
->assertArrayEquals($expected, $selection
->getConfiguration());
}
public function testSetConfigurationBcLevel() {
$config = [
'target_type' => 'some_entity_type_id',
'handler' => 'test_selector',
'setting1' => 'foo',
];
$selection = new TestSelection($config, 'test_selector', []);
$expected = [
'target_type' => 'some_entity_type_id',
'handler' => 'test_selector',
'entity' => NULL,
'setting1' => 'foo',
'setting2' => [
'qux' => 'qux value',
],
'setting3' => 'foobar',
'handler_settings' => [
'setting1' => 'foo',
'setting2' => [
'qux' => 'qux value',
],
'setting3' => 'foobar',
],
];
$this
->assertArrayEquals($expected, $selection
->getConfiguration());
$config = $selection
->getConfiguration();
$config['setting1'] = 'bar';
$selection
->setConfiguration($config);
$expected['setting1'] = 'bar';
$expected['handler_settings']['setting1'] = 'bar';
$this
->assertArrayEquals($expected, $selection
->getConfiguration());
}
public function testDeprecationErrorTriggering() {
$config = [
'handler_settings' => [
'setting1' => TRUE,
],
];
new TestSelection($config, 'test_selector', []);
$this
->assertTrue(TRUE);
}
}
class TestSelection extends SelectionPluginBase {
public function defaultConfiguration() {
return [
'setting2' => [
'qux' => 'qux value',
],
'setting3' => 'foobar',
] + parent::defaultConfiguration();
}
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
}
public function validateReferenceableEntities(array $ids) {
}
public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
}
}
class TestSelectionWithInvalidDefaultConfiguration extends TestSelection {
public function defaultConfiguration() {
return [
'handler_settings' => [
'foo' => 'bar',
],
];
}
}