class GroupCheckTest in Organic groups 8
Tests the group check access.
@group og @coversDefaultClass \Drupal\og\Access\GroupCheck
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\og\Unit\GroupCheckTest
Expanded class hierarchy of GroupCheckTest
File
- tests/
src/ Unit/ GroupCheckTest.php, line 26
Namespace
Drupal\Tests\og\UnitView source
class GroupCheckTest extends UnitTestCase {
/**
* The entity type manager prophecy used in the test.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityTypeManager;
/**
* The entity type prophecy used in the test.
*
* @var \Drupal\Core\Entity\EntityTypeInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityType;
/**
* The entity storage prophecy used in the test.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityStorage;
/**
* The OG access service prophecy used in the test.
*
* @var \Drupal\og\OgAccess|\Prophecy\Prophecy\ObjectProphecy
*/
protected $ogAccess;
/**
* The route service prophecy used in the test.
*
* @var \Symfony\Component\Routing\Route|\Prophecy\Prophecy\ObjectProphecy
*/
protected $route;
/**
* A user used in the test.
*
* @var \Drupal\user\UserInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $user;
/**
* The entity type ID of the test group.
*
* @var string
*/
protected $entityTypeId;
/**
* The bundle ID of the test group.
*
* @var string
*/
protected $bundle;
/**
* The test group entity used in the test..
*
* @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $group;
/**
* A random entity ID.
*
* @var int
*/
protected $entityId;
/**
* The group manager used in the test.
*
* @var \Drupal\og\GroupTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $groupTypeManager;
/**
* The access result used in the test.
*
* @var \Drupal\Core\Access\AccessResultInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $accessResult;
/**
* The route match service used in the test.
*
* @var \\Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $routeMatch;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityType = $this
->prophesize(EntityTypeInterface::class);
$this->entityStorage = $this
->prophesize(EntityStorageInterface::class);
$this->ogAccess = $this
->prophesize(OgAccessInterface::class);
$this->route = $this
->prophesize(Route::class);
$this->routeMatch = $this
->prophesize(RouteMatchInterface::class);
$this->entityTypeId = $this
->randomMachineName();
$this->bundle = $this
->randomMachineName();
$this->entityId = rand(10, 50);
$this->groupTypeManager = $this
->prophesize(GroupTypeManagerInterface::class);
$this->user = $this
->prophesize(AccountInterface::class);
$this->group = $this
->prophesize(EntityInterface::class);
$this->accessResult = $this
->prophesize(AccessResultInterface::class);
$container = new ContainerBuilder();
$container
->set('og.group_type_manager', $this->groupTypeManager
->reveal());
\Drupal::setContainer($container);
}
/**
* Tests an invalid entity type.
*
* @covers ::access
*/
public function testInvalidEntityType() {
$this->entityTypeManager
->getDefinition($this->entityTypeId, FALSE)
->willReturn(NULL);
$result = $this
->getAccessResult();
$this
->assertTrue($result
->isForbidden());
}
/**
* Tests a non-existing group.
*
* @covers ::access
*/
public function testNoGroup() {
$this->entityTypeManager
->getDefinition($this->entityTypeId, FALSE)
->willReturn($this->entityType);
$this->entityTypeManager
->getStorage($this->entityTypeId)
->willReturn($this->entityStorage);
$this->entityStorage
->load($this->entityId)
->willReturn(NULL);
$this
->getAccessResult();
$result = $this
->getAccessResult();
$this
->assertTrue($result
->isForbidden());
}
/**
* Tests an entity that is not of group type.
*
* @covers ::access
*/
public function testNotGroupType() {
$this->entityTypeManager
->getDefinition($this->entityTypeId, FALSE)
->willReturn($this->entityType);
$this->entityTypeManager
->getStorage($this->entityTypeId)
->willReturn($this->entityStorage);
$this->entityStorage
->load($this->entityId)
->willReturn($this->group
->reveal());
$this->group
->bundle()
->willReturn($this->bundle);
$this->groupTypeManager
->isGroup($this->entityTypeId, $this->bundle)
->willReturn(FALSE);
$result = $this
->getAccessResult();
$this
->assertTrue($result
->isForbidden());
}
/**
* Tests an in-accessible and accessible routes.
*
* @covers ::access
* @dataProvider permissionsProvider
*/
public function testPermissions($permissions, $expected) {
$this->entityTypeManager
->getDefinition($this->entityTypeId, FALSE)
->willReturn($this->entityType);
$this->entityTypeManager
->getStorage($this->entityTypeId)
->willReturn($this->entityStorage);
$this->entityStorage
->load($this->entityId)
->willReturn($this->group);
$this->group
->bundle()
->willReturn($this->bundle);
$this->groupTypeManager
->isGroup($this->entityTypeId, $this->bundle)
->willReturn(TRUE);
$this->route
->getRequirement('_og_user_access_group')
->willReturn($permissions);
foreach (explode('|', $permissions) as $permission) {
// Check explicitly that only the permissions we passed were used.
$this->ogAccess
->userAccess($this->group
->reveal(), $permission, $this->user
->reveal())
->willReturn($this->accessResult);
}
$this->accessResult
->isAllowed()
->willReturn($expected);
$result = $this
->getAccessResult();
$actual = $expected ? $result
->isAllowed() : $result
->isForbidden();
$this
->assertTrue($actual);
}
/**
* Provides test data to test permissions.
*
* @return array
* Array with the permission names, and the expected access result as
* boolean.
*/
public function permissionsProvider() {
return [
[
'foo',
FALSE,
],
[
'foo',
TRUE,
],
[
'foo|bar',
FALSE,
],
[
'foo|bar',
TRUE,
],
];
}
/**
* Tests fetching arguments from the route match without "getOption" defined.
*/
public function testNoArgumentsFromRouteMatch() {
$this->routeMatch
->getRouteObject()
->willReturn($this->route);
$this->route
->getOption('_og_entity_type_id')
->willReturn(NULL);
// Call the group check without the entity related arguments.
$group_check = new GroupCheck($this->entityTypeManager
->reveal(), $this->ogAccess
->reveal());
$this
->expectException(\BadMethodCallException::class);
$group_check
->access($this->user
->reveal(), $this->route
->reveal(), $this->routeMatch
->reveal());
}
/**
* Tests fetching arguments from the route match with invalid group entity.
*/
public function testNoGroupFromRouteMatch() {
$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(NULL);
// Call the group check without the entity related arguments.
$group_check = new GroupCheck($this->entityTypeManager
->reveal(), $this->ogAccess
->reveal());
$result = $group_check
->access($this->user
->reveal(), $this->route
->reveal(), $this->routeMatch
->reveal());
$this
->assertTrue($result
->isForbidden());
}
/**
* Return the access result.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
protected function getAccessResult() {
$group_check = new GroupCheck($this->entityTypeManager
->reveal(), $this->ogAccess
->reveal());
return $group_check
->access($this->user
->reveal(), $this->route
->reveal(), $this->routeMatch
->reveal(), $this->entityTypeId, $this->entityId);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GroupCheckTest:: |
protected | property | The access result used in the test. | |
GroupCheckTest:: |
protected | property | The bundle ID of the test group. | |
GroupCheckTest:: |
protected | property | A random entity ID. | |
GroupCheckTest:: |
protected | property | The entity storage prophecy used in the test. | |
GroupCheckTest:: |
protected | property | The entity type prophecy used in the test. | |
GroupCheckTest:: |
protected | property | The entity type ID of the test group. | |
GroupCheckTest:: |
protected | property | The entity type manager prophecy used in the test. | |
GroupCheckTest:: |
protected | property | The test group entity used in the test.. | |
GroupCheckTest:: |
protected | property | The group manager used in the test. | |
GroupCheckTest:: |
protected | property | The OG access service prophecy used in the test. | |
GroupCheckTest:: |
protected | property | The route service prophecy used in the test. | |
GroupCheckTest:: |
protected | property | The route match service used in the test. | |
GroupCheckTest:: |
protected | property | A user used in the test. | |
GroupCheckTest:: |
protected | function | Return the access result. | |
GroupCheckTest:: |
public | function | Provides test data to test permissions. | |
GroupCheckTest:: |
protected | function |
Overrides UnitTestCase:: |
|
GroupCheckTest:: |
public | function | Tests an invalid entity type. | |
GroupCheckTest:: |
public | function | Tests fetching arguments from the route match without "getOption" defined. | |
GroupCheckTest:: |
public | function | Tests a non-existing group. | |
GroupCheckTest:: |
public | function | Tests fetching arguments from the route match with invalid group entity. | |
GroupCheckTest:: |
public | function | Tests an entity that is not of group type. | |
GroupCheckTest:: |
public | function | Tests an in-accessible and 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. |