You are here

public function EntityReferenceFormatterTest::testAccess in Zircon Profile 8

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

Assert inaccessible items don't change the data of the fields.

File

core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php, line 128
Contains \Drupal\field\Tests\EntityReference\EntityReferenceFormatterTest.

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\field\Tests\EntityReference

Code

public function testAccess() {

  // Revoke the 'view test entity' permission for this test.
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->revokePermission('view test entity')
    ->save();
  $field_name = $this->fieldName;
  $referencing_entity = entity_create($this->entityType, array(
    'name' => $this
      ->randomMachineName(),
  ));
  $referencing_entity
    ->save();
  $referencing_entity->{$field_name}->entity = $this->referencedEntity;

  // Assert user doesn't have access to the entity.
  $this
    ->assertFalse($this->referencedEntity
    ->access('view'), 'Current user does not have access to view the referenced entity.');
  $formatter_manager = $this->container
    ->get('plugin.manager.field.formatter');

  // Get all the existing formatters.
  foreach ($formatter_manager
    ->getOptions('entity_reference') as $formatter => $name) {

    // Set formatter type for the 'full' view mode.
    entity_get_display($this->entityType, $this->bundle, 'default')
      ->setComponent($field_name, array(
      'type' => $formatter,
    ))
      ->save();

    // Invoke entity view.
    entity_view($referencing_entity, 'default');

    // Verify the un-accessible item still exists.
    $this
      ->assertEqual($referencing_entity->{$field_name}->target_id, $this->referencedEntity
      ->id(), format_string('The un-accessible item still exists after @name formatter was executed.', array(
      '@name' => $name,
    )));
  }
}