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