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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sam152 created an issue. See original summary.

Sam152’s picture

Component: entity system » comment.module
Sam152’s picture

Status: Active » Needs review
FileSize
33.66 KB

Combining patch with the blocker to see what breaks.

Status: Needs review » Needs work

The last submitted patch, 3: 2975217-COMBINED-3.patch, failed testing. View results

Sam152’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Sam152’s picture

Title: [PP-1] Update the default comment entity owner to the current user » Update the default comment entity owner to the current user
Sam152’s picture

Status: Needs work » Needs review
FileSize
510 bytes

Retesting.

Status: Needs review » Needs work

The last submitted patch, 8: 2975217-8.patch, failed testing. View results

Sam152’s picture

Sam152’s picture

Status: Needs work » Needs review
FileSize
3.61 KB

I 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.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This looks great! It does fix both bugs and it looks like a easy win because of the new trait. Great work @Sam152!

Sam152’s picture

Issue summary: View changes

Thanks for reviewing! I have updated the issue summary.

Berdir’s picture

Looks 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.

Sam152’s picture

I 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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0b8908a and pushed to 8.7.x. Thanks!

  • alexpott committed 0b8908a on 8.7.x
    Issue #2975217 by Sam152, Berdir: Update the default comment entity...
Wim Leers’s picture

Wim Leers’s picture

Status: Fixed » Needs work
+++ b/core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php
@@ -314,7 +314,7 @@ public function testPostDxWithoutCriticalBaseFields() {
     // DX: 422 when missing 'entity_type' field.
-    $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['field_name' => TRUE]), static::$format);
+    $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['entity_type' => TRUE]), static::$format);

I see what you did here, but actually, the comment was wrong, the title was right.

Looking at that method BEFORE this patch:

    // DX: 422 when missing 'entity_type' field.
    $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['entity_type' => TRUE]), static::$format);
    $response = $this->request('POST', $url, $request_options);
    // @todo Uncomment, remove next 3 lines in https://www.drupal.org/node/2820364.
    $this->assertSame(500, $response->getStatusCode());
    $this->assertSame(['text/plain; charset=UTF-8'], $response->getHeader('Content-Type'));
    $this->assertStringStartsWith('The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpKernel\Exception\HttpException</em>: Internal Server Error in <em class="placeholder">Drupal\rest\Plugin\rest\resource\EntityResource-&gt;post()</em>', (string) $response->getBody());
    // $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_type: This value should not be null.\n", $response);

[SNIP]

    // DX: 422 when missing 'field_name' field.
    $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity(), ['field_name' => TRUE]), static::$format);
    $response = $this->request('POST', $url, $request_options);
    // @todo Uncomment, remove next 2 lines in https://www.drupal.org/node/2820364.
    $this->assertSame(500, $response->getStatusCode());
    $this->assertSame(['text/plain; charset=UTF-8'], $response->getHeader('Content-Type'));
    // $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nfield_name: This value should not be null.\n", $response);

So the correct fix would've been to just change the comment :)

  • alexpott committed 06f44db on 8.7.x
    Revert "Issue #2975217 by Sam152, Berdir: Update the default comment...
Wim Leers’s picture

Sam152’s picture

Hm, 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.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
1.39 KB
3.71 KB

That 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.

Status: Needs review » Needs work

The last submitted patch, 23: 2975217-23.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Needs review
FileSize
5.7 KB

That 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 :)

Status: Needs review » Needs work

The last submitted patch, 25: comment-fix-the-DX-problems-2975217-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Berdir’s picture

Status: Needs work » Needs review

Needed 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.

Berdir’s picture

Status: Needs review » Needs work
Wim Leers’s picture

Title: Update the default comment entity owner to the current user » [PP-1] Update the default comment entity owner to the current user
Status: Needs work » Postponed
Related issues: +#2885809: The 'entity_type' and 'field_name' base fields on Comment are required

[…] 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 […]

Splendid! 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.

Wim Leers’s picture

Just bumped the blocking issue.

Berdir’s picture

Status: Postponed » Needs work

That went in, so back to work here :)

Wim Leers’s picture

Title: [PP-1] Update the default comment entity owner to the current user » Update the default comment entity owner to the current user

Indeed! 🎉

Berdir’s picture

Status: Needs work » Needs review
FileSize
2.46 KB

Reroll of #23, without the change to the REST test.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

dmitry.kazberovich’s picture

Hi 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.

Wim Leers’s picture

Priority: Normal » Major

You're right, and this has been ready for ages — this just fell off the radar 😞

Re-testing against latest D8. Bumping priority.

Sam152’s picture

Status: Needs review » Reviewed & tested by the community

Patch still applies.

alexpott’s picture

Version: 8.9.x-dev » 8.8.x-dev
Category: Task » Bug report
Issue summary: View changes
Status: Reviewed & tested by the community » Patch (to be ported)

#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

  • alexpott committed 85fa773 on 9.0.x
    Issue #2975217 by Sam152, Berdir, Wim Leers, alexpott: Update the...

  • alexpott committed 30c76f8 on 8.9.x
    Issue #2975217 by Sam152, Berdir, Wim Leers, alexpott: Update the...
alexpott’s picture

Status: Patch (to be ported) » Fixed

@catch +1'd the backport.

  • alexpott committed d957455 on 8.8.x
    Issue #2975217 by Sam152, Berdir, Wim Leers, alexpott: Update the...
Wim Leers’s picture

Great, thanks for the backport to 8.8!

Status: Fixed » Closed (fixed)

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