You are here

public function EntityFieldManagerTest::testGetFieldMap in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php \Drupal\Tests\Core\Entity\EntityFieldManagerTest::testGetFieldMap()

@covers ::getFieldMap

File

core/tests/Drupal/Tests/Core/Entity/EntityFieldManagerTest.php, line 630
Contains \Drupal\Tests\Core\Entity\EntityFieldManagerTest.

Class

EntityFieldManagerTest
@coversDefaultClass \Drupal\Core\Entity\EntityFieldManager @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetFieldMap() {
  $this->entityTypeBundleInfo
    ->getBundleInfo('test_entity_type')
    ->willReturn([])
    ->shouldBeCalled();

  // Set up a content entity type.
  $entity_type = $this
    ->prophesize(ContentEntityTypeInterface::class);
  $entity_class = EntityTypeManagerTestEntity::class;

  // Define an ID field definition as a base field.
  $id_definition = $this
    ->prophesize(FieldDefinitionInterface::class);
  $id_definition
    ->getType()
    ->willReturn('integer');
  $base_field_definitions = [
    'id' => $id_definition
      ->reveal(),
  ];
  $entity_class::$baseFieldDefinitions = $base_field_definitions;

  // Set up the stored bundle field map.
  $key_value_store = $this
    ->prophesize(KeyValueStoreInterface::class);
  $this->keyValueFactory
    ->get('entity.definitions.bundle_field_map')
    ->willReturn($key_value_store
    ->reveal());
  $key_value_store
    ->getAll()
    ->willReturn([
    'test_entity_type' => [
      'by_bundle' => [
        'type' => 'string',
        'bundles' => [
          'second_bundle' => 'second_bundle',
        ],
      ],
    ],
  ]);

  // Set up a non-content entity type.
  $non_content_entity_type = $this
    ->prophesize(EntityTypeInterface::class);

  // Mock the base field definition override.
  $override_entity_type = $this
    ->prophesize(EntityTypeInterface::class);
  $this
    ->setUpEntityTypeDefinitions([
    'test_entity_type' => $entity_type,
    'non_fieldable' => $non_content_entity_type,
    'base_field_override' => $override_entity_type,
  ]);
  $entity_type
    ->getClass()
    ->willReturn($entity_class);
  $entity_type
    ->getKeys()
    ->willReturn([
    'default_langcode' => 'default_langcode',
  ]);
  $entity_type
    ->entityClassImplements(FieldableEntityInterface::class)
    ->willReturn(TRUE);
  $entity_type
    ->isTranslatable()
    ->shouldBeCalled();
  $entity_type
    ->isRevisionable()
    ->shouldBeCalled();
  $entity_type
    ->getProvider()
    ->shouldBeCalled();
  $non_content_entity_type
    ->entityClassImplements(FieldableEntityInterface::class)
    ->willReturn(FALSE);
  $override_entity_type
    ->entityClassImplements(FieldableEntityInterface::class)
    ->willReturn(FALSE);

  // Set up the entity type bundle info to return two bundles for the
  // fieldable entity type.
  $this->entityTypeBundleInfo
    ->getBundleInfo('test_entity_type')
    ->willReturn([
    'first_bundle' => 'first_bundle',
    'second_bundle' => 'second_bundle',
  ])
    ->shouldBeCalled();
  $this->moduleHandler
    ->getImplementations('entity_base_field_info')
    ->willReturn([]);
  $expected = [
    'test_entity_type' => [
      'id' => [
        'type' => 'integer',
        'bundles' => [
          'first_bundle' => 'first_bundle',
          'second_bundle' => 'second_bundle',
        ],
      ],
      'by_bundle' => [
        'type' => 'string',
        'bundles' => [
          'second_bundle' => 'second_bundle',
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected, $this->entityFieldManager
    ->getFieldMap());
}