View source
<?php
class TitleTranslationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Replaced fields translation',
'description' => 'Test replaced field translation.',
'group' => 'Title',
'dependencies' => array(
'entity_translation',
),
);
}
protected $profile = 'testing';
public function setUp(array $modules = array()) {
$modules[] = 'field_test';
$modules[] = 'locale';
$modules[] = 'taxonomy';
$modules[] = 'entity_translation';
$modules[] = 'title';
$modules[] = 'title_test';
parent::setUp($modules);
$permissions = array(
'administer modules',
'view the administration theme',
'administer languages',
'administer taxonomy',
'administer entity translation',
'translate any entity',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$edit = array(
'langcode' => 'it',
);
$this
->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this
->assertTrue(drupal_multilingual(), t('Italian language installed.'));
$name = 'language_content[enabled][locale-url]';
$edit = array(
$name => 1,
);
$this
->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
$this
->assertFieldByName($name, 1, t('URL language negotiation enabled.'));
$name = 'entity_translation_entity_types[taxonomy_term]';
$edit = array(
$name => 1,
);
$this
->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
$this
->assertFieldByName($name, 'taxonomy_term', t('Taxonomy translation enabled.'));
$name = drupal_strtolower($this
->randomName());
$edit = array(
'name' => $this
->randomString(),
'machine_name' => $name,
'entity_translation_taxonomy' => 1,
);
$this
->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
$this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
$this
->assertTrue($this->vocabulary, t('Vocabulary created.'));
$entity_type = 'taxonomy_term';
foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
title_field_replacement_toggle($entity_type, $name, $legacy_field);
$t_args = array(
'%legacy_field' => $legacy_field,
);
$this
->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
}
drupal_static_reset();
}
public function testProgrammaticTranslationWorkflow() {
$langcode = 'it';
$original_values = array(
'name' => $langcode . '_' . $this
->randomName(),
'description' => $langcode . '_' . $this
->randomName(),
);
$term = (object) ($original_values + array(
'format' => 'filtered_html',
'vocabulary_machine_name' => $this->vocabulary->machine_name,
'vid' => $this->vocabulary->vid,
));
entity_translation_get_handler('taxonomy_term', $term)
->setOriginalLanguage($langcode);
taxonomy_term_save($term);
$this
->assertTrue($this
->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this
->termLoad($term->tid);
$this
->assertTrue($this
->checkFieldValues($term, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');
title_entity_sync('taxonomy_term', $term, $langcode);
$translation_langcode = language_default()->language;
$translated_values = array(
'name' => $translation_langcode . '_' . $this
->randomName(),
'description' => $translation_langcode . '_' . $this
->randomName(),
);
foreach ($translated_values as $name => $value) {
$term->{$name} = $value;
}
$translation = array(
'language' => $translation_langcode,
'source' => $langcode,
'uid' => $this->loggedInUser->uid,
'status' => 1,
'translate' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
entity_translation_get_handler('taxonomy_term', $term)
->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);
$this
->assertTrue($this
->checkFieldValues($term, $translated_values, $translation_langcode), 'Replacing field translations correctly created.');
$this
->assertTrue($this
->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');
entity_translation_get_handler('taxonomy_term', $term)
->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.');
entity_translation_get_handler('taxonomy_term', $term)
->setOriginalLanguage(LANGUAGE_NONE);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode];
$term->{$field_name}[$langcode] = array();
}
taxonomy_term_save($term);
$this
->assertTrue($this
->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this
->termLoad($term->tid);
$this
->assertTrue($this
->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');
entity_translation_get_handler('taxonomy_term', $term)
->setOriginalLanguage($translation_langcode);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE];
$term->{$field_name}[LANGUAGE_NONE] = array();
}
taxonomy_term_save($term);
$this
->assertTrue($this
->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this
->termLoad($term->tid, $translation_langcode);
$this
->assertTrue($this
->checkFieldValues($term, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');
$field_name = 'name_field';
$field = field_info_field($field_name);
$field['translatable'] = FALSE;
field_update_field($field);
$original_values['name'] = LANGUAGE_NONE . '_' . $this
->randomName();
$term->name = $original_values['name'];
taxonomy_term_save($term);
$this
->assertTrue($this
->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this
->termLoad($term->tid);
$this
->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['name'], 'Untranslatable replacing field on translatable entity correctly handled.');
}
public function testFormTranslationWorkflow() {
$original_values = array(
'name' => $this
->randomName(),
'description' => $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));
$this
->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));
$translated_values = array(
'name' => $this
->randomName(),
'description' => $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'));
$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.'));
$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.'));
$translated_values = array(
'name' => $this
->randomName(),
'description' => $this
->randomName(),
);
$edit = $this
->editValues($translated_values, $translation_langcode);
$this
->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'));
$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.'));
$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.'));
}
protected function termLoad($tid, $langcode = NULL) {
drupal_static_reset();
title_active_language($langcode);
return current(entity_load('taxonomy_term', array(
$tid,
), array(), TRUE));
}
protected function editValues($values, $langcode) {
$edit = array();
foreach ($values as $name => $value) {
$edit["{$name}_field[{$langcode}][0][value]"] = $value;
}
return $edit;
}
protected function checkFieldValues($term, $values, $langcode, $legacy_match = TRUE) {
foreach ($values as $name => $value) {
$field_value = $term->{$name . '_field'}[$langcode][0]['value'];
if ($field_value != $value || $legacy_match !== ($field_value == $term->{$name})) {
return FALSE;
}
}
return TRUE;
}
protected function checkLegacyValues($term, $values) {
$record = db_query('SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid', array(
':tid' => $term->tid,
))
->fetchAssoc();
foreach ($values as $name => $value) {
if ($record[$name] != $value) {
return FALSE;
}
}
return TRUE;
}
}