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..89193b0 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationDefaultLanguageTest.php @@ -0,0 +1,143 @@ + 'Entity Translation default language', + 'description' => 'Test that entities are created with correct language code.', + 'group' => 'Entity API', + ); + } + + 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. + $this->activateSpanishLanguage(); + + // 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 English language by default. + $this->createContentType('cten', 'en'); + } + + /* + * Tests that default language code is properly set for new nodes. + */ + function testEntityTranslationDefaultLanguageViaCode() { + // With language module activated, content type "und" by default. + $node = $this->createNodeViaCode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + $node = $this->createNodeViaCode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module activated, content type "en" by default. + $node = $this->createNodeViaCode('cten'); + $this->assertEqual($node->langcode->value, 'en'); + $node = $this->createNodeViaCode('cten', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // Disable language module. + $this->container->get('module_handler')->uninstall(array('language'), FALSE); + + // With language module disabled, content type "und" by default. + $node = $this->createNodeViaCode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + $node = $this->createNodeViaCode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module disabled, content type "en" by default. + $node = $this->createNodeViaCode('cten'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + $node = $this->createNodeViaCode('cten', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + } + + /** + * Creates a new node content type. + * + * @param $type + * The node content type. + * @param $langcode + * Default language code of the nodes of this type. + */ + 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 existing content type configuration. + * + * @return \Drupal\node\Entity + * The node created. + */ + function createNodeViaCode($type, $langcode = NULL) { + $title = $this->randomName(); + $data = array( + 'type' => $type, + 'title' => $title, + ); + if (!empty($langcode)) { + $data['langcode'] = $langcode; + } + $node = entity_create( + 'node', + $data + ); + return $node; + } + + /** + * Activates Spanish language. + */ + function activateSpanishLanguage() { + $edit = array( + 'predefined_langcode' => 'es', + ); + $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); + } + +}