View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityResolverManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class EntityResolverManagerTest extends UnitTestCase {
protected $entityResolverManager;
protected $entityTypeManager;
protected $classResolver;
protected $container;
protected function setUp() : void {
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->container = $this
->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->classResolver = $this
->getClassResolverStub();
$this->entityResolverManager = new EntityResolverManager($this->entityTypeManager, $this->classResolver);
}
public function testSetRouteOptionsWithStandardRoute($controller) {
$route = new Route('/example', [
'_controller' => $controller,
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithStandardRoute() {
return [
[
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethod',
],
[
'Drupal\\Tests\\Core\\Entity\\test_function_controller',
],
];
}
public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
$route = new Route('/example/{argument}', [
'_controller' => $controller,
'argument' => 'test',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithStandardRouteWithArgument() {
return [
[
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
],
[
'Drupal\\Tests\\Core\\Entity\\test_function_controller_with_argument',
],
];
}
public function testSetRouteOptionsWithContentController($controller) {
$route = new Route('/example/{argument}', [
'_controller' => $controller,
'argument' => 'test',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithContentController() {
return [
[
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
],
[
'Drupal\\Tests\\Core\\Entity\\test_function_controller_with_argument',
],
];
}
public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_controller' => $controller,
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithEntityTypeNoUpcasting() {
return [
[
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityNoUpcasting',
],
[
'Drupal\\Tests\\Core\\Entity\\test_function_controller_no_upcasting',
],
];
}
public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_controller' => $controller,
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'entity_test' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function providerTestSetRouteOptionsWithEntityTypeUpcasting() {
return [
[
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityUpcasting',
],
[
'Drupal\\Tests\\Core\\Entity\\test_function_controller_entity_upcasting',
],
];
}
public function testSetRouteOptionsWithEntityFormUpcasting() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_form' => 'Drupal\\Tests\\Core\\Entity\\BasicForm',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'entity_test' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function testSetRouteOptionsWithEntityUpcastingNoCreate() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoContainerInjectionInterface',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'entity_test' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function testSetRouteOptionsWithEntityFormNoUpcasting() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoUpcasting',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function testSetRouteOptionsWithEntityViewRouteAndManualParameters() {
$this
->setupEntityTypes();
$route = new Route('/example/{foo}', [
'_entity_view' => 'entity_test.view',
], [], [
'parameters' => [
'foo' => [
'type' => 'entity:entity_test',
],
],
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'foo' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function testSetRouteOptionsWithEntityViewRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_entity_view' => 'entity_test.view',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'entity_test' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function testSetRouteOptionsWithEntityListRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_entity_list' => 'entity_test',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertNull($parameters);
}
public function testSetRouteOptionsWithEntityFormRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', [
'_entity_form' => 'entity_test.edit',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals([
'entity_test' => [
'type' => 'entity:entity_test',
],
], $parameters);
}
public function testSetRouteOptionsWithEntityFormRouteAndArgument() {
$this
->setupEntityTypes();
$route = new Route('/example/{argument}/{entity_test}', [
'_entity_form' => 'entity_test.edit',
]);
$route
->setOption('parameters', [
'argument' => [
'type' => 'argument_type',
],
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$expect = [
'argument' => [
'type' => 'argument_type',
],
'entity_test' => [
'type' => 'entity:entity_test',
],
];
$this
->assertEquals($expect, $parameters);
}
public function testSetRouteOptionsWithEntityAddFormRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/add', [
'_entity_form' => 'entity_test.add',
]);
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertFalse($route
->hasOption('parameters'));
}
protected function setupEntityTypes() {
$definition = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
$definition
->expects($this
->any())
->method('isRevisionable')
->willReturn(FALSE);
$revisionable_definition = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$revisionable_definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
$revisionable_definition
->expects($this
->any())
->method('isRevisionable')
->willReturn(TRUE);
$this->entityTypeManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue([
'entity_test' => $definition,
'entity_test_rev' => $revisionable_definition,
]));
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->willReturnCallback(function ($entity_type) use ($definition, $revisionable_definition) {
if ($entity_type == 'entity_test') {
return $definition;
}
elseif ($entity_type === 'entity_test_rev') {
return $revisionable_definition;
}
else {
return NULL;
}
});
}
}
class BasicControllerClass {
public function exampleControllerMethod() {
}
public function exampleControllerMethodWithArgument($argument) {
}
public function exampleControllerWithEntityNoUpcasting($entity_test) {
}
public function exampleControllerWithEntityUpcasting(EntityInterface $entity_test) {
}
}
class SimpleTestEntity extends EntityBase {
}
class BasicForm extends FormBase {
public function getFormId() {
}
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_test = NULL) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
class BasicFormNoUpcasting extends FormBase {
public function getFormId() {
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_test = NULL) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
class BasicFormNoContainerInjectionInterface implements FormInterface {
public function getFormId() {
}
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_test = NULL) {
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
function test_function_controller() {
}
function test_function_controller_with_argument($argument) {
}
function test_function_controller_no_upcasting($entity_test) {
}
function test_function_controller_entity_upcasting(EntityInterface $entity_test) {
}