You are here

class GroupCheckTest in Organic groups 8

Tests the group check access.

@group og @coversDefaultClass \Drupal\og\Access\GroupCheck

Hierarchy

Expanded class hierarchy of GroupCheckTest

File

tests/src/Unit/GroupCheckTest.php, line 26

Namespace

Drupal\Tests\og\Unit
View 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

Namesort descending Modifiers Type Description Overrides
GroupCheckTest::$accessResult protected property The access result used in the test.
GroupCheckTest::$bundle protected property The bundle ID of the test group.
GroupCheckTest::$entityId protected property A random entity ID.
GroupCheckTest::$entityStorage protected property The entity storage prophecy used in the test.
GroupCheckTest::$entityType protected property The entity type prophecy used in the test.
GroupCheckTest::$entityTypeId protected property The entity type ID of the test group.
GroupCheckTest::$entityTypeManager protected property The entity type manager prophecy used in the test.
GroupCheckTest::$group protected property The test group entity used in the test..
GroupCheckTest::$groupTypeManager protected property The group manager used in the test.
GroupCheckTest::$ogAccess protected property The OG access service prophecy used in the test.
GroupCheckTest::$route protected property The route service prophecy used in the test.
GroupCheckTest::$routeMatch protected property The route match service used in the test.
GroupCheckTest::$user protected property A user used in the test.
GroupCheckTest::getAccessResult protected function Return the access result.
GroupCheckTest::permissionsProvider public function Provides test data to test permissions.
GroupCheckTest::setUp protected function Overrides UnitTestCase::setUp
GroupCheckTest::testInvalidEntityType public function Tests an invalid entity type.
GroupCheckTest::testNoArgumentsFromRouteMatch public function Tests fetching arguments from the route match without "getOption" defined.
GroupCheckTest::testNoGroup public function Tests a non-existing group.
GroupCheckTest::testNoGroupFromRouteMatch public function Tests fetching arguments from the route match with invalid group entity.
GroupCheckTest::testNotGroupType public function Tests an entity that is not of group type.
GroupCheckTest::testPermissions public function Tests an in-accessible and accessible routes.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.