You are here

public function BlockAccessTest::testAccess in Block Access 8

Tests block access.

File

tests/src/Kernel/BlockAccessTest.php, line 151

Class

BlockAccessTest
Tests block access functionality.

Namespace

Drupal\Tests\block_access\Kernel

Code

public function testAccess() {

  // User has edit-any permission.
  $this
    ->assertTrue($this->blockContent1
    ->access('update', $this->user1));

  // User has edit-own permission.
  $this
    ->assertTrue($this->blockContent1
    ->access('update', $this->user2));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent1
    ->access('update', $this->user3));

  // User has delete-any permission.
  $this
    ->assertTrue($this->blockContent1
    ->access('delete', $this->user1));

  // User has delete-own permission.
  $this
    ->assertTrue($this->blockContent1
    ->access('delete', $this->user2));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent1
    ->access('delete', $this->user3));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent2
    ->access('update', $this->user1));

  // User has edit-any permission.
  $this
    ->assertTrue($this->blockContent2
    ->access('update', $this->user2));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent2
    ->access('update', $this->user3));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent2
    ->access('delete', $this->user1));

  // User has delete-any permission.
  $this
    ->assertTrue($this->blockContent2
    ->access('delete', $this->user2));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent2
    ->access('delete', $this->user3));

  // User has edit-own permission.
  $this
    ->assertTrue($this->blockContent3
    ->access('update', $this->user1));

  // User has edit-any permission.
  $this
    ->assertTrue($this->blockContent3
    ->access('update', $this->user2));

  // User has no permission.
  $this
    ->assertFalse($this->blockContent3
    ->access('update', $this->user3));

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $handler = $entity_type_manager
    ->getAccessControlHandler('block_content');

  // User has create access.
  $this
    ->assertTrue($handler
    ->createAccess('type1', $this->user1));
  $this
    ->assertTrue($handler
    ->createAccess('type2', $this->user2));
  $this
    ->assertFalse($handler
    ->createAccess('type1', $this->user2));
  $this
    ->assertFalse($handler
    ->createAccess('type2', $this->user1));
}