EntityConverterTest.php
Same filename in this branch
Same filename and directory in other branches
- 11.x core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php
- 11.x core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
- 10 core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php
- 10 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
- 9 core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php
- 9 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
- 8.9.x core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
Namespace
Drupal\KernelTests\Core\ParamConverterFile
-
core/
tests/ Drupal/ KernelTests/ Core/ ParamConverter/ EntityConverterTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\ParamConverter;
use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\EntityTestHelper;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
/**
* Tests the entity param converter.
*/
class EntityConverterTest extends KernelTestBase {
use UserCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'config_test',
'entity_test',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->setUpCurrentUser();
$this->installEntitySchema('entity_test');
// Create some testing bundles for 'entity_test' entity type.
EntityTestHelper::createBundle('foo', 'Foo');
EntityTestHelper::createBundle('bar', 'Bar');
EntityTestHelper::createBundle('baz', 'Baz');
}
/**
* Tests an entity route parameter having 'bundle' definition property.
*
* @legacy-covers ::convert
*/
public function testRouteParamWithBundleDefinition() : void {
$converter = $this->container
->get('paramconverter.entity');
$entity1 = EntityTest::create([
'name' => $this->randomString(),
'type' => 'foo',
]);
$entity1->save();
$entity2 = EntityTest::create([
'name' => $this->randomString(),
'type' => 'bar',
]);
$entity2->save();
$entity3 = EntityTest::create([
'name' => $this->randomString(),
'type' => 'baz',
]);
$entity3->save();
$definition = [
'type' => 'entity:entity_test',
'bundle' => [
'foo',
'bar',
],
];
// An entity whose bundle is in the definition list is converted.
$converted = $converter->convert($entity1->id(), $definition, 'qux', []);
$this->assertSame($entity1->id(), $converted->id());
// An entity whose bundle is in the definition list is converted.
$converted = $converter->convert($entity2->id(), $definition, 'qux', []);
$this->assertSame($entity2->id(), $converted->id());
// An entity whose bundle is missed from definition is not converted.
$converted = $converter->convert($entity3->id(), $definition, 'qux', []);
$this->assertNull($converted);
// A non-existing entity returns NULL.
$converted = $converter->convert('some-non-existing-entity-id', $definition, 'qux', []);
$this->assertNull($converted);
$definition = [
'type' => 'entity:entity_test',
];
// Check that all entities are returned when 'bundle' is not defined.
$converted = $converter->convert($entity1->id(), $definition, 'qux', []);
$this->assertSame($entity1->id(), $converted->id());
$converted = $converter->convert($entity2->id(), $definition, 'qux', []);
$this->assertSame($entity2->id(), $converted->id());
$converted = $converter->convert($entity3->id(), $definition, 'qux', []);
$this->assertSame($entity3->id(), $converted->id());
$converted = $converter->convert('some-non-existing-entity-id', $definition, 'qux', []);
$this->assertNull($converted);
}
/**
* Tests convert() for dynamic config entity types.
*
* @legacy-covers ::convert
*/
public function testConvertDynamicConfigEntityRejectsNonAscii() : void {
$converter = $this->container
->get('paramconverter.entity');
$config_entity = $this->container
->get('entity_type.manager')
->getStorage('config_test')
->create([
'id' => 'test_id',
'label' => 'Test',
]);
$config_entity->save();
$definition = [
'type' => 'entity:{entity_type}',
];
// Test that the entity is loaded.
$defaults = [
'example' => "test_id",
'entity_type' => 'config_test',
];
$converted = $converter->convert("test_id", $definition, 'example', $defaults);
$this->assertSame('test_id', $converted->id());
// Test that an invalid entity ID is rejected without triggering an
// exception.
$defaults = [
'example' => "invalid\x80value",
'entity_type' => 'config_test',
];
$converted = $converter->convert("invalid\x80value", $definition, 'example', $defaults);
$this->assertNull($converted);
}
}
Classes
| Title | Deprecated | Summary |
|---|---|---|
| EntityConverterTest | Tests the entity param converter. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.