You are here

function PluginsTest::testBasicWorkflow in Translation Management Tool 8

File

tests/src/Kernel/PluginsTest.php, line 48

Class

PluginsTest
Tests interaction between core and the plugins.

Namespace

Drupal\Tests\tmgmt\Kernel

Code

function testBasicWorkflow() {

  // Submit a translation job.
  $submit_job = $this
    ->createJobWithItems('submit');
  $submit_job
    ->requestTranslation();
  $submit_job = Job::load($submit_job
    ->id());
  $this
    ->assertTrue($submit_job
    ->isActive());
  $messages = $submit_job
    ->getMessages();
  $last_message = end($messages);
  $this
    ->assertEqual('Test submit.', $last_message->message->value);

  // Translate a job.
  $translate_job = $this
    ->createJobWithItems('translate');
  $translate_job
    ->requestTranslation();
  $translate_job = Job::load($translate_job
    ->id());
  foreach ($translate_job
    ->getItems() as $job_item) {
    $this
      ->assertTrue($job_item
      ->isNeedsReview());
  }
  $messages = $translate_job
    ->getMessages();

  // array_values() results in numeric keys, which is necessary for list.
  list($debug, $translated, $needs_review) = array_values($messages);
  $this
    ->assertEqual('Test translator called.', $debug->message->value);
  $this
    ->assertEqual('debug', $debug->type->value);
  $this
    ->assertEqual('Test translation created.', $translated->message->value);
  $this
    ->assertEqual('status', $translated->type->value);

  // The third message is specific to a job item and has different state
  // constants.
  $this
    ->assertEqual('The translation of <a href=":source_url">@source</a> to @language is finished and can now be <a href=":review_url">reviewed</a>.', $needs_review->message->value);
  $this
    ->assertEqual('status', $needs_review->type->value);
  $i = 1;
  foreach ($translate_job
    ->getItems() as $item) {

    // Check the translated text.
    if ($i != 3) {
      $expected_text = 'de(de-ch): Text for job item with type ' . $item
        ->getItemType() . ' and id ' . $item
        ->getItemId() . '.';
    }
    else {

      // The third item has an explicitly stored data value.
      $expected_text = 'de(de-ch): Stored data';
    }
    $item_data = $item
      ->getData();
    $this
      ->assertEqual($expected_text, $item_data['dummy']['deep_nesting']['#translation']['#text']);
    $i++;
  }
  foreach ($translate_job
    ->getItems() as $job_item) {
    $job_item
      ->acceptTranslation();
  }

  // @todo Accepting does not result in messages on the job anymore.
  // Update once there are job item messages.

  /*
      $messages = $translate_job->getMessages();
      $last_message = end($messages);
      $this->assertEqual('Job accepted', $last_message->message->value);
      $this->assertEqual('status', $last_message->type);*/

  // Check if the translations have been "saved".
  foreach ($translate_job
    ->getItems() as $item) {
    $this
      ->assertNotEmpty(\Drupal::state()
      ->get('tmgmt_test_saved_translation_' . $item
      ->getItemType() . '_' . $item
      ->getItemId(), FALSE));
  }

  // A rejected job.
  $reject_job = $this
    ->createJobWithItems('reject');
  $reject_job
    ->requestTranslation();

  // Still rejected.
  $this
    ->assertTrue($reject_job
    ->isRejected());
  $messages = $reject_job
    ->getMessages();
  $last_message = end($messages);
  $this
    ->assertEqual('This is not supported.', $last_message->message->value);
  $this
    ->assertEqual('error', $last_message
    ->getType());

  // A failing job.
  $failing_job = $this
    ->createJobWithItems('fail');
  $failing_job
    ->requestTranslation();

  // Still new.
  $this
    ->assertTrue($failing_job
    ->isUnprocessed());
  $messages = $failing_job
    ->getMessages();
  $last_message = end($messages);
  $this
    ->assertEqual('Service not reachable.', $last_message->message->value);
  $this
    ->assertEqual('error', $last_message
    ->getType());
}