You are here

public function LingotekNodeNotificationCallbackTest::testAutomatedNotificationNodeTranslation in Lingotek Translation 8

Tests that a node can be translated using the links on the management page.

File

src/Tests/LingotekNodeNotificationCallbackTest.php, line 68

Class

LingotekNodeNotificationCallbackTest
Tests translating a node using the notification callback.

Namespace

Drupal\lingotek\Tests

Code

public function testAutomatedNotificationNodeTranslation() {

  // Login as admin.
  $this
    ->drupalLogin($this->rootUser);

  // Create a node.
  $edit = array();
  $edit['title[0][value]'] = 'Llamas are cool';
  $edit['body[0][value]'] = 'Llamas are very cool';
  $edit['langcode[0][value]'] = 'en';
  $edit['lingotek_translation_profile'] = 'automatic';
  $this
    ->drupalPostForm('node/add/article', $edit, t('Save and publish'));

  /** @var NodeInterface $node */
  $node = Node::load(1);

  /** @var LingotekContentTranslationServiceInterface $content_translation_service */
  $content_translation_service = \Drupal::service('lingotek.content_translation');

  // Assert the content is importing.
  $this
    ->assertIdentical(Lingotek::STATUS_IMPORTING, $content_translation_service
    ->getSourceStatus($node));
  $this
    ->goToContentBulkManagementForm();

  // Simulate the notification of content successfully uploaded.
  $request = $this
    ->drupalPost(Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'complete' => 'false',
      'type' => 'document_uploaded',
      'progress' => '0',
    ],
  ]), 'application/json', []);
  $response = json_decode($request, true);
  $this
    ->assertIdentical([
    'es',
  ], $response['result']['request_translations'], 'Spanish language has been requested after notification automatically.');
  $this
    ->goToContentBulkManagementForm();
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');

  // The node cache needs to be reset before reload.
  $node_storage
    ->resetCache(array(
    1,
  ));
  $node = $node_storage
    ->load(1);

  // Assert the content is imported.
  $this
    ->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
    ->getSourceStatus($node));

  // Assert the target is pending.
  $this
    ->assertIdentical(Lingotek::STATUS_PENDING, $content_translation_service
    ->getTargetStatus($node, 'es'));
  $this
    ->goToContentBulkManagementForm();

  // Simulate the notification of content successfully translated.
  $request = $this
    ->drupalPost(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',
    ],
  ]), 'application/json', []);
  $response = json_decode($request, true);
  $this
    ->verbose($request);
  $this
    ->assertTrue($response['result']['download'], 'Spanish language has been downloaded after notification automatically.');
  $this
    ->goToContentBulkManagementForm();
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');

  // The node cache needs to be reset before reload.
  $node_storage
    ->resetCache(array(
    1,
  ));
  $node = $node_storage
    ->load(1);

  // Assert the target is ready.
  $this
    ->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
    ->getTargetStatus($node, 'es'));
  $this
    ->goToContentBulkManagementForm();
}