You are here

public function FormTest::testFieldFormAccess in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormAccess()
  2. 10 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 477

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

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');
  $this
    ->assertNoFieldByName("{$field_name_no_access}[0][value]", '', 'Widget is not displayed if field access is denied.');

  // Create entity.
  $edit = [
    "{$field_name}[0][value]" => 1,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('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
    ->assertEqual($entity->{$field_name_no_access}->value, 99, 'Default value was saved for the field with no edit access.');
  $this
    ->assertEqual($entity->{$field_name}->value, 1, 'Entered value vas saved for the field with edit access.');

  // Create a new revision.
  $edit = [
    "{$field_name}[0][value]" => 2,
    'revision' => TRUE,
  ];
  $this
    ->drupalPostForm($entity_type . '/manage/' . $id . '/edit', $edit, t('Save'));

  // Check that the new revision has the expected values.
  $storage
    ->resetCache([
    $id,
  ]);
  $entity = $storage
    ->load($id);
  $this
    ->assertEqual($entity->{$field_name_no_access}->value, 99, 'New revision has the expected value for the field with no edit access.');
  $this
    ->assertEqual($entity->{$field_name}->value, 2, '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
    ->assertEqual($entity->{$field_name_no_access}->value, 99, 'New revision has the expected value for the field with no edit access.');
  $this
    ->assertEqual($entity->{$field_name}->value, 2, 'New revision has the expected value for the field with edit access.');
}