diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationDefaultLanguageTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationDefaultLanguageTest.php new file mode 100644 index 0000000..6854f46 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationDefaultLanguageTest.php @@ -0,0 +1,155 @@ + 'Entity Translation default language', + 'description' => 'Test that entities are created with correct language code.', + 'group' => 'Entity API', + ); + } + + public function setUp() { + parent::setUp(); + + // Create a new administrator user for the test. + $admin = $this->drupalCreateUser( + array( + 'administer content types', + 'administer languages', + 'bypass node access', + ) + ); + $this->drupalLogin($admin); + + // Activate Spanish language, so there are two languages activated. + $edit = array( + 'predefined_langcode' => 'es', + ); + $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); + + // Create a new content type which has Undefined language by default. + $this->createContentType('ctund', Language::LANGCODE_NOT_SPECIFIED); + // Create a new content type which has Spanish language by default. + $this->createContentType('ctes', 'es'); + } + + /** + * Tests that default language code is properly set for new nodes. + */ + public function testEntityTranslationDefaultLanguageViaCode() { + // With language module activated, and content type that is configured to + // have language "und" by default, a new node of this type will have "und" + // language code when language is not specified. + $node = $this->createNodeViaCode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module activated, and content type that is configured to + // have language "und" by default, a new node of this type will have "es" + // language code when language "es" is specified. + $node = $this->createNodeViaCode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module activated, and content type that is configured to + // have language "es" by default, a new node of this type will have "es" + // language code when language is not specified. + $node = $this->createNodeViaCode('ctes'); + $this->assertEqual($node->langcode->value, 'es'); + // With language module activated, and content type that is configured to + // have language "es" by default, a new node of this type will have "en" + // language code when language "en" is specified. + $node = $this->createNodeViaCode('ctes', 'en'); + $this->assertEqual($node->langcode->value, 'en'); + + // Disable language module. + $this->container->get('module_handler')->uninstall(array('language'), FALSE); + + // With language module disabled, and content type that is configured to + // have language "und" by default, a new node of this type will have "und" + // language code when language is not specified. + $node = $this->createNodeViaCode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module disabled, and content type that is configured to + // have language "und" by default, a new node of this type will have "es" + // language code when language "es" is specified. + $node = $this->createNodeViaCode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module disabled, and content type that is configured to + // have language "es" by default, a new node of this type will have "und" + // language code when language is not specified. + $node = $this->createNodeViaCode('ctes'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module disabled, and content type that is configured to + // have language "es" by default, a new node of this type will have "en" + // language code when language "en" is specified. + $node = $this->createNodeViaCode('ctes', 'en'); + $this->assertEqual($node->langcode->value, 'en'); + } + + /** + * Creates a new node content type. + * + * @param name + * The content type name. + * @param $langcode + * Default language code of the nodes of this type. + */ + protected function createContentType($name, $langcode) { + $edit = array( + 'name' => 'Test ' . $name, + 'title_label' => 'Title', + 'type' => $name, + 'language_configuration[langcode]' => $langcode, + ); + $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type')); + } + + /** + * Creates a new node of given type and language using Entity API. + * + * @param $type + * The node content type. + * @param $langcode + * (optional) Language code of the node to create. + * If blank, it will be determined by the content type configuration. + * + * @return \Drupal\node\Entity + * The node created. + */ + protected function createNodeViaCode($type, $langcode = NULL) { + $data = array( + 'type' => $type, + 'title' => $this->randomName(), + ); + if (!empty($langcode)) { + $data['langcode'] = $langcode; + } + $node = entity_create( + 'node', + $data + ); + return $node; + } + +}