public function FormTest::testFieldFormAccess in Drupal 9
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormAccess()
Tests fields with no 'edit' access.
File
- core/
modules/ field/ tests/ src/ Functional/ FormTest.php, line 486
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\Tests\field\FunctionalCode
public function testFieldFormAccess() {
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$entity_type = 'entity_test_rev';
// Create a "regular" field.
$field_storage = $this->fieldStorageSingle;
$field_storage['entity_type'] = $entity_type;
$field_name = $field_storage['field_name'];
$field = $this->field;
$field['field_name'] = $field_name;
$field['entity_type'] = $entity_type;
$field['bundle'] = $entity_type;
FieldStorageConfig::create($field_storage)
->save();
FieldConfig::create($field)
->save();
$display_repository
->getFormDisplay($entity_type, $entity_type)
->setComponent($field_name)
->save();
// Create a field with no edit access. See
// field_test_entity_field_access().
$field_storage_no_access = [
'field_name' => 'field_no_edit_access',
'entity_type' => $entity_type,
'type' => 'test_field',
];
$field_name_no_access = $field_storage_no_access['field_name'];
$field_no_access = [
'field_name' => $field_name_no_access,
'entity_type' => $entity_type,
'bundle' => $entity_type,
'default_value' => [
0 => [
'value' => 99,
],
],
];
FieldStorageConfig::create($field_storage_no_access)
->save();
FieldConfig::create($field_no_access)
->save();
$display_repository
->getFormDisplay($field_no_access['entity_type'], $field_no_access['bundle'])
->setComponent($field_name_no_access)
->save();
// Test that the form structure includes full information for each delta
// apart from #access.
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'id' => 0,
'revision_id' => 0,
]);
$display = $display_repository
->getFormDisplay($entity_type, $entity_type);
$form = [];
$form_state = new FormState();
$display
->buildForm($entity, $form, $form_state);
$this
->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');
// Display creation form.
$this
->drupalGet($entity_type . '/add');
// Check that the widget is not displayed if field access is denied.
$this
->assertSession()
->fieldNotExists("{$field_name_no_access}[0][value]");
// Create entity.
$edit = [
"{$field_name}[0][value]" => 1,
];
$this
->submitForm($edit, 'Save');
preg_match("|{$entity_type}/manage/(\\d+)|", $this
->getUrl(), $match);
$id = $match[1];
// Check that the default value was saved.
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->load($id);
$this
->assertEquals(99, $entity->{$field_name_no_access}->value, 'Default value was saved for the field with no edit access.');
$this
->assertEquals(1, $entity->{$field_name}->value, 'Entered value vas saved for the field with edit access.');
// Create a new revision.
$edit = [
"{$field_name}[0][value]" => 2,
'revision' => TRUE,
];
$this
->drupalGet($entity_type . '/manage/' . $id . '/edit');
$this
->submitForm($edit, 'Save');
// Check that the new revision has the expected values.
$storage
->resetCache([
$id,
]);
$entity = $storage
->load($id);
$this
->assertEquals(99, $entity->{$field_name_no_access}->value, 'New revision has the expected value for the field with no edit access.');
$this
->assertEquals(2, $entity->{$field_name}->value, 'New revision has the expected value for the field with edit access.');
// Check that the revision is also saved in the revisions table.
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->loadRevision($entity
->getRevisionId());
$this
->assertEquals(99, $entity->{$field_name_no_access}->value, 'New revision has the expected value for the field with no edit access.');
$this
->assertEquals(2, $entity->{$field_name}->value, 'New revision has the expected value for the field with edit access.');
}