function EntityFieldManagerTest::testGetFieldMapByFieldType

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

@covers ::getFieldMapByFieldType

File

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

Class

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

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetFieldMapByFieldType() : void {
    // Set up a content entity type.
    $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
    $entity_class = EntityTypeManagerTestEntity::class;
    // 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
        ->invokeAllWith('entity_base_field_info', Argument::any())
        ->shouldBeCalled();
    // Define an ID field definition as a base field.
    $id_definition = $this->prophesize(FieldDefinitionInterface::class);
    $id_definition->getType()
        ->willReturn('integer')
        ->shouldBeCalled();
    $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())
        ->shouldBeCalled();
    $key_value_store->getAll()
        ->willReturn([
        'test_entity_type' => [
            'by_bundle' => [
                'type' => 'string',
                'bundles' => [
                    'second_bundle' => 'second_bundle',
                ],
            ],
        ],
    ])
        ->shouldBeCalled();
    // Mock the base field definition override.
    $override_entity_type = $this->prophesize(EntityTypeInterface::class);
    $this->setUpEntityTypeDefinitions([
        'test_entity_type' => $entity_type,
        'base_field_override' => $override_entity_type,
    ]);
    $entity_type->getClass()
        ->willReturn($entity_class)
        ->shouldBeCalled();
    $entity_type->getKeys()
        ->willReturn([
        'default_langcode' => 'default_langcode',
    ])
        ->shouldBeCalled();
    $entity_type->entityClassImplements(FieldableEntityInterface::class)
        ->willReturn(TRUE)
        ->shouldBeCalled();
    $entity_type->isTranslatable()
        ->shouldBeCalled();
    $entity_type->isRevisionable()
        ->shouldBeCalled();
    $entity_type->getProvider()
        ->shouldBeCalled();
    $override_entity_type->entityClassImplements(FieldableEntityInterface::class)
        ->willReturn(FALSE)
        ->shouldBeCalled();
    $integerFields = $this->entityFieldManager
        ->getFieldMapByFieldType('integer');
    $this->assertCount(1, $integerFields['test_entity_type']);
    $this->assertArrayNotHasKey('non_fieldable', $integerFields);
    $this->assertArrayHasKey('id', $integerFields['test_entity_type']);
    $this->assertArrayNotHasKey('by_bundle', $integerFields['test_entity_type']);
    $stringFields = $this->entityFieldManager
        ->getFieldMapByFieldType('string');
    $this->assertCount(1, $stringFields['test_entity_type']);
    $this->assertArrayNotHasKey('non_fieldable', $stringFields);
    $this->assertArrayHasKey('by_bundle', $stringFields['test_entity_type']);
    $this->assertArrayNotHasKey('id', $stringFields['test_entity_type']);
}

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