13ba3cf93daab6f32ab4af21d70ca6291cd16a7e
[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\FieldStorageDefinitionInterface;
12 use Drupal\Core\Field\FieldTypePluginManagerInterface;
13 use Drupal\field\Entity\FieldStorageConfig;
14 use Drupal\Tests\UnitTestCase;
15
16 /**
17  * @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
18  *
19  * @group field
20  */
21 class FieldStorageConfigEntityUnitTest extends UnitTestCase {
22
23   /**
24    * The entity manager used for testing.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $entityManager;
29
30   /**
31    * The ID of the type of the entity under test.
32    *
33    * @var string
34    */
35   protected $entityTypeId;
36
37   /**
38    * The UUID generator used for testing.
39    *
40    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
41    */
42   protected $uuid;
43
44   /**
45    * The field type manager.
46    *
47    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48    */
49   protected $fieldTypeManager;
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function setUp() {
55     $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
56     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
57     $this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
58
59     $container = new ContainerBuilder();
60     $container->set('entity.manager', $this->entityManager);
61     $container->set('uuid', $this->uuid);
62     $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
63     \Drupal::setContainer($container);
64   }
65
66   /**
67    * @covers ::calculateDependencies
68    */
69   public function testCalculateDependencies() {
70     // Create a mock entity type for FieldStorageConfig.
71     $fieldStorageConfigentityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
72     $fieldStorageConfigentityType->expects($this->any())
73       ->method('getProvider')
74       ->will($this->returnValue('field'));
75
76     // Create a mock entity type to attach the field to.
77     $attached_entity_type_id = $this->randomMachineName();
78     $attached_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
79     $attached_entity_type->expects($this->any())
80       ->method('getProvider')
81       ->will($this->returnValue('entity_provider_module'));
82
83     // Get definition is called three times. Twice in
84     // ConfigEntityBase::addDependency() to get the provider of the field config
85     // entity type and once in FieldStorageConfig::calculateDependencies() to
86     // get the provider of the entity type that field is attached to.
87     $this->entityManager->expects($this->any())
88       ->method('getDefinition')
89       ->willReturnMap([
90         ['field_storage_config', TRUE, $fieldStorageConfigentityType],
91         [$attached_entity_type_id, TRUE, $attached_entity_type],
92       ]);
93
94     $this->fieldTypeManager->expects($this->atLeastOnce())
95       ->method('getDefinition')
96       ->with('test_field_type', FALSE)
97       ->willReturn([
98         'class' => TestFieldType::class,
99       ]);
100
101     $field_storage = new FieldStorageConfig([
102       'entity_type' => $attached_entity_type_id,
103       'field_name' => 'test_field',
104       'type' => 'test_field_type',
105       'module' => 'test_module',
106     ]);
107
108     $dependencies = $field_storage->calculateDependencies()->getDependencies();
109     $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']);
110     $this->assertEquals(['stark'], $dependencies['theme']);
111   }
112
113 }
114
115 /**
116  * A test class to test field storage dependencies.
117  *
118  * @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
119  */
120 class TestFieldType {
121
122   /**
123    * {@inheritdoc}
124    */
125   public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
126     $dependencies = [];
127     $dependencies['module'] = ['entity_test'];
128     $dependencies['theme'] = ['stark'];
129
130     return $dependencies;
131   }
132
133 }