You are here

public function EntityDisplayTest::testEntityDisplayCRUD in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field_ui/src/Tests/EntityDisplayTest.php \Drupal\field_ui\Tests\EntityDisplayTest::testEntityDisplayCRUD()

Tests basic CRUD operations on entity display objects.

File

core/modules/field_ui/src/Tests/EntityDisplayTest.php, line 49
Contains \Drupal\field_ui\Tests\EntityDisplayTest.

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\field_ui\Tests

Code

public function testEntityDisplayCRUD() {
  $display = EntityViewDisplay::create(array(
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ));
  $expected = array();

  // Check that providing no 'weight' results in the highest current weight
  // being assigned. The 'name' field's formatter has weight -5, therefore
  // these follow.
  $expected['component_1'] = array(
    'weight' => -4,
    'settings' => array(),
    'third_party_settings' => array(),
  );
  $expected['component_2'] = array(
    'weight' => -3,
    'settings' => array(),
    'third_party_settings' => array(),
  );
  $display
    ->setComponent('component_1');
  $display
    ->setComponent('component_2');
  $this
    ->assertEqual($display
    ->getComponent('component_1'), $expected['component_1']);
  $this
    ->assertEqual($display
    ->getComponent('component_2'), $expected['component_2']);

  // Check that arbitrary options are correctly stored.
  $expected['component_3'] = array(
    'weight' => 10,
    'third_party_settings' => array(
      'field_test' => array(
        'foo' => 'bar',
      ),
    ),
    'settings' => array(),
  );
  $display
    ->setComponent('component_3', $expected['component_3']);
  $this
    ->assertEqual($display
    ->getComponent('component_3'), $expected['component_3']);

  // Check that the display can be properly saved and read back.
  $display
    ->save();
  $display = entity_load('entity_view_display', $display
    ->id());
  foreach (array(
    'component_1',
    'component_2',
    'component_3',
  ) as $name) {
    $this
      ->assertEqual($display
      ->getComponent($name), $expected[$name]);
  }

  // Ensure that third party settings were added to the config entity.
  // These are added by entity_test_entity_presave() implemented in
  // entity_test module.
  $this
    ->assertEqual('bar', $display
    ->getThirdPartySetting('entity_test', 'foo'), 'Third party settings were added to the entity view display.');

  // Check that getComponents() returns options for all components.
  $expected['name'] = array(
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
    'settings' => array(
      'link_to_entity' => FALSE,
    ),
    'third_party_settings' => array(),
  );
  $this
    ->assertEqual($display
    ->getComponents(), $expected);

  // Check that a component can be removed.
  $display
    ->removeComponent('component_3');
  $this
    ->assertNULL($display
    ->getComponent('component_3'));

  // Check that the removal is correctly persisted.
  $display
    ->save();
  $display = entity_load('entity_view_display', $display
    ->id());
  $this
    ->assertNULL($display
    ->getComponent('component_3'));

  // Check that createCopy() creates a new component that can be correctly
  // saved.
  EntityViewMode::create(array(
    'id' => $display
      ->getTargetEntityTypeId() . '.other_view_mode',
    'targetEntityType' => $display
      ->getTargetEntityTypeId(),
  ))
    ->save();
  $new_display = $display
    ->createCopy('other_view_mode');
  $new_display
    ->save();
  $new_display = entity_load('entity_view_display', $new_display
    ->id());
  $dependencies = $new_display
    ->calculateDependencies()
    ->getDependencies();
  $this
    ->assertEqual(array(
    'config' => array(
      'core.entity_view_mode.entity_test.other_view_mode',
    ),
    'module' => array(
      'entity_test',
    ),
  ), $dependencies);
  $this
    ->assertEqual($new_display
    ->getTargetEntityTypeId(), $display
    ->getTargetEntityTypeId());
  $this
    ->assertEqual($new_display
    ->getTargetBundle(), $display
    ->getTargetBundle());
  $this
    ->assertEqual($new_display
    ->getMode(), 'other_view_mode');
  $this
    ->assertEqual($new_display
    ->getComponents(), $display
    ->getComponents());
}