OgAdminRoutesControllerTest.php in Organic groups 8
File
tests/src/Unit/OgAdminRoutesControllerTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
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 Symfony\Component\Routing\Route;
class OgAdminRoutesControllerTest extends UnitTestCase {
protected $accessManager;
protected $routeProvider;
protected $route;
protected $routeMatch;
protected $eventDispatcher;
protected $group;
protected $event;
protected $entityTypeId;
protected $routesInfo;
protected $url;
protected $entityId;
protected function setUp() : void {
$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(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()
->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(OgAdminRoutesEventInterface::EVENT_NAME, Argument::type(OgAdminRoutesEvent::class))
->willReturn($this->event
->reveal())
->shouldBeCalled();
$this->event
->getRoutes($this->entityTypeId)
->willReturn($this->routesInfo)
->shouldBeCalled();
$translation = $this
->getStringTranslationStub();
$container = new ContainerBuilder();
$container
->set('access_manager', $this->accessManager
->reveal());
$container
->set('string_translation', $translation);
\Drupal::setContainer($container);
}
public function testRoutesWithNoAccess() {
$result = $this
->getRenderElementResult(FALSE);
$this
->assertEquals('You do not have any administrative items.', $result['#markup']);
}
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']);
}
}
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);
}
$og_admin_routes_controller = new OgAdminRoutesController($this->eventDispatcher
->reveal(), $this->accessManager
->reveal());
return $og_admin_routes_controller
->overview($this->routeMatch
->reveal());
}
}