You are here

function FormTest::testFieldFormAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testFieldFormAccess()

Tests fields with no 'edit' access.

File

core/modules/field/src/Tests/FormTest.php, line 508
Contains \Drupal\field\Tests\FormTest.

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testFieldFormAccess() {
  $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;
  entity_create('field_storage_config', $field_storage)
    ->save();
  entity_create('field_config', $field)
    ->save();
  entity_get_form_display($entity_type, $entity_type, 'default')
    ->setComponent($field_name)
    ->save();

  // Create a field with no edit access. See
  // field_test_entity_field_access().
  $field_storage_no_access = array(
    '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 = array(
    'field_name' => $field_name_no_access,
    'entity_type' => $entity_type,
    'bundle' => $entity_type,
    'default_value' => array(
      0 => array(
        'value' => 99,
      ),
    ),
  );
  entity_create('field_storage_config', $field_storage_no_access)
    ->save();
  entity_create('field_config', $field_no_access)
    ->save();
  entity_get_form_display($field_no_access['entity_type'], $field_no_access['bundle'], 'default')
    ->setComponent($field_name_no_access)
    ->save();

  // Test that the form structure includes full information for each delta
  // apart from #access.
  $entity = entity_create($entity_type, array(
    'id' => 0,
    'revision_id' => 0,
  ));
  $display = entity_get_form_display($entity_type, $entity_type, 'default');
  $form = array();
  $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 = array(
    "{$field_name}[0][value]" => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match("|{$entity_type}/manage/(\\d+)|", $this->url, $match);
  $id = $match[1];

  // Check that the default value was saved.
  $entity = entity_load($entity_type, $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 = array(
    "{$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.
  $this->container
    ->get('entity.manager')
    ->getStorage($entity_type)
    ->resetCache(array(
    $id,
  ));
  $entity = entity_load($entity_type, $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 = entity_revision_load($entity_type, $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.');
}