function FileTranslatorTest::testXLIFFCDATA

Test the CDATA option for XLIFF export and import.

File

translators/tmgmt_file/tests/src/Functional/FileTranslatorTest.php, line 149

Class

FileTranslatorTest
Tests for the file translator.

Namespace

Drupal\Tests\tmgmt_file\Functional

Code

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

  // Get the source text.
  $source_text = trim(file_get_contents(\Drupal::service('extension.list.module')
    ->getPath('tmgmt') . '/tests/testing_html/sample.html'));

  // Create a new job.
  $job = $this
    ->createJob();
  $job->translator = $translator
    ->id();
  $job
    ->addItem('test_html_source', 'test', '1');
  $job
    ->requestTranslation();
  $messages = $job
    ->getMessages();
  $message = reset($messages);

  // Get XLIFF content.
  $variables = $message->variables;
  $download_url = $variables->{'@link'};
  $this
    ->assertFalse((bool) strpos('< a', $download_url));
  $xliff = file_get_contents($download_url);
  $dom = new \DOMDocument();
  $dom
    ->loadXML($xliff);
  $this
    ->assertTrue($dom
    ->schemaValidate(\Drupal::service('extension.list.module')
    ->getPath('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
          ->assertEquals('', $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.
  $this
    ->drupalGet($job
    ->toUrl());
  $edit = array(
    'files[file]' => $translated_file,
  );
  $this
    ->submitForm($edit, 'Import');

  // Reset caches and reload job.
  \Drupal::entityTypeManager()
    ->getStorage('tmgmt_job')
    ->resetCache();
  \Drupal::entityTypeManager()
    ->getStorage('tmgmt_job_item')
    ->resetCache();
  $job = Job::load($job
    ->id());
  $item_data = $job
    ->getData(array(
    1,
    'dummy',
    'deep_nesting',
  ));
  $this
    ->assertEquals(str_replace($source_text, $xml->file['target-language'] . '_' . $source_text, $source_text), trim($item_data[1]['#translation']['#text']));
}