class EntityReferenceSelectionUnitTest in Drupal 8
Provides unit testing for selection handlers.
@coversDefaultClass \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase
@group entity_reference @group legacy
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\EntityReferenceSelection\EntityReferenceSelectionUnitTest
Expanded class hierarchy of EntityReferenceSelectionUnitTest
File
- core/
tests/ Drupal/ Tests/ Core/ EntityReferenceSelection/ EntityReferenceSelectionUnitTest.php, line 16
Namespace
Drupal\Tests\Core\EntityReferenceSelectionView source
class EntityReferenceSelectionUnitTest extends UnitTestCase {
/**
* Tests invalid default configuration.
*
* @covers ::defaultConfiguration
* @covers ::resolveBackwardCompatibilityConfiguration
*/
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',
]);
}
/**
* Tests the selection handler with malformed 'handler_settings' value.
*
* @covers ::setConfiguration
* @covers ::resolveBackwardCompatibilityConfiguration
*/
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',
]);
}
/**
* Provides test data for ::testSetConfiguration()
*
* @return array
*
* @see \Drupal\Tests\Core\EntityReferenceSelection\testSetConfiguration
*/
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' => [
// Same setting from root level takes precedence.
'setting2' => 'this will be overwritten',
],
],
],
];
}
/**
* Tests selection handler plugin configuration set.
*
* @dataProvider providerTestSetConfiguration
* @covers ::setConfiguration
* @covers ::resolveBackwardCompatibilityConfiguration
* @covers ::ensureBackwardCompatibilityConfiguration
*
* @param array $options
* The configuration passed to the plugin.
*/
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());
}
/**
* Tests the selection handler plugin BC structure.
*
* @covers ::setConfiguration
* @covers ::resolveBackwardCompatibilityConfiguration
* @covers ::ensureBackwardCompatibilityConfiguration
*/
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());
// Read the stored values and override a setting.
$config = $selection
->getConfiguration();
$config['setting1'] = 'bar';
$selection
->setConfiguration($config);
$expected['setting1'] = 'bar';
$expected['handler_settings']['setting1'] = 'bar';
$this
->assertArrayEquals($expected, $selection
->getConfiguration());
}
/**
* Tests deprecation error triggering.
*
* @covers ::setConfiguration
* @covers ::resolveBackwardCompatibilityConfiguration
* @expectedDeprecation Providing settings under 'handler_settings' is deprecated in drupal:8.4.0 support for 'handler_settings' is removed from drupal:9.0.0. Move the settings in the root of the configuration array. See https://www.drupal.org/node/2870971
*/
public function testDeprecationErrorTriggering() {
// Configuration with BC level.
$config = [
'handler_settings' => [
'setting1' => TRUE,
],
];
new TestSelection($config, 'test_selector', []);
// Ensure at least one assertion.
$this
->assertTrue(TRUE);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceSelectionUnitTest:: |
public | function | Provides test data for ::testSetConfiguration() | |
EntityReferenceSelectionUnitTest:: |
public | function | Tests deprecation error triggering. | |
EntityReferenceSelectionUnitTest:: |
public | function | Tests invalid default configuration. | |
EntityReferenceSelectionUnitTest:: |
public | function | Tests the selection handler with malformed 'handler_settings' value. | |
EntityReferenceSelectionUnitTest:: |
public | function | Tests selection handler plugin configuration set. | |
EntityReferenceSelectionUnitTest:: |
public | function | Tests the selection handler plugin BC structure. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |