5 * Contains \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest.
8 namespace Drupal\Tests\field\Unit;
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
11 use Drupal\Core\Field\FieldException;
12 use Drupal\Core\Field\FieldStorageDefinitionInterface;
13 use Drupal\Core\Field\FieldTypePluginManagerInterface;
14 use Drupal\field\Entity\FieldStorageConfig;
15 use Drupal\Tests\UnitTestCase;
18 * @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
22 class FieldStorageConfigEntityUnitTest extends UnitTestCase {
25 * The entity manager used for testing.
27 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $entityManager;
32 * The ID of the type of the entity under test.
36 protected $entityTypeId;
39 * The UUID generator used for testing.
41 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
46 * The field type manager.
48 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50 protected $fieldTypeManager;
55 protected function setUp() {
56 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
57 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
58 $this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
60 $container = new ContainerBuilder();
61 $container->set('entity.manager', $this->entityManager);
62 $container->set('uuid', $this->uuid);
63 $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
64 \Drupal::setContainer($container);
68 * @covers ::calculateDependencies
70 public function testCalculateDependencies() {
71 // Create a mock entity type for FieldStorageConfig.
72 $fieldStorageConfigentityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
73 $fieldStorageConfigentityType->expects($this->any())
74 ->method('getProvider')
75 ->will($this->returnValue('field'));
77 // Create a mock entity type to attach the field to.
78 $attached_entity_type_id = $this->randomMachineName();
79 $attached_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
80 $attached_entity_type->expects($this->any())
81 ->method('getProvider')
82 ->will($this->returnValue('entity_provider_module'));
84 // Get definition is called three times. Twice in
85 // ConfigEntityBase::addDependency() to get the provider of the field config
86 // entity type and once in FieldStorageConfig::calculateDependencies() to
87 // get the provider of the entity type that field is attached to.
88 $this->entityManager->expects($this->any())
89 ->method('getDefinition')
91 ['field_storage_config', TRUE, $fieldStorageConfigentityType],
92 [$attached_entity_type_id, TRUE, $attached_entity_type],
95 $this->fieldTypeManager->expects($this->atLeastOnce())
96 ->method('getDefinition')
97 ->with('test_field_type', FALSE)
99 'class' => TestFieldType::class,
102 $field_storage = new FieldStorageConfig([
103 'entity_type' => $attached_entity_type_id,
104 'field_name' => 'test_field',
105 'type' => 'test_field_type',
106 'module' => 'test_module',
109 $dependencies = $field_storage->calculateDependencies()->getDependencies();
110 $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']);
111 $this->assertEquals(['stark'], $dependencies['theme']);
115 * Tests stored cardinality.
117 * @covers ::getCardinality
119 public function testStoredCardinality() {
120 $this->fieldTypeManager->expects($this->any())
121 ->method('getDefinition')
122 ->with('test_field_type')
124 'class' => TestFieldType::class,
125 // The field type definition has no enforced cardinality.
126 'cardinality' => NULL,
129 $field_storage = new FieldStorageConfig([
130 'entity_type' => 'entity_test',
131 'field_name' => 'test_field',
132 'type' => 'test_field_type',
133 'module' => 'test_module',
135 $field_storage->setCardinality(8);
137 // Check that the stored cardinality is returned.
138 $this->assertEquals(8, $field_storage->getCardinality());
142 * Tests enforced cardinality.
144 * @covers ::getCardinality
146 public function testEnforcedCardinality() {
147 $this->fieldTypeManager->expects($this->any())
148 ->method('getDefinition')
149 ->with('test_field_type')
151 'class' => TestFieldType::class,
152 // This field type defines an enforced cardinality.
156 $field_storage = new FieldStorageConfig([
157 'entity_type' => 'entity_test',
158 'field_name' => 'test_field',
159 'type' => 'test_field_type',
160 'module' => 'test_module',
162 // Custom cardinality tentative.
163 $field_storage->setCardinality(8);
165 // Check that the enforced cardinality is returned.
166 $this->assertEquals(21, $field_storage->getCardinality());
170 * Tests invalid enforced cardinality.
172 * @covers ::getCardinality
173 * @dataProvider providerInvalidEnforcedCardinality
175 * @param mixed $enforced_cardinality
176 * Enforced cardinality
178 public function testInvalidEnforcedCardinality($enforced_cardinality) {
179 $this->fieldTypeManager->expects($this->any())
180 ->method('getDefinition')
181 ->with('test_field_type')
183 'class' => TestFieldType::class,
184 'cardinality' => $enforced_cardinality,
187 $field_storage = new FieldStorageConfig([
188 'entity_type' => 'entity_test',
189 'field_name' => 'test_field',
190 'type' => 'test_field_type',
191 'module' => 'test_module',
194 $this->setExpectedException(FieldException::class, "Invalid enforced cardinality '$enforced_cardinality'. Allowed values: a positive integer or -1.");
195 $field_storage->getCardinality();
199 * Data provider for ::testInvalidEnforcedCardinality()
204 public function providerInvalidEnforcedCardinality() {
207 'negative_other_than_-1' => [-70],
208 'non_numeric' => ['abc%$#@!'],
215 * A test class to test field storage dependencies.
217 * @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
219 class TestFieldType {
224 public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
226 $dependencies['module'] = ['entity_test'];
227 $dependencies['theme'] = ['stark'];
229 return $dependencies;