View source
<?php
namespace Drupal\Tests\bibcite_entity\Kernel;
use Drupal\bibcite_entity\Entity\Reference;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class ReferenceFieldAccessTest extends EntityKernelTestBase {
public static $modules = [
'serialization',
'bibcite',
'bibcite_entity',
];
protected $administrativeFields = [
'uid',
'created',
];
protected $readOnlyFields = [
'changed',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('bibcite_reference_type');
$this
->installEntitySchema('bibcite_reference');
$this
->installConfig('bibcite_entity');
}
public function testAccessToAdministrativeFields() {
$content_admin_user = $this
->createUser([
'uid' => 2,
], [
'administer bibcite_reference',
]);
$page_creator_user = $this
->createUser([], [
'create book bibcite_reference',
'edit own book bibcite_reference',
'delete own book bibcite_reference',
]);
$page_manager_user = $this
->createUser([], [
'create book bibcite_reference',
'edit any book bibcite_reference',
'delete any book bibcite_reference',
]);
$page_unrelated_user = $this
->createUser([], [
'view bibcite_reference',
]);
$test_users = [
$content_admin_user,
$page_creator_user,
$page_manager_user,
$page_unrelated_user,
];
$reference1 = Reference::create([
'title' => $this
->randomMachineName(8),
'uid' => $page_creator_user
->id(),
'type' => 'book',
]);
$reference2 = Reference::create([
'title' => $this
->randomMachineName(8),
'uid' => $page_manager_user
->id(),
'type' => 'book',
]);
foreach ($this->administrativeFields as $field) {
foreach ($test_users as $account) {
$may_view = $reference1->{$field}
->access('view', $account);
$this
->assertTrue($may_view, new FormattableMarkup('Any user may view the field @name.', [
'@name' => $field,
]));
}
$may_update = $reference1->{$field}
->access('edit', $page_creator_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit own book bibcite_reference" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $reference2->{$field}
->access('edit', $page_creator_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit own book bibcite_reference" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $reference2->{$field}
->access('edit', $page_manager_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit any book bibcite_reference" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $reference1->{$field}
->access('edit', $page_manager_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users with permission "edit any book bibcite_reference" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $reference2->{$field}
->access('edit', $page_unrelated_user);
$this
->assertFalse($may_update, new FormattableMarkup('Users not having permission "edit any book bibcite_reference" is not allowed to the field @name.', [
'@name' => $field,
]));
$may_update = $reference1->{$field}
->access('edit', $content_admin_user);
$this
->assertTrue($may_update, new FormattableMarkup('Users with permission "administer bibcite_reference" may edit @name fields on all references.', [
'@name' => $field,
]));
}
foreach ($this->readOnlyFields as $field) {
foreach ($test_users as $account) {
$may_view = $reference1->{$field}
->access('view', $account);
$this
->assertTrue($may_view, new FormattableMarkup('Any user may view the field @name.', [
'@name' => $field,
]));
}
foreach ($test_users as $account) {
$may_view = $reference1->{$field}
->access('edit', $account);
$this
->assertFalse($may_view, new FormattableMarkup('No user is not allowed to edit the field @name.', [
'@name' => $field,
]));
}
}
}
}