Commit 6d8b0b81 authored by alexpott's avatar alexpott

Issue #2321393 by damiankloip, undertext: Fixed Unable to pass function name...

Issue #2321393 by damiankloip, undertext: Fixed Unable to pass function name to '_content' property of the route.
parent 61150517
......@@ -94,21 +94,28 @@ protected function getController(array $defaults) {
/**
* Sets the upcasting information using reflection.
*
* @param array $controller
* An array of class instance and method name.
* @param string|array $controller
* A PHP callable representing the controller.
* @param \Symfony\Component\Routing\Route $route
* The route object to populate without upcasting information.
*
* @return bool
* 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();
$parameter_definitions = $route->getOption('parameters') ?: array();
$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();
foreach ($parameters as $parameter) {
$parameter_name = $parameter->getName();
......
......@@ -5,7 +5,7 @@
* 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\EntityInterface;
......@@ -78,10 +78,12 @@ protected function setUp() {
*
* @covers ::setRouteOptions()
* @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithStandardRoute
*/
public function testSetRouteOptionsWithStandardRoute() {
public function testSetRouteOptionsWithStandardRoute($controller) {
$route = new Route('/example', array(
'_controller' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethod',
'_controller' => $controller,
));
$this->setupControllerResolver($route->getDefault('_controller'));
......@@ -91,15 +93,27 @@ public function testSetRouteOptionsWithStandardRoute() {
$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.
*
* @covers ::setRouteOptions()
* @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithStandardRouteWithArgument
*/
public function testSetRouteOptionsWithStandardRouteWithArgument() {
public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
$route = new Route('/example/{argument}', array(
'_controller' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument',
'_controller' => $controller,
'argument' => 'test',
));
$this->setupControllerResolver($route->getDefault('_controller'));
......@@ -110,15 +124,27 @@ public function testSetRouteOptionsWithStandardRouteWithArgument() {
$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.
*
* @covers ::setRouteOptions()
* @covers ::getController()
*
* @dataProvider providerTestSetRouteOptionsWithContentController
*/
public function testSetRouteOptionsWithContentController() {
public function testSetRouteOptionsWithContentController($controller) {
$route = new Route('/example/{argument}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerMethodWithArgument',
'_content' => $controller,
'argument' => 'test',
));
$this->setupControllerResolver($route->getDefault('_content'));
......@@ -129,6 +155,16 @@ public function testSetRouteOptionsWithContentController() {
$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.
*
......@@ -136,12 +172,14 @@ public function testSetRouteOptionsWithContentController() {
* @covers ::getController()
* @covers ::getEntityTypes()
* @covers ::setParametersFromReflection()
*
* @dataProvider providerTestSetRouteOptionsWithEntityTypeNoUpcasting
*/
public function testSetRouteOptionsWithEntityTypeNoUpcasting() {
public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
$this->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityNoUpcasting',
'_content' => $controller,
));
$this->setupControllerResolver($route->getDefault('_content'));
......@@ -151,6 +189,16 @@ public function testSetRouteOptionsWithEntityTypeNoUpcasting() {
$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.
*
......@@ -158,12 +206,14 @@ public function testSetRouteOptionsWithEntityTypeNoUpcasting() {
* @covers ::getController()
* @covers ::getEntityTypes()
* @covers ::setParametersFromReflection()
*
* @dataProvider providerTestSetRouteOptionsWithEntityTypeUpcasting
*/
public function testSetRouteOptionsWithEntityTypeUpcasting() {
public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
$this->setupEntityTypes();
$route = new Route('/example/{entity_test}', array(
'_content' => 'Drupal\Tests\Core\Entity\BasicControllerClass::exampleControllerWithEntityUpcasting',
'_content' => $controller,
));
$this->setupControllerResolver($route->getDefault('_content'));
......@@ -174,6 +224,16 @@ public function testSetRouteOptionsWithEntityTypeUpcasting() {
$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.
*
......@@ -345,11 +405,19 @@ public function testSetRouteOptionsWithEntityFormRoute() {
*/
protected function setupControllerResolver($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())
->method('getControllerFromDefinition')
->with($controller_definition)
->will($this->returnValue(array(new $class, $method)));
->will($this->returnValue($expected));
}
/**
......@@ -482,3 +550,22 @@ 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) {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment