diff --git a/src/Tests/LingotekUtilitiesDisassociateAllDocumentsTest.php b/src/Tests/LingotekUtilitiesDisassociateAllDocumentsTest.php new file mode 100644 index 0000000..5943b9f --- /dev/null +++ b/src/Tests/LingotekUtilitiesDisassociateAllDocumentsTest.php @@ -0,0 +1,220 @@ +drupalCreateContentType(array( + 'type' => 'article', + 'name' => 'Article' + )); + + $this->vocabulary = $this->createVocabulary(); + $vocabulary_id = $this->vocabulary->id(); + + // Add a language. + ConfigurableLanguage::createFromLangcode('es')->save(); + + // Enable translation for the articles and our vocabulary and ensure the + // change is picked up. + 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(); + // Rebuild the container so that the new languages are picked up by services + // that hold a list of languages. + $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() { + // Create a node. + $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(); + + // Clicking English must init the upload of content. + $this->clickLink('English'); + // There is a link for checking status. + $this->clickLink('English'); + // Request the Spanish translation. + $this->clickLink('ES'); + // Check status of the Spanish translation. + $this->clickLink('ES'); + // Download the Spanish translation. + $this->clickLink('ES'); + } + + public function translateTermWithLinks() { + // This is a hack for avoiding writing different lingotek endpoint mocks. + \Drupal::state()->set('lingotek.uploaded_content_type', 'taxonomy_term'); + + $bundle = $this->vocabulary->id(); + + // Create a term. + $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'); + + // Clicking English must init the upload of content. + $this->clickLink('English'); + // There is a link for checking status. + $this->clickLink('English'); + // Request the Spanish translation. + $this->clickLink('ES'); + // Check status of the Spanish translation. + $this->clickLink('ES'); + // Download the Spanish translation. + $this->clickLink('ES'); + } + + + public function translateSystemSiteConfig() { + // This is a hack for avoiding writing different lingotek endpoint mocks. + \Drupal::state()->set('lingotek.uploaded_content_type', 'system.site'); + + // Go to the bulk config management page. + $this->goToConfigBulkManagementForm(); + // Clicking English must init the upload of content. + $this->clickLink('English', 1); + // There is a link for checking status. + $this->clickLink('English', 1); + // Request the Spanish translation. + $this->clickLink('ES'); + // Check status of the Spanish translation. + $this->clickLink('ES'); + // Download the Chinese translation. + $this->clickLink('ES'); + } + + public function translateArticleContentType() { + // This is a hack for avoiding writing different lingotek endpoint mocks. + \Drupal::state()->set('lingotek.uploaded_content_type', 'content_type'); + + // Go to the bulk config management page. + $this->goToConfigBulkManagementForm('node_type'); + + // Clicking English must init the upload of content. + $this->clickLink('English'); + // There is a link for checking status. + $this->clickLink('English'); + // Request the Spanish translation. + $this->clickLink('ES'); + // Check status of the Spanish translation. + $this->clickLink('ES'); + // Download the Spanish translation. + $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); + + /** @var LingotekContentTranslationServiceInterface $content_translation_service */ + $content_translation_service = \Drupal::service('lingotek.content_translation'); + /** @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $config_translation_service */ + $config_translation_service = \Drupal::service('lingotek.config_translation'); + + // Ensure we have disassociated the node. + $this->assertNull($content_translation_service->getDocumentId($node)); + $this->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service->getSourceStatus($node)); + + // Ensure we have disassociated the term. + $this->assertNull($content_translation_service->getDocumentId($term)); + $this->assertIdentical(Lingotek::STATUS_UNTRACKED, $content_translation_service->getSourceStatus($term)); + + // ToDo: We should disassociate config files too. + // Ensure we are disassociated the article type. +// $article_type = \Drupal::entityManager()->getStorage('node_type')->load('article'); +// $this->assertNull($config_translation_service->getDocumentId($article_type)); +// $this->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service->getSourceStatus($article_type)); + + // Ensure we are disassociated the config system.site. +// $mappers = \Drupal::service('plugin.manager.config_translation.mapper')->getMappers(); +// $mapper = $mappers['system.site_information_settings']; +// $this->assertNull($config_translation_service->getConfigDocumentId($mapper)); +// $this->assertIdentical(Lingotek::STATUS_UNTRACKED, $config_translation_service->getConfigSourceStatus($mapper)); + + // Just for visual debugging. + // ToDo: Add asserts? +// $this->goToContentBulkManagementForm('node'); +// $this->goToContentBulkManagementForm('taxonomy_term'); +// $this->goToConfigBulkManagementForm(); +// $this->goToConfigBulkManagementForm('node_type'); + + } + + +}