View source
<?php
namespace Drupal\lingotek\Tests;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Entity\LingotekProfile;
use Drupal\lingotek\Lingotek;
use Drupal\lingotek\LingotekContentTranslationServiceInterface;
class LingotekContentTypeNotificationCallbackTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
];
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('es')
->save();
\Drupal::state()
->set('lingotek.uploaded_content_type', 'content_type');
}
public function testAutomatedNotificationContentTypeTranslation() {
$this
->drupalLogin($this->rootUser);
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'automatic',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$this
->drupalPostForm('/admin/structure/types/add', [
'type' => 'article',
'name' => 'Article',
], 'Save content type');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('article');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_type');
$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
->verbose($request);
$this
->assertIdentical([
'es',
], $response['result']['request_translations'], 'Spanish language has been requested after notification automatically.');
$this
->goToConfigBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node_type');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getSourceStatus($entity));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $config_translation_service
->getTargetStatus($entity, '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
->goToConfigBulkManagementForm();
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
}
public function testManualNotificationContentTypeTranslation() {
$this
->drupalLogin($this->rootUser);
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'manual',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$type = entity_create('node_type', [
'type' => 'article',
'name' => 'Article',
]);
$status = $type
->save();
\Drupal::service('router.builder')
->rebuild();
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('article');
$this
->assertIdentical(Lingotek::STATUS_EDITED, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_type');
$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
->goToConfigBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node_type');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getSourceStatus($entity));
$this
->assertIdentical(Lingotek::STATUS_REQUEST, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
$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
->goToConfigBulkManagementForm();
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_READY, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
$this
->clickLink('ES');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
}
public function testPhaseNotificationContentTypeTranslation() {
$this
->drupalLogin($this->rootUser);
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'automatic',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$this
->drupalPostForm('/admin/structure/types/add', [
'type' => 'article',
'name' => 'Article',
], 'Save content type');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('article');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_type');
\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
->goToConfigBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node_type');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getSourceStatus($entity));
$this
->assertIdentical(Lingotek::STATUS_INTERMEDIATE, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->drupalGet('admin/structure/types/manage/article/translate');
$this
->assertLinkByHref('admin/structure/types/manage/article/translate/es/edit');
$this
->goToConfigBulkManagementForm();
\Drupal::state()
->set('lingotek.document_completion', TRUE);
$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
->goToConfigBulkManagementForm();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node_type');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
}
public function testProfileTargetOverridesNotificationContentTypeTranslation() {
$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 = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'profile2',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$type = entity_create('node_type', [
'type' => 'article',
'name' => 'Article',
]);
$status = $type
->save();
\Drupal::service('router.builder')
->rebuild();
$node_storage = $this->container
->get('entity.manager')
->getStorage('node_type');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('article');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_type');
$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
->goToConfigBulkManagementForm();
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getSourceStatus($entity));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $config_translation_service
->getTargetStatus($entity, 'de'));
$this
->goToConfigBulkManagementForm();
$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
->goToConfigBulkManagementForm();
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_READY, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'de'));
$this
->goToConfigBulkManagementForm();
$this
->clickLink('ES');
$node_storage
->resetCache();
$entity = $node_storage
->load('article');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'de'));
}
public function testDisabledLanguagesAreNotRequested() {
$italian = ConfigurableLanguage::createFromLangcode('it');
$italian
->save();
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'automatic',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$this
->drupalPostForm('/admin/structure/types/add', [
'type' => 'article',
'name' => 'Article',
], 'Save content type');
$this
->drupalPostForm('/admin/structure/types/add', [
'type' => 'page',
'name' => 'Page',
], 'Save content type');
$this
->drupalLogin($this->rootUser);
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('article');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_type');
$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);
$entity = \Drupal::entityManager()
->getStorage('node_type')
->load('page');
$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.');
}
}