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