You are here

public function LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError in Lingotek Translation 3.6.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  2. 4.0.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  3. 3.0.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  4. 3.1.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  5. 3.2.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  6. 3.3.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  7. 3.4.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  8. 3.5.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  9. 3.7.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()
  10. 3.8.x tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php \Drupal\Tests\lingotek\Functional\LingotekNodeNotificationCallbackQueueWorkerTest::testAutomatedNotificationNodeTranslationWithError()

Test that a notification with a failure in download responded with an error.

File

tests/src/Functional/LingotekNodeNotificationCallbackQueueWorkerTest.php, line 272

Class

LingotekNodeNotificationCallbackQueueWorkerTest
Tests translating a node using the notification callback with a queue worker.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testAutomatedNotificationNodeTranslationWithError() {

  // Add an additional language.
  ConfigurableLanguage::createFromLangcode('it')
    ->save();

  // Create a node.
  $edit = [];
  $edit['title[0][value]'] = 'Llamas are cool';
  $edit['body[0][value]'] = 'Llamas are very cool';
  $edit['langcode[0][value]'] = 'en';
  $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'automatic_worker';
  $this
    ->saveAndPublishNodeForm($edit);

  // Simulate the notification of content successfully uploaded.
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'complete' => 'false',
      'type' => 'document_uploaded',
      'progress' => '0',
    ],
  ])
    ->setAbsolute()
    ->toString();
  $request = $this->client
    ->post($url, [
    'cookies' => $this->cookies,
    'headers' => [
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
    ],
    'http_errors' => FALSE,
  ]);

  // Simulate the notification of content successfully translated.
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'locale_code' => 'es-ES',
      'locale' => 'es_ES',
      'complete' => 'true',
      'type' => 'target',
      'progress' => '100',
    ],
  ])
    ->setAbsolute()
    ->toString();
  $request = $this->client
    ->post($url, [
    'cookies' => $this->cookies,
    'headers' => [
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
    ],
    'http_errors' => FALSE,
  ]);
  $response = json_decode($request
    ->getBody(), TRUE);
  $this
    ->verbose($request);
  $this
    ->assertTrue($response['result']['download_queued'], 'Spanish language has been queued after notification automatically.');
  $this
    ->assertEqual('Download for target es_ES in document dummy-document-hash-id has been queued.', $response['messages'][0]);

  // Go to the bulk node management page.
  $this
    ->goToContentBulkManagementForm();

  // All the links are pending until cron runs.
  $this
    ->assertTargetStatus('ES', Lingotek::STATUS_READY);

  // Run cron.
  $this->container
    ->get('cron')
    ->run();
  $this
    ->goToContentBulkManagementForm();
  $this
    ->assertTargetStatus('ES', Lingotek::STATUS_CURRENT);

  // We ensure it fails.
  \Drupal::state()
    ->set('lingotek.must_error_in_download', TRUE);

  // Simulate the notification of content successfully translated.
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'locale_code' => 'it-IT',
      'locale' => 'it_IT',
      'complete' => 'true',
      'type' => 'target',
      'progress' => '100',
    ],
  ])
    ->setAbsolute()
    ->toString();
  $request = $this->client
    ->post($url, [
    'cookies' => $this->cookies,
    'headers' => [
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
    ],
    'http_errors' => FALSE,
  ]);
  $response = json_decode($request
    ->getBody(), TRUE);
  $this
    ->verbose($request);
  $this
    ->assertTrue($response['result']['download_queued'], 'Italian language has been queued after notification automatically.');
  $this
    ->assertEqual('Download for target it_IT in document dummy-document-hash-id has been queued.', $response['messages'][0]);
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'locale_code' => 'it-IT',
      'locale' => 'it_IT',
      'complete' => 'true',
      'type' => 'target',
      'progress' => '100',
    ],
  ])
    ->setAbsolute()
    ->toString();
  $request = \Drupal::httpClient()
    ->postAsync($url);
  try {
    $response = $request
      ->wait();
  } catch (ServerException $exception) {
    if ($exception
      ->getCode() === Response::HTTP_SERVICE_UNAVAILABLE) {
      $this
        ->fail('The request returned a 503 status code.');
    }
    else {
      $this
        ->fail('The request fail with an unexpected status code.');
    }
  }
  $this
    ->verbose(var_export($response, TRUE));

  // Run cron.
  $this->container
    ->get('cron')
    ->run();
  $this
    ->goToContentBulkManagementForm();

  // Check the right class is added.
  $this
    ->assertTargetStatus('IT', Lingotek::STATUS_ERROR);

  // Try to re-download the Italian translation.
  $this
    ->clickLink('IT');
  $this
    ->assertText('The download for node Llamas are cool failed. Please try again.');

  // Check that the Target Status is Error
  $node = Node::load(1);
  $content_translation_service = \Drupal::service('lingotek.content_translation');
  $this
    ->assertIdentical(Lingotek::STATUS_ERROR, $content_translation_service
    ->getTargetStatus($node, 'it'));
}