5 * Contains \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest.
8 namespace Drupal\Tests\field\Unit;
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
11 use Drupal\Core\Entity\EntityManager;
12 use Drupal\Core\Entity\EntityTypeManagerInterface;
13 use Drupal\Core\Field\FieldException;
14 use Drupal\Core\Field\FieldStorageDefinitionInterface;
15 use Drupal\Core\Field\FieldTypePluginManagerInterface;
16 use Drupal\field\Entity\FieldStorageConfig;
17 use Drupal\Tests\UnitTestCase;
20 * @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
24 class FieldStorageConfigEntityUnitTest extends UnitTestCase {
27 * The entity type manager used for testing.
29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
31 protected $entityTypeManager;
34 * The ID of the type of the entity under test.
38 protected $entityTypeId;
41 * The UUID generator used for testing.
43 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
48 * The field type manager.
50 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
52 protected $fieldTypeManager;
57 protected function setUp() {
58 $entity_manager = new EntityManager();
59 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
60 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
61 $this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
63 $container = new ContainerBuilder();
64 $container->set('entity.manager', $entity_manager);
65 $container->set('entity_type.manager', $this->entityTypeManager);
66 $container->set('uuid', $this->uuid);
67 $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
68 // Inject the container into entity.manager so it can defer to
69 // entity_type.manager.
70 $entity_manager->setContainer($container);
71 \Drupal::setContainer($container);
75 * @covers ::calculateDependencies
77 public function testCalculateDependencies() {
78 // Create a mock entity type for FieldStorageConfig.
79 $fieldStorageConfigentityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
80 $fieldStorageConfigentityType->expects($this->any())
81 ->method('getProvider')
82 ->will($this->returnValue('field'));
84 // Create a mock entity type to attach the field to.
85 $attached_entity_type_id = $this->randomMachineName();
86 $attached_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
87 $attached_entity_type->expects($this->any())
88 ->method('getProvider')
89 ->will($this->returnValue('entity_provider_module'));
91 // Get definition is called three times. Twice in
92 // ConfigEntityBase::addDependency() to get the provider of the field config
93 // entity type and once in FieldStorageConfig::calculateDependencies() to
94 // get the provider of the entity type that field is attached to.
95 $this->entityTypeManager->expects($this->any())
96 ->method('getDefinition')
98 ['field_storage_config', TRUE, $fieldStorageConfigentityType],
99 [$attached_entity_type_id, TRUE, $attached_entity_type],
102 $this->fieldTypeManager->expects($this->atLeastOnce())
103 ->method('getDefinition')
104 ->with('test_field_type', FALSE)
106 'class' => TestFieldType::class,
109 $field_storage = new FieldStorageConfig([
110 'entity_type' => $attached_entity_type_id,
111 'field_name' => 'test_field',
112 'type' => 'test_field_type',
113 'module' => 'test_module',
116 $dependencies = $field_storage->calculateDependencies()->getDependencies();
117 $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']);
118 $this->assertEquals(['stark'], $dependencies['theme']);
122 * Tests stored cardinality.
124 * @covers ::getCardinality
126 public function testStoredCardinality() {
127 $this->fieldTypeManager->expects($this->any())
128 ->method('getDefinition')
129 ->with('test_field_type')
131 'class' => TestFieldType::class,
132 // The field type definition has no enforced cardinality.
133 'cardinality' => NULL,
136 $field_storage = new FieldStorageConfig([
137 'entity_type' => 'entity_test',
138 'field_name' => 'test_field',
139 'type' => 'test_field_type',
140 'module' => 'test_module',
142 $field_storage->setCardinality(8);
144 // Check that the stored cardinality is returned.
145 $this->assertEquals(8, $field_storage->getCardinality());
149 * Tests enforced cardinality.
151 * @covers ::getCardinality
153 public function testEnforcedCardinality() {
154 $this->fieldTypeManager->expects($this->any())
155 ->method('getDefinition')
156 ->with('test_field_type')
158 'class' => TestFieldType::class,
159 // This field type defines an enforced cardinality.
163 $field_storage = new FieldStorageConfig([
164 'entity_type' => 'entity_test',
165 'field_name' => 'test_field',
166 'type' => 'test_field_type',
167 'module' => 'test_module',
169 // Custom cardinality tentative.
170 $field_storage->setCardinality(8);
172 // Check that the enforced cardinality is returned.
173 $this->assertEquals(21, $field_storage->getCardinality());
177 * Tests invalid enforced cardinality.
179 * @covers ::getCardinality
180 * @dataProvider providerInvalidEnforcedCardinality
182 * @param mixed $enforced_cardinality
183 * Enforced cardinality
185 public function testInvalidEnforcedCardinality($enforced_cardinality) {
186 $this->fieldTypeManager->expects($this->any())
187 ->method('getDefinition')
188 ->with('test_field_type')
190 'class' => TestFieldType::class,
191 'cardinality' => $enforced_cardinality,
194 $field_storage = new FieldStorageConfig([
195 'entity_type' => 'entity_test',
196 'field_name' => 'test_field',
197 'type' => 'test_field_type',
198 'module' => 'test_module',
201 $this->setExpectedException(FieldException::class, "Invalid enforced cardinality '$enforced_cardinality'. Allowed values: a positive integer or -1.");
202 $field_storage->getCardinality();
206 * Data provider for ::testInvalidEnforcedCardinality()
211 public function providerInvalidEnforcedCardinality() {
214 'negative_other_than_-1' => [-70],
215 'non_numeric' => ['abc%$#@!'],
222 * A test class to test field storage dependencies.
224 * @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
226 class TestFieldType {
231 public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
233 $dependencies['module'] = ['entity_test'];
234 $dependencies['theme'] = ['stark'];
236 return $dependencies;