You are here

public function LingotekFieldBodyNotificationCallbackTest::testImportFailureWhileUpdating in Lingotek Translation 3.3.x

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

Tests that an import_failure callback is handled after document update.

File

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

Class

LingotekFieldBodyNotificationCallbackTest
Tests translating a content type using the notification callback.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testImportFailureWhileUpdating() {

  // 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'));

  // Edit the field.
  $edit = [
    'label' => 'Body EDITED',
  ];
  $this
    ->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
  $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 importing.
  $this
    ->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
    ->getSourceStatus($entity));

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

  // Assert the document id changed.
  $this
    ->assertIdentical($config_translation_service
    ->getDocumentId($entity), 'dummy-document-hash-id-1');

  // Simulate the notification of failed import document.
  $url = Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'prev_document_id' => 'dummy-document-hash-id',
      'document_id' => 'dummy-document-hash-id-1',
      'type' => 'import_failure',
    ],
  ])
    ->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 import for entity Body EDITED failed. Reverting dummy-document-hash-id-1 to previous id dummy-document-hash-id');
  $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 document id was restored.
  $this
    ->assertEquals($config_translation_service
    ->getDocumentId($entity), 'dummy-document-hash-id');
  $this
    ->assertIdentical(Lingotek::STATUS_ERROR, $config_translation_service
    ->getSourceStatus($entity));
}