#2099131: Use #pre_render pattern for entity render caching added CommentDefaultFormatterCacheTagsTest as a web test instead of as a unit test. This fixes that.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, comment-unit-test.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review

comment-unit-test.patch queued for re-testing.

sun’s picture

Status: Needs review » Reviewed & tested by the community
Wim Leers’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentDefaultFormatterCacheTagsTest.php
@@ -39,9 +40,17 @@ public static function getInfo() {
-    $this->drupalLogin($this->drupalCreateUser(array(
-      'access comments',
-    )));
+    // Set the current user to uid=1 to bypass all permission checks, and
+    // therefore, render all comments.
+    $this->container->set('current_user', new UserSession(array('uid' => 1)));

No!

This must explicitly remain a user with only the 'access comments' permission, for the reasons outlined in #2099131-201: Use #pre_render pattern for entity render caching.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
2.6 KB
1.28 KB
Wim Leers’s picture

effulgentsia’s picture

#5 looks good, but given the comment in the interdiff, is it a problem that the original patch passed? Are we missing an assertion related to unpublished comments?

Wim Leers’s picture

#7: It says "published comment" in the code comment because that's what CommentDefaultFormatter's if-test says. But the reason I'm insisting on using 'access comments' is because the condition to render comments that applies to 99% of users (who don't have the permission to administer comments) looks like this:
(($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments'))
And in my work on #2099131: Use #pre_render pattern for entity render caching, I discovered that $comment->save() did NOT reset the commented entity's storage cache, which caused comment_count to remain at zero, even after comments were posted.

Giving users all permissions tests the 1% case, giving the user only permission to access comments tests the 99% case.

Hopefully this is more clear. But I know, it's confusing and frustratingly complex. Again, see #2099131-201: Use #pre_render pattern for entity render caching for all the details, and an interdiff that shows the problems explicitly.

effulgentsia’s picture

Status: Needs review » Reviewed & tested by the community

I discovered that $comment->save() did NOT reset the commented entity's storage cache

If we don't have one, a separate unit test for this would be good, rather than solely testing it incidentally in a formatter's test. But regardless, that's separate scope from this issue. #5 is closer to HEAD than the original patch, so RTBC.

Wim Leers’s picture

Agreed.

The key problem is that CommentDefaultFormatter was added with ZERO test coverage. I only added the regression test coverage needed to prevent regressions of the bugs I fixed and discovered while working on something completely independent.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

  • Commit 8f60160 on 8.x by catch:
    Issue #2257421 by Wim Leers, effulgentsia: Convert...
dawehner’s picture

Mh, sadly this test does not really work as you would expect it and causes kind of "random" failures on other patches, see #2263329: CommentDefaultFormatterCacheTagsTest just works because nothing uses AccountProxyInterface in HEAD

dawehner’s picture

Ha, effulgentsia should have know that better.

Status: Fixed » Closed (fixed)

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