You are here

class GroupContentEntityAccessControlHandlerTest in Group 8

Tests the group content entity access control handler.

@coversDefaultClass \Drupal\group\Entity\Access\GroupContentAccessControlHandler @group group

Hierarchy

Expanded class hierarchy of GroupContentEntityAccessControlHandlerTest

File

tests/src/Unit/GroupContentEntityAccessControlHandlerTest.php, line 29

Namespace

Drupal\Tests\group\Unit
View source
class GroupContentEntityAccessControlHandlerTest extends UnitTestCase {

  /**
   * The account to test with.
   *
   * @var \Drupal\Core\Session\AccountInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $account;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * The group relation manager.
   *
   * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $groupContentEnablerManager;

  /**
   * The access control handler.
   *
   * @var \Drupal\group\Entity\Access\GroupContentAccessControlHandler|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $accessControlHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->account = $this
      ->prophesize(AccountInterface::class);
    $this->account
      ->id()
      ->willReturn(1986);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->groupContentEnablerManager = $this
      ->prophesize(GroupContentEnablerManagerInterface::class);
    $moduleHandler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $moduleHandler
      ->invokeAll(Argument::cetera())
      ->willReturn([]);
    $entity_type_repository = $this
      ->prophesize(EntityTypeRepositoryInterface::class);
    $entity_type_repository
      ->getEntityTypeFromClass(Argument::any())
      ->willReturn('group_content_type');
    $container = $this
      ->prophesize(ContainerInterface::class);
    $container
      ->get('entity_type.manager')
      ->willReturn($this->entityTypeManager
      ->reveal());
    $container
      ->get('entity_type.repository')
      ->willReturn($entity_type_repository
      ->reveal());
    $container
      ->get('plugin.manager.group_content_enabler')
      ->willReturn($this->groupContentEnablerManager
      ->reveal());
    $container
      ->get('module_handler')
      ->willReturn($moduleHandler
      ->reveal());
    \Drupal::setContainer($container
      ->reveal());
    $entityType = $this
      ->prophesize(EntityTypeInterface::class);
    $this->accessControlHandler = new GroupContentAccessControlHandler($entityType
      ->reveal());
  }

  /**
   * Tests access.
   *
   * @covers ::checkAccess
   * @uses ::access
   */
  public function testCheckAccess() {
    $content_enabler = $this
      ->prophesize(GroupContentEnablerInterface::class);
    $language = $this
      ->prophesize(LanguageInterface::class);
    $language
      ->getId()
      ->willReturn('nl');
    $group_content = $this
      ->prophesize(GroupContentInterface::class);
    $group_content
      ->id()
      ->willReturn(1337);
    $group_content
      ->uuid()
      ->willReturn('baz');
    $group_content
      ->language()
      ->willReturn($language
      ->reveal());
    $group_content
      ->getRevisionId()
      ->willReturn(9001);
    $group_content
      ->getEntityTypeId()
      ->willReturn('group_content');
    $group_content
      ->getContentPlugin()
      ->willReturn($content_enabler
      ->reveal());
    $access_result = AccessResult::allowed();
    $content_enabler
      ->checkAccess($group_content
      ->reveal(), 'some_operation', $this->account
      ->reveal())
      ->shouldBeCalled()
      ->willReturn($access_result);
    $result = $this->accessControlHandler
      ->access($group_content
      ->reveal(), 'some_operation', $this->account
      ->reveal());
    $this
      ->assertEquals($access_result
      ->isAllowed(), $result);
  }

  /**
   * Tests create access.
   *
   * @covers ::checkCreateAccess
   * @uses ::createAccess
   */
  public function testCheckCreateAccess() {
    $group = $this
      ->prophesize(GroupInterface::class);
    $content_enabler = $this
      ->prophesize(GroupContentEnablerInterface::class);
    $group_content_type = $this
      ->prophesize(GroupContentTypeInterface::class);
    $group_content_type
      ->getContentPlugin()
      ->willReturn($content_enabler
      ->reveal());
    $group_content_type_storage = $this
      ->prophesize(GroupContentTypeStorageInterface::class);
    $group_content_type_storage
      ->load('foo')
      ->willReturn($group_content_type
      ->reveal());
    $this->entityTypeManager
      ->getStorage('group_content_type')
      ->willReturn($group_content_type_storage
      ->reveal());
    $access_result = AccessResult::allowed();
    $content_enabler
      ->createAccess($group
      ->reveal(), $this->account
      ->reveal())
      ->shouldBeCalled()
      ->willReturn($access_result);
    $result = $this->accessControlHandler
      ->createAccess('foo', $this->account
      ->reveal(), [
      'group' => $group
        ->reveal(),
    ]);
    $this
      ->assertEquals($access_result
      ->isAllowed(), $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupContentEntityAccessControlHandlerTest::$accessControlHandler protected property The access control handler.
GroupContentEntityAccessControlHandlerTest::$account protected property The account to test with.
GroupContentEntityAccessControlHandlerTest::$entityTypeManager protected property The entity type manager.
GroupContentEntityAccessControlHandlerTest::$groupContentEnablerManager protected property The group relation manager.
GroupContentEntityAccessControlHandlerTest::setUp protected function Overrides UnitTestCase::setUp
GroupContentEntityAccessControlHandlerTest::testCheckAccess public function Tests access.
GroupContentEntityAccessControlHandlerTest::testCheckCreateAccess public function Tests create access.
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.