comparison core/modules/language/tests/src/Unit/ContentLanguageSettingsUnitTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\language\Unit;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\language\Entity\ContentLanguageSettings;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11 * @coversDefaultClass \Drupal\language\Entity\ContentLanguageSettings
12 * @group language
13 */
14 class ContentLanguageSettingsUnitTest extends UnitTestCase {
15
16 /**
17 * The entity type used for testing.
18 *
19 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
20 */
21 protected $entityType;
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 typed configuration manager used for testing.
46 *
47 * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48 */
49 protected $typedConfigManager;
50
51 /**
52 * The typed configuration manager used for testing.
53 *
54 * @var \Drupal\Core\Config\Entity\ConfigEntityStorage|\PHPUnit_Framework_MockObject_MockObject
55 */
56 protected $configEntityStorageInterface;
57
58 /**
59 * {@inheritdoc}
60 */
61 protected function setUp() {
62 $this->entityTypeId = $this->randomMachineName();
63 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
64
65 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
66
67 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
68
69 $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
70
71 $this->configEntityStorageInterface = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
72
73 $container = new ContainerBuilder();
74 $container->set('entity.manager', $this->entityManager);
75 $container->set('uuid', $this->uuid);
76 $container->set('config.typed', $this->typedConfigManager);
77 $container->set('config.storage', $this->configEntityStorageInterface);
78 \Drupal::setContainer($container);
79 }
80
81 /**
82 * @covers ::calculateDependencies
83 */
84 public function testCalculateDependencies() {
85 // Mock the interfaces necessary to create a dependency on a bundle entity.
86 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
87 $target_entity_type->expects($this->any())
88 ->method('getBundleConfigDependency')
89 ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
90
91 $this->entityManager->expects($this->any())
92 ->method('getDefinition')
93 ->with('test_entity_type')
94 ->will($this->returnValue($target_entity_type));
95
96 $config = new ContentLanguageSettings([
97 'target_entity_type_id' => 'test_entity_type',
98 'target_bundle' => 'test_bundle',
99 ], 'language_content_settings');
100 $dependencies = $config->calculateDependencies()->getDependencies();
101 $this->assertContains('test.test_entity_type.id', $dependencies['config']);
102 }
103
104 /**
105 * @covers ::id
106 */
107 public function testId() {
108 $config = new ContentLanguageSettings([
109 'target_entity_type_id' => 'test_entity_type',
110 'target_bundle' => 'test_bundle',
111 ], 'language_content_settings');
112 $this->assertSame('test_entity_type.test_bundle', $config->id());
113 }
114
115 /**
116 * @covers ::getTargetEntityTypeId
117 */
118 public function testTargetEntityTypeId() {
119 $config = new ContentLanguageSettings([
120 'target_entity_type_id' => 'test_entity_type',
121 'target_bundle' => 'test_bundle',
122 ], 'language_content_settings');
123 $this->assertSame('test_entity_type', $config->getTargetEntityTypeId());
124 }
125
126 /**
127 * @covers ::getTargetBundle
128 */
129 public function testTargetBundle() {
130 $config = new ContentLanguageSettings([
131 'target_entity_type_id' => 'test_entity_type',
132 'target_bundle' => 'test_bundle',
133 ], 'language_content_settings');
134 $this->assertSame('test_bundle', $config->getTargetBundle());
135 }
136
137 /**
138 * @covers ::getDefaultLangcode
139 * @covers ::setDefaultLangcode
140 *
141 * @dataProvider providerDefaultLangcode
142 */
143 public function testDefaultLangcode(ContentLanguageSettings $config, $expected) {
144 $this->assertSame($expected, $config->getDefaultLangcode());
145 }
146
147 public function providerDefaultLangcode() {
148 $langcode = $this->randomMachineName();
149 $config = new ContentLanguageSettings([
150 'target_entity_type_id' => 'test_entity_type',
151 'target_bundle' => 'test_bundle',
152 ], 'language_content_settings');
153 $config->setDefaultLangcode($langcode);
154
155 $defaultConfig = new ContentLanguageSettings([
156 'target_entity_type_id' => 'test_entity_type',
157 'target_bundle' => 'test_default_language_bundle',
158 ], 'language_content_settings');
159
160 return [
161 [$config, $langcode],
162 [$defaultConfig, LanguageInterface::LANGCODE_SITE_DEFAULT],
163 ];
164 }
165
166 /**
167 * @covers ::setLanguageAlterable
168 * @covers ::isLanguageAlterable
169 *
170 * @dataProvider providerLanguageAlterable
171 */
172 public function testLanguageAlterable(ContentLanguageSettings $config, $expected) {
173 $this->assertSame($expected, $config->isLanguageAlterable());
174 }
175
176 public function providerLanguageAlterable() {
177 $alterableConfig = new ContentLanguageSettings([
178 'target_entity_type_id' => 'test_entity_type',
179 'target_bundle' => 'test_bundle',
180 ], 'language_content_settings');
181 $alterableConfig->setLanguageAlterable(TRUE);
182
183 $nonAlterableConfig = new ContentLanguageSettings([
184 'target_entity_type_id' => 'test_entity_type',
185 'target_bundle' => 'test_fixed_language_bundle',
186 ], 'language_content_settings');
187 $nonAlterableConfig->setLanguageAlterable(FALSE);
188
189 $defaultConfig = new ContentLanguageSettings([
190 'target_entity_type_id' => 'test_entity_type',
191 'target_bundle' => 'test_default_language_bundle',
192 ], 'language_content_settings');
193
194 return [
195 [$alterableConfig, TRUE],
196 [$nonAlterableConfig, FALSE],
197 [$defaultConfig, FALSE],
198 ];
199 }
200
201 /**
202 * @covers ::isDefaultConfiguration
203 *
204 * @dataProvider providerIsDefaultConfiguration
205 */
206 public function testIsDefaultConfiguration(ContentLanguageSettings $config, $expected) {
207 $this->assertSame($expected, $config->isDefaultConfiguration());
208 }
209
210 public function providerIsDefaultConfiguration() {
211 $alteredLanguage = new ContentLanguageSettings([
212 'target_entity_type_id' => 'test_entity_type',
213 'target_bundle' => 'test_bundle',
214 ], 'language_content_settings');
215 $alteredLanguage->setLanguageAlterable(TRUE);
216
217 $alteredDefaultLangcode = new ContentLanguageSettings([
218 'target_entity_type_id' => 'test_entity_type',
219 'target_bundle' => 'test_fixed_language_bundle',
220 ], 'language_content_settings');
221 $alteredDefaultLangcode->setDefaultLangcode($this->randomMachineName());
222
223 $defaultConfig = new ContentLanguageSettings([
224 'target_entity_type_id' => 'test_entity_type',
225 'target_bundle' => 'test_default_language_bundle',
226 ], 'language_content_settings');
227
228 return [
229 [$alteredLanguage, FALSE],
230 [$alteredDefaultLangcode, FALSE],
231 [$defaultConfig, TRUE],
232 ];
233 }
234
235 /**
236 * @covers ::loadByEntityTypeBundle
237 *
238 * @dataProvider providerLoadByEntityTypeBundle
239 */
240 public function testLoadByEntityTypeBundle($config_id, ContentLanguageSettings $existing_config = NULL, $expected_langcode, $expected_language_alterable) {
241 list($type, $bundle) = explode('.', $config_id);
242
243 $nullConfig = new ContentLanguageSettings([
244 'target_entity_type_id' => $type,
245 'target_bundle' => $bundle,
246 ], 'language_content_settings');
247 $this->configEntityStorageInterface
248 ->expects($this->any())
249 ->method('load')
250 ->with($config_id)
251 ->will($this->returnValue($existing_config));
252 $this->configEntityStorageInterface
253 ->expects($this->any())
254 ->method('create')
255 ->will($this->returnValue($nullConfig));
256
257 $this->entityManager
258 ->expects($this->any())
259 ->method('getStorage')
260 ->with('language_content_settings')
261 ->will($this->returnValue($this->configEntityStorageInterface));
262 $this->entityManager->expects($this->any())
263 ->method('getEntityTypeFromClass')
264 ->with('Drupal\language\Entity\ContentLanguageSettings')
265 ->willReturn('language_content_settings');
266
267 $config = ContentLanguageSettings::loadByEntityTypeBundle($type, $bundle);
268
269 $this->assertSame($expected_langcode, $config->getDefaultLangcode());
270 $this->assertSame($expected_language_alterable, $config->isLanguageAlterable());
271 }
272
273 public function providerLoadByEntityTypeBundle() {
274 $alteredLanguage = new ContentLanguageSettings([
275 'target_entity_type_id' => 'test_entity_type',
276 'target_bundle' => 'test_bundle',
277 ], 'language_content_settings');
278 $alteredLanguage->setLanguageAlterable(TRUE);
279
280 $langcode = $this->randomMachineName();
281 $alteredDefaultLangcode = new ContentLanguageSettings([
282 'target_entity_type_id' => 'test_entity_type',
283 'target_bundle' => 'test_fixed_language_bundle',
284 ], 'language_content_settings');
285 $alteredDefaultLangcode->setDefaultLangcode($langcode);
286
287 $defaultConfig = new ContentLanguageSettings([
288 'target_entity_type_id' => 'test_entity_type',
289 'target_bundle' => 'test_default_language_bundle',
290 ], 'language_content_settings');
291
292 return [
293 ['test_entity_type.test_bundle', $alteredLanguage, LanguageInterface::LANGCODE_SITE_DEFAULT, TRUE],
294 ['test_entity_type.test_fixed_language_bundle', $alteredDefaultLangcode, $langcode, FALSE],
295 ['test_entity_type.test_default_language_bundle', $defaultConfig, LanguageInterface::LANGCODE_SITE_DEFAULT, FALSE],
296 ['test_entity_type.null_bundle', NULL, LanguageInterface::LANGCODE_SITE_DEFAULT, FALSE],
297 ];
298 }
299
300 }