You are here

public function LingotekNodeNotificationCallbackQueueWorkerTest::testNotificationsInARow in Lingotek Translation 3.8.x

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

Testing handling several notifications in a row.

File

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

Class

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

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testNotificationsInARow() {
  ConfigurableLanguage::createFromLangcode('it')
    ->save();
  ConfigurableLanguage::createFromLangcode('ca')
    ->save();
  ConfigurableLanguage::createFromLangcode('hu')
    ->save();
  ConfigurableLanguage::createFromLangcode('de')
    ->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);
  $this
    ->goToContentBulkManagementForm();

  // Upload the node.
  $this
    ->clickLink('EN');
  $this
    ->assertText('The import for node Llamas are cool is complete.');

  // Request languages.
  $languages = [
    'DE' => 'de_DE',
    'ES' => 'es_ES',
    'HU' => 'hu_HU',
    'IT' => 'it_IT',
    'CA' => 'ca_ES',
  ];
  foreach ($languages as $langcode => $locale) {
    $this
      ->clickLink($langcode);
    $this
      ->assertText(new FormattableMarkup("Locale '@locale' was added as a translation target for node Llamas are cool.", [
      '@locale' => $locale,
    ]));
  }

  /** @var \GuzzleHttp\Promise\PromiseInterface[] $requests */
  $requests = [];
  foreach ($languages as $langcode => $locale) {
    $url = Url::fromRoute('lingotek.notify', [], [
      'query' => [
        'project_id' => 'test_project',
        'document_id' => 'dummy-document-hash-id',
        'locale_code' => str_replace('_', '-', $locale),
        'locale' => $locale,
        'complete' => 'true',
        'type' => 'target',
        'progress' => '100',
      ],
    ])
      ->setAbsolute()
      ->toString();
    $requests[] = \Drupal::httpClient()
      ->postAsync($url);
  }
  $count = 0;

  // We wait for the requests to finish.
  foreach ($requests as $request) {
    try {
      $request
        ->then(function ($response) use ($request) {
        $message = new TranslatableMarkup('FULFILLED. Got a response with status %status and body: %body', [
          '%status' => $response
            ->getStatusCode(),
          '%body' => (string) $response
            ->getBody(TRUE),
        ]);
        $this
          ->verbose($message);
      }, function ($response) use ($request) {
        $message = new TranslatableMarkup('REJECTED. Got a response with status %status and body: %body', [
          '%status' => $response
            ->getStatusCode(),
          '%body' => (string) $response
            ->getBody(TRUE),
        ]);
        $this
          ->verbose($message);
      });
    } catch (\Exception $error) {
      $count++;
    }
  }
  foreach ($requests as $request) {
    $request
      ->wait(TRUE);
  }

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

  // All the links are ready.
  $current_links = $this
    ->xpath("//a[contains(@class,'language-icon') and contains(@class, 'target-ready')]");
  $this
    ->assertEqual(count($current_links), count($languages) - $count, new FormattableMarkup('Various languages (@var) are ready.', [
    '@var' => count($languages) - $count,
  ]));
  $this
    ->assertTrue(TRUE, new FormattableMarkup('@count target languages failed, but error where given back so the TMS can retry.', [
    '@count' => $count,
  ]));
  $this
    ->assertEqual(5, count($current_links), new FormattableMarkup('All languages (@var) are ready.', [
    '@var' => count($current_links),
  ]));

  // Run cron.
  $this->container
    ->get('cron')
    ->run();
  $this
    ->goToContentBulkManagementForm();
  $current_links = $this
    ->xpath("//a[contains(@class,'language-icon') and contains(@class, 'target-current')]");
  $this
    ->assertEqual(count($current_links), count($languages) - $count, new FormattableMarkup('Various languages (@var) are current.', [
    '@var' => count($languages) - $count,
  ]));
  $this
    ->assertTrue(TRUE, new FormattableMarkup('@count target languages failed, but error where given back so the TMS can retry.', [
    '@count' => $count,
  ]));
  $this
    ->assertEqual(5, count($current_links), new FormattableMarkup('All languages (@var) are current.', [
    '@var' => count($current_links),
  ]));
}