function CrudTest::testAddingTranslatedData in Translation Management Tool 8
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\KernelCode
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
->assertEqual(count($messages), 1);
$last_message = end($messages);
$this
->assertEqual($last_message->message->value, 'The translation of <a href=":source_url">@source</a> to @language is finished and can now be <a href=":review_url">reviewed</a>.');
// Initial state - translation has been received for the first time.
$this
->assertEqual($data['#translation']['#text'], 'translated 1');
$this
->assertTrue(empty($data['#translation']['#text_revisions']));
$this
->assertEqual($data['#translation']['#origin'], 'remote');
$this
->assertEqual($data['#translation']['#timestamp'], \Drupal::time()
->getRequestTime());
// 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
->assertEqual($data['#status'], TMGMT_DATA_ITEM_STATE_TRANSLATED);
// 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
->assertEqual($data['#translation']['#text'], 'translated 2');
$this
->assertEqual($data['#translation']['#timestamp'], \Drupal::time()
->getRequestTime() - 5);
// Previous translation is among text_revisions.
$this
->assertEqual($data['#translation']['#text_revisions'][0]['#text'], 'translated 1');
$this
->assertEqual($data['#translation']['#text_revisions'][0]['#origin'], 'remote');
$this
->assertEqual($data['#translation']['#text_revisions'][0]['#timestamp'], \Drupal::time()
->getRequestTime());
// Current translation origin is local.
$this
->assertEqual($data['#translation']['#origin'], 'local');
// Check job messages.
$messages = $job
->getMessages();
$this
->assertEqual(count($messages), 1);
// 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
->assertEqual($data['#translation']['#text'], 'translated 2');
$this
->assertEqual($data['#translation']['#timestamp'], \Drupal::time()
->getRequestTime() - 5);
// Received translation is the latest revision.
$last_revision = end($data['#translation']['#text_revisions']);
$this
->assertEqual($last_revision['#text'], 'translated 3');
$this
->assertEqual($last_revision['#origin'], 'remote');
$this
->assertEqual($last_revision['#timestamp'], \Drupal::time()
->getRequestTime());
// Current translation origin is local.
$this
->assertEqual($data['#translation']['#origin'], 'local');
// Check job messages.
$messages = $job
->getMessages();
$this
->assertEqual(count($messages), 2);
$last_message = end($messages);
$this
->assertEqual($last_message->message->value, 'Translation for customized @key received. Revert your changes if you wish to use it.');
// Revert to previous revision which is the latest received translation.
$item1
->dataItemRevert($key);
$data = $item1
->getData($key);
// The translation text is updated.
$this
->assertEqual($data['#translation']['#text'], 'translated 3');
$this
->assertEqual($data['#translation']['#origin'], 'remote');
$this
->assertEqual($data['#translation']['#timestamp'], \Drupal::time()
->getRequestTime());
// 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
->assertEqual($last_revision['#timestamp'], \Drupal::time()
->getRequestTime() - 5);
// Check job messages.
$messages = $job
->getMessages();
$this
->assertEqual(count($messages), 3);
$last_message = end($messages);
$this
->assertEqual($last_message->message->value, 'Translation for @key reverted to the latest version.');
// There should be three revisions now.
$this
->assertEqual(count($data['#translation']['#text_revisions']), 3);
// 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
->assertEqual(count($data['#translation']['#text_revisions']), 3);
// 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
->assertEqual($data['#translation']['#text'], 'translated 3');
// Received translation is the latest revision.
$this
->assertEqual(count($data['#translation']['#text_revisions']), 4);
$last_revision = end($data['#translation']['#text_revisions']);
$this
->assertEqual($last_revision['#text'], 'translated 4');
$this
->assertEqual($last_revision['#origin'], 'remote');
$this
->assertEqual($last_revision['#timestamp'], \Drupal::time()
->getRequestTime());
// Check job messages.
$messages = $job
->getMessages();
$this
->assertEqual(count($messages), 4);
$last_message = end($messages);
$this
->assertEqual($last_message->message->value, 'Translation for already reviewed @key received and stored as a new revision. Revert to it if you wish to use it.');
// 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());
}