Problem/Motivation

In Content Moderation we have the ModerationState constraint. This is used on both the computed moderation_state field added to selected entity types, and on the entity reference moderation_state field on ContentModerationState entity type.

- Do we need the same constraint on both fields?
- Is it even valid?
- Does it work since we changed to a computed field?
- Does validation get checked anywhere when creating ContentModerationState entities?

Proposed resolution

Answer above questions.
Make sure there is test coverage for this.
Add extra constraints and validation where needed.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

timmillwood created an issue. See original summary.

AniG’s picture

This is a test (ignore)

AniG’s picture

Issue summary: View changes
AniG’s picture

Issue summary: View changes
Sam152’s picture

I've had a look at this and found that:

Do we need the same constraint on both fields?

Nope, when the constraint is checked for the ContentModerationState entity, it fails the first check in the constraint:

    if (!$this->moderationInformation->isModeratedEntity($entity)) {
      return;
    }

Given the checks seem centered around the entity actually being moderated, it seems like it would be a lot of effort to make the constraint work with both entities for not much benefit, given the data stored in the field on the unsupported entity has already been validated.

- Is it even valid?

Looks like there is a test which tries to set invalid states (EntityStateChangeValidationTest) and the constraint is used there.

- Does it work since we changed to a computed field?

Yep, it looks like the constraint is checked for computed fields.

tl;dr, it can be removed from ContentModerationState, it's valid for the computed field with tests to prove it.

Status: Needs review » Needs work

The last submitted patch, 5: 2781907-constraint-clean-up-5.patch, failed testing.

Sam152’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs work » Needs review

Wrong version.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

timmillwood’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Needs review » Needs work

Patch in #5 looks correct, but marking as "needs work" for tests. Think we also need a test only patch. Although not sure how we'd test this yet.

Sam152’s picture

Yeah, I'm stumped on what we would actually be testing with this removed, given it never did anything to begin with.

timmillwood’s picture

Status: Needs work » Needs review
FileSize
2.04 KB

He's a test to prove the validation is useless, but also proves maybe we need something.

Sam152’s picture

Personally I don't see the need for a test or to validate this stuff. updateOrCreateFromEntity is marked as internal, so I see this issue as removing some dead code.

Based on the save method interacting with the parent entity, I think this actually proves there isn't a way to trigger this without using an internal method. We could write a validator specifically for this entity, but personally I think it's extra code to maintain for not much benefit.

timmillwood’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests
FileSize
809 bytes

ok, fair enough!

Patch seems ok.
Not worth testing.
We can add a new constraint if ever needed.

Re-uploading patch for clarity.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: 2781907-constraint-clean-up-5.patch, failed testing.

timmillwood’s picture

Status: Needs work » Reviewed & tested by the community
xjm’s picture

The title of this issue is way scarier than the patch in it.

xjm’s picture

Title: Refactor moderation state transition validation » Remove unnecessary constraint from moderation state transition validation
alexpott’s picture

Yeah this constraint is probably a copy&paste error...

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $value->getEntity();

    // Ignore entities that are not subject to moderation anyway.
    if (!$this->moderationInformation->isModeratedEntity($entity)) {
      return;
    }

It is always going to just return cause you can't moderation and ContentModerationState content entity - fortunately because that would be beyond inception.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 2a55d2d to 8.4.x and 6983b38 to 8.3.x. Thanks!

  • alexpott committed 2a55d2d on 8.4.x
    Issue #2781907 by timmillwood, Sam152: Remove unnecessary constraint...

  • alexpott committed 6983b38 on 8.3.x
    Issue #2781907 by timmillwood, Sam152: Remove unnecessary constraint...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.