public function EntityDisplayTest::testEntityDisplayCRUD

Tests basic CRUD operations on EntityDisplay objects.

File

drupal/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php, line 35
Contains \Drupal\entity\Tests\EntityDisplayTest.

Class

EntityDisplayTest
Tests the EntityDisplay configuration entities.

Namespace

Drupal\entity\Tests

Code

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

  // Check that providing no 'weight' results in the highest current weight
  // being assigned.
  $expected['component_1'] = array(
    'weight' => 0,
  );
  $expected['component_2'] = array(
    'weight' => 1,
  );
  $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,
    'foo' => 'bar',
  );
  $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_display', $display
    ->id());
  foreach (array(
    'component_1',
    'component_2',
    'component_3',
  ) as $name) {
    $this
      ->assertEqual($display
      ->getComponent($name), $expected[$name]);
  }

  // Check that getComponents() returns options for all components.
  $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_display', $display
    ->id());
  $this
    ->assertNULL($display
    ->getComponent('component_3'));

  // Check that CreateCopy() creates a new component that can be correclty
  // saved.
  $new_display = $display
    ->createCopy('other_view_mode');
  $new_display
    ->save();
  $new_display = entity_load('entity_display', $new_display
    ->id());
  $this
    ->assertEqual($new_display->targetEntityType, $display->targetEntityType);
  $this
    ->assertEqual($new_display->bundle, $display->bundle);
  $this
    ->assertEqual($new_display->mode, 'other_view_mode');
  $this
    ->assertEqual($new_display
    ->getComponents(), $display
    ->getComponents());
}