3 namespace Drupal\Tests\language\Kernel;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Entity\ContentLanguageSettings;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests default language code is properly generated for entities.
14 class EntityDefaultLanguageTest extends KernelTestBase {
21 public static $modules = ['language', 'node', 'field', 'text', 'user', 'system'];
26 protected function setUp() {
29 $this->installEntitySchema('user');
31 // Activate Spanish language, so there are two languages activated.
32 $language = $this->container->get('entity.manager')->getStorage('configurable_language')->create([
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');
44 * Tests that default language code is properly set for new nodes.
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');
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');
69 // Disable language module.
70 $this->disableModules(['language']);
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');
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');
96 * Creates a new node content type.
99 * The content type name.
100 * @param string $langcode
101 * Default language code of the nodes of this type.
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',
108 'create_body' => FALSE,
110 $content_type->save();
111 ContentLanguageSettings::loadByEntityTypeBundle('node', $name)
112 ->setLanguageAlterable(FALSE)
113 ->setDefaultLangcode($langcode)
119 * Creates a new node of given type and language using Entity API.
121 * @param string $type
122 * The node content type.
123 * @param string $langcode
124 * (optional) Language code to pass to entity create.
126 * @return \Drupal\node\NodeInterface
129 protected function createNode($type, $langcode = NULL) {
132 'title' => $this->randomString(),
134 if (!empty($langcode)) {
135 $values['langcode'] = $langcode;
137 $node = $this->container->get('entity.manager')->getStorage('node')->create($values);