View source
<?php
namespace Drupal\lingotek\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Lingotek;
use Drupal\lingotek\LingotekContentTranslationServiceInterface;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\VocabularyInterface;
use Drupal\taxonomy\Tests\TaxonomyTestTrait;
class LingotekUtilitiesDisassociateAllDocumentsTest extends LingotekTestBase {
use TaxonomyTestTrait;
protected $vocabulary;
public static $modules = [
'node',
'taxonomy',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'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::entityManager()
->clearCachedDefinitions();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
$this
->rebuildContainer();
$edit = [
'node[article][enabled]' => 1,
'node[article][profiles]' => 'manual',
'node[article][fields][title]' => 1,
'node[article][fields][body]' => 1,
"taxonomy_term[{$vocabulary_id}][enabled]" => 1,
"taxonomy_term[{$vocabulary_id}][profiles]" => 'manual',
"taxonomy_term[{$vocabulary_id}][fields][name]" => 1,
"taxonomy_term[{$vocabulary_id}][fields][description]" => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => 'manual',
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$this
->translateNodeWithLinks();
$this
->translateTermWithLinks();
$this
->translateSystemSiteConfig();
$this
->translateArticleContentType();
}
public function translateNodeWithLinks() {
$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'));
$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 = array();
$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('admin/lingotek/settings', [], 'Disassociate');
$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::entityManager()
->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 testDisassociateOrphanContent() {
\Drupal::database()
->insert('lingotek_content_metadata')
->fields([
'document_id',
'entity_type',
'entity_id',
])
->values([
'document_id' => 'a_document_id',
'entity_type' => 'node',
'entity_id' => 1,
])
->execute();
$this
->drupalGet('/admin/lingotek/settings');
$this
->drupalPostForm('admin/lingotek/settings', [], 'Disassociate');
}
}