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

Code before:
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

from wagtail.models import Collection
from wagtail.models.media import CollectionViewRestriction


class TestCollectionTreeOperations(TestCase):
    def setUp(self):
        self.root_collection = Collection.get_first_root_node()
        self.holiday_photos_collection = self.root_collection.add_child(
            name="Holiday photos"
        )
        self.evil_plans_collection = self.root_collection.add_child(name="Evil plans")
        # self.holiday_photos_collection's path has been updated out from under it by the addition of a sibling with
        # an alphabetically earlier name (due to Collection.node_order_by = ['name']), so we need to refresh it from
        # the DB to get the new path.
        self.holiday_photos_collection.refresh_from_db()

    def test_alphabetic_sorting(self):
        old_evil_path = self.evil_plans_collection.path
        old_holiday_path = self.holiday_photos_collection.path
        # Add a child to Root that has an earlier name than "Evil plans" and "Holiday Photos".
        alpha_collection = self.root_collection.add_child(name="Alpha")
        # Take note that self.evil_plans_collection and self.holiday_photos_collection have not yet changed.
        self.assertEqual(old_evil_path, self.evil_plans_collection.path)
        self.assertEqual(old_holiday_path, self.holiday_photos_collection.path)
        # Update the two Collections from the database.
        self.evil_plans_collection.refresh_from_db()

Review comment (refactor):
OK, we should be putting these tests in the view tests. see `wagtail/admin/tests/test_collections_views.py`.

When you look at other Wagtail view tests, we have a lot of helper classes to avoid the boilerplate of creating admin users etc. 

I have just moved the tests over and here's what it could look like.

```py
class TestCollectionPrivacyStatusLabels(WagtailTestUtils, TestCase):

    def setUp(self):
        self.user = self.login()

        self.root_collection = Collection.get_first_root_node()
        self.public_collection = self.root_collection.add_child(name="Public_photos1")
        self.private_collection = self.root_collection.add_child(name="Private_photos1")
        CollectionViewRestriction.objects.create(
            collection=self.private_collection,
            restriction_type=CollectionViewRestriction.LOGIN,
        )
        self.root_collection.refresh_from_db()
        self.private_collection.refresh_from_db()
        self.public_collection.refresh_from_db()

        self.sub_private_collection = self.private_collection.add_child(
            name="Sub_private_photos1"
        )

        self.sub_public_collection = self.public_collection.add_child(
            name="Sub_public_photos1"
        )

    def test_admin_displays_private_tag_for_private_base_collections(self):
        request = self.client.get(
            reverse("wagtailadmin_collections:edit", args=[self.private_collection.pk])
        )

        self.assertTrue('class="privacy-indicator private"' in str(request.content))
        self.assertFalse('class="privacy-indicator "' in str(request.content))

    def test_admin_displays_private_tag_for_private_sub_collections(self):
        request = self.client.get(
            reverse(
                "wagtailadmin_collections:edit", args=[self.sub_private_collection.pk]
            )
        )

        self.assertTrue('class="privacy-indicator private"' in str(request.content))
        self.assertFalse('class="privacy-indicator public"' in str(request.content))

    def test_admin_displays_public_tag_for_public_base_collections(self):
        request = self.client.get(
            reverse("wagtailadmin_collections:edit", args=[self.public_collection.pk])
        )

        self.assertTrue('class="privacy-indicator public"' in str(request.content))
        self.assertFalse('class="privacy-indicator private"' in str(request.content))

    def test_admin_displays_public_tag_for_public_sub_collections(self):
        request = self.client.get(
            reverse(
                "wagtailadmin_collections:edit", args=[self.sub_public_collection.pk]
            )
        )

        self.assertTrue('class="privacy-indicator public"' in str(request.content))
        self.assertFalse('class="privacy-indicator private"' in str(request.content))

```


Code after:
from django.test import TestCase

from wagtail.models import Collection


class TestCollectionTreeOperations(TestCase):
    def setUp(self):
        self.root_collection = Collection.get_first_root_node()
        self.holiday_photos_collection = self.root_collection.add_child(
            name="Holiday photos"
        )
        self.evil_plans_collection = self.root_collection.add_child(name="Evil plans")
        # self.holiday_photos_collection's path has been updated out from under it by the addition of a sibling with
        # an alphabetically earlier name (due to Collection.node_order_by = ['name']), so we need to refresh it from
        # the DB to get the new path.
        self.holiday_photos_collection.refresh_from_db()

    def test_alphabetic_sorting(self):
        old_evil_path = self.evil_plans_collection.path
        old_holiday_path = self.holiday_photos_collection.path
        # Add a child to Root that has an earlier name than "Evil plans" and "Holiday Photos".
        alpha_collection = self.root_collection.add_child(name="Alpha")
        # Take note that self.evil_plans_collection and self.holiday_photos_collection have not yet changed.
        self.assertEqual(old_evil_path, self.evil_plans_collection.path)
        self.assertEqual(old_holiday_path, self.holiday_photos_collection.path)
        # Update the two Collections from the database.
        self.evil_plans_collection.refresh_from_db()
        self.holiday_photos_collection.refresh_from_db()
        # Confirm that the "Evil plans" and "Holiday photos" paths have changed in the DB due to adding "Alpha".
        self.assertNotEqual(old_evil_path, self.evil_plans_collection.path)