You are here

function TMGMTFileTestCase::testXLIFFTextProcessing in Translation Management Tool 7

Test the content processing for XLIFF export and import.

File

translators/file/tmgmt_file.test, line 30
Test cases for the file translator module.

Class

TMGMTFileTestCase
Basic tests for the file translator.

Code

function testXLIFFTextProcessing() {
  $translator = $this
    ->createTranslator();
  $translator->plugin = 'file';
  $translator->settings = array(
    'export_format' => 'xlf',
    'xliff_processing' => TRUE,
  );
  $translator
    ->save();

  // 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->name;
  $job
    ->addItem('test_html_source', 'test', '1');

  // 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);
  $uri = $job
    ->uri();
  $edit = array(
    'files[file]' => $translated_file,
  );
  $this
    ->drupalPost($uri['path'] . '/manage', $edit, t('Import'));

  // Reset caches and reload job.
  entity_get_controller('tmgmt_job')
    ->resetCache();
  entity_get_controller('tmgmt_job_item')
    ->resetCache();
  $job = tmgmt_job_load($job->tjid);

  // 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();

  /** @var TMGMTJobItem $job_item */
  $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->name;
  $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);
  $uri = $job
    ->uri();
  $edit = array(
    'files[file]' => $translated_file,
  );
  $this
    ->drupalPost($uri['path'] . '/manage', $edit, t('Import'));
  entity_get_controller('tmgmt_job')
    ->resetCache();
  entity_get_controller('tmgmt_job_item')
    ->resetCache();
  $job = tmgmt_job_load($job->tjid);
  $this
    ->assertIntegrityCheck($job);

  // Set the XLIFF processing to FALSE and test it results in the source
  // text not being XLIFF processed.
  $translator->settings['xliff_processing'] = FALSE;
  $translator
    ->save();
  $job = $this
    ->createJob();
  $job->translator = $translator->name;
  $job
    ->addItem('test_html_source', 'test', '1');
  $job
    ->requestTranslation();
  $targets = $this
    ->getTransUnitsContent($job);
  $this
    ->assertEqual(trim(html_entity_decode($targets['0']['source'])), $source_text);
}