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