You are here

public function LingotekFieldBodyNotificationCallbackTest::testTargetCancelledAfterUploading in Lingotek Translation 3.5.x

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

Tests that a target_cancelled callback is handled after document upload.

File

tests/src/Functional/LingotekFieldBodyNotificationCallbackTest.php, line 1414

Class

LingotekFieldBodyNotificationCallbackTest
Tests translating a content type using the notification callback.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testTargetCancelledAfterUploading() {

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

  // Enable translation for the current entity type and ensure the change is
  // picked up.
  $this
    ->saveLingotekConfigTranslationSettings([
    'node_fields' => 'automatic',
  ]);

  // Create Article node types.
  $type = $this
    ->drupalCreateContentType([
    'type' => 'article',
    'name' => 'Article',
  ]);
  node_add_body_field($type);

  /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $field_storage */
  $field_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('field_config');

  // The node cache needs to be reset before reload.
  $field_storage
    ->resetCache();
  $entity = $field_storage
    ->load('node.article.body');

  /** @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $config_translation_service */
  $config_translation_service = \Drupal::service('lingotek.config_translation');

  // Assert the content is importing.
  $this
    ->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
    ->getSourceStatus($entity));
  $this
    ->assertIdentical($config_translation_service
    ->getDocumentId($entity), 'dummy-document-hash-id');
  $this
    ->goToConfigBulkManagementForm('node_fields');

  // 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,
  ]);
  $response = json_decode($request
    ->getBody(), TRUE);
  $this
    ->verbose($request);
  $this
    ->assertIdentical([
    'es',
  ], $response['result']['request_translations'], 'Spanish language has been requested after notification automatically.');
  $this
    ->goToConfigBulkManagementForm('node_fields');

  // The node cache needs to be reset before reload.
  $field_storage
    ->resetCache();
  $entity = $field_storage
    ->load('node.article.body');

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

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

  // Simulate the notification of document_cancelled document.
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'type' => 'target_cancelled',
      'locale' => 'es_ES',
    ],
  ])
    ->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
    ->assertIdentical($response['messages'][0], 'Document Body target es_ES cancelled in TMS.');
  $this
    ->goToConfigBulkManagementForm('node_fields');

  // The node cache needs to be reset before reload.
  $field_storage
    ->resetCache();
  $entity = $field_storage
    ->load('node.article.body');
  $this
    ->assertIdentical('dummy-document-hash-id', $config_translation_service
    ->getDocumentId($entity));
  $this
    ->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
    ->getSourceStatus($entity));
  $this
    ->assertIdentical(Lingotek::STATUS_CANCELLED, $config_translation_service
    ->getTargetStatus($entity, 'es'));
}