class FieldStorageConfigEntityUnitTest

Same name in other branches
  1. 9 core/modules/field/tests/src/Unit/FieldStorageConfigEntityUnitTest.php \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest
  2. 8.9.x core/modules/field/tests/src/Unit/FieldStorageConfigEntityUnitTest.php \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest
  3. 10 core/modules/field/tests/src/Unit/FieldStorageConfigEntityUnitTest.php \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest

@coversDefaultClass \Drupal\field\Entity\FieldStorageConfig

@group field

Hierarchy

Expanded class hierarchy of FieldStorageConfigEntityUnitTest

File

core/modules/field/tests/src/Unit/FieldStorageConfigEntityUnitTest.php, line 20

Namespace

Drupal\Tests\field\Unit
View source
class FieldStorageConfigEntityUnitTest extends UnitTestCase {
    
    /**
     * The entity type manager used for testing.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The ID of the type of the entity under test.
     *
     * @var string
     */
    protected $entityTypeId;
    
    /**
     * The UUID generator used for testing.
     *
     * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $uuid;
    
    /**
     * The field type manager.
     *
     * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $fieldTypeManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->uuid = $this->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
        $this->fieldTypeManager = $this->createMock(FieldTypePluginManagerInterface::class);
        $container = new ContainerBuilder();
        $container->set('entity_type.manager', $this->entityTypeManager);
        $container->set('uuid', $this->uuid);
        $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
        \Drupal::setContainer($container);
    }
    
    /**
     * @covers ::calculateDependencies
     */
    public function testCalculateDependencies() : void {
        // Create a mock entity type for FieldStorageConfig.
        $fieldStorageConfigentityType = $this->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
        $fieldStorageConfigentityType->expects($this->any())
            ->method('getProvider')
            ->willReturn('field');
        // Create a mock entity type to attach the field to.
        $attached_entity_type_id = $this->randomMachineName();
        $attached_entity_type = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
        $attached_entity_type->expects($this->any())
            ->method('getProvider')
            ->willReturn('entity_provider_module');
        // Get definition is called three times. Twice in
        // ConfigEntityBase::addDependency() to get the provider of the field config
        // entity type and once in FieldStorageConfig::calculateDependencies() to
        // get the provider of the entity type that field is attached to.
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->willReturnMap([
            [
                'field_storage_config',
                TRUE,
                $fieldStorageConfigentityType,
            ],
            [
                $attached_entity_type_id,
                TRUE,
                $attached_entity_type,
            ],
        ]);
        $this->fieldTypeManager
            ->expects($this->atLeastOnce())
            ->method('getDefinition')
            ->with('test_field_type', FALSE)
            ->willReturn([
            'class' => TestFieldType::class,
        ]);
        $field_storage = new FieldStorageConfig([
            'entity_type' => $attached_entity_type_id,
            'field_name' => 'test_field',
            'type' => 'test_field_type',
            'module' => 'test_module',
        ]);
        $dependencies = $field_storage->calculateDependencies()
            ->getDependencies();
        $this->assertEquals([
            'entity_provider_module',
            'entity_test',
            'test_module',
        ], $dependencies['module']);
        $this->assertEquals([
            'stark',
        ], $dependencies['theme']);
    }
    
    /**
     * Tests stored cardinality.
     *
     * @covers ::getCardinality
     */
    public function testStoredCardinality() : void {
        $this->fieldTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->with('test_field_type')
            ->willReturn([
            'class' => TestFieldType::class,
            // The field type definition has no enforced cardinality.
'cardinality' => NULL,
        ]);
        $field_storage = new FieldStorageConfig([
            'entity_type' => 'entity_test',
            'field_name' => 'test_field',
            'type' => 'test_field_type',
            'module' => 'test_module',
        ]);
        $field_storage->setCardinality(8);
        // Check that the stored cardinality is returned.
        $this->assertEquals(8, $field_storage->getCardinality());
    }
    
    /**
     * Tests enforced cardinality.
     *
     * @covers ::getCardinality
     */
    public function testEnforcedCardinality() : void {
        $this->fieldTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->with('test_field_type')
            ->willReturn([
            'class' => TestFieldType::class,
            // This field type defines an enforced cardinality.
'cardinality' => 21,
        ]);
        $field_storage = new FieldStorageConfig([
            'entity_type' => 'entity_test',
            'field_name' => 'test_field',
            'type' => 'test_field_type',
            'module' => 'test_module',
        ]);
        // Custom cardinality tentative.
        $field_storage->setCardinality(8);
        // Check that the enforced cardinality is returned.
        $this->assertEquals(21, $field_storage->getCardinality());
    }
    
    /**
     * Tests invalid enforced cardinality.
     *
     * @param mixed $enforced_cardinality
     *   Enforced cardinality.
     *
     * @covers ::getCardinality
     * @dataProvider providerInvalidEnforcedCardinality
     */
    public function testInvalidEnforcedCardinality($enforced_cardinality) : void {
        $this->fieldTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->with('test_field_type')
            ->willReturn([
            'class' => TestFieldType::class,
            'cardinality' => $enforced_cardinality,
        ]);
        $field_storage = new FieldStorageConfig([
            'entity_type' => 'entity_test',
            'field_name' => 'test_field',
            'type' => 'test_field_type',
            'module' => 'test_module',
        ]);
        $this->expectException(FieldException::class);
        $this->expectExceptionMessage("Invalid enforced cardinality '{$enforced_cardinality}'. Allowed values: a positive integer or -1.");
        $field_storage->getCardinality();
    }
    
    /**
     * Data provider for ::testInvalidEnforcedCardinality()
     *
     * @return array
     *   Test cases.
     */
    public static function providerInvalidEnforcedCardinality() {
        return [
            'zero' => [
                0,
            ],
            'negative_other_than_-1' => [
                -70,
            ],
            'non_numeric' => [
                'abc%$#@!',
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FieldStorageConfigEntityUnitTest::$entityTypeId protected property The ID of the type of the entity under test.
FieldStorageConfigEntityUnitTest::$entityTypeManager protected property The entity type manager used for testing.
FieldStorageConfigEntityUnitTest::$fieldTypeManager protected property The field type manager.
FieldStorageConfigEntityUnitTest::$uuid protected property The UUID generator used for testing.
FieldStorageConfigEntityUnitTest::providerInvalidEnforcedCardinality public static function Data provider for ::testInvalidEnforcedCardinality()
FieldStorageConfigEntityUnitTest::setUp protected function Overrides UnitTestCase::setUp
FieldStorageConfigEntityUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
FieldStorageConfigEntityUnitTest::testEnforcedCardinality public function Tests enforced cardinality.
FieldStorageConfigEntityUnitTest::testInvalidEnforcedCardinality public function Tests invalid enforced cardinality.
FieldStorageConfigEntityUnitTest::testStoredCardinality public function Tests stored cardinality.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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