View source
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\linkchecker\Entity\LinkCheckerLink;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\RoleInterface;
class LinkCheckerLinkAccessTest extends KernelTestBase {
use UserCreationTrait {
createUser as drupalCreateUser;
createRole as drupalCreateRole;
createAdminRole as drupalCreateAdminRole;
}
public static $modules = [
'node',
'datetime',
'user',
'system',
'filter',
'field',
'text',
'path_alias',
'dynamic_entity_reference',
'linkchecker',
];
protected $accessHandler;
protected $entityTypeDefinitions;
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installSchema('linkchecker', 'linkchecker_index');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('path_alias');
$this
->installEntitySchema('linkcheckerlink');
$this
->installConfig('node');
$this
->installConfig('linkchecker');
$entityTypeManager = $this->container
->get('entity_type.manager');
$this->accessHandler = $entityTypeManager
->getAccessControlHandler('linkcheckerlink');
foreach ($entityTypeManager
->getDefinitions() as $definition) {
if ($definition
->entityClassImplements(FieldableEntityInterface::class) && $definition
->id() != 'linkcheckerlink') {
$this->entityTypeDefinitions[] = $definition;
}
}
$this->entityTypeManager = $entityTypeManager;
$this
->config('user.role.' . RoleInterface::AUTHENTICATED_ID)
->set('permissions', [])
->save();
$this
->drupalCreateUser();
}
public function testLinkAccess() {
$webUsers = [];
$webUsers[] = $this
->drupalCreateUser([
'administer linkchecker',
'access content',
]);
$webUsers[] = $this
->drupalCreateUser([
'edit linkchecker link settings',
'access content',
]);
$webUsers[] = $this
->drupalCreateUser([
'administer linkchecker',
]);
$webUsers[] = $this
->drupalCreateUser([
'edit linkchecker link settings',
]);
foreach ($this->entityTypeDefinitions as $entityTypeDefinition) {
$bundleId = $this
->createBundle($entityTypeDefinition);
$field_storage = [
'field_name' => 'test_text_field',
'entity_type' => $entityTypeDefinition
->id(),
'type' => 'text_long',
];
FieldStorageConfig::create($field_storage)
->save();
$field = [
'field_name' => $field_storage['field_name'],
'entity_type' => $entityTypeDefinition
->id(),
'bundle' => $bundleId,
];
FieldConfig::create($field)
->save();
$entity = $this
->createEntity($entityTypeDefinition, $bundleId);
$link = LinkCheckerLink::create([
'url' => 'http://example.com/',
'entity_id' => [
'target_id' => $entity
->id(),
'target_type' => $entity
->getEntityTypeId(),
],
'entity_field' => $field_storage['field_name'],
'entity_langcode' => $entity
->language()
->getId(),
]);
$link
->save();
foreach ($webUsers as $user) {
$access = $entity
->get($field_storage['field_name'])
->access('view', $user, FALSE);
$access = $access && $entity
->access('view', $user, FALSE);
$this
->assertLinkAccess([
'view' => $access,
], $link, $user);
}
}
$link = LinkCheckerLink::create([
'url' => 'http://example.com/',
'entity_id' => [
'target_id' => 9999,
'target_type' => 'node',
],
'entity_field' => 'test_text_field',
'entity_langcode' => 'en',
]);
$link
->save();
foreach ($webUsers as $user) {
$this
->assertLinkAccess([
'view' => FALSE,
], $link, $user);
}
}
public function assertLinkAccess(array $ops, LinkCheckerLinkInterface $link, AccountInterface $account) {
foreach ($ops as $op => $result) {
$this
->assertEquals($result, $this->accessHandler
->access($link, $op, $account), $this
->linkAccessAssertMessage($op, $result));
}
}
public function linkAccessAssertMessage($operation, $result) {
return new FormattableMarkup('LinkCheckerLink access returns @result with operation %op.', [
'@result' => $result ? 'true' : 'false',
'%op' => $operation,
]);
}
protected function createBundle(EntityTypeInterface $entityTypeDefinition) {
if ($bundleEntityType = $entityTypeDefinition
->getBundleEntityType()) {
$bundleStorage = $this->entityTypeManager
->getStorage($bundleEntityType);
do {
$bundleId = strtolower($this
->randomMachineName(8));
} while ($bundleStorage
->load($bundleId));
$bundleTypeDefinition = $this->entityTypeManager
->getDefinition($bundleEntityType);
$bundle = $bundleStorage
->create([
$bundleTypeDefinition
->getKey('id') => $bundleId,
$bundleTypeDefinition
->getKey('label') => $bundleId,
]);
$bundle
->save();
}
else {
$bundleId = $entityTypeDefinition
->id();
}
return $bundleId;
}
protected function createEntity(EntityTypeInterface $entityTypeDefinition, $bundleId) {
$entityData = [];
$entityData[$entityTypeDefinition
->getKey('bundle')] = $bundleId;
if ($entityTypeDefinition
->hasKey('published')) {
$entityData[$entityTypeDefinition
->getKey('published')] = 1;
}
$entity = $this->entityTypeManager
->getStorage($entityTypeDefinition
->id())
->create($entityData);
foreach ($entity
->getFieldDefinitions() as $fieldDefinition) {
if ($fieldDefinition
->isReadOnly()) {
continue;
}
if (!$fieldDefinition
->isRequired()) {
continue;
}
if (!$entity
->get($fieldDefinition
->getName())
->isEmpty()) {
continue;
}
$field = $entity
->get($fieldDefinition
->getName());
switch ($fieldDefinition
->getType()) {
case 'integer':
$field
->setValue([
'value' => 1,
]);
break;
default:
$field
->setValue([
'value' => $this
->randomString(),
]);
break;
}
}
$entity
->save();
return $entity;
}
}