Problem/Motivation

Moving from D10 -> D11, content moderation is now validating old revision incorrectly.

If there are revisions of entity with moderation states like this (revision/state)
- 31 / draft
- 32 / published
- 33 / published

and we load revision 32, set it to Archived and validate the entity, violation shows

Invalid state transition from Archived to Archived

Steps to reproduce

With few steps, this can be reproduced in vanilla Drupal.

  • Install vanilla Drupal.
  • Enable content_moderation and workflows modules
  • Modify the example workflow (Editorial) with following
    • Enable for Article node type
    • Modify Archive transition so that it allows from both Draft and Published to be moved to Archived
  • Run sample script with drush scr
// This part is just to remove the user has no access violation.
/** @var \Drupal\Core\Session\AccountSwitcher $userService */
$userService = \Drupal::service('account_switcher');
$user = \Drupal\user\Entity\User::load(1);
$userService->switchTo($user);

// From now on the actual bug.
$entityTypeManager = \Drupal::entityTypeManager();
$storage = $entityTypeManager->getStorage('node');

// Create draft.
$node= $storage->create(['type' => 'article', 'title' => 'Foobar']);
$node->save();
// Fetch draft point.
$oldId = $node->getRevisionId();

// Set draft -> published. Allowed.
$node->set('moderation_state', 'published');
$node->setNewRevision(TRUE);
$node->save();

// Load draft point.
$node = $storage->loadRevision($oldId);
// Set draft -> archived. Allowed
$node->set('moderation_state', 'archived');

foreach ($node->validate() as $error) {
  if ($error->getPropertyPath() == 'moderation_state') {
    // Violation for trying to set archived -> archived. Not allowed.
    var_dump($error->getMessage());
  }
}

Proposed resolution

Correctly load unchanged revision when validating changes.

Remaining tasks

Issue fork drupal-3582448

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dropa created an issue. See original summary.

dropa’s picture

Status: Active » Needs review
StatusFileSize
new1 KB

Here's quick patch for anyone who happens to be stuck with this issue.

This likely still would need test to be created, the script in description might work as skeleton. Moving for in review already to hear if I've completely misunderstood the change from D10 -> D11 and this was somehow intended change.

smustgrave’s picture

Version: 11.3.x-dev » main
Status: Needs review » Needs work
Issue tags: +Needs tests

Hello thank you for reporting.

Fixes need to be in MRs please landing in main first and depending when they do it can be backported.

Also bugs should have test coverage please

Thanks!

dropa’s picture

Issue summary: View changes

sivaji_ganesh_jojodae made their first commit to this issue’s fork.

sivaji_ganesh_jojodae’s picture

I've ported the patch #1 to MR.