You are here

public function LingotekContentTypeNotificationCallbackTest::testManualNotificationContentTypeTranslation in Lingotek Translation 8

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

File

src/Tests/LingotekContentTypeNotificationCallbackTest.php, line 125

Class

LingotekContentTypeNotificationCallbackTest
Tests translating a content type using the notification callback.

Namespace

Drupal\lingotek\Tests

Code

public function testManualNotificationContentTypeTranslation() {

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

  // Enable translation for the current entity type and ensure the change is
  // picked up.
  $edit = [
    'table[node_type][enabled]' => 1,
    'table[node_type][profile]' => 'manual',
  ];
  $this
    ->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');

  // Create Article node types.
  // We cannot use drupalCreateContentType(), as it asserts that the last entity
  // created returns SAVED_NEW, but it will return SAVED_UPDATED as we will
  // save the third party settings.
  $type = entity_create('node_type', [
    'type' => 'article',
    'name' => 'Article',
  ]);
  $status = $type
    ->save();
  \Drupal::service('router.builder')
    ->rebuild();

  /** @var LingotekConfigTranslationServiceInterface $config_translation_service */
  $config_translation_service = \Drupal::service('lingotek.config_translation');
  $entity = \Drupal::entityManager()
    ->getStorage('node_type')
    ->load('article');

  // Assert the content is edited, but not auto-uploaded.
  $this
    ->assertIdentical(Lingotek::STATUS_EDITED, $config_translation_service
    ->getSourceStatus($entity));

  // Go to the bulk config management page.
  $this
    ->goToConfigBulkManagementForm('node_type');

  // Clicking English must init the upload of content.
  $this
    ->clickLink('EN');

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

  // Translations are not requested.
  $this
    ->assertIdentical([], $response['result']['request_translations'], 'No translations has been requested after notification automatically.');

  // Go to the bulk config management page.
  $this
    ->goToConfigBulkManagementForm();

  /** @var ConfigEntityStorageInterface $node_storage */
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node_type');

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

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

  // Assert the target is ready to be requested.
  $this
    ->assertIdentical(Lingotek::STATUS_REQUEST, $config_translation_service
    ->getTargetStatus($entity, 'es'));

  // Go to the bulk config management page.
  $this
    ->goToConfigBulkManagementForm();

  // Request a translation.
  $this
    ->clickLink('ES');

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

  // Go to the bulk config management page.
  $this
    ->goToConfigBulkManagementForm();

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

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

  // Go to the bulk config management page.
  $this
    ->goToConfigBulkManagementForm();

  // Download the translation.
  $this
    ->clickLink('ES');

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

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