Skip to content

Commit

Permalink
Remove AdminRoutes event because imho additional admin routes should …
Browse files Browse the repository at this point in the history
…be defined in routing.yml fix the test
  • Loading branch information
DiDebru committed Nov 21, 2018
1 parent 275739f commit 00f499d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 241 deletions.
5 changes: 1 addition & 4 deletions src/Controller/OgAdminMembersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 3 additions & 14 deletions src/Controller/OgAdminRoutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
*/
class OgAdminRoutesController extends ControllerBase {

/**
* The event dispatcher service.
*
* @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
*/
protected $eventDispatcher;

/**
* The access manager service.
*
Expand All @@ -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;
}

Expand All @@ -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')
);
}
Expand All @@ -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.
Expand Down
76 changes: 0 additions & 76 deletions src/Event/OgAdminRoutesEvent.php

This file was deleted.

49 changes: 0 additions & 49 deletions src/Event/OgAdminRoutesEventInterface.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/EventSubscriber/OgEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static function getSubscribedEvents() {
['provideDefaultNodePermissions'],
],
DefaultRoleEventInterface::EVENT_NAME => [['provideDefaultRoles']],
OgAdminRoutesEventInterface::EVENT_NAME => [['provideOgAdminRoutes']],
];
}

Expand Down Expand Up @@ -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);
}

}
26 changes: 0 additions & 26 deletions src/Routing/RouteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
71 changes: 12 additions & 59 deletions tests/src/Unit/OgAdminRoutesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,20 @@ class OgAdminRoutesControllerTest extends UnitTestCase {
*/
protected $routeMatch;

/**
* The event dispatcher service.
*
* @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher|\Prophecy\Prophecy\ObjectProphecy
*/
protected $eventDispatcher;

/**
* The group entity.
*
* @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $group;

/**
* The OG admin route event.
*
* @var \Drupal\og\Event\OgAdminRoutesEvent
*/
protected $event;

/**
* The entity type ID of the group entity.
*
* @var string
*/
protected $entityTypeId;

/**
* The routes info as returned from the event subscribers.
*
* @var array
*/
protected $routesInfo;

/**
* The Url object.
*
Expand All @@ -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()
Expand All @@ -142,7 +107,7 @@ public function setUp() {

$this
->routeMatch
->getParameter($parameter_name)
->getParameter('group')
->willReturn($this->group->reveal());

$this
Expand All @@ -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();
Expand Down Expand Up @@ -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']);
}

}
Expand All @@ -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());
}

Expand Down

0 comments on commit 00f499d

Please sign in to comment.