View source
<?php
namespace Drupal\Tests\field\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\UnitTestCase;
class FieldStorageConfigEntityUnitTest extends UnitTestCase {
protected $entityTypeManager;
protected $entityTypeId;
protected $uuid;
protected $fieldTypeManager;
protected function setUp() : void {
$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);
}
public function testCalculateDependencies() {
$fieldStorageConfigentityType = $this
->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$fieldStorageConfigentityType
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('field'));
$attached_entity_type_id = $this
->randomMachineName();
$attached_entity_type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$attached_entity_type
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('entity_provider_module'));
$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']);
}
public function testStoredCardinality() {
$this->fieldTypeManager
->expects($this
->any())
->method('getDefinition')
->with('test_field_type')
->willReturn([
'class' => TestFieldType::class,
'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);
$this
->assertEquals(8, $field_storage
->getCardinality());
}
public function testEnforcedCardinality() {
$this->fieldTypeManager
->expects($this
->any())
->method('getDefinition')
->with('test_field_type')
->willReturn([
'class' => TestFieldType::class,
'cardinality' => 21,
]);
$field_storage = new FieldStorageConfig([
'entity_type' => 'entity_test',
'field_name' => 'test_field',
'type' => 'test_field_type',
'module' => 'test_module',
]);
$field_storage
->setCardinality(8);
$this
->assertEquals(21, $field_storage
->getCardinality());
}
public function testInvalidEnforcedCardinality($enforced_cardinality) {
$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();
}
public function providerInvalidEnforcedCardinality() {
return [
'zero' => [
0,
],
'negative_other_than_-1' => [
-70,
],
'non_numeric' => [
'abc%$#@!',
],
];
}
}
class TestFieldType {
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
$dependencies = [];
$dependencies['module'] = [
'entity_test',
];
$dependencies['theme'] = [
'stark',
];
return $dependencies;
}
}