View source
<?php
namespace Drupal\Tests\Core\Entity {
use Drupal\Core\Entity\Entity;
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 $entityManager;
protected $classResolver;
protected $container;
protected function setUp() {
$this->entityManager = $this
->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$this->container = $this
->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->classResolver = $this
->getClassResolverStub();
$this->entityResolverManager = new EntityResolverManager($this->entityManager, $this->classResolver);
}
public function testSetRouteOptionsWithStandardRoute($controller) {
$route = new Route('/example', array(
'_controller' => $controller,
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithStandardRoute() {
return array(
array(
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethod',
),
array(
'test_function_controller',
),
);
}
public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
$route = new Route('/example/{argument}', array(
'_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 array(
array(
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
),
array(
'test_function_controller_with_argument',
),
);
}
public function testSetRouteOptionsWithContentController($controller) {
$route = new Route('/example/{argument}', array(
'_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 array(
array(
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
),
array(
'test_function_controller_with_argument',
),
);
}
public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_controller' => $controller,
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$this
->assertEmpty($route
->getOption('parameters'));
}
public function providerTestSetRouteOptionsWithEntityTypeNoUpcasting() {
return array(
array(
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityNoUpcasting',
),
array(
'test_function_controller_no_upcasting',
),
);
}
public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_controller' => $controller,
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'entity_test' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
public function providerTestSetRouteOptionsWithEntityTypeUpcasting() {
return array(
array(
'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityUpcasting',
),
array(
'test_function_controller_entity_upcasting',
),
);
}
public function testSetRouteOptionsWithEntityFormUpcasting() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_form' => 'Drupal\\Tests\\Core\\Entity\\BasicForm',
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'entity_test' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
public function testSetRouteOptionsWithEntityUpcastingNoCreate() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoContainerInjectionInterface',
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'entity_test' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
public function testSetRouteOptionsWithEntityFormNoUpcasting() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_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}', array(
'_entity_view' => 'entity_test.view',
), array(), array(
'parameters' => array(
'foo' => array(
'type' => 'entity:entity_test',
),
),
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'foo' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
public function testSetRouteOptionsWithEntityViewRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_entity_view' => 'entity_test.view',
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'entity_test' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
public function testSetRouteOptionsWithEntityListRoute() {
$this
->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_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}', array(
'_entity_form' => 'entity_test.edit',
));
$defaults = $route
->getDefaults();
$this->entityResolverManager
->setRouteOptions($route);
$this
->assertEquals($defaults, $route
->getDefaults());
$parameters = $route
->getOption('parameters');
$this
->assertEquals(array(
'entity_test' => array(
'type' => 'entity:entity_test',
),
), $parameters);
}
protected function setupEntityTypes() {
$definition = $this
->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
$this->entityManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue(array(
'entity_test' => $definition,
)));
$this->entityManager
->expects($this
->any())
->method('getDefinition')
->will($this
->returnCallback(function ($entity_type) use ($definition) {
if ($entity_type == 'entity_test') {
return $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 Entity {
}
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) {
}
}
}
namespace {
use Drupal\Core\Entity\EntityInterface;
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) {
}
}