diff --git a/core/modules/content_moderation/src/Form/RevisionRevertForm.php b/core/modules/content_moderation/src/Form/RevisionRevertForm.php index 7c5b1d49c5..54bb6d90c6 100644 --- a/core/modules/content_moderation/src/Form/RevisionRevertForm.php +++ b/core/modules/content_moderation/src/Form/RevisionRevertForm.php @@ -79,7 +79,7 @@ protected function prepareRevertedRevision(NodeInterface $revision, FormStateInt if ($form_state->hasValue('revert_state')) { $revision->moderation_state = $form_state->getValue('revert_state'); } - return $revision; + return $this->nodeStorage->createRevision($revision); } } diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationRevisionRevertTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationRevisionRevertTest.php index a827781b4f..c61555cb51 100644 --- a/core/modules/content_moderation/tests/src/Functional/ModerationRevisionRevertTest.php +++ b/core/modules/content_moderation/tests/src/Functional/ModerationRevisionRevertTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\content_moderation\Functional; +use Drupal\node\NodeInterface; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait; use Drupal\Tests\node\Traits\ContentTypeCreationTrait; @@ -31,6 +32,13 @@ class ModerationRevisionRevertTest extends BrowserTestBase { */ protected $defaultTheme = 'stark'; + /** + * Node entity storage. + * + * @var \Drupal\node\NodeStorageInterface + */ + protected $nodeStorage; + /** * An array of admin permissions. * @@ -64,6 +72,8 @@ public function setUp() { $admin = $this->drupalCreateUser($this->adminPermissions); $this->drupalLogin($admin); + + $this->nodeStorage = $this->container->get('entity_type.manager')->getStorage('node'); } /** @@ -124,60 +134,108 @@ public function testEditingAfterRevertRevision() { */ public function testRevertingToState() { $this->drupalPostForm('node/add/moderated_bundle', [ - 'title[0][value]' => 'First draft node', + 'title[0][value]' => 'First revision (originally draft)', 'moderation_state[0][state]' => 'draft', - ], t('Save')); + ], 'Save'); + $draft_revision = $this->getNodeByTitle('First revision (originally draft)'); + $node_id = $draft_revision->id(); $this->drupalPostForm('node/1/edit', [ - 'title[0][value]' => 'Published node', + 'title[0][value]' => 'Second revision (originally published)', 'moderation_state[0][state]' => 'published', - ], t('Save')); + ], 'Save'); + $published_revision = $this->getNodeByTitle('Second revision (originally published)'); - // Verify the revert state does not exist for users without any revert - // permissions. - $revision_url = 'node/1/revisions/1/revert'; - $this->drupalGet($revision_url); + $this->getRevisionRevert($draft_revision); $this->assertSession()->fieldNotExists('revert_state'); - $admin_with_revert_to_draft = $this->drupalCreateUser(array_merge($this->adminPermissions, [ - 'revert editorial revisions to published', - ])); - $this->drupalLogin($admin_with_revert_to_draft); - - $this->drupalGet($revision_url); - $this->assertSession()->fieldExists('revert_state'); + $this->loginAsAdmin(['revert editorial revisions to published']); + $this->getRevisionRevert($draft_revision); $this->assertSession()->optionExists('revert_state', 'published'); $this->assertSession()->optionNotExists('revert_state', 'draft'); - // Now revert and set to a published state. - $this->drupalPostForm(NULL, ['revert_state' => 'published'], t('Revert')); - $node = $this->getNodeByTitle('First draft node'); - $this->assertTrue($node->isPublished()); + // Test reverting from a draft to a published revision. + $this->getRevisionRevert($draft_revision); + $this->drupalPostForm(NULL, ['revert_state' => 'published'], 'Revert'); + $this->assertLatestRevision($node_id, 'published', TRUE, TRUE); - $admin_with_revert_to_published_and_draft = $this->drupalCreateUser(array_merge($this->adminPermissions, [ + $this->loginAsAdmin([ 'revert editorial revisions to published', 'revert editorial revisions to draft', - ])); - $this->drupalLogin($admin_with_revert_to_published_and_draft); - - $this->drupalGet($revision_url); - $this->assertSession()->fieldExists('revert_state'); + ]); + $this->getRevisionRevert($draft_revision); $this->assertSession()->optionExists('revert_state', 'draft'); $this->assertSession()->optionExists('revert_state', 'published'); - // Revert as draft and then publish new draft. - $this->drupalPostForm(NULL, ['revert_state' => 'draft'], t('Revert')); + // Test reverting from a draft to a draft revision. + $this->getRevisionRevert($draft_revision); + $this->drupalPostForm(NULL, ['revert_state' => 'draft'], 'Revert'); + $this->assertLatestRevision($node_id, 'draft', FALSE, FALSE); + + // Test reverting from a pending draft to a published revision. + $this->clickLink('Set as current revision'); + $this->drupalPostForm(NULL, ['revert_state' => 'published'], 'Revert'); + $this->assertLatestRevision($node_id, 'published', TRUE, TRUE); + + // Test reverting from a published revision to a draft revision. + $this->getRevisionRevert($published_revision); + $this->drupalPostForm(NULL, ['revert_state' => 'draft'], 'Revert'); + $this->assertLatestRevision($node_id, 'draft', FALSE, FALSE); + + // Test reverting from a published revision to a published revision. + $this->getRevisionRevert($published_revision); + $this->drupalPostForm(NULL, ['revert_state' => 'published'], 'Revert'); + $this->assertLatestRevision($node_id, 'published', TRUE, TRUE); + } - $node_storage = \Drupal::entityTypeManager()->getStorage('node'); - $revision = $node_storage->loadRevision($node_storage->getLatestRevisionId($node->id())); - $this->assertFalse($revision->isPublished()); + /** + * Login as an admin, with additional permissions. + * + * @param array $additional_permissions + * Additional permissions to add to the admin permissions. + * + * @return \Drupal\user\Entity\User + * The new account that was created. + */ + protected function loginAsAdmin($additional_permissions = []) { + $account = $this->drupalCreateUser(array_merge($this->adminPermissions, $additional_permissions)); + $this->drupalLogin($account); + return $account; + } - $this->clickLink(t('Set as current revision')); - $this->drupalPostForm(NULL, ['revert_state' => 'published'], t('Revert')); + /** + * Visit the URL which will revert to the given + * + * @param \Drupal\node\NodeInterface $node_revision + * The node revision to revert to. + */ + protected function getRevisionRevert(NodeInterface $node_revision) { + $this->drupalGet("node/{$node_revision->id()}/revisions/{$node_revision->getRevisionId()}/revert"); + } - $node_storage->resetCache(); - $revision = $node_storage->loadRevision($node_storage->getLatestRevisionId($node->id())); - $this->assertTrue($revision->isPublished()); + /** + * Assert the latest node revision matches the given parameters. + * + * @param int $id + * The ID of the node to load and assert. + * @param string $moderation_state + * The moderation state. + * @param bool $published + * If the revision is published. + * @param bool $default + * If the revision is default. + * + * @return \Drupal\node\NodeInterface + * The revision which was asserted. + */ + protected function assertLatestRevision($id, $moderation_state, $published, $default) { + $this->nodeStorage->resetCache(); + /** @var \Drupal\node\NodeInterface $revision */ + $revision = $this->nodeStorage->loadRevision($this->nodeStorage->getLatestRevisionId($id)); + $this->assertEquals($moderation_state, $revision->moderation_state->value); + $this->assertEquals($published, $revision->isPublished()); + $this->assertEquals($default, $revision->isDefaultRevision()); + return $revision; } } diff --git a/core/modules/node/src/Form/NodeRevisionRevertForm.php b/core/modules/node/src/Form/NodeRevisionRevertForm.php index 4e4345ed01..da5cba023c 100644 --- a/core/modules/node/src/Form/NodeRevisionRevertForm.php +++ b/core/modules/node/src/Form/NodeRevisionRevertForm.php @@ -112,7 +112,7 @@ public function getDescription() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) { - $this->revision = $this->nodeStorage->createRevision($this->nodeStorage->loadRevision($node_revision)); + $this->revision = $this->nodeStorage->loadRevision($node_revision); $form = parent::buildForm($form, $form_state); return $form;