diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/LanguageItem.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/LanguageItem.php index 387c103..86b0fad 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/LanguageItem.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/LanguageItem.php @@ -89,7 +89,7 @@ public function setValue($values, $notify = TRUE) { */ public function applyDefaultValue($notify = TRUE) { // Default to LANGCODE_NOT_SPECIFIED. - if (module_exists('language') && $entity = $this->getParent()->getEntity()) { + if (module_exists('language') && $entity = $this->getEntity()) { $langcode = language_get_default_langcode($entity->entityType(), $entity->bundle()); } else { @@ -112,4 +112,5 @@ public function onChange($property_name) { } parent::onChange($property_name); } + } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php b/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php index 05bf993..770b52e 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php @@ -57,7 +57,7 @@ function setUp() { $user->langcode = Language::LANGCODE_NOT_SPECIFIED; $user->save(); - ViewTestData::importTestViews(get_class($this), array('content_translation_test_views')); + ViewTestData::createTestViews(get_class($this), array('content_translation_test_views')); } /** 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')); + } + +}