View source
<?php
namespace Drupal\lingotek\Tests;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Entity\LingotekProfile;
use Drupal\lingotek\Lingotek;
use Drupal\lingotek\LingotekConfigurationServiceInterface;
use Drupal\lingotek\LingotekContentTranslationServiceInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
class LingotekNodeNotificationCallbackTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
];
protected $node;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ConfigurableLanguage::createFromLangcode('es')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
drupal_static_reset();
\Drupal::entityManager()
->clearCachedDefinitions();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
$this
->rebuildContainer();
$edit = [
'node[article][enabled]' => 1,
'node[article][profiles]' => 'automatic',
'node[article][fields][title]' => 1,
'node[article][fields][body]' => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
}
public function testAutomatedNotificationNodeTranslation() {
$this
->drupalLogin($this->rootUser);
$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'));
$node = Node::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $content_translation_service
->getSourceStatus($node));
$this
->goToContentBulkManagementForm();
$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');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getSourceStatus($node));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->goToContentBulkManagementForm();
$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');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->goToContentBulkManagementForm();
}
public function testPhaseNotificationNodeTranslation() {
$this
->drupalLogin($this->rootUser);
$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'));
$node = Node::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $content_translation_service
->getSourceStatus($node));
$this
->goToContentBulkManagementForm();
\Drupal::state()
->set('lingotek.document_completion', FALSE);
$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' => 'phase',
'progress' => '100',
],
]), 'application/json', []);
$response = json_decode($request, true);
$this
->assertTrue($response['result']['download'], 'Spanish language has been downloaded after notification automatically.');
$this
->goToContentBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getSourceStatus($node));
$this
->assertIdentical(Lingotek::STATUS_INTERMEDIATE, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->drupalGet('node/1/translations');
$this
->assertLink('Las llamas son chulas');
\Drupal::state()
->set('lingotek.document_completion', TRUE);
$this
->goToContentBulkManagementForm();
$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');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->goToContentBulkManagementForm();
}
public function testManualNotificationNodeTranslation() {
$this
->drupalLogin($this->rootUser);
$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'] = 'manual';
$this
->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$node = Node::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$this
->assertIdentical(Lingotek::STATUS_EDITED, $content_translation_service
->getSourceStatus($node));
$this
->goToContentBulkManagementForm();
$this
->clickLink('EN');
$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([], $response['result']['request_translations'], 'No translations has been requested after notification automatically.');
$this
->goToContentBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getSourceStatus($node));
$this
->assertIdentical(Lingotek::STATUS_REQUEST, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$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.');
$this
->goToContentBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_READY, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'es'));
}
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();
$this
->drupalLogin($this->rootUser);
$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'));
$node = Node::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $content_translation_service
->getSourceStatus($node));
$this
->goToContentBulkManagementForm();
$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');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getSourceStatus($node));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $content_translation_service
->getTargetStatus($node, 'de'));
$this
->goToContentBulkManagementForm();
$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');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_READY, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'de'));
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$node_storage
->resetCache(array(
1,
));
$node = $node_storage
->load(1);
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'es'));
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $content_translation_service
->getTargetStatus($node, 'de'));
}
public function testDisabledLanguagesAreNotRequested() {
$italian = ConfigurableLanguage::createFromLangcode('it');
$italian
->save();
$this
->drupalLogin($this->rootUser);
$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'));
$node = Node::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $content_translation_service
->getSourceStatus($node));
$this
->goToContentBulkManagementForm();
$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([
'it',
'es',
], $response['result']['request_translations'], 'Spanish and Italian languages have been requested after notification automatically.');
$lingotek_config = \Drupal::service('lingotek.configuration');
$lingotek_config
->disableLanguage($italian);
$edit = array();
$edit['title[0][value]'] = 'Llamas are cool 2';
$edit['body[0][value]'] = 'Llamas are very cool 2';
$edit['langcode[0][value]'] = 'en';
$edit['lingotek_translation_profile'] = 'automatic';
$this
->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$request = $this
->drupalPost(Url::fromRoute('lingotek.notify', [], [
'query' => [
'project_id' => 'test_project',
'document_id' => 'dummy-document-hash-id-1',
'complete' => 'false',
'type' => 'document_uploaded',
'progress' => '0',
],
]), 'application/json', []);
$response = json_decode($request, true);
$this
->assertIdentical([
'es',
], $response['result']['request_translations'], 'Italian language has not been requested after notification automatically because it is disabled.');
}
}