View source
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Url;
class ModerationFormTest extends ModerationStateTestBase {
protected static $modules = [
'node',
'content_moderation',
'locale',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
$this
->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
}
public function testModerationForm() {
$this
->drupalGet('node/add/moderated_content');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'published',
'title[0][value]' => 'Some moderated content',
'body[0][value]' => 'First version of the content.',
], 'Preview');
$this
->clickLink('Back to content editing');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save');
$node = $this
->drupalGetNodeByTitle('Some moderated content');
$canonical_path = sprintf('node/%d', $node
->id());
$edit_path = sprintf('node/%d/edit', $node
->id());
$latest_version_path = sprintf('node/%d/latest', $node
->id());
$this
->assertTrue($this->adminUser
->hasPermission('edit any moderated_content content'));
$this
->drupalGet($canonical_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('edit-new-state');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($edit_path);
$this
->submitForm([
'body[0][value]' => 'Second version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet($canonical_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('edit-new-state');
$this
->drupalGet($edit_path);
$this
->submitForm([
'body[0][value]' => 'Second version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Preview');
$preview_url = Url::fromRoute('entity.node.preview', [
'node_preview' => $node
->uuid(),
'view_mode_id' => 'full',
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($preview_url);
$this
->assertSession()
->fieldNotExists('edit-new-state');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($edit_path);
$this
->submitForm([
'body[0][value]' => 'Third version of the content.',
'moderation_state[0][state]' => 'published',
], 'Save');
$this
->drupalGet($edit_path);
$this
->assertSession()
->fieldValueEquals('moderation_state[0][state]', 'published');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Preview');
$this
->clickLink('Back to content editing');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->drupalGet($canonical_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('edit-new-state');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($edit_path);
$this
->submitForm([
'body[0][value]' => 'Fourth version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet($canonical_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('edit-new-state');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('edit-new-state');
$this
->assertSession()
->pageTextContains('Draft');
$this
->drupalGet($latest_version_path);
$this
->submitForm([
'new_state' => 'published',
], 'Apply');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals(403);
}
public function testNonBundleModerationForm() {
$this
->drupalLogin($this->rootUser);
$this->workflow
->getTypePlugin()
->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
$this->workflow
->save();
$this
->drupalGet('entity_test_mulrevpub/add');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet('/entity_test_mulrevpub/manage/1/latest');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('entity_test_mulrevpub/manage/1/edit');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet('/entity_test_mulrevpub/manage/1/latest');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('entity_test_mulrevpub/manage/1/edit');
$this
->submitForm([
'moderation_state[0][state]' => 'published',
], 'Save');
$this
->drupalGet('entity_test_mulrevpub/manage/1');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Status');
$this
->drupalGet('entity_test_mulrevpub/manage/1/latest');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('entity_test_mulrevpub/manage/1/edit');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet('entity_test_mulrevpub/manage/1');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Status');
$this
->drupalGet('entity_test_mulrevpub/manage/1/latest');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Moderation state');
$this
->assertSession()
->pageTextContains('Draft');
$this
->drupalGet('entity_test_mulrevpub/manage/1/latest');
$this
->submitForm([
'new_state' => 'published',
], 'Apply');
$this
->drupalGet('entity_test_mulrevpub/manage/1/latest');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testModerationFormSetsRevisionAuthor() {
$node = $this
->createNode([
'type' => 'moderated_content',
'moderation_state' => 'published',
]);
$node->title = $this
->randomMachineName();
$node->moderation_state->value = 'draft';
$node
->setRevisionCreationTime(12345);
$node
->save();
$another_user = $this
->drupalCreateUser($this->permissions);
$this
->grantUserPermissionToCreateContentOfType($another_user, 'moderated_content');
$this
->drupalLogin($another_user);
$this
->drupalGet(sprintf('node/%d/latest', $node
->id()));
$this
->submitForm([
'new_state' => 'published',
], 'Apply');
$this
->drupalGet(sprintf('node/%d/revisions', $node
->id()));
$this
->assertSession()
->pageTextContains('by ' . $another_user
->getAccountName());
$node = $node
->load($node
->id());
$this
->assertGreaterThan(12345, $node
->getRevisionCreationTime());
}
public function testContentTranslationNodeForm() {
$this
->drupalLogin($this->rootUser);
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->drupalGet('admin/config/regional/content-language');
$edit = [
'entity_types[node]' => TRUE,
'settings[node][moderated_content][translatable]' => TRUE,
'settings[node][moderated_content][settings][language][language_alterable]' => TRUE,
];
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
$this
->drupalGet('node/add/moderated_content');
$this
->submitForm([
'title[0][value]' => 'Some moderated content',
'body[0][value]' => 'First version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->assertNotEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$node = $this
->drupalGetNodeByTitle('Some moderated content');
$this
->assertNotEmpty($node
->language(), 'en');
$edit_path = sprintf('node/%d/edit', $node
->id());
$translate_path = sprintf('node/%d/translations/add/en/fr', $node
->id());
$latest_version_path = sprintf('node/%d/latest', $node
->id());
$french = \Drupal::languageManager()
->getLanguage('fr');
$this
->drupalGet($latest_version_path);
$this
->assertSession()
->statusCodeEquals('403');
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($translate_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Second version of the content.',
'moderation_state[0][state]' => 'published',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path, [
'language' => $french,
]);
$this
->assertSession()
->statusCodeEquals('403');
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Preview');
$this
->clickLink('Back to content editing');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Third version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path, [
'language' => $french,
]);
$this
->assertNotEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path);
$this
->clickLink('Delete');
$this
->assertSession()
->buttonExists('Delete');
$this
->drupalGet($latest_version_path);
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Fifth version of the content.',
'moderation_state[0][state]' => 'published',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path, [
'language' => $french,
]);
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Sixth version of the content.',
'moderation_state[0][state]' => 'published',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path);
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->drupalGet($edit_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Seventh version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path);
$this
->assertNotEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($latest_version_path, [
'language' => $french,
]);
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'body[0][value]' => 'Eighth version of the content.',
'moderation_state[0][state]' => 'published',
], 'Save (this translation)');
$this
->drupalGet($latest_version_path);
$this
->assertEmpty($this
->xpath('//ul[@class="entity-moderation-form"]'));
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->drupalGet($edit_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->drupalGet('node/add/moderated_content');
$this
->submitForm([
'title[0][value]' => 'Third moderated content',
'moderation_state[0][state]' => 'published',
], 'Save');
$node = $this
->drupalGetNodeByTitle('Third moderated content');
$this
->assertNotEmpty($node
->language(), 'en');
$edit_path = sprintf('node/%d/edit', $node
->id());
$translate_path = sprintf('node/%d/translations/add/en/fr', $node
->id());
$this
->drupalGet($translate_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save (this translation)');
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save (this translation)');
$this
->drupalGet($edit_path, [
'language' => $french,
]);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionNotExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'published',
], 'Save (this translation)');
$this
->drupalGet($edit_path);
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'draft');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'published');
$this
->assertSession()
->optionExists('moderation_state[0][state]', 'archived');
$this
->submitForm([
'moderation_state[0][state]' => 'draft',
], 'Save (this translation)');
}
public function testAlternativeModerationStateWidget() {
$entity_form_display = EntityFormDisplay::load('node.moderated_content.default');
$entity_form_display
->setComponent('moderation_state', [
'type' => 'string_textfield',
'region' => 'content',
]);
$entity_form_display
->save();
$this
->drupalGet('node/add/moderated_content');
$this
->submitForm([
'title[0][value]' => 'Test content',
'moderation_state[0][value]' => 'published',
], 'Save');
$this
->assertSession()
->pageTextContains('Moderated content Test content has been created.');
}
public function testWorkflowInUse() {
$user = $this
->createUser([
'administer workflows',
'create moderated_content content',
'edit own moderated_content content',
'use editorial transition create_new_draft',
'use editorial transition publish',
'use editorial transition archive',
]);
$this
->drupalLogin($user);
$paths = [
'archived_state' => 'admin/config/workflow/workflows/manage/editorial/state/archived/delete',
'editorial_workflow' => 'admin/config/workflow/workflows/manage/editorial/delete',
];
$messages = [
'archived_state' => 'This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.',
'editorial_workflow' => 'This workflow is in use. You cannot remove this workflow until you have removed all content using it.',
];
foreach ($paths as $path) {
$this
->drupalGet($path);
$this
->assertSession()
->buttonExists('Delete');
}
$this
->drupalGet('node/add/moderated_content');
$this
->submitForm([
'title[0][value]' => 'Some moderated content',
'body[0][value]' => 'First version of the content.',
'moderation_state[0][state]' => 'draft',
], 'Save');
$this
->drupalGet($paths['archived_state']);
$this
->assertSession()
->buttonExists('Delete');
$this
->drupalGet($paths['editorial_workflow']);
$this
->assertSession()
->buttonNotExists('Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($messages['editorial_workflow']);
$node = $this
->drupalGetNodeByTitle('Some moderated content');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([
'moderation_state[0][state]' => 'published',
], 'Save');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([
'moderation_state[0][state]' => 'archived',
], 'Save');
foreach ($paths as $type => $path) {
$this
->drupalGet($path);
$this
->assertSession()
->buttonNotExists('Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($messages[$type]);
}
}
}