function EntityDisplayTest::testEntityDisplayCRUD

Same name in other branches
  1. 9 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 53

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->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'] = [
        'weight' => 10,
        'third_party_settings' => [
            'field_test' => [
                'foo' => 'bar',
            ],
        ],
        'settings' => [],
    ];
    $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 = EntityViewDisplay::load($display->id());
    foreach ([
        'component_1',
        'component_2',
        'component_3',
    ] as $name) {
        $expected[$name]['region'] = 'content';
        $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'] = [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
        'settings' => [
            'link_to_entity' => FALSE,
        ],
        'third_party_settings' => [],
        'region' => 'content',
    ];
    $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 = 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->assertEqual([
        'config' => [
            'core.entity_view_mode.entity_test.other_view_mode',
        ],
        'module' => [
            '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());
}

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