og-8.x-1.x-dev/tests/src/Unit/GroupCheckTest.php

tests/src/Unit/GroupCheckTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Unit;

use Drupal\user\UserInterface;
use Drupal\og\OgAccess;
use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
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 Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Routing\Route;

/**
 * Tests the group check access.
 *
 * @group og
 * @coversDefaultClass \Drupal\og\Access\GroupCheck
 */
class GroupCheckTest extends UnitTestCase {

  use ProphecyTrait;

  /**
   * The entity type manager prophecy used in the test.
   */
  protected EntityTypeManagerInterface|ObjectProphecy $entityTypeManager;

  /**
   * The entity type prophecy used in the test.
   */
  protected EntityTypeInterface|ObjectProphecy $entityType;

  /**
   * The entity storage prophecy used in the test.
   */
  protected EntityStorageInterface|ObjectProphecy $entityStorage;

  /**
   * The OG access service prophecy used in the test.
   */
  protected OgAccess|ObjectProphecy $ogAccess;

  /**
   * The route service prophecy used in the test.
   */
  protected Route|ObjectProphecy $route;

  /**
   * A user used in the test.
   */
  protected UserInterface|ObjectProphecy $user;

  /**
   * The entity type ID of the test group.
   */
  protected string $entityTypeId;

  /**
   * The bundle ID of the test group.
   */
  protected string $bundle;

  /**
   * The test group entity used in the test.
   */
  protected EntityInterface|ObjectProphecy $group;

  /**
   * A random entity ID.
   */
  protected int $entityId;

  /**
   * The group manager used in the test.
   */
  protected GroupTypeManagerInterface|ObjectProphecy $groupTypeManager;

  /**
   * The access result used in the test.
   */
  protected AccessResultInterface|ObjectProphecy $accessResult;

  /**
   * The route match service used in the test.
   */
  protected RouteMatchInterface|ObjectProphecy $routeMatch;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $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(): void {
    $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(): void {
    $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(): void {
    $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(string $permissions, bool $expected): void {
    $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 static function permissionsProvider(): array {
    return [
      ['foo', FALSE],
      ['foo', TRUE],
      ['foo|bar', FALSE],
      ['foo|bar', TRUE],
    ];
  }

  /**
   * Tests fetching arguments from the route match without "getOption" defined.
   */
  public function testNoArgumentsFromRouteMatch(): void {
    $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(): void {
    $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(): AccessResultInterface {
    $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);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc