You are here

function TMGMTFileTestCase::testXLIFFCDATA in Translation Management Tool 7

Test the CDATA option for XLIFF export and import.

File

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

Class

TMGMTFileTestCase
Basic tests for the file translator.

Code

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

  // Get the source text.
  $source_text = trim(file_get_contents(drupal_get_path('module', 'tmgmt') . '/tests/testing_html/sample.html'));

  // Create a new job.
  $job = $this
    ->createJob();
  $job->translator = $translator->name;
  $job
    ->addItem('test_html_source', 'test', '1');
  $job
    ->requestTranslation();
  $messages = $job
    ->getMessages();
  $message = reset($messages);
  $download_url = $message->variables['!link'];

  // Get XLIFF content.
  $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, '');

        // Update translations using CDATA.
        $node = dom_import_simplexml($transunit->target);
        $owner = $node->ownerDocument;
        $node
          ->appendChild($owner
          ->createCDATASection($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;
      }
    }
  }
  $translated_file = 'public://tmgmt_file/translated file.xlf';
  $xml
    ->asXML($translated_file);

  // Import the file and check 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'));
  foreach ($translated_text[1] as $key => $value) {
    $this
      ->assertText(htmlspecialchars($value));
  }
}