View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Lingotek;
use Drupal\workflows\Entity\Workflow;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class LingotekContentModerationTest extends LingotekTestBase {
use ContentModerationTestTrait;
public static $modules = [
'block',
'node',
'content_moderation',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block', [
'region' => 'content',
'weight' => -5,
]);
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'content',
'weight' => -10,
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'page', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
$workflow = $this
->createEditorialWorkflow();
$this
->enableModerationThroughUI('article');
$this
->addReviewStateToEditorialWorkflow();
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'automatic',
'fields' => [
'title' => 1,
'body' => 1,
],
'moderation' => [
'upload_status' => 'draft',
'download_transition' => 'request_review',
],
],
'page' => [
'profiles' => 'automatic',
'fields' => [
'title' => 1,
'body' => 1,
],
],
],
]);
}
public function testCreateEntityWithAutomaticProfileButNotInUploadState() {
$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]'] = 'automatic';
$this
->saveAsRequestReviewNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testCreateEntityWithManualProfileButNotInUploadState() {
$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
->saveAsRequestReviewNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testCreateEntityWithAutomaticProfileAndInUploadState() {
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->assertText('Llamas are cool sent to Lingotek successfully.');
}
public function testCreateEntityWithManualProfileAndInUploadState() {
$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
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testUpdateEntityWithAutomaticProfileButNotInUploadState() {
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->editAsRequestReviewNodeForm('/node/1/edit', $edit);
$this
->assertText('Article Llamas are cool has been updated.');
$this
->assertNoText('Llamas are cool was updated and sent to Lingotek successfully.');
}
public function testUpdateEntityWithManualProfileButNotInUploadState() {
$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
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->editAsRequestReviewNodeForm('/node/1/edit', $edit);
$this
->assertText('Article Llamas are cool has been updated.');
$this
->assertNoText('Llamas are cool was updated and sent to Lingotek successfully.');
}
public function testUpdateEntityWithAutomaticProfileAndInUploadState() {
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$edit['body[0][value]'] = 'Llamas are very cool!';
$this
->editAsNewDraftNodeForm('/node/1/edit', $edit);
$this
->assertText('Article Llamas are cool has been updated.');
$this
->assertText('Llamas are cool was updated and sent to Lingotek successfully.');
}
public function testUpdateEntityWithManualProfileAndInUploadState() {
$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
->saveAsNewDraftNodeForm($edit, 'article');
$this
->assertText('Article Llamas are cool has been created.');
$this
->editAsNewDraftNodeForm('/node/1/edit', $edit);
$this
->assertText('Article Llamas are cool has been updated.');
$this
->assertNoText('Llamas are cool was updated and sent to Lingotek successfully.');
}
protected function configureNeedsReviewAsUploadState() {
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'automatic',
'fields' => [
'title' => 1,
'body' => 1,
],
'moderation' => [
'upload_status' => 'needs_review',
'download_transition' => 'publish',
],
],
],
]);
}
public function testModerationToUploadStateWithAutomaticProfileTriggersUpload() {
$this
->configureNeedsReviewAsUploadState();
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$edit = [
'new_state' => 'needs_review',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$this
->assertText('The moderation state has been updated.');
$this
->assertText('Llamas are cool sent to Lingotek successfully.');
}
public function testModerationToNonUploadStateWithAutomaticProfileDoesntTriggerUpload() {
$this
->configureNeedsReviewAsUploadState();
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$edit = [
'new_state' => 'published',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$this
->assertText('The moderation state has been updated.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testModerationToUploadStateWithManualProfileDoesntTriggerUpload() {
$this
->configureNeedsReviewAsUploadState();
$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
->saveAsNewDraftNodeForm($edit, 'article');
$edit = [
'new_state' => 'needs_review',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$this
->assertText('The moderation state has been updated.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testModerationToNonUploadStateWithManualProfileDoesntTriggerUpload() {
$this
->configureNeedsReviewAsUploadState();
$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
->saveAsNewDraftNodeForm($edit, 'article');
$edit = [
'new_state' => 'published',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$this
->assertText('The moderation state has been updated.');
$this
->assertNoText('Llamas are cool sent to Lingotek successfully.');
}
public function testDownloadFromUploadStateTriggersATransition() {
$this
->configureNeedsReviewAsUploadState();
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEqual($value, 'Draft', 'Workbench current status is draft');
$edit = [
'new_state' => 'needs_review',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEqual($value, 'Needs Review', 'Workbench current status is Needs Review');
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('Llamas are cool');
$this
->assertNoFieldByName('new_state', 'The transition to a new content moderation status happened (so no moderation form is shown).');
}
public function testDownloadWhenContentModerationWasSetupAfterLingotek() {
$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]'] = 'automatic';
$this
->saveAndPublishNodeForm($edit, 'page');
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$this
->enableModerationThroughUI('page');
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->assertTargetStatus('ES', Lingotek::STATUS_CURRENT);
}
public function testDownloadWithInvalidTransition() {
$this
->configureNeedsReviewAsUploadState();
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$this
->config('lingotek.settings')
->set('translate.entity.node.article.content_moderation.download_transition', 'invalid_transition')
->save();
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEquals($value, 'Draft', 'Workbench current status is draft');
$edit = [
'new_state' => 'needs_review',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEquals($value, 'Needs Review', 'Workbench current status is Needs Review');
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->assertText('The translation of node Llamas are cool into es_MX has been downloaded.');
$this
->clickLink('Llamas are cool');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEquals($value, 'Needs Review', 'Content moderation current status is Needs Review');
}
public function testDownloadFromNotUploadStateDoesntTriggerATransition() {
\Drupal::state()
->set('lingotek.uploaded_content_type', 'node+revision');
$this
->configureNeedsReviewAsUploadState();
$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]'] = 'automatic';
$this
->saveAsNewDraftNodeForm($edit, 'article');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEqual($value, 'Draft', 'Workbench current status is draft');
$edit = [
'new_state' => 'needs_review',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$edit = [
'new_state' => 'draft',
];
$this
->drupalPostForm(NULL, $edit, 'Apply');
$this
->goToContentBulkManagementForm();
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('ES');
$this
->clickLink('Llamas are cool');
$value = $this
->xpath('//div[@id="edit-current"]/text()');
$value = trim($value[1]
->getText());
$this
->assertEqual($value, 'Draft', 'The transition to a new content moderation status didn\'t happen because the source wasn\'t the expected.');
}
public function testUnconfiguredBundle() {
$this
->drupalGet('/admin/lingotek/settings');
$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]'] = 'automatic';
$this
->saveAndPublishNodeForm($edit, 'page');
$this
->assertText('Page Llamas are cool has been created.');
$this
->assertText('Llamas are cool sent to Lingotek successfully.');
}
protected function enableModerationThroughUI($content_type_id) {
$this
->drupalGet('/admin/config/workflow/workflows/manage/editorial/type/node');
$this
->assertFieldByName("bundles[{$content_type_id}]");
$edit["bundles[{$content_type_id}]"] = TRUE;
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
protected function addReviewStateToEditorialWorkflow() {
$workflow = Workflow::load('editorial');
$definition = $workflow
->getTypePlugin();
$definition
->addState('needs_review', 'Needs Review');
$definition
->addTransition('request_review', 'Request Review', [
'draft',
], 'needs_review');
$definition
->addTransition('publish_review', 'Publish Review', [
'needs_review',
], 'published');
$definition
->addTransition('back_to_draft', 'Back to Draft', [
'needs_review',
], 'draft');
$workflow
->save();
}
}