Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / field / tests / src / Unit / FieldStorageConfigEntityUnitTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest.
6  */
7
8 namespace Drupal\Tests\field\Unit;
9
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;
18
19 /**
20  * @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
21  *
22  * @group field
23  */
24 class FieldStorageConfigEntityUnitTest extends UnitTestCase {
25
26   /**
27    * The entity type manager used for testing.
28    *
29    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
30    */
31   protected $entityTypeManager;
32
33   /**
34    * The ID of the type of the entity under test.
35    *
36    * @var string
37    */
38   protected $entityTypeId;
39
40   /**
41    * The UUID generator used for testing.
42    *
43    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
44    */
45   protected $uuid;
46
47   /**
48    * The field type manager.
49    *
50    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
51    */
52   protected $fieldTypeManager;
53
54   /**
55    * {@inheritdoc}
56    */
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);
62
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);
72   }
73
74   /**
75    * @covers ::calculateDependencies
76    */
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'));
83
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'));
90
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')
97       ->willReturnMap([
98         ['field_storage_config', TRUE, $fieldStorageConfigentityType],
99         [$attached_entity_type_id, TRUE, $attached_entity_type],
100       ]);
101
102     $this->fieldTypeManager->expects($this->atLeastOnce())
103       ->method('getDefinition')
104       ->with('test_field_type', FALSE)
105       ->willReturn([
106         'class' => TestFieldType::class,
107       ]);
108
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',
114     ]);
115
116     $dependencies = $field_storage->calculateDependencies()->getDependencies();
117     $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']);
118     $this->assertEquals(['stark'], $dependencies['theme']);
119   }
120
121   /**
122    * Tests stored cardinality.
123    *
124    * @covers ::getCardinality
125    */
126   public function testStoredCardinality() {
127     $this->fieldTypeManager->expects($this->any())
128       ->method('getDefinition')
129       ->with('test_field_type')
130       ->willReturn([
131         'class' => TestFieldType::class,
132         // The field type definition has no enforced cardinality.
133         'cardinality' => NULL,
134       ]);
135
136     $field_storage = new FieldStorageConfig([
137       'entity_type' => 'entity_test',
138       'field_name' => 'test_field',
139       'type' => 'test_field_type',
140       'module' => 'test_module',
141     ]);
142     $field_storage->setCardinality(8);
143
144     // Check that the stored cardinality is returned.
145     $this->assertEquals(8, $field_storage->getCardinality());
146   }
147
148   /**
149    * Tests enforced cardinality.
150    *
151    * @covers ::getCardinality
152    */
153   public function testEnforcedCardinality() {
154     $this->fieldTypeManager->expects($this->any())
155       ->method('getDefinition')
156       ->with('test_field_type')
157       ->willReturn([
158         'class' => TestFieldType::class,
159         // This field type defines an enforced cardinality.
160         'cardinality' => 21,
161       ]);
162
163     $field_storage = new FieldStorageConfig([
164       'entity_type' => 'entity_test',
165       'field_name' => 'test_field',
166       'type' => 'test_field_type',
167       'module' => 'test_module',
168     ]);
169     // Custom cardinality tentative.
170     $field_storage->setCardinality(8);
171
172     // Check that the enforced cardinality is returned.
173     $this->assertEquals(21, $field_storage->getCardinality());
174   }
175
176   /**
177    * Tests invalid enforced cardinality.
178    *
179    * @covers ::getCardinality
180    * @dataProvider providerInvalidEnforcedCardinality
181    *
182    * @param mixed $enforced_cardinality
183    *   Enforced cardinality
184    */
185   public function testInvalidEnforcedCardinality($enforced_cardinality) {
186     $this->fieldTypeManager->expects($this->any())
187       ->method('getDefinition')
188       ->with('test_field_type')
189       ->willReturn([
190         'class' => TestFieldType::class,
191         'cardinality' => $enforced_cardinality,
192       ]);
193
194     $field_storage = new FieldStorageConfig([
195       'entity_type' => 'entity_test',
196       'field_name' => 'test_field',
197       'type' => 'test_field_type',
198       'module' => 'test_module',
199     ]);
200
201     $this->setExpectedException(FieldException::class, "Invalid enforced cardinality '$enforced_cardinality'. Allowed values: a positive integer or -1.");
202     $field_storage->getCardinality();
203   }
204
205   /**
206    * Data provider for ::testInvalidEnforcedCardinality()
207    *
208    * @return array
209    *   Test cases.
210    */
211   public function providerInvalidEnforcedCardinality() {
212     return [
213       'zero' => [0],
214       'negative_other_than_-1' => [-70],
215       'non_numeric' => ['abc%$#@!'],
216     ];
217   }
218
219 }
220
221 /**
222  * A test class to test field storage dependencies.
223  *
224  * @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
225  */
226 class TestFieldType {
227
228   /**
229    * {@inheritdoc}
230    */
231   public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
232     $dependencies = [];
233     $dependencies['module'] = ['entity_test'];
234     $dependencies['theme'] = ['stark'];
235
236     return $dependencies;
237   }
238
239 }