og-8.x-1.x-dev/tests/src/Unit/OgAdminRoutesControllerTest.php
tests/src/Unit/OgAdminRoutesControllerTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Routing\RouteProvider;
use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Drupal\og\Controller\OgAdminRoutesController;
use Drupal\og\Event\OgAdminRoutesEvent;
use Drupal\og\Event\OgAdminRoutesEventInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route;
/**
* Tests the OG admin routes overview route.
*
* @group og
* @coversDefaultClass \Drupal\og\Controller\OgAdminRoutesController
*/
class OgAdminRoutesControllerTest extends UnitTestCase {
use ProphecyTrait;
/**
* The access manager service.
*/
protected AccessManagerInterface|ObjectProphecy $accessManager;
/**
* Route provider object.
*/
protected RouteProvider|ObjectProphecy $routeProvider;
/**
* The route service.
*/
protected Route|ObjectProphecy $route;
/**
* The route match service.
*/
protected RouteMatchInterface|ObjectProphecy $routeMatch;
/**
* The event dispatcher service.
*/
protected EventDispatcherInterface|ObjectProphecy $eventDispatcher;
/**
* The group entity.
*/
protected EntityInterface|ObjectProphecy $group;
/**
* The OG admin route event.
*/
protected OgAdminRoutesEvent|ObjectProphecy $event;
/**
* The entity type ID of the group entity.
*/
protected string $entityTypeId;
/**
* The routes info as returned from the event subscribers.
*
* @var array<array-key, non-empty-array<array-key, string>>
*/
protected array $routesInfo;
/**
* The Url object.
*/
protected Url|ObjectProphecy $url;
/**
* The entity ID.
*/
protected int $entityId;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->accessManager = $this->prophesize(AccessManagerInterface::class);
$this->routeMatch = $this->prophesize(RouteMatchInterface::class);
$this->group = $this->prophesize(EntityInterface::class);
$this->event = $this->prophesize(OgAdminRoutesEvent::class);
$this->eventDispatcher = $this->prophesize(EventDispatcher::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()
->willReturn($this->route);
$parameter_name = $this->randomMachineName();
$this
->route
->getOption('_og_entity_type_id')
->willReturn($parameter_name);
$this
->routeMatch
->getParameter($parameter_name)
->willReturn($this->group->reveal());
$this
->group
->getEntityTypeId()
->willReturn($this->entityTypeId);
$this
->group
->id()
->willReturn($this->entityId);
$this
->eventDispatcher
->dispatch(Argument::type(OgAdminRoutesEvent::class), OgAdminRoutesEventInterface::EVENT_NAME)
->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();
$container->set('access_manager', $this->accessManager->reveal());
$container->set('string_translation', $translation);
\Drupal::setContainer($container);
}
/**
* Tests overview with non-accessible routes.
*
* @covers ::overview
*/
public function testRoutesWithNoAccess(): void {
$result = $this->getRenderElementResult(FALSE);
$this->assertEquals('You do not have any administrative items.', $result['#markup']);
}
/**
* Tests overview with accessible routes.
*
* @covers ::overview
*/
public function testRoutesWithAccess(): void {
$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']);
}
}
/**
* Return the render array from calling the "overview" method.
*
* @param bool $allow_access
* Indicate if access to the routes should be given.
*
* @return array
* The render array.
*/
protected function getRenderElementResult(bool $allow_access): array {
$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);
}
$og_admin_routes_controller = new OgAdminRoutesController($this->eventDispatcher->reveal(), $this->accessManager->reveal());
return $og_admin_routes_controller->overview($this->routeMatch->reveal());
}
}
