Problem/Motivation
The WorkspacePrePublishEvent subscriber in Workflows implements a query on tracked revisions that neither takes into account that there might not be tracked types, nor that there might be different entity types with the same revision id tracked.
On trying to publish a workspace which only has default state published content that is ready this causes:
- in the first instance a fatal error because of a SQL syntax violation (see below);
- and in the second instance an error message that incorrectly states that there is content in an unpublished workflow state.
Steps to reproduce
Automated tests
https://git.drupalcode.org/project/drupal/-/merge_requests/9062/diffs#di...
Manual tests
From OP on this issue:
- Enable the core content moderation, workflows and workspace modules
- Remove the default content moderation from all content types
- Add the default content moderation to the workspace entity type
- Create a new workspace or use the default Stage workspace
- Switch to the workspace and modify or create some content
- Try to deploy the changes from the workspace
- You should see the status message "Deployment failed. All errors have been logged."
- Content has not been deployed from the workspace.
- Logs will reveal no errors
From OP on #3132022: Content moderation check does not take into account entity IDs being used by two content entities:
- Enable the core content moderation, workflows and workspace modules
- Remove the default content moderation from all content types
- Add the default content moderation to the workspace entity type
- Create a new workspace or use the default Stage workspace
- Switch to the workspace and modify or create some content
- Try to deploy the changes from the workspace
- You should see the status message "Deployment failed. All errors have been logged."
- Content has not been deployed from the workspace.
- Logs will reveal no errors
Also more simply:
- Enable the core content moderation, workflows and workspace modules
- Switch to Stage
- Enable content moderation on two different entity types
- Create an entity of type 1 in an unpublished default state. To create an tracked revision id 1.
- Edit the entity to a published state. To create a tracked default revision id 2.
- Create an entity of type 2 in a published default state. To create a tracked published default revision id 1.
- Try to publish the state workspace.
- It will fail because no longer default revision of entity type 1 is in an unpublished state, but is reported matching for entity of type 2 revision 1
Proposed resolution
Add a check that there are tracked entity types, and include the entity type along with the revision id in the moderation state query. This is done in:
MR https://git.drupalcode.org/project/drupal/-/merge_requests/9062/diffs
Remaining tasks
Needs review.
Footnote
SQL error from OP:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
GROUP BY base_table.revision_id, base_table.id) subquery' at line 2: SELECT COUNT(*) AS expression
FROM
(SELECT 1 AS expression
FROM
{content_moderation_state_revision} base_table
INNER JOIN {content_moderation_state_field_revision} content_moderation_state_field_revision ON content_moderation_state_field_revision.revision_id = base_table.revision_id
WHERE (content_moderation_state_field_revision.content_entity_revision_id IN (:db_condition_placeholder_0, ....)) AND ()
GROUP BY base_table.revision_id, base_table.id)| Comment | File | Size | Author |
|---|---|---|---|
| #30 | 3179199-3132022-content-moderation-workspaces-query.patch | 7.49 KB | ekes |
Issue fork drupal-3179199
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:
- 3179199-3132022-content-moderation-workspaces-query
changes, plain diff MR !9062
Comments
Comment #2
acrazyanimal commentedComment #3
acrazyanimal commentedHere is a patch to resolve this issue.
Comment #4
acrazyanimal commentedComment #5
acrazyanimal commentedComment #6
acrazyanimal commentedComment #7
sam152 commentedThanks for reporting and fixing this bug, it would be great to add a test for this problem.
Comment #8
acrazyanimal commentedI've actually dug a little deeper and tested a few other scenarios and it seems there are additional problems with this access hook implementation. So the patch is only a partial solution to the overall issue.
Within
content_moderation_workspace_access()it first looks for all revision ids associated with a workspace. Then to determine access it checks what workflow states are associated with those revision ids. However, it does not check the states for the ids in combination with a condition on the entity type in question. It bulk checks all ids as a flat statement. The problem here is if there is a mix of entity types in the system using content moderation or not and some content managed entities being tracked in a workspace and some not, then the query can wrongly return results.A couple real world scenarios come to mind:
Steps to reproduce
Proposed resolution
This code will need to be refactored to check the revision ids in association with their respective entity type.
Comment #9
acrazyanimal commentedI will see if I can create some tests for this issue. In the meantime here is a patch that contains a fix for the extended issue highlighted in #8 above. This patch replaces the existing condition on the bulk set of revision ids with grouped conditions that check revision ids AND entity type.
Comment #10
acrazyanimal commentedHere is the patch from #9 but with tests. I added tests that cover the following scenarios:
Comment #11
sam152 commentedNice work @acrazyanimal, really thorough investigation and work.
Has the scope of this issue grown to include #3132022: Content moderation check does not take into account entity IDs being used by two content entities? We should either try to limit the scope of this issue and tackle the problems separately, or update the issue summary to describe all the scenarios that are being tested and the bugs that are being fixed.
In my experience, it's easier to get issues committed that expose a single bug and include a single focused test or test change to demonstrate it.
Comment #12
sam152 commentedComment #13
acrazyanimal commentedThanks @Sam152. Yes I it does look like it covers the scenarios from the issue you linked above. Reviewing the comments in that issue's thread it looks like Alexj12 also increased the scope of that issue to cover the original issue here lol. I'm not sure what to do really. My inclination is to redefine this issue as an all encompassing issue since the tests are overlapping. I will think it over and possibly reduce this and contribute to the other issue.
Comment #18
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #20
adriancidReroll for 10.1
Comment #21
smustgrave commentedWill have to be rerolled for 11.x
Did not test or review yet.
Comment #22
ekes commented@sam152
> Has the scope of this issue grown to include #3132022: Content moderation check does not take into account entity IDs being used by two content entities? We should either try to limit the scope of this issue and tackle the problems separately, or update the issue summary to describe all the scenarios that are being tested and the bugs that are being fixed.
@acrazyanimal
> Thanks @Sam152. Yes I it does look like it covers the scenarios from the issue you linked above. Reviewing the comments in that issue's thread it looks like Alexj12 also increased the scope of that issue to cover the original issue here lol. I'm not sure what to do really. My inclination is to redefine this issue as an all encompassing issue since the tests are overlapping. I will think it over and possibly reduce this and contribute to the other issue.
Looking I'm starting to get convinced https://www.drupal.org/project/drupal/issues/3132022#comment-15708738 that the query there is just going to throw too many false positives, for this, and the referenced issue, and for the other example I mention. So it might needs tweaking, not just filtering out revision ids.
Comment #23
ekes commentedIn creating the branch https://git.drupalcode.org/issue/drupal-3179199/-/compare/11.x...3179199... against 11.x I ended up with the same code as @acrazyanimal in #9 it truly does fix both #3179199 and #3132022 but rewrote the tests against the present state of the class and found that to make them clear they should be separated I think. The tests are in the first commit, so can easily be run to confirm they do fail with the present code.
Comment #25
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #26
ekes commentedIn the MR !9062 now. Which is passing tests.
Comment #27
ekes commentedComment #28
ekes commentedComment #29
ekes commentedComment #30
ekes commentedAdding a patch version of the MR for a composer patch ready snapshot that won't change.
Comment #31
smustgrave commentedAppears to have 1 open thread for a small tweak.
Comment #32
ekes commentedComment #33
smustgrave commentedBelieve the test needs to be updated too
Appears super close!
Comment #34
ekes commentedSerivce name typo should also be fixed. Tests pass I think.
Comment #35
amateescu commentedPosted a few comments on the MR, once those are resolved I think this is good to go :)
Comment #40
amateescu commentedComment #41
amateescu commentedI've closed #3132022: Content moderation check does not take into account entity IDs being used by two content entities as a duplicate and transferred over the credit.
Comment #42
ekes commentedComment #43
yuvaniaI've tested the latest changes in my environment and everything seems to be working perfectly. Thanks everyone for the great work!
Comment #44
yuvaniaComment #45
catchOne question on the MR.
Comment #46
ekes commentedHopefully clarified.
Comment #47
smustgrave commentedTracked types doesn't seem bad. Debated "tracked content" but don't feel strong enough to request the change
Comment #48
ekes commentedNoticed some possible issues with workflows publishing. Going to put this on needs work until clarified.
Comment #49
ekes commentedIt was an unrelated issue, not caused by this one. Behaviour here is correct.
Comment #54
catchCommitted/pushed to 11.x and cherry-picked back through to 10.3.x, thanks!