function FileTranslatorTest::testHTML

Tests export and import for the HTML format.

File

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

Class

FileTranslatorTest
Tests for the file translator.

Namespace

Drupal\Tests\tmgmt_file\Functional

Code

function testHTML() {
  $translator = Translator::load('file');
  $translator
    ->setSetting('export_format', 'html')
    ->save();
  $job = $this
    ->createJob();
  $job->translator = $translator
    ->id();
  $job
    ->addItem('test_source', 'test', '1');
  $job
    ->addItem('test_source', 'test', '2');
  $job
    ->requestTranslation();
  $messages = $job
    ->getMessages();
  $message = reset($messages);
  $download_url = $message->variables->{'@link'};
  $this
    ->assertFalse((bool) strpos('< a', $download_url));

  // "Translate" items.
  $xml = simplexml_load_file($download_url);
  $translated_text = array();
  foreach ($xml->body
    ->children() as $group) {
    for ($i = 0; $i < $group
      ->count(); $i++) {

      // This does not actually override the whole object, just the content.
      $group->div[$i] = (string) $xml->head->meta[3]['content'] . '_' . (string) $group->div[$i];

      // Store the text to allow assertions later on.
      $translated_text[(string) $group['id']][(string) $group->div[$i]['id']] = (string) $group->div[$i];
    }
  }
  $translated_file = 'public://tmgmt_file/translated.html';
  $xml
    ->asXML($translated_file);
  $this
    ->importFile($translated_file, $translated_text, $job);
}