function EntityDisplayTest::testEntityDisplayCRUD

Same name and namespace in other branches
  1. 8.9.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testEntityDisplayCRUD()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testEntityDisplayCRUD()
  3. 11.x core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testEntityDisplayCRUD()

Tests basic CRUD operations on entity display objects.

File

core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php, line 56

Class

EntityDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testEntityDisplayCRUD() {
  $display = EntityViewDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ]);
  $expected = [];
  // 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'] = [
    'weight' => -4,
    'settings' => [],
    'third_party_settings' => [],
  ];
  $expected['component_2'] = [
    'weight' => -3,
    'settings' => [],
    'third_party_settings' => [],
  ];
  $display->setComponent('component_1');
  $display->setComponent('component_2');
  $this->assertEquals($expected['component_1'], $display->getComponent('component_1'));
  $this->assertEquals($expected['component_2'], $display->getComponent('component_2'));
  // Check that arbitrary options are correctly stored.
  $expected['component_3'] = [
    'weight' => 10,
    'third_party_settings' => [
      'field_test' => [
        'foo' => 'bar',
      ],
    ],
    'settings' => [],
  ];
  $display->setComponent('component_3', $expected['component_3']);
  $this->assertEquals($expected['component_3'], $display->getComponent('component_3'));
  // Check that the display can be properly saved and read back.
  $display->save();
  $display = EntityViewDisplay::load($display->id());
  foreach ([
    'component_1',
    'component_2',
    'component_3',
  ] as $name) {
    $expected[$name]['region'] = 'content';
    $this->assertEquals($expected[$name], $display->getComponent($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->assertEquals('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'] = [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
    'settings' => [
      'link_to_entity' => FALSE,
    ],
    'third_party_settings' => [],
    'region' => 'content',
  ];
  $this->assertEquals($expected, $display->getComponents());
  // 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 = EntityViewDisplay::load($display->id());
  $this->assertNULL($display->getComponent('component_3'));
  // Check that createCopy() creates a new component that can be correctly
  // saved.
  EntityViewMode::create([
    'id' => $display->getTargetEntityTypeId() . '.other_view_mode',
    'targetEntityType' => $display->getTargetEntityTypeId(),
  ])
    ->save();
  $new_display = $display->createCopy('other_view_mode');
  $new_display->save();
  $new_display = EntityViewDisplay::load($new_display->id());
  $dependencies = $new_display->calculateDependencies()
    ->getDependencies();
  $this->assertEquals([
    'config' => [
      'core.entity_view_mode.entity_test.other_view_mode',
    ],
    'module' => [
      'entity_test',
    ],
  ], $dependencies);
  $this->assertEquals($display->getTargetEntityTypeId(), $new_display->getTargetEntityTypeId());
  $this->assertEquals($display->getTargetBundle(), $new_display->getTargetBundle());
  $this->assertEquals('other_view_mode', $new_display->getMode());
  $this->assertEquals($display->getComponents(), $new_display->getComponents());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.