Skip to content

Commit

Permalink
D8AGE-291: Updated the functional test.
Browse files Browse the repository at this point in the history
  • Loading branch information
intelektron committed May 29, 2024
1 parent 76b2907 commit c25aaf5
Showing 1 changed file with 179 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function setUp(): void {
/**
* Tests the CDT translation provider configuration form.
*/
public function testCdtProviderConfiguration(): void {
public function txestCdtProviderConfiguration(): void {
$user = $this->drupalCreateUser([
'administer remote translators',
'access administration pages',
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testCdtProviderConfiguration(): void {
* This is similar to RemoteTranslationTest::testSingleTranslationFlow but
* simplified and focusing more on the CDT specific aspects.
*/
public function testCdtSingleTranslationFlow(): void {
public function txestCdtSingleTranslationFlow(): void {
// Set PT language mapping.
$translator = RemoteTranslatorProvider::load('cdt');
assert($translator instanceof RemoteTranslatorProviderInterface);
Expand All @@ -140,54 +140,19 @@ public function testCdtSingleTranslationFlow(): void {
$translator->setProviderConfiguration($configuration);
$translator->save();

$node = $this->createBasicTestNode('oe_demo_translatable_page', "The translation's page");
$this->drupalGet($node->toUrl('drupal:content-translation-overview'));
$this->clickLink('Remote translations');

$select = $this->assertSession()->selectExists('Translator');
// The CDT translator is preselected because it's the only one.
$this->assertEquals('cdt', $select->find('css', 'option[selected]')->getValue());
$this->assertSession()->pageTextContains('New translation request using CDT');

// Select the translation settings.
$this->getSession()->getPage()->fillField('Comments', 'Test Translation');
$this->getSession()->getPage()->fillField('Confidentiality', 'NO');
$this->getSession()->getPage()->fillField('translator_configuration[cdt][contact_usernames][0][value]', 'TESTUSER1');
$this->getSession()->getPage()->fillField('translator_configuration[cdt][deliver_to][0][value]', 'TESTUSER1');
$this->getSession()->getPage()->fillField('Department', '123');
$this->getSession()->getPage()->fillField('Phone number', '123456');
$this->getSession()->getPage()->fillField('Priority', 'NO');

// Assert the languages' validation.
$this->getSession()->getPage()->pressButton('Save and send');
$this->assertSession()->pageTextContains('Please select at least one language.');

// Select 2 languages.
$this->getSession()->getPage()->checkField('Bulgarian');
$this->getSession()->getPage()->checkField('Portuguese');
$this->getSession()->getPage()->pressButton('Save and send');

$this->assertSession()->pageTextContains('The translation request has been sent to CDT.');
$this->assertSession()->pageTextContains('No new translation request can be made because there is already an active translation request for this entity version.');

// Assert that we got back a correct response and our request was updated.
$provider_manager = $this->container->get('plugin.manager.oe_translation_remote.remote_translation_provider_manager');
$requests = $provider_manager->getExistingTranslationRequests($node, TRUE);
$request = end($requests);
$this->assertEquals('en', $request->getSourceLanguageCode());
$target_languages = $request->getTargetLanguages();
$this->assertEquals(new LanguageWithStatus('bg', TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED), $target_languages['bg']);
$this->assertEquals(new LanguageWithStatus('pt-pt', TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED), $target_languages['pt-pt']);
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED, $request->getRequestStatus());
$this->assertEquals('cdt', $request->getTranslatorProvider()->id());
$request = $this->createTestTranslation([
'bg' => 'Bulgarian',
'pt-pt' => 'Portuguese',
]);

// Request the permanent ID.
$this->assertSession()->linkNotExists('Refresh status', 'The Refresh status link should not be available yet.');
$this->clickLink('Get Permanent ID');
$this->assertSession()->pageTextContains('The permanent ID has been updated.');
$request = $this->getUncachedTranslationRequest($request);
$cdt_id = $request->getCdtId();
$this->assertNotEmpty($cdt_id);
$this->assertNotEmpty($cdt_id, 'The CDT ID should be set.');
$this->assertEquals($cdt_id, sprintf('%s/%s', date('Y'), $request->getCorrelationId()));

// Refresh the status, nothing should change.
$this->clickLink('Refresh status');
Expand Down Expand Up @@ -281,6 +246,122 @@ public function testCdtSingleTranslationFlow(): void {
$this->assertLogMessagesTable($expected_logs);
}

/**
* Tests the cancelled remote translation flow using CDT.
*/
public function testCancelledTranslationFlow(): void {
$request = $this->createTestTranslation([
'de' => 'German',
]);

// Request the permanent ID.
$this->clickLink('Get Permanent ID');

// Mock the cancellation of the DE language. Index 0 belongs to the request.
$this->getSession()->getPage()->find('css', 'tr[hreflang="de"]')->pressButton('List additional actions');
$this->clickLink('Update status to Cancelled (mock)', 1);
$request = $this->getUncachedTranslationRequest($request);
$this->assertEquals(TranslationRequestCdtInterface::STATUS_LANGUAGE_CANCELLED, $request->getTargetLanguages()['de']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_REQUEST_FINISHED, $request->getRequestStatus());

// Assert the log messages.
$this->drupalGet('translation-request/' . $request->id());
$expected_logs = [
1 => [
'Info',
'The translation request was successfully validated.',
$this->user->label(),
],
2 => [
'Info',
"The translation request was successfully sent to CDT with correlation ID: {$request->getCorrelationId()}.",
$this->user->label(),
],
3 => [
'Info',
"Manually updated the permanent ID.Updated cdt_id field to {$request->getCdtId()}.",
$this->user->label(),
],
4 => [
'Info',
'Received CDT callback, updating the job...The following languages are updated: de (Requested => Cancelled).',
$this->user->label(),
],
];
$this->assertLogMessagesTable($expected_logs);
}

/**
* Tests the partially cancelled remote translation flow using CDT.
*/
public function testPartiallyCancelledTranslationFlow(): void {
$request = $this->createTestTranslation([
'fr' => 'French',
'sk' => 'Slovak',
]);

// Request the permanent ID.
$this->clickLink('Get Permanent ID');

// Mock the cancellation of the FR language. Index 0 belongs to the request.
$this->getSession()->getPage()->find('css', 'tr[hreflang="fr"]')->pressButton('List additional actions');
$this->clickLink('Update status to Cancelled (mock)', 1);
$request = $this->getUncachedTranslationRequest($request);
$this->assertEquals(TranslationRequestCdtInterface::STATUS_LANGUAGE_CANCELLED, $request->getTargetLanguages()['fr']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED, $request->getTargetLanguages()['sk']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_REQUEST_REQUESTED, $request->getRequestStatus());

// Mock the acceptance of the SK language.
$this->clickLink('Update status to Completed (mock)', 2);
$request = $this->getUncachedTranslationRequest($request);
$this->assertEquals(TranslationRequestCdtInterface::STATUS_LANGUAGE_CANCELLED, $request->getTargetLanguages()['fr']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_LANGUAGE_REVIEW, $request->getTargetLanguages()['sk']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_REQUEST_TRANSLATED, $request->getRequestStatus());
$this->clickLink('Fetch the translation');
$this->clickLink('Review');
$this->getSession()->getPage()->pressButton('Save and synchronise');
$request = $this->getUncachedTranslationRequest($request);
$this->assertEquals(TranslationRequestCdtInterface::STATUS_LANGUAGE_CANCELLED, $request->getTargetLanguages()['fr']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_LANGUAGE_SYNCHRONISED, $request->getTargetLanguages()['sk']->getStatus());
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_REQUEST_FINISHED, $request->getRequestStatus());

// Assert the log messages.
$this->drupalGet('translation-request/' . $request->id());
$expected_logs = [
1 => [
'Info',
'The translation request was successfully validated.',
$this->user->label(),
],
2 => [
'Info',
"The translation request was successfully sent to CDT with correlation ID: {$request->getCorrelationId()}.",
$this->user->label(),
],
3 => [
'Info',
"Manually updated the permanent ID.Updated cdt_id field to {$request->getCdtId()}.",
$this->user->label(),
],
4 => [
'Info',
'Received CDT callback, updating the job...The following languages are updated: fr (Requested => Cancelled).',
$this->user->label(),
],
5 => [
'Info',
'Received CDT callback, updating the job...The following languages are updated: sk (Requested => Review).',
$this->user->label(),
],
6 => [
'Info',
'The Slovak translation has been synchronised with the content.',
$this->user->label(),
],
];
$this->assertLogMessagesTable($expected_logs);
}

/**
* Asserts the log messages table output.
*
Expand Down Expand Up @@ -324,4 +405,59 @@ protected function getUncachedTranslationRequest(TranslationRequestCdtInterface
return $refreshed_request;
}

/**
* Creates a test translation with specified languages.
*
* @param array $requested_languages
* The requested languages keyed by the language code and the label.
*
* @return \Drupal\oe_translation_cdt\TranslationRequestCdtInterface
* The created translation request.
*/
protected function createTestTranslation(array $requested_languages): TranslationRequestCdtInterface {
$node = $this->createBasicTestNode('oe_demo_translatable_page', "The translation's page");
$this->drupalGet($node->toUrl('drupal:content-translation-overview'));
$this->clickLink('Remote translations');

$select = $this->assertSession()->selectExists('Translator');
// The CDT translator is preselected because it's the only one.
$this->assertEquals('cdt', $select->find('css', 'option[selected]')->getValue());
$this->assertSession()->pageTextContains('New translation request using CDT');

// Select the translation settings.
$this->getSession()->getPage()->fillField('Comments', 'Test Translation');
$this->getSession()->getPage()->fillField('Confidentiality', 'NO');
$this->getSession()->getPage()->fillField('translator_configuration[cdt][contact_usernames][0][value]', 'TESTUSER1');
$this->getSession()->getPage()->fillField('translator_configuration[cdt][deliver_to][0][value]', 'TESTUSER1');
$this->getSession()->getPage()->fillField('Department', '123');
$this->getSession()->getPage()->fillField('Phone number', '123456');
$this->getSession()->getPage()->fillField('Priority', 'NO');

// Assert the languages' validation.
$this->getSession()->getPage()->pressButton('Save and send');
$this->assertSession()->pageTextContains('Please select at least one language.');

foreach ($requested_languages as $label) {
$this->getSession()->getPage()->checkField($label);
}
$this->getSession()->getPage()->pressButton('Save and send');

$this->assertSession()->pageTextContains('The translation request has been sent to CDT.');
$this->assertSession()->pageTextContains('No new translation request can be made because there is already an active translation request for this entity version.');

// Assert that we got back a correct response and our request was updated.
$provider_manager = $this->container->get('plugin.manager.oe_translation_remote.remote_translation_provider_manager');
$requests = $provider_manager->getExistingTranslationRequests($node, TRUE);
$request = end($requests);
$this->assertEquals('en', $request->getSourceLanguageCode());
$target_languages = $request->getTargetLanguages();
foreach ($requested_languages as $langcode => $label) {
$this->assertEquals(new LanguageWithStatus($langcode, TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED), $target_languages[$langcode]);
}
$this->assertEquals(TranslationRequestRemoteInterface::STATUS_LANGUAGE_REQUESTED, $request->getRequestStatus());
$this->assertEquals('cdt', $request->getTranslatorProvider()->id());

return $request;
}

}

0 comments on commit c25aaf5

Please sign in to comment.