View source
<?php
namespace Drupal\Tests\block_access\Kernel;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class BlockAccessTest extends KernelTestBase {
use UserCreationTrait;
protected $blockType1;
protected $blockType2;
protected $blockContent1;
protected $blockContent2;
protected $blockContent3;
protected $user1;
protected $user2;
protected $user3;
public static $modules = [
'block_access',
'block_content',
'block',
'system',
'user',
];
public function setUp() {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('block_content');
$this
->installEntitySchema('user');
$this->blockType1 = BlockContentType::create([
'id' => 'type1',
'label' => 'name1',
]);
$this->blockType1
->save();
$this->blockType2 = BlockContentType::create([
'id' => 'type2',
'label' => 'name2',
]);
$this->blockType2
->save();
$this
->createUser();
$this->user1 = $this
->createUser([
'update any type1 block_content',
'delete any type1 block_content',
'create type1 block_content',
'update own type2 block_content',
'delete own type2 block_content',
]);
$this->user2 = $this
->createUser([
'update any type2 block_content',
'delete any type2 block_content',
'create type2 block_content',
'update own type1 block_content',
'delete own type1 block_content',
]);
$this->user3 = $this
->createUser();
$this->blockContent1 = BlockContent::create([
'type' => 'type1',
'info' => 'block1',
'revision_user' => $this->user2
->id(),
]);
$this->blockContent1
->save();
$this->blockContent2 = BlockContent::create([
'type' => 'type2',
'info' => 'block2',
'revision_user' => $this->user2
->id(),
]);
$this->blockContent2
->save();
$this->blockContent3 = BlockContent::create([
'type' => 'type2',
'info' => 'block3',
'revision_user' => $this->user1
->id(),
]);
$this->blockContent3
->save();
}
public function testAccess() {
$this
->assertTrue($this->blockContent1
->access('update', $this->user1));
$this
->assertTrue($this->blockContent1
->access('update', $this->user2));
$this
->assertFalse($this->blockContent1
->access('update', $this->user3));
$this
->assertTrue($this->blockContent1
->access('delete', $this->user1));
$this
->assertTrue($this->blockContent1
->access('delete', $this->user2));
$this
->assertFalse($this->blockContent1
->access('delete', $this->user3));
$this
->assertFalse($this->blockContent2
->access('update', $this->user1));
$this
->assertTrue($this->blockContent2
->access('update', $this->user2));
$this
->assertFalse($this->blockContent2
->access('update', $this->user3));
$this
->assertFalse($this->blockContent2
->access('delete', $this->user1));
$this
->assertTrue($this->blockContent2
->access('delete', $this->user2));
$this
->assertFalse($this->blockContent2
->access('delete', $this->user3));
$this
->assertTrue($this->blockContent3
->access('update', $this->user1));
$this
->assertTrue($this->blockContent3
->access('update', $this->user2));
$this
->assertFalse($this->blockContent3
->access('update', $this->user3));
$entity_type_manager = $this->container
->get('entity_type.manager');
$handler = $entity_type_manager
->getAccessControlHandler('block_content');
$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));
}
}