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;
use Drupal\node\Entity\NodeType;
class LingotekFieldBodyNotificationCallbackTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
'field_ui',
'dblog',
];
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('es')
->save();
\Drupal::state()
->set('lingotek.uploaded_content_type', 'body');
}
public function testAutomatedNotificationFieldTranslation() {
$this
->drupalLogin($this->rootUser);
$edit = [
'table[node_fields][enabled]' => 1,
'table[node_fields][profile]' => 'automatic',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$type = $this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
node_add_body_field($type);
$this
->goToConfigBulkManagementForm('node_fields');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('field_config')
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$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();
$field_storage = $this->container
->get('entity.manager')
->getStorage('field_config');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getSourceStatus($entity));
$this
->assertIdentical(Lingotek::STATUS_PENDING, $config_translation_service
->getTargetStatus($entity, 'es'));
$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
->assertTrue($response['result']['download'], 'Spanish language has been downloaded after notification automatically.');
$this
->goToConfigBulkManagementForm();
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
}
public function testManualNotificationContentTypeTranslation() {
$this
->drupalLogin($this->rootUser);
$edit = [
'table[node_fields][enabled]' => 1,
'table[node_fields][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();
node_add_body_field($type);
\Drupal::service('router.builder')
->rebuild();
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('field_config')
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_EDITED, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_fields');
$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();
$field_storage = $this->container
->get('entity.manager')
->getStorage('field_config');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$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();
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_READY, $config_translation_service
->getTargetStatus($entity, 'es'));
$this
->goToConfigBulkManagementForm();
$this
->clickLink('ES');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_CURRENT, $config_translation_service
->getTargetStatus($entity, 'es'));
}
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_fields][enabled]' => 1,
'table[node_fields][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();
node_add_body_field($type);
\Drupal::service('router.builder')
->rebuild();
$field_storage = $this->container
->get('entity.manager')
->getStorage('field_config');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$entity = \Drupal::entityManager()
->getStorage('field_config')
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_fields');
$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();
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$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();
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$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');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$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_fields][enabled]' => 1,
'table[node_fields][profile]' => 'automatic',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$type1 = entity_create('node_type', [
'type' => 'article',
'name' => 'Article',
]);
$status = $type1
->save();
node_add_body_field($type1);
$type2 = entity_create('node_type', [
'type' => 'page',
'name' => 'Page',
]);
$status = $type2
->save();
node_add_body_field($type2);
$this
->drupalLogin($this->rootUser);
$config_translation_service = \Drupal::service('lingotek.config_translation');
$field_storage = $this->container
->get('entity.manager')
->getStorage('field_config');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.article.body');
$this
->assertIdentical(Lingotek::STATUS_IMPORTING, $config_translation_service
->getSourceStatus($entity));
$this
->goToConfigBulkManagementForm('node_fields');
$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);
$field_storage = $this->container
->get('entity.manager')
->getStorage('field_config');
$field_storage
->resetCache();
$entity = $field_storage
->load('node.page.body');
$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.');
}
}