You are here

class BundleEntityAccessControlHandlerTest in Entity API 8

@coversDefaultClass \Drupal\entity\BundleEntityAccessControlHandler @group entity

Hierarchy

Expanded class hierarchy of BundleEntityAccessControlHandlerTest

File

tests/src/Unit/BundleEntityAccessControlHandlerTest.php, line 22

Namespace

Drupal\Tests\entity\Unit
View source
class BundleEntityAccessControlHandlerTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $module_handler
      ->invokeAll(Argument::any(), Argument::any())
      ->willReturn([]);
    $cache_contexts_manager = $this
      ->prophesize(CacheContextsManager::class);
    $cache_contexts_manager
      ->assertValidTokens(Argument::any())
      ->willReturn(TRUE);
    $container = new ContainerBuilder();
    $container
      ->set('module_handler', $module_handler
      ->reveal());
    $container
      ->set('cache_contexts_manager', $cache_contexts_manager
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider accessProvider
   */
  public function testAccess(EntityInterface $entity, $operation, $account, $allowed) {
    $handler = new BundleEntityAccessControlHandler($entity
      ->getEntityType());
    $handler
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $result = $handler
      ->access($entity, $operation, $account);
    $this
      ->assertEquals($allowed, $result);
  }

  /**
   * Data provider for testAccess().
   *
   * @return array
   *   A list of testAccess method arguments.
   */
  public function accessProvider() {
    $entity_type = $this
      ->prophesize(ContentEntityTypeInterface::class);
    $entity_type
      ->id()
      ->willReturn('green_entity_bundle');
    $entity_type
      ->getBundleOf()
      ->willReturn('green_entity');
    $entity_type
      ->getAdminPermission()
      ->willReturn('administer green_entity');
    $entity_type = $entity_type
      ->reveal();
    $entity = $this
      ->prophesize(ConfigEntityInterface::class);
    $entity
      ->getEntityType()
      ->willReturn($entity_type);
    $entity
      ->getEntityTypeId()
      ->willReturn('green_entity_bundle');
    $entity
      ->id()
      ->willReturn('default');
    $entity
      ->uuid()
      ->willReturn('fake uuid');
    $entity
      ->language()
      ->willReturn(new Language([
      'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]));

    // User with no access.
    $user = $this
      ->buildMockUser(1, 'access content');
    $data[] = [
      $entity
        ->reveal(),
      'view label',
      $user
        ->reveal(),
      FALSE,
    ];

    // Permissions which grant "view label" access.
    $permissions = [
      'administer green_entity',
      'view green_entity',
      'view default green_entity',
      'view own green_entity',
      'view any green_entity',
      'view own default green_entity',
      'view any default green_entity',
    ];
    foreach ($permissions as $index => $permission) {
      $user = $this
        ->buildMockUser(10 + $index, $permission);
      $data[] = [
        $entity
          ->reveal(),
        'view label',
        $user
          ->reveal(),
        TRUE,
      ];
    }
    return $data;
  }

  /**
   * Builds a mock user.
   *
   * @param int $uid
   *   The user ID.
   * @param string $permission
   *   The permission to grant.
   *
   * @return \Prophecy\Prophecy\ObjectProphecy
   *   The user mock.
   */
  protected function buildMockUser($uid, $permission) {
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->id()
      ->willReturn($uid);
    $account
      ->hasPermission($permission)
      ->willReturn(TRUE);
    $account
      ->hasPermission(Argument::any())
      ->willReturn(FALSE);
    return $account;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BundleEntityAccessControlHandlerTest::accessProvider public function Data provider for testAccess().
BundleEntityAccessControlHandlerTest::buildMockUser protected function Builds a mock user.
BundleEntityAccessControlHandlerTest::setUp protected function Overrides UnitTestCase::setUp
BundleEntityAccessControlHandlerTest::testAccess public function @covers ::checkAccess
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.