function TMGMTFileTestCase::testXLIFF in Translation Management Tool 7
Tests import and export for the XLIFF format.
File
- translators/
file/ tmgmt_file.test, line 270 - Test cases for the file translator module.
Class
- TMGMTFileTestCase
- Basic tests for the file translator.
Code
function testXLIFF() {
$translator = $this
->createTranslator();
$translator->plugin = 'file';
$translator->settings = array(
'export_format' => 'xlf',
);
$translator
->save();
// Set multiple data items for the source.
variable_set('tmgmt_test_source_data', array(
'dummy' => array(
'deep_nesting' => array(
'#text' => file_get_contents(drupal_get_path('module', 'tmgmt') . '/tests/testing_html/sample.html') . ' @id.',
'#label' => 'Label of deep nested item @id',
),
),
'another_item' => array(
'#text' => 'Text of another item @id.',
'#label' => 'Label of another item @id.',
),
));
$job = $this
->createJob();
$job->translator = $translator->name;
$first_item = $job
->addItem('test_source', 'test', '1');
// Keep the first item data for later use.
$first_item_data = tmgmt_flatten_data($first_item
->getData());
$job
->addItem('test_source', 'test', '2');
$job
->requestTranslation();
$messages = $job
->getMessages();
$message = reset($messages);
$download_url = $message->variables['!link'];
$xliff = file_get_contents($download_url);
$dom = new DOMDocument();
$dom
->loadXML($xliff);
$this
->assertTrue($dom
->schemaValidate(drupal_get_path('module', 'tmgmt_file') . '/xliff-core-1.2-strict.xsd'));
// "Translate" items.
$xml = simplexml_import_dom($dom);
$translated_text = array();
foreach ($xml->file->body
->children() as $group) {
foreach ($group
->children() as $transunit) {
if ($transunit
->getName() == 'trans-unit') {
// The target should be empty.
$this
->assertEqual($transunit->target, '');
$transunit->target = $xml->file['target-language'] . '_' . (string) $transunit->source;
// Store the text to allow assertions later on.
$translated_text[(string) $group['id']][(string) $transunit['id']] = (string) $transunit->target;
}
}
}
// Change the job id to a non-existing one and try to import it.
$wrong_xml = clone $xml;
$wrong_xml->file->header->{'phase-group'}->phase['job-id'] = 500;
$wrong_file = 'public://tmgmt_file/wrong_file.xlf';
$wrong_xml
->asXML($wrong_file);
$uri = $job
->uri();
$edit = array(
'files[file]' => $wrong_file,
);
$this
->drupalPost($uri['path'] . '/manage', $edit, t('Import'));
$this
->assertText(t('Failed to validate file, import aborted.'));
// Change the job id to a wrong one and try to import it.
$wrong_xml = clone $xml;
$second_job = $this
->createJob();
$second_job->translator = $translator->name;
// We need to add the elements count value into settings, otherwise the
// validation will fail on integrity check.
$second_job->settings['xliff_validation'][1] = 0;
$second_job->settings['xliff_validation'][2] = 0;
$second_job
->save();
$wrong_xml->file->header->{'phase-group'}->phase['job-id'] = $second_job->tjid;
$wrong_file = 'public://tmgmt_file/wrong_file.xlf';
$wrong_xml
->asXML($wrong_file);
$uri = $job
->uri();
$edit = array(
'files[file]' => $wrong_file,
);
$this
->drupalPost($uri['path'] . '/manage', $edit, t('Import'));
$this
->assertRaw(t('The imported file job id @file_tjid does not match the job id @job_tjid.', array(
'@file_tjid' => $second_job->tjid,
'@job_tjid' => $job->tjid,
)));
$translated_file = 'public://tmgmt_file/translated file.xlf';
$xml
->asXML($translated_file);
// Import the file and accept translation for the "dummy" item.
$uri = $job
->uri();
$edit = array(
'files[file]' => $translated_file,
);
$this
->drupalPost($uri['path'] . '/manage', $edit, t('Import'));
$this
->clickLink(t('review'));
$this
->drupalPostAJAX(NULL, NULL, array(
'reviewed-dummy|deep_nesting' => '✓',
));
// Update the translation for "another" item and import.
$xml->file->body->group[0]->{'trans-unit'}[1]->target = $xml->file->body->group[0]->{'trans-unit'}[1]->target . ' updated';
$xml
->asXML($translated_file);
$uri = $job
->uri();
$edit = array(
'files[file]' => $translated_file,
);
$this
->drupalPost($uri['path'] . '/manage', $edit, t('Import'));
// At this point we must have the "dummy" item accepted and intact. The
// "another" item must have updated translation.
$this
->clickLink(t('review'));
$this
->assertFieldByName('dummy|deep_nesting[translation]', 'de_' . $first_item_data['dummy][deep_nesting']['#text']);
$this
->assertFieldByName('another_item[translation]', 'de_' . $first_item_data['another_item']['#text'] . ' updated');
// Now finish the import/save as completed process doing another extra
// import. The extra import will test that a duplicate import of the same
// file does not break the process.
$this
->importFile($translated_file, $translated_text, $job);
$this
->assertNoText(t('Import translated file'));
// Create a job, assign to the file translator and delete before attaching
// a file.
$other_job = $this
->createJob();
$other_job->translator = $translator->name;
$other_job
->save();
$other_job
->delete();
// Make sure the file of the other job still exists.
$response = drupal_http_request($download_url);
$this
->assertEqual(200, $response->code);
// Delete the job and then make sure that the file has been deleted.
$job
->delete();
$response = drupal_http_request($download_url);
$this
->assertEqual(404, $response->code);
}