Issue #2321393 by damiankloip, undertext: Fixed Unable to pass function name to '_content' property of the route.

8.0.x
Alex Pott 2014-09-08 11:21:55 +01:00
parent 61150517df
commit 6d8b0b81d3
2 changed files with 112 additions and 18 deletions

View File

@ -94,21 +94,28 @@ class EntityResolverManager {
/** /**
* Sets the upcasting information using reflection. * Sets the upcasting information using reflection.
* *
* @param array $controller * @param string|array $controller
* An array of class instance and method name. * A PHP callable representing the controller.
* @param \Symfony\Component\Routing\Route $route * @param \Symfony\Component\Routing\Route $route
* The route object to populate without upcasting information. * The route object to populate without upcasting information.
* *
* @return bool * @return bool
* Returns TRUE if the upcasting parameters could be set, FALSE otherwise. * Returns TRUE if the upcasting parameters could be set, FALSE otherwise.
*/ */
protected function setParametersFromReflection(array $controller, Route $route) { protected function setParametersFromReflection($controller, Route $route) {
$entity_types = $this->getEntityTypes(); $entity_types = $this->getEntityTypes();
$parameter_definitions = $route->getOption('parameters') ?: array(); $parameter_definitions = $route->getOption('parameters') ?: array();
$result = FALSE; $result = FALSE;
list($instance, $method) = $controller;
$reflection = new \ReflectionMethod($instance, $method); if (is_array($controller)) {
list($instance, $method) = $controller;
$reflection = new \ReflectionMethod($instance, $method);
}
else {
$reflection = new \ReflectionFunction($controller);
}
$parameters = $reflection->getParameters(); $parameters = $reflection->getParameters();
foreach ($parameters as $parameter) { foreach ($parameters as $parameter) {
$parameter_name = $parameter->getName(); $parameter_name = $parameter->getName();

View File

@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Core\Entity\EntityResolverManagerTest. * Contains \Drupal\Tests\Core\Entity\EntityResolverManagerTest.
*/ */
namespace Drupal\Tests\Core\Entity; namespace Drupal\Tests\Core\Entity {
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
@ -78,10 +78,12 @@ class EntityResolverManagerTest extends UnitTestCase {
* *
* @covers ::setRouteOptions() * @covers ::setRouteOptions()
* @covers ::getController() * @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithStandardRoute
*/ */
public function testSetRouteOptionsWithStandardRoute() { public function testSetRouteOptionsWithStandardRoute($controller) {
$route = new Route('/example', array( $route = new Route('/example', array(
'_controller' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethod', '_controller' => $controller,
)); ));
$this->setupControllerResolver($route->getDefault('_controller')); $this->setupControllerResolver($route->getDefault('_controller'));
@ -91,15 +93,27 @@ class EntityResolverManagerTest extends UnitTestCase {
$this->assertEmpty($route->getOption('parameters')); $this->assertEmpty($route->getOption('parameters'));
} }
/**
* Data provider for testSetRouteOptionsWithStandardRoute.
*/
public function providerTestSetRouteOptionsWithStandardRoute() {
return array(
array('Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethod'),
array('test_function_controller'),
);
}
/** /**
* Tests setRouteOptions() with a controller with a non entity argument. * Tests setRouteOptions() with a controller with a non entity argument.
* *
* @covers ::setRouteOptions() * @covers ::setRouteOptions()
* @covers ::getController() * @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithStandardRouteWithArgument
*/ */
public function testSetRouteOptionsWithStandardRouteWithArgument() { public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
$route = new Route('/example/{argument}', array( $route = new Route('/example/{argument}', array(
'_controller' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument', '_controller' => $controller,
'argument' => 'test', 'argument' => 'test',
)); ));
$this->setupControllerResolver($route->getDefault('_controller')); $this->setupControllerResolver($route->getDefault('_controller'));
@ -110,15 +124,27 @@ class EntityResolverManagerTest extends UnitTestCase {
$this->assertEmpty($route->getOption('parameters')); $this->assertEmpty($route->getOption('parameters'));
} }
/**
* Data provider for testSetRouteOptionsWithStandardRouteWithArgument.
*/
public function providerTestSetRouteOptionsWithStandardRouteWithArgument() {
return array(
array('Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument'),
array('test_function_controller_with_argument'),
);
}
/** /**
* Tests setRouteOptions() with a _content default. * Tests setRouteOptions() with a _content default.
* *
* @covers ::setRouteOptions() * @covers ::setRouteOptions()
* @covers ::getController() * @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithContentController
*/ */
public function testSetRouteOptionsWithContentController() { public function testSetRouteOptionsWithContentController($controller) {
$route = new Route('/example/{argument}', array( $route = new Route('/example/{argument}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument', '_content' => $controller,
'argument' => 'test', 'argument' => 'test',
)); ));
$this->setupControllerResolver($route->getDefault('_content')); $this->setupControllerResolver($route->getDefault('_content'));
@ -129,6 +155,16 @@ class EntityResolverManagerTest extends UnitTestCase {
$this->assertEmpty($route->getOption('parameters')); $this->assertEmpty($route->getOption('parameters'));
} }
/**
* Data provider for testSetRouteOptionsWithContentController.
*/
public function providerTestSetRouteOptionsWithContentController() {
return array(
array('Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument'),
array('test_function_controller_with_argument'),
);
}
/** /**
* Tests setRouteOptions() with an entity type parameter. * Tests setRouteOptions() with an entity type parameter.
* *
@ -136,12 +172,14 @@ class EntityResolverManagerTest extends UnitTestCase {
* @covers ::getController() * @covers ::getController()
* @covers ::getEntityTypes() * @covers ::getEntityTypes()
* @covers ::setParametersFromReflection() * @covers ::setParametersFromReflection()
*
* @dataProvider providerTestSetRouteOptionsWithEntityTypeNoUpcasting
*/ */
public function testSetRouteOptionsWithEntityTypeNoUpcasting() { public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
$this->setupEntityTypes(); $this->setupEntityTypes();
$route = new Route('/example/{entity_test}', array( $route = new Route('/example/{entity_test}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityNoUpcasting', '_content' => $controller,
)); ));
$this->setupControllerResolver($route->getDefault('_content')); $this->setupControllerResolver($route->getDefault('_content'));
@ -151,6 +189,16 @@ class EntityResolverManagerTest extends UnitTestCase {
$this->assertEmpty($route->getOption('parameters')); $this->assertEmpty($route->getOption('parameters'));
} }
/**
* Data provider for testSetRouteOptionsWithEntityTypeNoUpcasting.
*/
public function providerTestSetRouteOptionsWithEntityTypeNoUpcasting() {
return array(
array('Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityNoUpcasting'),
array('test_function_controller_no_upcasting'),
);
}
/** /**
* Tests setRouteOptions() with an entity type parameter, upcasting. * Tests setRouteOptions() with an entity type parameter, upcasting.
* *
@ -158,12 +206,14 @@ class EntityResolverManagerTest extends UnitTestCase {
* @covers ::getController() * @covers ::getController()
* @covers ::getEntityTypes() * @covers ::getEntityTypes()
* @covers ::setParametersFromReflection() * @covers ::setParametersFromReflection()
*
* @dataProvider providerTestSetRouteOptionsWithEntityTypeUpcasting
*/ */
public function testSetRouteOptionsWithEntityTypeUpcasting() { public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
$this->setupEntityTypes(); $this->setupEntityTypes();
$route = new Route('/example/{entity_test}', array( $route = new Route('/example/{entity_test}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityUpcasting', '_content' => $controller,
)); ));
$this->setupControllerResolver($route->getDefault('_content')); $this->setupControllerResolver($route->getDefault('_content'));
@ -174,6 +224,16 @@ class EntityResolverManagerTest extends UnitTestCase {
$this->assertEquals(array('entity_test' => array('type' => 'entity:entity_test')), $parameters); $this->assertEquals(array('entity_test' => array('type' => 'entity:entity_test')), $parameters);
} }
/**
* Data provider for testSetRouteOptionsWithEntityTypeUpcasting.
*/
public function providerTestSetRouteOptionsWithEntityTypeUpcasting() {
return array(
array('Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityUpcasting'),
array('test_function_controller_entity_upcasting'),
);
}
/** /**
* Tests setRouteOptions() with an entity type parameter form. * Tests setRouteOptions() with an entity type parameter form.
* *
@ -345,11 +405,19 @@ class EntityResolverManagerTest extends UnitTestCase {
*/ */
protected function setupControllerResolver($controller_definition) { protected function setupControllerResolver($controller_definition) {
$controller = $controller_definition; $controller = $controller_definition;
list($class, $method) = explode('::', $controller);
if (strpos($controller, '::')) {
list($class, $method) = explode('::', $controller);
$expected = array(new $class(), $method);
}
else {
$expected = $controller;
}
$this->controllerResolver->expects($this->atLeastOnce()) $this->controllerResolver->expects($this->atLeastOnce())
->method('getControllerFromDefinition') ->method('getControllerFromDefinition')
->with($controller_definition) ->with($controller_definition)
->will($this->returnValue(array(new $class, $method))); ->will($this->returnValue($expected));
} }
/** /**
@ -482,3 +550,22 @@ class BasicFormNoContainerInjectionInterface implements FormInterface {
} }
} }
}
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) {
}
}