class OgAdminRoutesControllerTest in Organic groups 8
Tests the OG admin routes overview route.
@group og @coversDefaultClass \Drupal\og\Controller\OgAdminRoutesController
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\og\Unit\OgAdminRoutesControllerTest
Expanded class hierarchy of OgAdminRoutesControllerTest
File
- tests/
src/ Unit/ OgAdminRoutesControllerTest.php, line 26
Namespace
Drupal\Tests\og\UnitView source
class OgAdminRoutesControllerTest extends UnitTestCase {
/**
* The access manager service.
*
* @var \Drupal\Core\Access\AccessManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $accessManager;
/**
* Route provider object.
*
* @var \Drupal\Core\Routing\RouteProvider|\Prophecy\Prophecy\ObjectProphecy
*/
protected $routeProvider;
/**
* The route service.
*
* @var \Symfony\Component\Routing\Route|\Prophecy\Prophecy\ObjectProphecy
*/
protected $route;
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy
*/
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.
*
* @var \Drupal\Core\Url
*/
protected $url;
/**
* The entity ID.
*
* @var int
*/
protected $entityId;
/**
* {@inheritdoc}
*/
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();
// 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() {
$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() {
$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($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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgAdminRoutesControllerTest:: |
protected | property | The access manager service. | |
OgAdminRoutesControllerTest:: |
protected | property | The entity ID. | |
OgAdminRoutesControllerTest:: |
protected | property | The entity type ID of the group entity. | |
OgAdminRoutesControllerTest:: |
protected | property | The OG admin route event. | |
OgAdminRoutesControllerTest:: |
protected | property | The event dispatcher service. | |
OgAdminRoutesControllerTest:: |
protected | property | The group entity. | |
OgAdminRoutesControllerTest:: |
protected | property | The route service. | |
OgAdminRoutesControllerTest:: |
protected | property | The route match service. | |
OgAdminRoutesControllerTest:: |
protected | property | Route provider object. | |
OgAdminRoutesControllerTest:: |
protected | property | The routes info as returned from the event subscribers. | |
OgAdminRoutesControllerTest:: |
protected | property | The Url object. | |
OgAdminRoutesControllerTest:: |
protected | function | Return the render array from calling the "overview" method. | |
OgAdminRoutesControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
OgAdminRoutesControllerTest:: |
public | function | Tests overview with accessible routes. | |
OgAdminRoutesControllerTest:: |
public | function | Tests overview with non-accessible routes. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |