function CrudTest::testAddingTranslatedData

Tests adding translated data and revision handling.

File

tests/src/Kernel/CrudTest.php, line 259

Class

CrudTest
Basic crud operations for jobs and translators

Namespace

Drupal\Tests\tmgmt\Kernel

Code

function testAddingTranslatedData() {
  $translator = $this
    ->createTranslator();
  $job = $this
    ->createJob();
  $job->translator = $translator
    ->id();
  $job
    ->save();

  // Add some test items.
  $item1 = $job
    ->addItem('test_source', 'test_with_long_label', 5);

  // Test the job label - it must not exceed the TMGMT_JOB_LABEL_MAX_LENGTH.
  $this
    ->assertTrue(Job::LABEL_MAX_LENGTH >= strlen($job
    ->label()));
  $key = array(
    'dummy',
    'deep_nesting',
  );
  $translation['dummy']['deep_nesting']['#text'] = 'translated 1';
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);

  // Check job messages.
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(1, $messages);
  $last_message = end($messages);
  $this
    ->assertEquals('The translation of <a href=":source_url">@source</a> to @language is finished and can now be <a href=":review_url">reviewed</a>.', $last_message->message->value);

  // Initial state - translation has been received for the first time.
  $this
    ->assertEquals('translated 1', $data['#translation']['#text']);
  $this
    ->assertTrue(empty($data['#translation']['#text_revisions']));
  $this
    ->assertEquals('remote', $data['#translation']['#origin']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $data['#translation']['#timestamp']);

  // Set status back to pending as if the data item was rejected.
  $item1
    ->updateData(array(
    'dummy',
    'deep_nesting',
  ), array(
    '#status' => TMGMT_DATA_ITEM_STATE_PENDING,
  ));

  // Add same translation text.
  $translation['dummy']['deep_nesting']['#text'] = 'translated 1';
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);

  // Check if the status has been updated back to translated.
  $this
    ->assertEquals(TMGMT_DATA_ITEM_STATE_TRANSLATED, $data['#status']);

  // Add translation, however locally customized.
  $translation['dummy']['deep_nesting']['#text'] = 'translated 2';
  $translation['dummy']['deep_nesting']['#origin'] = 'local';
  $translation['dummy']['deep_nesting']['#timestamp'] = \Drupal::time()
    ->getRequestTime() - 5;
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);

  // The translation text is updated.
  $this
    ->assertEquals('translated 2', $data['#translation']['#text']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime() - 5, $data['#translation']['#timestamp']);

  // Previous translation is among text_revisions.
  $this
    ->assertEquals('translated 1', $data['#translation']['#text_revisions'][0]['#text']);
  $this
    ->assertEquals('remote', $data['#translation']['#text_revisions'][0]['#origin']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $data['#translation']['#text_revisions'][0]['#timestamp']);

  // Current translation origin is local.
  $this
    ->assertEquals('local', $data['#translation']['#origin']);

  // Check job messages.
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(1, $messages);

  // Add translation - not local.
  $translation['dummy']['deep_nesting']['#text'] = 'translated 3';
  unset($translation['dummy']['deep_nesting']['#origin']);
  unset($translation['dummy']['deep_nesting']['#timestamp']);
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);

  // The translation text is NOT updated.
  $this
    ->assertEquals('translated 2', $data['#translation']['#text']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime() - 5, $data['#translation']['#timestamp']);

  // Received translation is the latest revision.
  $last_revision = end($data['#translation']['#text_revisions']);
  $this
    ->assertEquals('translated 3', $last_revision['#text']);
  $this
    ->assertEquals('remote', $last_revision['#origin']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $last_revision['#timestamp']);

  // Current translation origin is local.
  $this
    ->assertEquals('local', $data['#translation']['#origin']);

  // Check job messages.
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(2, $messages);
  $last_message = end($messages);
  $this
    ->assertEquals('Translation for customized @key received. Revert your changes if you wish to use it.', $last_message->message->value);

  // Revert to previous revision which is the latest received translation.
  $item1
    ->dataItemRevert($key);
  $data = $item1
    ->getData($key);

  // The translation text is updated.
  $this
    ->assertEquals('translated 3', $data['#translation']['#text']);
  $this
    ->assertEquals('remote', $data['#translation']['#origin']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $data['#translation']['#timestamp']);

  // Latest revision is now the formerly added local translation.
  $last_revision = end($data['#translation']['#text_revisions']);
  $this
    ->assertNotEmpty($last_revision['#text'], 'translated 2');
  $this
    ->assertNotEmpty($last_revision['#origin'], 'remote');
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime() - 5, $last_revision['#timestamp']);

  // Check job messages.
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(3, $messages);
  $last_message = end($messages);
  $this
    ->assertEquals('Translation for @key reverted to the latest version.', $last_message->message->value);

  // There should be three revisions now.
  $this
    ->assertCount(3, $data['#translation']['#text_revisions']);

  // Attempt to update the translation with the same text, this should not
  // lead to a new revision.
  $translation['dummy']['deep_nesting']['#text'] = 'translated 3';

  //unset($translation['dummy']['deep_nesting']['#origin']);

  //unset($translation['dummy']['deep_nesting']['#timestamp']);
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);
  $this
    ->assertCount(3, $data['#translation']['#text_revisions']);

  // Mark the translation as reviewed, a new translation should not update the
  // existing one but create a new translation.
  $item1
    ->updateData($key, array(
    '#status' => TMGMT_DATA_ITEM_STATE_REVIEWED,
  ));
  $translation['dummy']['deep_nesting']['#text'] = 'translated 4';
  $item1
    ->addTranslatedData($translation);
  $data = $item1
    ->getData($key);

  // The translation text is NOT updated.
  $this
    ->assertEquals('translated 3', $data['#translation']['#text']);

  // Received translation is the latest revision.
  $this
    ->assertCount(4, $data['#translation']['#text_revisions']);
  $last_revision = end($data['#translation']['#text_revisions']);
  $this
    ->assertEquals('translated 4', $last_revision['#text']);
  $this
    ->assertEquals('remote', $last_revision['#origin']);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $last_revision['#timestamp']);

  // Check job messages.
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(4, $messages);
  $last_message = end($messages);
  $this
    ->assertEquals('Translation for already reviewed @key received and stored as a new revision. Revert to it if you wish to use it.', $last_message->message->value);

  // Add a new job item.
  $new_item = $job
    ->addItem('test_source', 'test_with_long_label', 6);
  $translation['dummy']['deep_nesting']['#text'] = 'translated 1';
  $new_item
    ->addTranslatedData($translation);
  $messages = $job
    ->getMessages();
  $this
    ->assertCount(5, $messages);
  $last_message = end($messages);

  // Assert that the job and job item are loaded correctly.
  $message_job = $last_message
    ->getJob();
  $this
    ->assertInstanceOf(JobInterface::class, $message_job);
  $this
    ->assertEquals($job
    ->id(), $message_job
    ->id());
  $message_job_item = $last_message
    ->getJobItem();
  $this
    ->assertInstanceOf(JobItemInterface::class, $message_job_item);
  $this
    ->assertEquals($new_item
    ->id(), $message_job_item
    ->id());
}