You are here

public function LingotekNodeNotificationCallbackTest::testProfileTargetOverridesNotificationNodeTranslation in Lingotek Translation 8

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

File

src/Tests/LingotekNodeNotificationCallbackTest.php, line 327

Class

LingotekNodeNotificationCallbackTest
Tests translating a node using the notification callback.

Namespace

Drupal\lingotek\Tests

Code

public function testProfileTargetOverridesNotificationNodeTranslation() {
  $profile = LingotekProfile::create([
    'id' => 'profile2',
    'label' => 'Profile with overrides',
    'auto_upload' => TRUE,
    'auto_download' => TRUE,
    'language_overrides' => [
      'es' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_download' => FALSE,
        ],
      ],
    ],
  ]);
  $profile
    ->save();
  ConfigurableLanguage::createFromLangcode('de')
    ->save();

  // 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'] = 'profile2';
  $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([
    'de',
    'es',
  ], $response['result']['request_translations'], 'Spanish and German 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
    ->assertIdentical(Lingotek::STATUS_PENDING, $content_translation_service
    ->getTargetStatus($node, 'de'));
  $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
    ->assertFalse($response['result']['download'], 'No translations has been downloaded after notification automatically.');
  $request = $this
    ->drupalPost(Url::fromRoute('lingotek.notify', [], [
    'query' => [
      'project_id' => 'test_project',
      'document_id' => 'dummy-document-hash-id',
      'locale_code' => 'de-DE',
      'locale' => 'de_DE',
      'complete' => 'true',
      'type' => 'target',
      'progress' => '100',
    ],
  ]), 'application/json', []);
  $response = json_decode($request, true);
  $this
    ->verbose($request);
  $this
    ->assertTrue($response['result']['download'], 'German 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_READY, $content_translation_service
    ->getTargetStatus($node, 'es'));
  $this
    ->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
    ->getTargetStatus($node, 'de'));

  // Go to the bulk node management page and download them.
  $this
    ->goToContentBulkManagementForm();
  $this
    ->clickLink('ES');

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

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