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;
public static $modules = [
'field_permission_example',
];
protected function setUp() {
parent::setUp();
$type_manager = $this->container
->get('entity_type.manager');
$type_manager
->getStorage('field_storage_config')
->create([
'field_name' => 'field_fieldnote',
'entity_type' => 'entity_test',
'type' => 'field_permission_example_fieldnote',
])
->save();
$type_manager
->getStorage('field_config')
->create([
'entity_type' => 'entity_test',
'field_name' => 'field_fieldnote',
'bundle' => 'entity_test',
])
->save();
$type_manager
->getStorage('entity_form_display')
->create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('field_fieldnote', [
'type' => 'field_permission_example_widget',
])
->save();
$type_manager
->getStorage('field_storage_config')
->create([
'field_name' => 'user_fieldnote',
'entity_type' => 'user',
'type' => 'field_permission_example_fieldnote',
])
->save();
$type_manager
->getStorage('field_config')
->create([
'entity_type' => 'user',
'field_name' => 'user_fieldnote',
'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_fieldnote', [
'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_fieldnote = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = $type_manager
->getStorage('entity_test')
->load($id);
$this
->assertTrue($entity->field_fieldnote instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_fieldnote[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_fieldnote->value, $value);
$this
->assertEqual($entity->field_fieldnote[0]->value, $value);
$new_value = $this
->randomMachineName();
$entity->field_fieldnote->value = $new_value;
$this
->assertEqual($entity->field_fieldnote->value, $new_value);
$entity
->save();
$entity = $type_manager
->getStorage('entity_test')
->load($id);
$this
->assertEqual($entity->field_fieldnote->value, $new_value);
$entity = $type_manager
->getStorage('entity_test')
->create([]);
$entity->field_fieldnote
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
public function testFieldNoteAccess() {
$scenarios = [
'admin_type' => [
'perms' => [
'administer the fieldnote 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 fieldnote',
],
'can_view_any' => TRUE,
'can_edit_any' => FALSE,
'can_view_own' => FALSE,
'can_edit_own' => FALSE,
],
'edit_any' => [
'perms' => [
'view test entity',
'view any fieldnote',
'edit any fieldnote',
],
'can_view_any' => TRUE,
'can_edit_any' => TRUE,
'can_view_own' => FALSE,
'can_edit_own' => FALSE,
],
'view_own' => [
'perms' => [
'view test entity',
'view own fieldnote',
],
'can_view_any' => FALSE,
'can_edit_any' => FALSE,
'can_view_own' => TRUE,
'can_edit_own' => FALSE,
],
'edit_own' => [
'perms' => [
'view test entity',
'view own fieldnote',
'edit own fieldnote',
],
'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_fieldnote = $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_fieldnote = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
foreach ([
'can_view_any',
'can_edit_any',
] as $op) {
$this
->doAccessAssertion($entity, 'field_fieldnote', $test_user, $name, $op, $scenario[$op]);
$this
->doAccessAssertion($arbitrary_user, 'user_fieldnote', $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_fieldnote = $value;
$test_user
->save();
foreach ([
'can_view_own',
'can_edit_own',
] as $op) {
$this
->doAccessAssertion($entity, 'field_fieldnote', $test_user, $name, $op, $scenario[$op]);
$this
->doAccessAssertion($test_user, 'user_fieldnote', $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);
}
}
}