View source
<?php
class TMGMTEntitySourceTestCase extends TMGMTEntityTestCaseUtility {
public $vocabulary;
static function getInfo() {
return array(
'name' => 'Entity Source tests',
'description' => 'Exporting source data from entities and saving translations back to entities.',
'group' => 'Translation Management',
'dependencies' => array(
'entity_translation',
),
);
}
function setUp() {
parent::setUp(array(
'tmgmt_entity',
'taxonomy',
'entity_translation',
));
$this
->loginAsAdmin(array(
'administer entity translation',
));
$this->vocabulary = $this
->createTaxonomyVocab(strtolower($this
->randomName()), $this
->randomName(), array(
FALSE,
TRUE,
TRUE,
TRUE,
));
$edit['entity_translation_entity_types[taxonomy_term]'] = 1;
$this
->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
}
function testEntitySourceNode() {
$this
->setEnvironment('de');
$this
->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
$job = $this
->createJob();
$job->translator = $this->default_translator->name;
$job->settings = array();
$job
->save();
for ($i = 1; $i <= 5; $i++) {
$node = $this
->createNode('article');
$item = $job
->addItem('entity', 'node', $node->nid);
$this
->assertEqual(t('@type (@bundle)', array(
'@type' => t('Node'),
'@bundle' => 'Article',
)), $item
->getSourceType());
}
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$this
->assertTrue($item
->isAccepted());
$entity = entity_load_single($item->item_type, $item->item_id);
$data = $item
->getData();
$this
->checkTranslatedData($entity, $data, 'de');
$this
->checkUntranslatedData($entity, $this->field_names['node']['article'], $data, 'de');
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
}
function testEntitySourceTerm() {
$this
->setEnvironment('de');
$job = $this
->createJob();
$job->translator = $this->default_translator->name;
$job->settings = array();
$job
->save();
$term = NULL;
for ($i = 1; $i <= 5; $i++) {
$term = $this
->createTaxonomyTerm($this->vocabulary);
$item = $job
->addItem('entity', 'taxonomy_term', $term->tid);
$this
->assertEqual(t('@type (@bundle)', array(
'@type' => t('Taxonomy term'),
'@bundle' => $this->vocabulary->name,
)), $item
->getSourceType());
}
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$entity = entity_load_single($item->item_type, $item->item_id);
$data = $item
->getData();
$this
->checkTranslatedData($entity, $data, 'de');
$this
->checkUntranslatedData($entity, $this->field_names['taxonomy_term'][$this->vocabulary->machine_name], $data, 'de');
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
}
function testAddingJobItemsWithEmptySourceText() {
$this
->setEnvironment('de');
$empty_term = new stdClass();
$empty_term->name = $this
->randomName();
$empty_term->description = $this
->randomName();
$empty_term->vid = $this->vocabulary->vid;
taxonomy_term_save($empty_term);
$job = tmgmt_job_create('en', NULL);
try {
$job
->addItem('entity', 'taxonomy_term', $empty_term->tid);
$this
->fail('Job item added with empty source text.');
} catch (TMGMTException $e) {
$this
->assert(empty($job->tjid), 'After adding a job item with empty source text its tjid has to be unset.');
}
$populated_content_term = $this
->createTaxonomyTerm($this->vocabulary);
$job
->addItem('entity', 'taxonomy_term', $populated_content_term->tid);
$this
->assert(!empty($job->tjid), 'After adding another job item with populated source text its tjid must be set.');
}
function testRequestDataForSpecificLanguage() {
$this
->setEnvironment('de');
$this
->setEnvironment('cs');
$this
->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
$job = $this
->createJob('en', 'de');
$job->translator = $this->default_translator->name;
$job->settings = array();
$job
->save();
$node = $this
->createNode('article', 'cs');
$node->body['en'][0]['value'] = 'en translation';
node_save($node);
$job
->addItem('entity', 'node', $node->nid);
$data = $job
->getData();
$this
->assertEqual($data[1]['body'][0]['value']['#text'], 'en translation');
}
function checkTranslatedData($tentity, $data, $langcode) {
foreach (element_children($data) as $field_name) {
foreach (element_children($data[$field_name]) as $delta) {
foreach (element_children($data[$field_name][$delta]) as $column) {
$column_value = $data[$field_name][$delta][$column];
if (!empty($column_value['#translate'])) {
$this
->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The field %field:%delta has been populated with the proper translated data.', array(
'%field' => $field_name,
'delta' => $delta,
)));
}
else {
$this
->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#text'], format_string('The field %field:%delta has been populated with the proper untranslated data.', array(
'%field' => $field_name,
'delta' => $delta,
)));
}
}
}
}
}
function checkUntranslatedData($tentity, $fields, $data, $langcode) {
foreach ($fields as $field_name) {
$field_info = field_info_field($field_name);
if (!$field_info['translatable']) {
if (isset($data[$field_name])) {
$this
->assertNull($data[$field_name]['#translation']['#text'], 'The not translatable field was not translated.');
}
if (isset($tentity->{$field_name}[$langcode])) {
$this
->assertNull($tentity->{$field_name}[$langcode], 'The entity has translated data in a field that is translatable.');
}
}
}
}
}