function EntityFieldManagerTest::testGetFieldDefinitions

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php \Drupal\Tests\Core\Entity\EntityFieldManagerTest::testGetFieldDefinitions()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php \Drupal\Tests\Core\Entity\EntityFieldManagerTest::testGetFieldDefinitions()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php \Drupal\Tests\Core\Entity\EntityFieldManagerTest::testGetFieldDefinitions()

Tests the getFieldDefinitions() method.

@covers ::getFieldDefinitions @covers ::buildBundleFieldDefinitions

File

core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php, line 239

Class

EntityFieldManagerTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityFieldManager.php/class/EntityFieldManager/10" title="Manages the discovery of entity fields." class="local">\Drupal\Core\Entity\EntityFieldManager</a> @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetFieldDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition();
    $bundle_field_definition = $this->prophesize()
        ->willImplement(FieldDefinitionInterface::class)
        ->willImplement(FieldStorageDefinitionInterface::class);
    // Define bundle fields to be stored on the default Entity class.
    $bundle_fields = [
        'the_entity_id' => [
            'test_entity_bundle' => [
                'id_bundle' => $bundle_field_definition->reveal(),
            ],
            'test_entity_bundle_class' => [
                'some_extra_field' => $bundle_field_definition->reveal(),
            ],
        ],
    ];
    // Define bundle fields to be stored on the bundle class.
    $bundle_class_fields = [
        'the_entity_id' => [
            'test_entity_bundle_class' => [
                'id_bundle_class' => $bundle_field_definition->reveal(),
            ],
        ],
    ];
    EntityTypeManagerTestEntity::$bundleFieldDefinitions = $bundle_fields;
    EntityTypeManagerTestEntityBundle::$bundleClassFieldDefinitions = $bundle_class_fields;
    // Test that only base fields are retrieved.
    $expected = [
        'id' => $field_definition,
    ];
    $this->assertSame($expected, $this->entityFieldManager
        ->getFieldDefinitions('test_entity_type', 'some_other_bundle'));
    // Test that base fields and bundle fields from the default entity class are
    // retrieved.
    $expected = [
        'id' => $field_definition,
        'id_bundle' => $bundle_fields['the_entity_id']['test_entity_bundle']['id_bundle'],
    ];
    $this->assertSame($expected, $this->entityFieldManager
        ->getFieldDefinitions('test_entity_type', 'test_entity_bundle'));
    // Test that base fields and bundle fields from the bundle class and
    // entity class are retrieved
    $expected = [
        'id' => $field_definition,
        'some_extra_field' => $bundle_fields['the_entity_id']['test_entity_bundle_class']['some_extra_field'],
        'id_bundle_class' => $bundle_class_fields['the_entity_id']['test_entity_bundle_class']['id_bundle_class'],
    ];
    $this->assertSame($expected, $this->entityFieldManager
        ->getFieldDefinitions('test_entity_type', 'test_entity_bundle_class'));
}

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