View source
<?php
namespace Drupal\Tests\field_permission_example\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class FieldNoteItemTest extends FieldKernelTestBase {
use UserCreationTrait;
protected static $modules = [
'field_permission_example',
];
protected function setUp() : void {
parent::setUp();
$type_manager = $this->container
->get('entity_type.manager');
$type_manager->getStorage('field_storage_config')
->create([
'field_name' => 'field_field_note',
'entity_type' => 'entity_test',
'type' => 'field_permission_example_field_note',
])
->save();
$type_manager->getStorage('field_config')
->create([
'entity_type' => 'entity_test',
'field_name' => 'field_field_note',
'bundle' => 'entity_test',
])
->save();
$type_manager->getStorage('entity_form_display')
->create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('field_field_note', [
'type' => 'field_permission_example_widget',
])
->save();
$type_manager->getStorage('field_storage_config')
->create([
'field_name' => 'user_field_note',
'entity_type' => 'user',
'type' => 'field_permission_example_field_note',
])
->save();
$type_manager->getStorage('field_config')
->create([
'entity_type' => 'user',
'field_name' => 'user_field_note',
'bundle' => 'user',
])
->save();
$entity_form_display = $type_manager->getStorage('entity_form_display')
->load('user.user.default');
if (empty($entity_form_display)) {
$entity_form_display = $type_manager->getStorage('entity_form_display')
->create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
}
$entity_form_display->setComponent('field_field_note', [
'type' => 'field_permission_example_widget',
])
->save();
}
public function testFieldNoteItem() {
$type_manager = $this->container
->get('entity_type.manager');
$entity = $type_manager->getStorage('entity_test')
->create([]);
$value = 'This is an epic entity';
$entity->field_field_note = $value;
$entity->name->value = $this->randomMachineName();
$entity->save();
$id = $entity->id();
$entity = $type_manager->getStorage('entity_test')
->load($id);
$this->assertTrue($entity->field_field_note instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_field_note[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEquals($entity->field_field_note->value, $value);
$this->assertEquals($entity->field_field_note[0]->value, $value);
$new_value = $this->randomMachineName();
$entity->field_field_note->value = $new_value;
$this->assertEquals($entity->field_field_note->value, $new_value);
$entity->save();
$entity = $type_manager->getStorage('entity_test')
->load($id);
$this->assertEquals($entity->field_field_note->value, $new_value);
$entity = $type_manager->getStorage('entity_test')
->create([]);
$entity->field_field_note
->generateSampleItems();
$this->entityValidateAndSave($entity);
}
public function testFieldNoteAccess() {
$scenarios = [
'admin_type' => [
'perms' => [
'administer the field note field',
],
'can_view_any' => TRUE,
'can_edit_any' => TRUE,
'can_view_own' => TRUE,
'can_edit_own' => TRUE,
],
'low_access' => [
'perms' => [
'view test entity',
],
'can_view_any' => FALSE,
'can_edit_any' => FALSE,
'can_view_own' => FALSE,
'can_edit_own' => FALSE,
],
'view_any' => [
'perms' => [
'view test entity',
'view any field note',
],
'can_view_any' => TRUE,
'can_edit_any' => FALSE,
'can_view_own' => FALSE,
'can_edit_own' => FALSE,
],
'edit_any' => [
'perms' => [
'view test entity',
'view any field note',
'edit any field note',
],
'can_view_any' => TRUE,
'can_edit_any' => TRUE,
'can_view_own' => FALSE,
'can_edit_own' => FALSE,
],
'view_own' => [
'perms' => [
'view test entity',
'view own field note',
],
'can_view_any' => FALSE,
'can_edit_any' => FALSE,
'can_view_own' => TRUE,
'can_edit_own' => FALSE,
],
'edit_own' => [
'perms' => [
'view test entity',
'view own field note',
'edit own field note',
],
'can_view_any' => FALSE,
'can_edit_any' => FALSE,
'can_view_own' => TRUE,
'can_edit_own' => TRUE,
],
];
$value = 'This is an epic entity';
$arbitrary_user = $this->createUser([], 'Some User');
$arbitrary_user->user_field_note = $value;
$arbitrary_user->save();
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
foreach ($scenarios as $name => $scenario) {
$test_user = $this->createUser($scenario['perms'], $name);
$entity = $storage->create([
'entity_test',
]);
$entity->field_field_note = $value;
$entity->name->value = $this->randomMachineName();
$entity->save();
foreach ([
'can_view_any',
'can_edit_any',
] as $op) {
$this->doAccessAssertion($entity, 'field_field_note', $test_user, $name, $op, $scenario[$op]);
$this->doAccessAssertion($arbitrary_user, 'user_field_note', $test_user, $name, $op, $scenario[$op]);
}
if ($scenario['can_view_own'] or $scenario['can_edit_own']) {
$entity->user_id = $test_user;
$entity->save();
$test_user->user_field_note = $value;
$test_user->save();
foreach ([
'can_view_own',
'can_edit_own',
] as $op) {
$this->doAccessAssertion($entity, 'field_field_note', $test_user, $name, $op, $scenario[$op]);
$this->doAccessAssertion($test_user, 'user_field_note', $test_user, $name, $op, $scenario[$op]);
}
}
}
}
protected function doAccessAssertion($entity, $field_name, $account, $name, $op, $expected) {
$expect_str = $expected ? "CAN" : "CANNOT";
$assert_str = "{$name} {$expect_str} do {$op} on field {$field_name}";
$operation = preg_match('/edit/', $op) ? "edit" : "view";
$result = $entity->{$field_name}
->access($operation, $account);
if ($expected) {
$this->assertTrue($result, $assert_str);
}
else {
$this->assertFalse($result, $assert_str);
}
}
}