Problem/Motivation
Follow up to #2949964: Add an EntityOwnerTrait to standardize the base field needed by EntityOwnerInterface.
Because of a bug in our test coverage, the Comment entity requires that the default owner is the anonymous user for the tests to pass. In order to provide better API and REST support for automatically associating the logged in user with created comments, we should ensure new comments are owned by the logged in user by default.
Proposed resolution
Delete getDefaultEntityOwner
, which requires no deprecation because it is a method override and we want the default behaviour of \Drupal\user\EntityOwnerTrait::getDefaultEntityOwner()
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#33 | 2975217-33.patch | 2.46 KB | Berdir |
#25 | comment-fix-the-DX-problems-2975217-25.patch | 5.7 KB | Berdir |
#23 | 2975217-23.patch | 3.71 KB | Wim Leers |
#23 | interdiff.txt | 1.39 KB | Wim Leers |
#11 | 2975217-11.patch | 3.61 KB | Sam152 |
Comments
Comment #2
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedComment #3
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedCombining patch with the blocker to see what breaks.
Comment #5
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedComment #7
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedComment #8
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedRetesting.
Comment #10
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedFixing this will also fix #2859812: Comments created with REST are assigned anonymous user despite of correct authenticication. Currently any comments created over REST are owned by anonymous.
Comment #11
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI think one of the rest tests had a copy/paste error in it and was failing for the wrong reasons. Fixing that and adding an additional test case for the default owner.
Comment #12
borisson_This looks great! It does fix both bugs and it looks like a easy win because of the new trait. Great work @Sam152!
Comment #13
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedThanks for reviewing! I have updated the issue summary.
Comment #14
BerdirLooks good to me as well.
I was wondering if there's something to clean up thanks to this in e.g. CommentForm but the author/uid handling there is very complex, with weird empty fallbacks and so on (Node has similar stuff when it should simply be so that the field is required), so lets not touch that here.
The reason for removing the default value method is that we only just added it to 8.7.x only, so it's not an API yet.
Comment #15
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI agree, had a look and decided that we shouldn't mess with the control structure there just because we can. Also added the note about BC to the issue summary.
Comment #16
alexpottCommitted 0b8908a and pushed to 8.7.x. Thanks!
Comment #18
Wim LeersAlso marked #2859812: Comments created with REST are assigned anonymous user despite of correct authenticication as a duplicate thanks to this :)
Comment #19
Wim LeersI see what you did here, but actually, the comment was wrong, the title was right.
Looking at that method BEFORE this patch:
So the correct fix would've been to just change the comment :)
Comment #21
Wim LeersAlso, as of this commit, JSON API's comment test coverage is failing. See #3001193: CommentTest::testPostIndividualDxWithoutCriticalBaseFields() fails on 8.7 since #2885809.
Comment #22
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedHm, apologies, not entirely sure what that test was doing. Updating the code to match the comment made it green, which was good enough for me! Will see if I can dig into this.
Comment #23
Wim LeersThat test and its comments are very confusing because the broken functionality that it's asserting is very confusing. Completely understandable that you misinterpreted it!
Attached patch should be RTBC'able.
Comment #25
BerdirThat 500 -> 201 change that also happens without any change there is why we assumed that this test coverage is no longer needed ;)
Looking at the actual exception that's causing the 500, that's happening in \Drupal\comment\CommentStatistics::update(), called by *Comment::postSave()*. Which means long after validation which was happy about this input.
Because of this line:
$this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache([$comment->getCommentedEntityId()]);
Because that translates to getStorage(NULL).
Everything else will create a lot of weird data in that comment statistics table but doesn't cause neither a hard fail *nor* a validation error.
And the reason this issue causes that change is that in \Drupal\comment\Plugin\Validation\Constraint\CommentNameConstraintValidator::validate(), we only check for getAnonymousContactDetailsSetting() which does indeed fail on having an empty entity_type but we only run that if owner_id === 0, which is now no longer the case now, so as a result it passes.
In other words, instead of solving the very hard to resolve #2820364: Entity + Field + Property validation constraints are processed in the incorrect order, we just need to make one single validator a tiny bit more resilient and mark the two field which should be required as required and we get exactly the DX that the test wishes to have for the comment entity :)
Just posting a quick patch that we should move to a dedicated issue. With these changes, it should pass with and without the patch from this issue. Maybe we need to postpone this on that, as this then shows that we are actually missing the required flag on that field and asserting a success condition here makes even less sense than asserting 500 exceptions :)
Comment #27
BerdirNeeded one more check for having a field name there. Moved the patch to #2885809: The 'entity_type' and 'field_name' base fields on Comment are required.
Comment #28
BerdirComment #29
Wim LeersSplendid! But we'll still need to resolve #2820364: Entity + Field + Property validation constraints are processed in the incorrect order at some point.
Thanks for spinning this out to #2885809: The 'entity_type' and 'field_name' base fields on Comment are required. I RTBC'd your patch, but @amateescu felt it should work differently. Now leaving it to you to review that. It seems sensible to me, but I'm not as much as an expert in this area as you are :)
Marking this as postponed on #2885809: The 'entity_type' and 'field_name' base fields on Comment are required.
Comment #30
Wim LeersJust bumped the blocking issue.
Comment #31
BerdirThat went in, so back to work here :)
Comment #32
Wim LeersIndeed! 🎉
Comment #33
BerdirReroll of #23, without the change to the REST test.
Comment #36
dmitry.kazberovichHi guys.
Are there any plans to have the latest patch merged to the core?
The issue is quite important since posting the comment via either rest or jsonapi causes to anonymous comment.
Comment #37
Wim LeersYou're right, and this has been ready for ages — this just fell off the radar 😞
Re-testing against latest D8. Bumping priority.
Comment #38
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedPatch still applies.
Comment #39
alexpott#36 makes this sound like a bug. Re-categorising and updated issue summary to explain why this is not an API change. I was wondering whether this needs a change record but I think this changing comments to have the expected behaviour so I don't think so.
Committed and pushed 85fa773d51 to 9.0.x and 30c76f8e11 to 8.9.x. Thanks!
Going to ask other committers for a backport to 8.8.x
Comment #42
alexpott@catch +1'd the backport.
Comment #44
Wim LeersGreat, thanks for the backport to 8.8!
Comment #46
Wim Leers#3083184: Drupal\comment\Entity\Comment::preCreate() should set the default value of `uid` to the current account ID was marked as a duplicate of this.