View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Lingotek;
class LingotekFieldBodyTranslationTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
'field_ui',
];
protected $node;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$type = $this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
node_add_body_field($type);
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
$this
->saveLingotekConfigTranslationSettings([
'node_fields' => 'automatic',
]);
\Drupal::state()
->set('lingotek.uploaded_content_type', 'body');
}
public function testFieldTranslation() {
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/config/regional/config-translation');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink(t('Upload'));
$this
->assertText(t('Body uploaded successfully'));
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->verbose(var_export($data, TRUE));
$this
->assertTrue(array_key_exists('label', $data['field.field.node.article.body']));
$this
->assertTrue(array_key_exists('description', $data['field.field.node.article.body']));
$used_profile = \Drupal::state()
->get('lingotek.used_profile');
$this
->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');
$this
->clickLink(t('Check upload status'));
$this
->assertText('Body status checked successfully');
$this
->clickLink(t('Request translation'));
$this
->assertText(t('Translation to es_MX requested successfully'));
$this
->assertIdentical('es_MX', \Drupal::state()
->get('lingotek.added_target_locale'));
$this
->clickLink(t('Check Download'));
$this
->assertText(t('Translation to es_MX status checked successfully'));
$this
->clickLink('Download');
$this
->assertText(t('Translation to es_MX downloaded successfully'));
$basepath = \Drupal::request()
->getBasePath();
$assert_session
->linkByHrefExists($basepath . '/admin/structure/types/manage/article/fields/node.article.body/translate/es/edit');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('translation[config_names][field.field.node.article.body][label]', 'Cuerpo');
$this
->assertFieldByName('translation[config_names][field.field.node.article.body][description]', 'Cuerpo del contenido');
}
public function testEditedFieldBodyTranslation() {
$assert_session = $this
->assertSession();
$this
->testFieldTranslation();
ConfigurableLanguage::createFromLangcode('eu')
->setThirdPartySetting('lingotek', 'locale', 'eu_ES')
->save();
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->clickLink(t('Translate'));
$assert_session
->linkByHrefExists('/admin/lingotek/config/request/node_fields/node.article.body/eu_ES');
$assert_session
->linkByHrefNotExists('/admin/lingotek/config/request/node_fields/node.article.body/es_MX');
$this
->clickLink('Check Download');
$this
->assertText('Translation to es_MX status checked successfully');
$this
->clickLink('Download');
$this
->assertText('Translation to es_MX downloaded successfully');
}
public function testLanguageDisabled() {
$assert_session = $this
->assertSession();
$italian = ConfigurableLanguage::createFromLangcode('it')
->setThirdPartySetting('lingotek', 'locale', 'it_IT');
$italian
->save();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/admin/config/regional/config-translation');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink(t('Upload'));
$this
->assertText(t('Body uploaded successfully'));
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->verbose(var_export($data, TRUE));
$this
->assertEqual(2, count($data['field.field.node.article.body']));
$this
->assertTrue(array_key_exists('label', $data['field.field.node.article.body']));
$this
->assertTrue(array_key_exists('description', $data['field.field.node.article.body']));
$this
->assertIdentical('en_US', \Drupal::state()
->get('lingotek.uploaded_locale'));
$used_profile = \Drupal::state()
->get('lingotek.used_profile');
$this
->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
$assert_session
->linkByHrefExists('/admin/lingotek/config/request/node_fields/node.article.body/it_IT');
$assert_session
->linkByHrefExists('/admin/lingotek/config/request/node_fields/node.article.body/es_MX');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.body/translate/it/add');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.body/translate/es/add');
$lingotek_config = \Drupal::service('lingotek.configuration');
$lingotek_config
->disableLanguage($italian);
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$assert_session
->linkByHrefNotExists('/admin/lingotek/config/request/node_fields/node.article.body/it_IT');
$assert_session
->linkByHrefExists('/admin/lingotek/config/request/node_fields/node.article.body/es_MX');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.body/translate/it/add');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.body/translate/es/add');
}
public function testUploadingWithAnError() {
\Drupal::state()
->set('lingotek.must_error_in_upload', TRUE);
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body upload failed. Please try again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_error_in_upload', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
}
public function testUpdatingWithAnError() {
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
\Drupal::state()
->set('lingotek.must_error_in_upload', TRUE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents update failed. Please try again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_error_in_upload', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUpdatingWithAnErrorViaAutomaticUpload() {
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
\Drupal::state()
->set('lingotek.must_error_in_upload', TRUE);
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->assertText('The update for field_config Contents failed. Please try again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_error_in_upload', FALSE);
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUpdatingWithADocumentArchivedError() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
\Drupal::state()
->set('lingotek.must_document_archived_error_in_update', TRUE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Document Contents has been archived. Please upload again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_UNTRACKED, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_document_archived_error_in_update', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents uploaded successfully');
}
public function testUpdatingWithADocumentArchivedErrorViaAutomaticUpload() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
\Drupal::state()
->set('lingotek.must_document_archived_error_in_update', TRUE);
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->assertText('Document field_config Contents has been archived. Please upload again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_UNTRACKED, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_document_archived_error_in_update', FALSE);
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents uploaded successfully');
}
public function testUpdatingWithADocumentLockedError() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
\Drupal::state()
->set('lingotek.must_document_locked_error_in_update', TRUE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Document field_config Contents has a new version. The document id has been updated for all future interactions. Please try again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_EDITED, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_document_locked_error_in_update', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUpdatingWithADocumentLockedErrorViaAutomaticUpload() {
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
\Drupal::state()
->set('lingotek.must_document_locked_error_in_update', TRUE);
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->assertText('Document field_config Contents has a new version. The document id has been updated for all future interactions. Please try again.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_EDITED, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_document_locked_error_in_update', FALSE);
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUpdatingWithAPaymentRequiredError() {
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
\Drupal::state()
->set('lingotek.must_payment_required_error_in_update', TRUE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_payment_required_error_in_update', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUpdatingWithAPaymentRequiredErrorViaAutomaticUpload() {
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
$this
->clickLink('Check upload status');
$this
->assertText('Body status checked successfully');
\Drupal::state()
->set('lingotek.must_payment_required_error_in_update', TRUE);
$edit = [
'label' => 'Contents',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
$this
->assertText('Saved Contents configuration.');
$this
->assertText('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_payment_required_error_in_update', FALSE);
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Contents has been updated.');
}
public function testUploadingWithAnErrorViaAutomaticUpload() {
\Drupal::state()
->set('lingotek.must_error_in_upload', TRUE);
$this
->drupalGet('admin/lingotek/settings');
$edit = [
'label' => 'Excerpt',
'new_storage_type' => 'text',
'field_name' => 'excerpt',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/add-field', $edit, 'Save and continue');
$this
->assertText('The upload for field_config Excerpt failed. Please try again.');
$fieldConfig = FieldConfig::load('node.article.field_excerpt');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
}
public function testUploadingWithAPaymentRequiredError() {
\Drupal::state()
->set('lingotek.must_payment_required_error_in_upload', TRUE);
$this
->drupalGet('/admin/config/regional/config-translation/node_fields');
$this
->clickLink(t('Translate'));
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
$fieldConfig = FieldConfig::load('node.article.body');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
\Drupal::state()
->set('lingotek.must_payment_required_error_in_upload', FALSE);
$this
->clickLink('Upload');
$this
->checkForMetaRefresh();
$this
->assertText('Body uploaded successfully');
}
public function testUploadingWithAPaymentRequiredErrorViaAutomaticUpload() {
\Drupal::state()
->set('lingotek.must_payment_required_error_in_upload', TRUE);
$this
->drupalGet('admin/lingotek/settings');
$edit = [
'label' => 'Excerpt',
'new_storage_type' => 'text',
'field_name' => 'excerpt',
];
$this
->drupalPostForm('/admin/structure/types/manage/article/fields/add-field', $edit, 'Save and continue');
$this
->assertText('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
$fieldConfig = FieldConfig::load('node.article.field_excerpt');
$translation_service = \Drupal::service('lingotek.config_translation');
$source_status = $translation_service
->getSourceStatus($fieldConfig);
$this
->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
}
}