comparison core/modules/language/tests/src/Kernel/EntityDefaultLanguageTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\language\Kernel;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Entity\ContentLanguageSettings;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10 * Tests default language code is properly generated for entities.
11 *
12 * @group language
13 */
14 class EntityDefaultLanguageTest extends KernelTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['language', 'node', 'field', 'text', 'user', 'system'];
22
23 /**
24 * {@inheritdoc}
25 */
26 protected function setUp() {
27 parent::setUp();
28
29 $this->installEntitySchema('user');
30
31 // Activate Spanish language, so there are two languages activated.
32 $language = $this->container->get('entity.manager')->getStorage('configurable_language')->create([
33 'id' => 'es',
34 ]);
35 $language->save();
36
37 // Create a new content type which has Undefined language by default.
38 $this->createContentType('ctund', LanguageInterface::LANGCODE_NOT_SPECIFIED);
39 // Create a new content type which has Spanish language by default.
40 $this->createContentType('ctes', 'es');
41 }
42
43 /**
44 * Tests that default language code is properly set for new nodes.
45 */
46 public function testEntityTranslationDefaultLanguageViaCode() {
47 // With language module activated, and a content type that is configured to
48 // have no language by default, a new node of this content type will have
49 // "und" language code when language is not specified.
50 $node = $this->createNode('ctund');
51 $this->assertEqual($node->langcode->value, LanguageInterface::LANGCODE_NOT_SPECIFIED);
52 // With language module activated, and a content type that is configured to
53 // have no language by default, a new node of this content type will have
54 // "es" language code when language is specified as "es".
55 $node = $this->createNode('ctund', 'es');
56 $this->assertEqual($node->langcode->value, 'es');
57
58 // With language module activated, and a content type that is configured to
59 // have language "es" by default, a new node of this content type will have
60 // "es" language code when language is not specified.
61 $node = $this->createNode('ctes');
62 $this->assertEqual($node->langcode->value, 'es');
63 // With language module activated, and a content type that is configured to
64 // have language "es" by default, a new node of this content type will have
65 // "en" language code when language "en" is specified.
66 $node = $this->createNode('ctes', 'en');
67 $this->assertEqual($node->langcode->value, 'en');
68
69 // Disable language module.
70 $this->disableModules(['language']);
71
72 // With language module disabled, and a content type that is configured to
73 // have no language specified by default, a new node of this content type
74 // will have site's default language code when language is not specified.
75 $node = $this->createNode('ctund');
76 $this->assertEqual($node->langcode->value, 'en');
77 // With language module disabled, and a content type that is configured to
78 // have no language specified by default, a new node of this type will have
79 // "es" language code when language "es" is specified.
80 $node = $this->createNode('ctund', 'es');
81 $this->assertEqual($node->langcode->value, 'es');
82
83 // With language module disabled, and a content type that is configured to
84 // have language "es" by default, a new node of this type will have site's
85 // default language code when language is not specified.
86 $node = $this->createNode('ctes');
87 $this->assertEqual($node->langcode->value, 'en');
88 // With language module disabled, and a content type that is configured to
89 // have language "es" by default, a new node of this type will have "en"
90 // language code when language "en" is specified.
91 $node = $this->createNode('ctes', 'en');
92 $this->assertEqual($node->langcode->value, 'en');
93 }
94
95 /**
96 * Creates a new node content type.
97 *
98 * @param string $name
99 * The content type name.
100 * @param string $langcode
101 * Default language code of the nodes of this type.
102 */
103 protected function createContentType($name, $langcode) {
104 $content_type = $this->container->get('entity.manager')->getStorage('node_type')->create([
105 'name' => 'Test ' . $name,
106 'title_label' => 'Title',
107 'type' => $name,
108 'create_body' => FALSE,
109 ]);
110 $content_type->save();
111 ContentLanguageSettings::loadByEntityTypeBundle('node', $name)
112 ->setLanguageAlterable(FALSE)
113 ->setDefaultLangcode($langcode)
114 ->save();
115
116 }
117
118 /**
119 * Creates a new node of given type and language using Entity API.
120 *
121 * @param string $type
122 * The node content type.
123 * @param string $langcode
124 * (optional) Language code to pass to entity create.
125 *
126 * @return \Drupal\node\NodeInterface
127 * The node created.
128 */
129 protected function createNode($type, $langcode = NULL) {
130 $values = [
131 'type' => $type,
132 'title' => $this->randomString(),
133 ];
134 if (!empty($langcode)) {
135 $values['langcode'] = $langcode;
136 }
137 $node = $this->container->get('entity.manager')->getStorage('node')->create($values);
138 return $node;
139 }
140
141 }