View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Entity\LingotekContentMetadata;
use Drupal\lingotek\Lingotek;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class LingotekUtilitiesDisassociateAllDocumentsTest extends LingotekTestBase {
use TaxonomyTestTrait;
protected $vocabulary;
public static $modules = [
'node',
'taxonomy',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->vocabulary = $this
->createVocabulary();
$vocabulary_id = $this->vocabulary
->id();
ConfigurableLanguage::createFromLangcode('es')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary_id)
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('taxonomy_term', $vocabulary_id, TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'manual',
'fields' => [
'title' => 1,
'body' => 1,
],
],
],
'taxonomy_term' => [
$vocabulary_id => [
'profiles' => 'manual',
'fields' => [
'name' => 1,
'description' => 1,
],
],
],
]);
$this
->saveLingotekConfigTranslationSettings([
'node_type' => 'manual',
]);
$this
->translateNodeWithLinks();
$this
->translateTermWithLinks();
$this
->translateSystemSiteConfig();
$this
->translateArticleContentType();
}
public function translateNodeWithLinks() {
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['body[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$edit['lingotek_translation_management[lingotek_translation_profile]'] = 'manual';
$this
->saveAndPublishNodeForm($edit);
$this
->goToContentBulkManagementForm();
$this
->clickLink('EN');
$this
->clickLink('EN');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
}
public function translateTermWithLinks() {
\Drupal::state()
->set('lingotek.uploaded_content_type', 'taxonomy_term');
$bundle = $this->vocabulary
->id();
$edit = [];
$edit['name[0][value]'] = 'Llamas are cool';
$edit['description[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$this
->drupalPostForm("admin/structure/taxonomy/manage/{$bundle}/add", $edit, t('Save'));
$this
->goToContentBulkManagementForm('taxonomy_term');
$this
->clickLink('EN');
$this
->clickLink('EN');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
}
public function translateSystemSiteConfig() {
\Drupal::state()
->set('lingotek.uploaded_content_type', 'system.site');
$this
->goToConfigBulkManagementForm();
$this
->clickLink('EN', 1);
$this
->clickLink('EN', 1);
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
}
public function translateArticleContentType() {
\Drupal::state()
->set('lingotek.uploaded_content_type', 'content_type');
$this
->goToConfigBulkManagementForm('node_type');
$this
->clickLink('EN');
$this
->clickLink('EN');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
}
public function testDisassociateAllDocuments() {
$this
->drupalGet('/admin/lingotek/settings');
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertRaw("Are you sure you want to disassociate everything from Lingotek?");
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertText('All translations have been disassociated.');
$node = Node::load(1);
$term = Term::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$this
->assertNull($content_translation_service
->getDocumentId($node), 'The node has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service
->getSourceStatus($node));
$this
->assertNull($content_translation_service
->getDocumentId($term), 'The term has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service
->getSourceStatus($term));
$article_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load('article');
$this
->assertNull($config_translation_service
->getDocumentId($article_type), 'The article node type has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service
->getSourceStatus($article_type));
$mappers = \Drupal::service('plugin.manager.config_translation.mapper')
->getMappers();
$mapper = $mappers['system.site_information_settings'];
$this
->assertNull($config_translation_service
->getConfigDocumentId($mapper), 'The system.site config mapper has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service
->getConfigSourceStatus($mapper));
$this
->goToContentBulkManagementForm('node');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 1, 'The node shows as untracked');
$this
->goToContentBulkManagementForm('taxonomy_term');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 1, 'The taxonomy term shows as untracked');
$this
->goToConfigBulkManagementForm('config');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 4, 'The configuration shows as untracked');
$this
->goToConfigBulkManagementForm('node_type');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 1, 'The article type shows as untracked');
}
public function testDisassociateAllDocumentsWithCancelledDocuments() {
$this
->createAndCancelANode();
$this
->goToContentBulkManagementForm('node');
$this
->assertSourceStatus('EN', Lingotek::STATUS_CURRENT);
$this
->assertSourceStatus('EN', Lingotek::STATUS_CANCELLED);
$this
->createAndCancelATerm();
$this
->goToContentBulkManagementForm('taxonomy_term');
$this
->assertSourceStatus('EN', Lingotek::STATUS_CURRENT);
$this
->assertSourceStatus('EN', Lingotek::STATUS_CANCELLED);
$this
->createAndCancelANodeType();
$this
->goToConfigBulkManagementForm('node_type');
$this
->assertSourceStatus('EN', Lingotek::STATUS_CURRENT);
$this
->assertSourceStatus('EN', Lingotek::STATUS_CANCELLED);
$this
->createAndCancelAConfig();
$this
->goToConfigBulkManagementForm('node_type');
$this
->assertSourceStatus('EN', Lingotek::STATUS_CURRENT);
$this
->assertSourceStatus('EN', Lingotek::STATUS_CANCELLED);
$this
->drupalGet('/admin/lingotek/settings');
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertRaw("Are you sure you want to disassociate everything from Lingotek?");
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertText('All translations have been disassociated.');
$node = Node::load(1);
$term = Term::load(1);
$content_translation_service = \Drupal::service('lingotek.content_translation');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$this
->assertNull($content_translation_service
->getDocumentId($node), 'The node has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service
->getSourceStatus($node));
$this
->assertNull($content_translation_service
->getDocumentId($term), 'The term has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service
->getSourceStatus($term));
$article_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load('article');
$this
->assertNull($config_translation_service
->getDocumentId($article_type), 'The article node type has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service
->getSourceStatus($article_type));
$mappers = \Drupal::service('plugin.manager.config_translation.mapper')
->getMappers();
$mapper = $mappers['system.site_information_settings'];
$this
->assertNull($config_translation_service
->getConfigDocumentId($mapper), 'The system.site config mapper has been disassociated from its Lingotek Document ID');
$this
->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service
->getConfigSourceStatus($mapper));
$this
->goToContentBulkManagementForm('node');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 2, 'The nodes show as untracked');
$this
->goToContentBulkManagementForm('taxonomy_term');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 2, 'The taxonomy term shows as untracked');
$this
->goToConfigBulkManagementForm('config');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 4, 'The configuration shows as untracked');
$this
->goToConfigBulkManagementForm('node_type');
$this
->assertSourceStatusStateCount(Lingotek::STATUS_UNTRACKED, 'EN', 2, 'The article type shows as untracked');
}
public function testDisassociateOrphanContent() {
$metadata = LingotekContentMetadata::create();
$metadata
->setDocumentId('a_document_id');
$metadata
->setContentEntityTypeId('node');
$metadata
->setContentEntityId(1);
$metadata
->save();
$this
->drupalGet('/admin/lingotek/settings');
$this
->drupalPostForm('admin/lingotek/settings', [], 'Disassociate');
$this
->assertRaw("Are you sure you want to disassociate everything from Lingotek?");
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertText('All translations have been disassociated.');
$metadata = LingotekContentMetadata::create();
$metadata
->setDocumentId('a_document_id');
$metadata
->setContentEntityTypeId(NULL);
$metadata
->setContentEntityId(NULL);
$metadata
->save();
$this
->drupalGet('/admin/lingotek/settings');
$this
->drupalPostForm('admin/lingotek/settings', [], 'Disassociate');
$this
->assertRaw("Are you sure you want to disassociate everything from Lingotek?");
$this
->drupalPostForm(NULL, [], 'Disassociate');
$this
->assertText('All translations have been disassociated.');
}
protected function createAndCancelANode() {
$edit = [];
$edit['title[0][value]'] = 'Dogs are cool';
$edit['body[0][value]'] = 'Dogs are very cool';
$edit['langcode[0][value]'] = 'en';
$edit['lingotek_translation_management[lingotek_translation_profile]'] = 'automatic';
$this
->saveAndPublishNodeForm($edit);
$this
->goToContentBulkManagementForm('node');
$edit = [
'table[2]' => TRUE,
$this
->getBulkOperationFormName() => $this
->getBulkOperationNameForCancel('node'),
];
$this
->drupalPostForm(NULL, $edit, $this
->getApplyActionsButtonLabel());
}
protected function createAndCancelATerm() {
$bundle = $this->vocabulary
->id();
$edit = [];
$edit['name[0][value]'] = 'Llamas are cool';
$edit['description[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$this
->drupalPostForm("admin/structure/taxonomy/manage/{$bundle}/add", $edit, t('Save'));
$this
->goToContentBulkManagementForm('taxonomy_term');
$edit = [
'table[2]' => TRUE,
$this
->getBulkOperationFormName() => $this
->getBulkOperationNameForCancel('taxonomy_term'),
];
$this
->drupalPostForm(NULL, $edit, $this
->getApplyActionsButtonLabel());
}
protected function createAndCancelANodeType() {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
$this
->goToConfigBulkManagementForm('node_type');
$edit = [
'table[page]' => TRUE,
$this
->getBulkOperationFormName() => $this
->getBulkOperationNameForCancel('node_type'),
];
$this
->drupalPostForm(NULL, $edit, $this
->getApplyActionsButtonLabel());
}
protected function createAndCancelAConfig() {
$this
->goToConfigBulkManagementForm('config');
$this
->clickLink('EN', 0);
$this
->clickLink('EN', 0);
$edit = [
'table[system.site_maintenance_mode]' => TRUE,
$this
->getBulkOperationFormName() => $this
->getBulkOperationNameForCancel('config'),
];
$this
->drupalPostForm(NULL, $edit, $this
->getApplyActionsButtonLabel());
}
}