diff --git a/tests/title.test b/tests/title.test index e66d095..7078514 100644 --- a/tests/title.test +++ b/tests/title.test @@ -336,7 +336,9 @@ class TitleTranslationTestCase extends DrupalWebTestCase { 'created' => REQUEST_TIME, 'changed' => REQUEST_TIME, ); - entity_translation_get_handler('taxonomy_term', $term)->setTranslation($translation); + $handler = entity_translation_get_handler('taxonomy_term', $term); + $handler->setFormLanguage($translation_langcode); + $handler->setTranslation($translation); taxonomy_term_save($term); $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.'); $term = $this->termLoad($term->tid, $translation_langcode); @@ -344,14 +346,14 @@ class TitleTranslationTestCase extends DrupalWebTestCase { $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.'); // Delete the translation. - entity_translation_get_handler('taxonomy_term', $term)->removeTranslation($translation_langcode); + $handler->removeTranslation($translation_langcode); taxonomy_term_save($term); $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.'); $term = $this->termLoad($term->tid, $langcode); $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field translations correctly deleted.'); // Make the term language neutral. - entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage(LANGUAGE_NONE); + $handler->setOriginalLanguage(LANGUAGE_NONE); foreach ($original_values as $name => $value) { $field_name = $name . '_field'; $term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode]; @@ -363,7 +365,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase { $this->assertTrue($this->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.'); // Change the term language to the former translation language. - entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($translation_langcode); + $handler->setOriginalLanguage($translation_langcode); foreach ($original_values as $name => $value) { $field_name = $name . '_field'; $term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE]; @@ -391,13 +393,14 @@ class TitleTranslationTestCase extends DrupalWebTestCase { * Tests taxonomy form translation workflow. */ public function testFormTranslationWorkflow() { + $languages = language_list(); + $langcode = 'en'; // Create a taxonomy term and check that legacy fields are properly // populated. $original_values = array( - 'name' => $this->randomName(), - 'description' => $this->randomName(), + 'name' => $langcode . '-' . $this->randomName(), + 'description' => $langcode . '-' . $this->randomName(), ); - $langcode = 'en'; $edit = $this->editValues($original_values, $langcode); $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save')); $term = current(entity_load('taxonomy_term', FALSE, array('name' => $original_values['name']), TRUE)); @@ -405,13 +408,14 @@ class TitleTranslationTestCase extends DrupalWebTestCase { // Translate the taxonomy term and check that both the original values and // the translations were correctly stored. + $translation_langcode = 'it'; $translated_values = array( - 'name' => $this->randomName(), - 'description' => $this->randomName(), + 'name' => $translation_langcode . '-' . $this->randomName(), + 'description' => $translation_langcode . '-' . $this->randomName(), ); - $translation_langcode = 'it'; $edit = $this->editValues($translated_values, 'it'); - $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save')); + $options_translated = array('language' => $languages[$translation_langcode]); + $this->drupalPost('/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'), $options_translated); $term = $this->termLoad($term->tid); $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.')); $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.')); @@ -420,14 +424,21 @@ class TitleTranslationTestCase extends DrupalWebTestCase { $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.')); $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.')); + // Check that legacy fields are properly synchronized on entity load. + $term = $this->termLoad($term->tid, $translation_langcode); + foreach ($translated_values as $name => $value) { + $this->assertEqual($term->{$name}, $value, t('Legacy field "@field" is properly synchronized when term is loaded in translation language.', array('@field' => $name))); + } + // Updated the taxonomy term translation and check that both the original // values and the translations were correctly stored. $translated_values = array( - 'name' => $this->randomName(), - 'description' => $this->randomName(), + 'name' => $translation_langcode . '-' . $this->randomName(), + 'description' => $translation_langcode . '-' . $this->randomName(), ); $edit = $this->editValues($translated_values, $translation_langcode); - $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save')); + $this->drupalPost('/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'), $options_translated); + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($langcode); $term = $this->termLoad($term->tid); $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.')); $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.')); @@ -435,6 +446,19 @@ class TitleTranslationTestCase extends DrupalWebTestCase { // Check that legacy fields have the correct values. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.')); $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.')); + + // Additionally, we test that everything works even if interface/content + // language is different from the translation language, for example, at + // en/taxonomy/term/123/edit/it path. + $options_original = array('language' => $languages[$langcode]); + $this->drupalPost('taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'), $options_original); + entity_translation_get_handler('taxonomy_term', $term)->setFormLanguage($langcode); + $term = $this->termLoad($term->tid); + $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.')); + $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.')); + // Check that legacy fields have the correct values. + $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.')); + $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.')); } /** @@ -450,7 +474,13 @@ class TitleTranslationTestCase extends DrupalWebTestCase { */ protected function termLoad($tid, $langcode = NULL) { drupal_static_reset(); - title_active_language($langcode); + if (!is_null($langcode) && module_invoke('entity_translation', 'enabled', 'taxonomy_term')) { + $entity = taxonomy_term_load($tid); + entity_translation_get_handler('taxonomy_term', $entity)->setFormLanguage($langcode); + // Clear the sync cache since we just loaded the entity with a wrong + // active language. + title_entity_sync_static_reset('taxonomy_term', $entity); + } return current(entity_load('taxonomy_term', array($tid), array(), TRUE)); } diff --git a/title.module b/title.module index 74d9c9a..678a8fa 100644 --- a/title.module +++ b/title.module @@ -135,13 +135,14 @@ function title_entity_presave($entity, $entity_type) { $entity_langcode = title_entity_language($entity_type, $entity); $langcode = $entity_langcode; - // If Entity Translation is enabled and the entity type is transltable,we need - // to check if we have a translation for the current active language. If so we - // need to synchronize the legacy field values into the replacing field + // If Entity Translation is enabled and the entity type is translatable, we + // need to check if we have a translation for the current active language. If + // so we need to synchronize the legacy field values into the replacing field // translations in the active language. if (module_invoke('entity_translation', 'enabled', $entity_type)) { - $langcode = title_active_language(); - $translations = entity_translation_get_handler($entity_type, $entity)->getTranslations(); + $handler = entity_translation_get_handler($entity_type, $entity); + $langcode = $handler->getFormLanguage(); + $translations = $handler->getTranslations(); // If we are removing a translation for the active language we need to skip // reverse synchronization, as we would store empty values in the original // replacing fields immediately afterwards. @@ -415,7 +416,12 @@ function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALS list($id, , $bundle) = entity_extract_ids($entity_type, $entity); if (!isset($langcode)) { - $langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language(); + $active_language = $GLOBALS['language_content']->language; + if (module_invoke('entity_translation', 'enabled', $entity_type)) { + $handler = entity_translation_get_handler($entity_type, $entity); + $active_language = $handler->getFormLanguage(); + } + $langcode = $set ? title_entity_language($entity_type, $entity) : $active_language; } // We do not need to perform synchronization more than once. @@ -517,31 +523,6 @@ function title_field_sync_set($entity_type, $entity, $legacy_field, $info, $lang } /** - * Returns and optionally stores the active language. - * - * @param string $langcode - * (optional) The active language to be set. If none is provided the active - * language is just returned. - * - * @return string - * The active language code. Defaults to the current content language. - */ -function title_active_language($langcode = NULL) { - static $drupal_static_fast; - if (!isset($drupal_static_fast)) { - $drupal_static_fast['active_language'] = &drupal_static(__FUNCTION__); - } - $active_langcode = &$drupal_static_fast['active_language']; - if (isset($langcode)) { - $active_langcode = $langcode; - } - if (empty($active_langcode)) { - $active_langcode = $GLOBALS['language_content']->language; - } - return $active_langcode; -} - -/** * Provide the original entity language. * * If a language property is defined for the current entity we synchronize the