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