Repository: wagtail/wagtail
File: wagtail/tests/test_comments.py (Python)

Code before:


class TestRevisionDeletion(CommentTestingUtils, TestCase):
    fixtures = ["test.json"]

    def setUp(self):
        super().setUp()
        self.revision_3 = self.page.save_revision()
        self.old_comment = self.create_comment(self.revision_1)
        self.new_comment = self.create_comment(self.revision_3)

    def test_deleting_old_revision_moves_comment_revision_created_forwards(self):
        # test that when a revision is deleted, a comment linked to it via revision_created has its revision_created moved
        # to the next revision
        self.revision_1.delete()
        self.old_comment.refresh_from_db()
        self.assertEqual(self.old_comment.revision_created, self.revision_2)

    def test_deleting_most_recent_revision_deletes_created_comments(self):
        # test that when the most recent revision is deleted, any comments created on it are also deleted
        self.revision_3.delete()
        with self.assertRaises(Comment.DoesNotExist):
            self.new_comment.refresh_from_db()


class TestContentPath(WagtailTestUtils, TestCase):
    def setUp(self):
        self.root_page = Page.get_first_root_node()
        self.commentable_page = CommentableJSONPage(
            title="Commentable JSON Page",
            slug="commentable-json-page",
            commentable_body={
                "header": {
                    "title": "Comments are Welcome",
                },
            },
            uncommentable_body={
                "title": "No feedback here",
            },
            stream_body=[
                {
                    "id": "1",
                    "type": "text",
                    "value": "This allows comments",
                }
            ],
        )

        self.root_page.add_child(instance=self.commentable_page)
        self.user = self.create_test_user()


Review comment (suggestion):
I'm a bit hesitant to have only unit tests specifically for the `has_valid_contentpath()` method instead of on a higher level where it is actually used, particularly because we end up mostly testing the behaviour of `get_block_by_content_path` in the `CommentableJSONField`... which was written specifically for the test (and not a Wagtail feature by itself).

Looking at the history of `has_valid_contentpath()`, it was introduced in #10798 to filter out comments on StreamField blocks that no longer exist. In that PR, `has_valid_contentpath()` is not tested directly, but rather via the following test:

https://github.com/wagtail/wagtail/blob/eaf082966627c17730e88551b8befbe510c384fd/wagtail/admin/tests/pages/test_edit_page.py#L4048-L4051

There are unit tests for `get_block_by_content_path()` in that PR, but that makes sense because it's written as an API of `StreamField`, whereas `CommentableJSONField` exists purely for our testing.

So, I think we should adapt the tests to `TestCommentOutput` instead. I've done this locally as I reviewed the PR, so I'll do this as I merge.

Code after:
from django.contrib.auth import get_user_model
from django.test import TestCase

from wagtail.models import Comment, Page


class CommentTestingUtils:
    def setUp(self):
        self.page = Page.objects.get(title="Welcome to the Wagtail test site!")
        self.revision_1 = self.page.save_revision()
        self.revision_2 = self.page.save_revision()

    def create_comment(self, revision_created):
        return Comment.objects.create(
            page=self.page,
            user=get_user_model().objects.first(),
            text="test",
            contentpath="title",
            revision_created=revision_created,
        )


class TestRevisionDeletion(CommentTestingUtils, TestCase):
    fixtures = ["test.json"]

    def setUp(self):
        super().setUp()
        self.revision_3 = self.page.save_revision()
        self.old_comment = self.create_comment(self.revision_1)
        self.new_comment = self.create_comment(self.revision_3)

    def test_deleting_old_revision_moves_comment_revision_created_forwards(self):
        # test that when a revision is deleted, a comment linked to it via revision_created has its revision_created moved
        # to the next revision
        self.revision_1.delete()
        self.old_comment.refresh_from_db()
        self.assertEqual(self.old_comment.revision_created, self.revision_2)

    def test_deleting_most_recent_revision_deletes_created_comments(self):
        # test that when the most recent revision is deleted, any comments created on it are also deleted
        self.revision_3.delete()
        with self.assertRaises(Comment.DoesNotExist):
            self.new_comment.refresh_from_db()