View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\Access\GroupCheck;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgAccessInterface;
use Symfony\Component\Routing\Route;
class GroupCheckTest extends UnitTestCase {
protected $entityTypeManager;
protected $entityType;
protected $entityStorage;
protected $ogAccess;
protected $route;
protected $user;
protected $entityTypeId;
protected $bundle;
protected $group;
protected $entityId;
protected $groupTypeManager;
protected $accessResult;
protected $routeMatch;
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);
}
public function testInvalidEntityType() {
$this->entityTypeManager
->getDefinition($this->entityTypeId, FALSE)
->willReturn(NULL);
$result = $this
->getAccessResult();
$this
->assertTrue($result
->isForbidden());
}
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());
}
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());
}
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) {
$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);
}
public function permissionsProvider() {
return [
[
'foo',
FALSE,
],
[
'foo',
TRUE,
],
[
'foo|bar',
FALSE,
],
[
'foo|bar',
TRUE,
],
];
}
public function testNoArgumentsFromRouteMatch() {
$this->routeMatch
->getRouteObject()
->willReturn($this->route);
$this->route
->getOption('_og_entity_type_id')
->willReturn(NULL);
$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());
}
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);
$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());
}
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);
}
}