public function FileTranslatorTest::testXLIFFTextProcessing in Translation Management Tool 8
Test the content processing for XLIFF export and import.
File
- translators/
tmgmt_file/ tests/ src/ Functional/ FileTranslatorTest.php, line 39
Class
- FileTranslatorTest
- Tests for the file translator.
Namespace
Drupal\Tests\tmgmt_file\FunctionalCode
public function testXLIFFTextProcessing() {
$translator = $this
->createTranslator([
'plugin' => 'file',
'settings' => [
'export_format' => 'xlf',
'xliff_processing' => TRUE,
'format_configuration' => [
'target' => '',
],
],
]);
// Get the source text.
$source_text = trim(file_get_contents(drupal_get_path('module', 'tmgmt') . '/tests/testing_html/sample.html'));
// Create the reader instance, it will be used through the tests.
$reader = new \XMLReader();
$xliff_elements = array(
'bpt',
'ept',
'ph',
'x',
'#text',
'#cdata-section',
'content',
);
// ==== First test the whole cycle ==== //
$job = $this
->createJob();
$job->translator = $translator
->id();
$job
->addItem('test_html_source', 'test', '1');
// Simulate an existing public://tmmgt_file directory that is not writable.
mkdir('public://tmgmt_file', 0555);
// Requesting translation will mask the html.
$job
->requestTranslation();
$content = $this
->getTransUnitsContent($job);
// Test that the exported trans unit contains only xliff elements.
$reader
->XML('<content>' . $content[0]['source'] . '</content>');
while ($reader
->read()) {
if (!in_array($reader->name, $xliff_elements)) {
$this
->fail(t('The source contains unexpected element %element', array(
'%element' => $reader->name,
)));
}
}
$reader
->XML('<content>' . $content[0]['target'] . '</content>');
while ($reader
->read()) {
if (!in_array($reader->name, $xliff_elements)) {
$this
->fail(t('The target contains unexpected element %element', array(
'%element' => $reader->name,
)));
}
}
// Import the file, make sure all the html has been revealed and no xliff
// elements are present in the job translation.
$messages = $job
->getMessages();
$message = reset($messages);
$translated_file = 'public://tmgmt_file/translated.xlf';
$this
->createTranslationFile($message->variables->{'@link'}, 'one paragraph', 'one translated paragraph', $translated_file);
$edit = array(
'files[file]' => $translated_file,
);
$this
->drupalPostForm($job
->toUrl(), $edit, t('Import'));
// Reset caches and reload job.
\Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('tmgmt_job_item')
->resetCache();
$job = Job::load($job
->id());
// Do the comparison of the translation text and the source. It must be the
// same as there was no change done to the translation.
$item_data = $job
->getData(array(
1,
'dummy',
'deep_nesting',
));
$this
->assertEqual(trim($item_data[1]['#translation']['#text']), str_replace('one paragraph', 'one translated paragraph', $source_text));
$job_items = $job
->getItems();
$job_item = array_shift($job_items);
// Job item must be in review.
$this
->assertTrue($job_item
->isNeedsReview());
$this
->assertIntegrityCheck($job, FALSE);
// ==== Test integrity check ==== //
$job = $this
->createJob();
$job->translator = $translator
->id();
$job
->addItem('test_html_source', 'test', '1');
$job
->requestTranslation();
$messages = $job
->getMessages();
$message = reset($messages);
// Get the xml content and remove the element representing <br />. This will
// result in different element counts in the source and target and should
// trigger an error and not import the translation.
$translated_file = 'public://tmgmt_file/translated.xlf';
$this
->createTranslationFile($message->variables->{'@link'}, '<x id="tjiid2-4" ctype="lb"/>', '', $translated_file);
$edit = array(
'files[file]' => $translated_file,
);
$this
->drupalPostForm($job
->toUrl(), $edit, t('Import'));
\Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('tmgmt_job_item')
->resetCache();
$job = Job::load($job
->id());
$this
->assertIntegrityCheck($job);
// Set the XLIFF processing to FALSE and test it results in the source
// text not being XLIFF processed.
$translator
->setSetting('xliff_processing', FALSE);
$translator
->save();
$job = $this
->createJob();
$job->translator = $translator
->id();
$job
->addItem('test_html_source', 'test', '1');
$job
->requestTranslation();
$targets = $this
->getTransUnitsContent($job);
$this
->assertEqual(trim(html_entity_decode($targets['0']['source'])), $source_text);
}