View source
<?php
class TMGMTNodeSourceTestCase extends TMGMTEntityTestCaseUtility {
static function getInfo() {
return array(
'name' => 'Node Source tests',
'description' => 'Exporting source data from nodes and saving translations back to nodes',
'group' => 'Translation Management',
);
}
function setUp() {
parent::setUp(array(
'tmgmt_node',
'translation',
));
$this
->loginAsAdmin();
$this
->setEnvironment('de');
$this
->createNodeType('page', 'Basic page', TRANSLATION_ENABLED, FALSE);
$this
->attachFields('node', 'page', array(
TRUE,
TRUE,
FALSE,
FALSE,
));
}
function testNodeSource() {
$job = $this
->createJob();
$job->translator = $this->default_translator->name;
$job->settings = array();
$job
->save();
for ($i = 0; $i < 2; $i++) {
$node = $this
->createNode('page');
$item = $job
->addItem('node', 'node', $node->nid);
$this
->assertEqual('Basic page', $item
->getSourceType());
}
$job
->requestTranslation();
foreach ($job
->getItems() as $item) {
$this
->assertJobItemLangCodes($item, 'en', array(
'en',
));
$item
->acceptTranslation();
$node = node_load($item->item_id);
$this
->assertTrue($node->tnid > 0, 'The source node is part of a translation set.');
$cached_translations =& drupal_static('translation_node_get_translations', array());
unset($cached_translations[$node->tnid]);
$translations = translation_node_get_translations($node->tnid);
$this
->assertNotNull($translations['de'], 'Translation found for the source node.');
if (isset($translations['de'])) {
$tnode = node_load($translations['de']->nid, NULL, TRUE);
$this
->checkTranslatedData($tnode, $item
->getData(), 'de');
}
$this
->assertJobItemLangCodes($item, 'en', array(
'de',
'en',
));
}
}
function testRequestDataForSpecificLanguage() {
$this
->setEnvironment('sk');
$this
->setEnvironment('es');
$content_type = $this
->drupalCreateContentType();
$node = $this
->drupalCreateNode(array(
'title' => $this
->randomName(),
'language' => 'sk',
'body' => array(
'sk' => array(
array(),
),
),
'type' => $content_type->type,
));
$this
->drupalCreateNode(array(
'title' => 'en translation',
'language' => 'en',
'tnid' => $node->nid,
'body' => array(
'en' => array(
array(),
),
),
'type' => $content_type->type,
));
$job = $this
->createJob('en', 'de');
$job
->save();
$job
->addItem('node', 'node', $node->nid);
$data = $job
->getData();
$this
->assertEqual($data[1]['node_title']['#text'], 'en translation');
$job = $this
->createJob('es', 'cs');
$job
->save();
try {
$job
->addItem('node', 'node', $node->nid);
$this
->fail('The job item should not be added as there is no translation for language "es"');
} catch (TMGMTException $e) {
$languages = language_list();
$this
->assertEqual(t('Unable to load %language translation for the node %title', array(
'%language' => $languages['es']->name,
'%title' => $node->title,
)), $e
->getMessage());
}
}
function checkTranslatedData($node, $data, $langcode) {
foreach (element_children($data) as $field_name) {
if ($field_name == 'node_title') {
$this
->assertEqual($node->title, $data['node_title']['#translation']['#text'], 'The title of the translated node matches the translated data.');
continue;
}
foreach (element_children($data[$field_name]) as $delta) {
$field_langcode = field_is_translatable('node', field_info_field($field_name)) ? $langcode : LANGUAGE_NONE;
foreach (element_children($data[$field_name][$delta]) as $column) {
$column_value = $data[$field_name][$delta][$column];
if (!isset($column_value['#translate']) || $column_value['#translate']) {
$this
->assertEqual($node->{$field_name}[$field_langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The translatable field %field:%delta has been populated with the proper translated data.', array(
'%field' => $field_name,
'delta' => $delta,
)));
}
}
}
}
}
}