View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\language\Entity\ConfigurableLanguage;
class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'url_alias');
}
function assertEntityAccess($ops, AccessibleInterface $object, AccountInterface $account = NULL) {
foreach ($ops as $op => $result) {
$message = format_string("Entity access returns @result with operation '@op'.", array(
'@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
'@op' => $op,
));
$this
->assertEqual($result, $object
->access($op, $account), $message);
}
}
function testEntityAccess() {
\Drupal::currentUser()
->setAccount($this
->createUser(array(
'uid' => 2,
), array(
'view test entity',
)));
$entity = entity_create('entity_test', array(
'name' => 'test',
));
$this
->assertEntityAccess(array(
'create' => FALSE,
'update' => FALSE,
'delete' => FALSE,
'view' => TRUE,
), $entity);
$custom_user = $this
->createUser();
$this
->assertEntityAccess(array(
'create' => FALSE,
'update' => FALSE,
'delete' => FALSE,
'view' => FALSE,
), $entity, $custom_user);
}
function testDefaultEntityAccess() {
\Drupal::currentUser()
->setAccount($this
->createUser(array(
'uid' => 2,
), array(
'view test entity',
)));
$entity = entity_create('entity_test', array(
'name' => 'forbid_access',
));
$this
->assertEntityAccess(array(
'create' => FALSE,
'update' => FALSE,
'delete' => FALSE,
'view' => FALSE,
), $entity);
}
function testEntityAccessDefaultController() {
\Drupal::currentUser()
->setAccount($this
->createUser(array(
'uid' => 2,
)));
$handler = $this->container
->get('entity.manager')
->getAccessControlHandler('entity_test_default_access');
$this
->assertTrue($handler instanceof EntityAccessControlHandler, 'The default entity handler is used for the entity_test_default_access entity type.');
$entity = entity_create('entity_test_default_access');
$this
->assertEntityAccess(array(
'create' => FALSE,
'update' => FALSE,
'delete' => FALSE,
'view' => FALSE,
), $entity);
}
function testEntityTranslationAccess() {
\Drupal::currentUser()
->setAccount($this
->createUser(array(
'uid' => 2,
), array(
'view test entity translations',
)));
foreach (array(
'foo',
'bar',
) as $langcode) {
ConfigurableLanguage::create(array(
'id' => $langcode,
'label' => $this
->randomString(),
))
->save();
}
$entity = entity_create('entity_test', array(
'name' => 'test',
'langcode' => 'foo',
));
$entity
->save();
$translation = $entity
->addTranslation('bar');
$this
->assertEntityAccess(array(
'view' => TRUE,
), $translation);
}
public function testHooks() {
$state = $this->container
->get('state');
$entity = entity_create('entity_test', array(
'name' => 'test',
));
$entity
->access('create');
$this
->assertEqual($state
->get('entity_test_entity_create_access'), TRUE);
$this
->assertEqual($state
->get('entity_test_entity_test_create_access'), TRUE);
$entity
->access('view');
$this
->assertEqual($state
->get('entity_test_entity_access'), TRUE);
$this
->assertEqual($state
->get('entity_test_entity_test_access'), TRUE);
}
}