From 00f499da14aeccd0ed7ae18f2e932fc77ea3ca42 Mon Sep 17 00:00:00 2001 From: Insasse Date: Wed, 21 Nov 2018 13:07:20 +0100 Subject: [PATCH] Remove AdminRoutes event because imho additional admin routes should be defined in routing.yml fix the test --- src/Controller/OgAdminMembersController.php | 5 +- src/Controller/OgAdminRoutesController.php | 17 +---- src/Event/OgAdminRoutesEvent.php | 76 ------------------- src/Event/OgAdminRoutesEventInterface.php | 49 ------------ src/EventSubscriber/OgEventSubscriber.php | 13 ---- src/Routing/RouteSubscriber.php | 26 ------- .../src/Unit/OgAdminRoutesControllerTest.php | 71 +++-------------- 7 files changed, 16 insertions(+), 241 deletions(-) delete mode 100644 src/Event/OgAdminRoutesEvent.php delete mode 100644 src/Event/OgAdminRoutesEventInterface.php diff --git a/src/Controller/OgAdminMembersController.php b/src/Controller/OgAdminMembersController.php index f0ba7a670..6018f3834 100644 --- a/src/Controller/OgAdminMembersController.php +++ b/src/Controller/OgAdminMembersController.php @@ -70,10 +70,7 @@ public function membersList(RouteMatchInterface $route_match) { * the function will return the default add member form. */ public function addPage(RouteMatchInterface $route_match) { - $entity_type_id = $route_match->getRouteObject() - ->getOption('_og_entity_type_id'); - - $group = $route_match->getParameter($entity_type_id); + $group = $route_match->getParameter('group'); $membership_types = $this->entityTypeManager ->getStorage('og_membership_type') diff --git a/src/Controller/OgAdminRoutesController.php b/src/Controller/OgAdminRoutesController.php index 3d3681fda..d6a9002ef 100644 --- a/src/Controller/OgAdminRoutesController.php +++ b/src/Controller/OgAdminRoutesController.php @@ -17,13 +17,6 @@ */ class OgAdminRoutesController extends ControllerBase { - /** - * The event dispatcher service. - * - * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher - */ - protected $eventDispatcher; - /** * The access manager service. * @@ -39,8 +32,7 @@ class OgAdminRoutesController extends ControllerBase { * @param \Drupal\Core\Access\AccessManagerInterface $access_manager * The access manager service. */ - public function __construct(ContainerAwareEventDispatcher $event_dispatcher, AccessManagerInterface $access_manager) { - $this->eventDispatcher = $event_dispatcher; + public function __construct(AccessManagerInterface $access_manager) { $this->accessManager = $access_manager; } @@ -49,7 +41,6 @@ public function __construct(ContainerAwareEventDispatcher $event_dispatcher, Acc */ public static function create(ContainerInterface $container) { return new static( - $container->get('event_dispatcher'), $container->get('access_manager') ); } @@ -64,16 +55,14 @@ public static function create(ContainerInterface $container) { * List of available admin routes for the current group. */ public function overview(RouteMatchInterface $route_match) { - $entity_type_id = $route_match->getRouteObject()->getOption('_og_entity_type_id'); - /** @var \Drupal\Core\Entity\EntityInterface $group */ - $group = $route_match->getParameter($entity_type_id); + $group = $route_match->getParameter('group'); // Get list from routes. $content = []; $route_name = "og_admin.members"; - $parameters = ['entity_type_id' => $entity_type_id, 'group' => $group->id()]; + $parameters = ['entity_type_id' => $group->getEntityTypeId(), 'group' => $group->id()]; // We don't use Url::fromRoute() here for the access check, as it will // prevent us from unit testing this method. diff --git a/src/Event/OgAdminRoutesEvent.php b/src/Event/OgAdminRoutesEvent.php deleted file mode 100644 index 4ae012172..000000000 --- a/src/Event/OgAdminRoutesEvent.php +++ /dev/null @@ -1,76 +0,0 @@ -routesInfo = $routes_info; - } - - /** - * {@inheritdoc} - */ - public function getRoutesInfo() { - return $this->routesInfo; - } - - /** - * {@inheritdoc} - */ - public function getRoutes($entity_type_id) { - $routes_info = []; - - foreach ($this->routesInfo as $name => $route_info) { - - $routes_info[$name] = $route_info; - - // Add default values. NestedArray::mergeDeep allows deep data to not be - // overwritten with the defaults. - $defaults = [ - 'description' => '', - - 'requirements' => [ - '_og_user_access_group' => 'administer group', - ], - - 'options' => [ - 'parameters' => [ - $entity_type_id => ['type' => 'entity:' . $entity_type_id], - ], - // The above parameters doesn't send the entity, - // so we will have to use the Route matcher to extract it. - '_og_entity_type_id' => $entity_type_id, - '_admin_route' => TRUE, - ], - - // Move the title and controller under the "defaults" key. - 'defaults' => [ - '_controller' => $route_info['controller'], - '_title' => $route_info['title'], - ], - ]; - - $routes_info[$name] = NestedArray::mergeDeep($defaults, $routes_info[$name]); - } - - return $routes_info; - } - -} diff --git a/src/Event/OgAdminRoutesEventInterface.php b/src/Event/OgAdminRoutesEventInterface.php deleted file mode 100644 index 5ad4bd851..000000000 --- a/src/Event/OgAdminRoutesEventInterface.php +++ /dev/null @@ -1,49 +0,0 @@ - [['provideDefaultRoles']], - OgAdminRoutesEventInterface::EVENT_NAME => [['provideOgAdminRoutes']], ]; } @@ -342,16 +341,4 @@ protected function generateEntityOperationPermissionList($group_content_entity_t return $permissions; } - /** - * Provide OG admin routes. - * - * @param \Drupal\og\Event\OgAdminRoutesEventInterface $event - * The OG admin routes event object. - */ - public function provideOgAdminRoutes(OgAdminRoutesEventInterface $event) { - $routes_info = $event->getRoutesInfo(); - - $event->setRoutesInfo($routes_info); - } - } diff --git a/src/Routing/RouteSubscriber.php b/src/Routing/RouteSubscriber.php index ca6296dff..c4f2505d0 100644 --- a/src/Routing/RouteSubscriber.php +++ b/src/Routing/RouteSubscriber.php @@ -95,36 +95,10 @@ protected function alterRoutes(RouteCollection $collection) { $collection->add($route_name, $route); - // Add the routes defined in the event subscribers. - $this->createRoutesFromEventSubscribers($og_admin_path, $entity_type_id, $collection); - } } - /** - * Add all the OG admin items to the route collection. - * - * @param string $og_admin_path - * The OG admin path. - * @param string $entity_type_id - * The entity type ID. - * @param \Symfony\Component\Routing\RouteCollection $collection - * The route collection object. - */ - protected function createRoutesFromEventSubscribers($og_admin_path, $entity_type_id, RouteCollection $collection) { - $event = new OgAdminRoutesEvent(); - $this->eventDispatcher->dispatch(OgAdminRoutesEventInterface::EVENT_NAME, $event); - - foreach ($event->getRoutes($entity_type_id) as $name => $route_info) { - // Add the parent route. - $parent_route_name = "entity.$entity_type_id.og_admin_routes.$name"; - $parent_path = $og_admin_path . '/' . $route_info['path']; - - $this->addRoute($collection, $parent_route_name, $parent_path, $route_info); - } - } - /** * Helper method to add route to collection. * diff --git a/tests/src/Unit/OgAdminRoutesControllerTest.php b/tests/src/Unit/OgAdminRoutesControllerTest.php index ddd303058..811c16466 100644 --- a/tests/src/Unit/OgAdminRoutesControllerTest.php +++ b/tests/src/Unit/OgAdminRoutesControllerTest.php @@ -51,13 +51,6 @@ class OgAdminRoutesControllerTest extends UnitTestCase { */ protected $routeMatch; - /** - * The event dispatcher service. - * - * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher|\Prophecy\Prophecy\ObjectProphecy - */ - protected $eventDispatcher; - /** * The group entity. * @@ -65,13 +58,6 @@ class OgAdminRoutesControllerTest extends UnitTestCase { */ protected $group; - /** - * The OG admin route event. - * - * @var \Drupal\og\Event\OgAdminRoutesEvent - */ - protected $event; - /** * The entity type ID of the group entity. * @@ -79,13 +65,6 @@ class OgAdminRoutesControllerTest extends UnitTestCase { */ protected $entityTypeId; - /** - * The routes info as returned from the event subscribers. - * - * @var array - */ - protected $routesInfo; - /** * The Url object. * @@ -109,25 +88,11 @@ public function setUp() { $this->routeMatch = $this->prophesize(RouteMatchInterface::class); $this->group = $this->prophesize(EntityInterface::class); - $this->event = $this->prophesize(OgAdminRoutesEvent::class); - $this->eventDispatcher = $this->prophesize(ContainerAwareEventDispatcher::class); $this->route = $this->prophesize(Route::class); $this->entityTypeId = $this->randomMachineName(); $this->entityId = rand(20, 30); $this->url = $this->prophesize(Url::class); - $this->routesInfo = [ - $this->randomMachineName() => [ - 'title' => $this->randomMachineName(), - 'description' => $this->randomMachineName(), - ], - - $this->randomMachineName() => [ - 'title' => $this->randomMachineName(), - 'description' => $this->randomMachineName(), - ], - ]; - $this ->routeMatch ->getRouteObject() @@ -142,7 +107,7 @@ public function setUp() { $this ->routeMatch - ->getParameter($parameter_name) + ->getParameter('group') ->willReturn($this->group->reveal()); $this @@ -155,18 +120,6 @@ public function setUp() { ->id() ->willReturn($this->entityId); - $this - ->eventDispatcher - ->dispatch(OgAdminRoutesEventInterface::EVENT_NAME, Argument::type(OgAdminRoutesEvent::class)) - ->willReturn($this->event->reveal()) - ->shouldBeCalled(); - - $this - ->event - ->getRoutes($this->entityTypeId) - ->willReturn($this->routesInfo) - ->shouldBeCalled(); - // Set the container for the string translation service. $translation = $this->getStringTranslationStub(); $container = new ContainerBuilder(); @@ -194,8 +147,8 @@ public function testRoutesWithAccess() { $result = $this->getRenderElementResult(TRUE); foreach ($result['og_admin_routes']['#content'] as $key => $value) { - $this->assertEquals($this->routesInfo[$key]['title'], $value['title']); - $this->assertEquals($this->routesInfo[$key]['description'], $value['description']); + $this->assertEquals('Members', $value['title']); + $this->assertEquals('Manage members', $value['description']); } } @@ -210,16 +163,16 @@ public function testRoutesWithAccess() { * The render array. */ protected function getRenderElementResult($allow_access) { - $parameters = [$this->entityTypeId => $this->entityId]; - foreach (array_keys($this->routesInfo) as $name) { - $route_name = "entity.{$this->entityTypeId}.og_admin_routes.$name"; - $this - ->accessManager - ->checkNamedRoute($route_name, $parameters) - ->willReturn($allow_access); - } + $parameters = ['entity_type_id' => $this->entityTypeId, 'group' => $this->entityId]; + + $route_name = "og_admin.members"; + $this + ->accessManager + ->checkNamedRoute($route_name, $parameters) + ->willReturn($allow_access); + - $og_admin_routes_controller = new OgAdminRoutesController($this->eventDispatcher->reveal(), $this->accessManager->reveal()); + $og_admin_routes_controller = new OgAdminRoutesController($this->accessManager->reveal()); return $og_admin_routes_controller->overview($this->routeMatch->reveal()); }