View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\workbench_moderation\Entity\ModerationState;
class LingotekWorkbenchModerationSettingsTest extends LingotekTestBase {
use TaxonomyTestTrait;
public static $modules = [
'node',
'taxonomy',
'workbench_moderation',
];
protected $vocabulary;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
$this->vocabulary = $this
->createVocabulary();
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);
ContentLanguageSettings::loadByEntityTypeBundle('user', 'user')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('user', 'user', TRUE);
ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $this->vocabulary
->id())
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('taxonomy_term', $this->vocabulary
->id(), TRUE);
$this
->applyEntityUpdates();
$this
->rebuildContainer();
}
public function testWorkbenchModerationSettings() {
$assert_session = $this
->assertSession();
$vocabulary_id = $this->vocabulary
->id();
$this
->drupalGet('admin/lingotek/settings');
$this
->assertNoField('node[article][moderation][upload_status]', 'The field for setting the state when a content should be uploaded does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertNoField('node[article][moderation][download_transition]', 'The field for setting the transition that must happen after download does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertNoField('node[page][moderation][upload_status]', 'The field for setting the state when a content should be uploaded does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertNoField('node[page][moderation][download_transition]', 'The field for setting the transition that must happen after download does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertText('This entity bundle is not enabled for moderation with workbench_moderation. You can change its settings here.');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/article/moderation');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/page/moderation');
$this
->enableModerationThroughUI('article', [
'draft',
'needs_review',
'published',
], 'draft');
$this
->drupalGet('admin/lingotek/settings');
$this
->assertField('node[article][moderation][upload_status]', 'The field for setting the state when a content should be uploaded exists.');
$this
->assertField('node[article][moderation][download_transition]', 'The field for setting the transition that must happen after download exists.');
$this
->assertOptionSelected('edit-node-article-moderation-upload-status', 'published', 'The default value is a published one.');
$this
->assertOptionSelected('edit-node-article-moderation-download-transition', 'published_published', 'The default transition is from published to published.');
$this
->assertNoField('node[page][moderation][upload_status]', 'The field for setting the state when a content should be uploaded does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertNoField('node[page][moderation][download_transition]', 'The field for setting the transition that must happen after download does not exist as workbench moderation is not enabled for this bundle.');
$this
->assertText('This entity bundle is not enabled for moderation with workbench_moderation. You can change its settings here.');
$assert_session
->linkByHrefNotExists('/admin/structure/types/manage/article/moderation');
$assert_session
->linkByHrefExists('/admin/structure/types/manage/page/moderation');
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'automatic',
'fields' => [
'title' => 1,
],
'moderation' => [
'upload_status' => 'draft',
'download_transition' => 'draft_needs_review',
],
],
],
]);
$this
->assertOptionSelected('edit-node-article-moderation-upload-status', 'draft', 'The desired status for upload is stored correctly.');
$this
->assertOptionSelected('edit-node-article-moderation-download-transition', 'draft_needs_review', 'The desired transition after download is stored correctly.');
$this
->assertNoField("taxonomy_term[{$vocabulary_id}][moderation][upload_status]", 'The field for setting the state when a content should be uploaded does not exist as workbench moderation is not available for this entity type.');
$this
->assertNoField("taxonomy_term[{$vocabulary_id}][moderation][download_transition]", 'The field for setting the transition that must happen after download does not exist as workbench moderation is not available for this entity type.');
$assert_session
->linkByHrefExists("/admin/structure/taxonomy/manage/{$vocabulary_id}/moderation");
$this
->assertNoField("user[user][moderation][upload_status]", 'The field for setting the state when a content should be uploaded does not exist as workbench moderation is not available for this entity type.');
$this
->assertNoField("user[user][moderation][download_transition]", 'The field for setting the transition that must happen after download does not exist as workbench moderation is not available for this entity type.');
$assert_session
->linkByHrefNotExists("/admin/structure/user/manage/user/moderation");
$header = $this
->xpath("//details[@id='edit-entity-node']//th[text()='Workbench Moderation']");
$this
->assertEqual(count($header), 1, 'There is a Workbench Moderation column for content.');
$header = $this
->xpath("//details[@id='edit-entity-taxonomy-term']//th[text()='Workbench Moderation']");
$this
->assertEqual(count($header), 1, 'There is a Workbench Moderation column for terms.');
$header = $this
->xpath("//details[@id='edit-entity-user']//th[text()='Workbench Moderation']");
$this
->assertEqual(count($header), 0, 'There is no Workbench Moderation column for users.');
}
protected function enableModerationThroughUI($content_type_id, array $allowed_states, $default_state) {
$this
->drupalGet('admin/structure/types/manage/' . $content_type_id . '/moderation');
$this
->assertFieldByName('enable_moderation_state');
$this
->assertNoFieldChecked('edit-enable-moderation-state');
$edit['enable_moderation_state'] = 1;
foreach (ModerationState::loadMultiple() as $id => $state) {
$key = $state
->isPublishedState() ? 'allowed_moderation_states_published[' . $state
->id() . ']' : 'allowed_moderation_states_unpublished[' . $state
->id() . ']';
$edit[$key] = (int) in_array($id, $allowed_states);
}
$edit['default_moderation_state'] = $default_state;
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
}