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 \Drupal\Core\Entity\EntityFieldManager[[api-linebreak]] @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetFieldDefinitions() : void {
  $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.