From 2ceefcf7bc1705d321046ab3e3bd6e7253d7b744 Mon Sep 17 00:00:00 2001 From: Robert Rollins Date: Wed, 2 Sep 2020 22:12:59 +0100 Subject: [PATCH] Tests for #6141 --- wagtail/admin/tests/test_collections_views.py | 61 +++++++++++++++++-- wagtail/core/tests/test_collection_model.py | 44 +++++++++---- wagtail/documents/tests/test_admin_views.py | 46 ++++++++++++++ wagtail/images/tests/test_admin_views.py | 46 ++++++++++++++ wagtail/users/tests.py | 10 +++ 5 files changed, 191 insertions(+), 16 deletions(-) diff --git a/wagtail/admin/tests/test_collections_views.py b/wagtail/admin/tests/test_collections_views.py index f5de4fff99..b5833a64b8 100644 --- a/wagtail/admin/tests/test_collections_views.py +++ b/wagtail/admin/tests/test_collections_views.py @@ -36,11 +36,31 @@ class TestCollectionsIndexView(TestCase, WagtailTestUtils): root_collection = Collection.get_first_root_node() root_collection.add_child(name="Milk") root_collection.add_child(name="Bread") - root_collection.add_child(name="Avacado") + root_collection.add_child(name="Avocado") response = self.get() + # Note that the Collections have been automatically sorted by name. self.assertEqual( [collection.name for collection in response.context['object_list']], - ['Avacado', 'Bread', 'Milk']) + ['Avocado', 'Bread', 'Milk']) + + def test_nested_ordering(self): + root_collection = Collection.get_first_root_node() + + vegetables = root_collection.add_child(name="Vegetable") + vegetables.add_child(name="Spinach") + vegetables.add_child(name="Cucumber") + + animals = root_collection.add_child(name="Animal") + animals.add_child(name="Dog") + animals.add_child(name="Cat") + + response = self.get() + # Note that while we added the collections at level 1 in reverse-alpha order, they come back out in alpha order. + # And we added the Collections at level 2 in reverse-alpha order as well, but they were also alphabetized + # within their respective trees. This is the result of setting Collection.node_order_by = ['name']. + self.assertEqual( + [collection.name for collection in response.context['object_list']], + ['Animal', 'Cat', 'Dog', 'Vegetable', 'Cucumber', 'Spinach']) class TestAddCollection(TestCase, WagtailTestUtils): @@ -80,6 +100,9 @@ class TestEditCollection(TestCase, WagtailTestUtils): self.login() self.root_collection = Collection.get_first_root_node() self.collection = self.root_collection.add_child(name="Holiday snaps") + self.l1 = self.root_collection.add_child(name="Level 1") + self.l2 = self.l1.add_child(name="Level 2") + self.l3 = self.l2.add_child(name="Level 3") def get(self, params={}, collection_id=None): return self.client.get( @@ -105,10 +128,22 @@ class TestEditCollection(TestCase, WagtailTestUtils): response = self.get(collection_id=100000) self.assertEqual(response.status_code, 404) + def test_move_collection(self): + self.post({'name': "Level 2", 'parent': self.root_collection.pk}, self.l2.pk) + self.assertEqual( + Collection.objects.get(pk=self.l2.pk).get_parent().pk, + self.root_collection.pk, + ) + + def test_cannot_move_parent_collection_to_descendant(self): + self.post({'name': "Level 2", 'parent': self.l3.pk}, self.l2.pk) + self.assertEqual( + Collection.objects.get(pk=self.l2.pk).get_parent().pk, + self.l1.pk + ) + def test_post(self): - response = self.post({ - 'name': "Skiing photos", - }) + response = self.post({'name': "Skiing photos"}, self.collection.pk) # Should redirect back to index self.assertRedirects(response, reverse('wagtailadmin_collections:index')) @@ -160,6 +195,13 @@ class TestDeleteCollection(TestCase, WagtailTestUtils): self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtailadmin/collections/delete_not_empty.html') + def test_get_collection_with_descendent(self): + self.collection.add_child(instance=Collection(name='Test collection')) + + response = self.get() + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'wagtailadmin/collections/delete_not_empty.html') + def test_post(self): response = self.post() @@ -180,3 +222,12 @@ class TestDeleteCollection(TestCase, WagtailTestUtils): # Check that the collection was not deleted self.assertTrue(Collection.objects.get(id=self.collection.id)) + + def test_post_collection_with_descendant(self): + self.collection.add_child(instance=Collection(name='Test collection')) + + response = self.post() + self.assertEqual(response.status_code, 403) + + # Check that the collection was not deleted + self.assertTrue(Collection.objects.get(id=self.collection.id)) diff --git a/wagtail/core/tests/test_collection_model.py b/wagtail/core/tests/test_collection_model.py index 657160d7e1..492d2a0186 100644 --- a/wagtail/core/tests/test_collection_model.py +++ b/wagtail/core/tests/test_collection_model.py @@ -12,6 +12,28 @@ class TestCollectionTreeOperations(TestCase): 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) + self.assertNotEqual(old_holiday_path, self.holiday_photos_collection.path) + # Confirm that Alpha is before Evil Plans and Holiday Photos, due to Collection.node_order_by = ['name']. + self.assertLess(alpha_collection.path, self.evil_plans_collection.path) + self.assertLess(alpha_collection.path, self.holiday_photos_collection.path) def test_get_ancestors(self): self.assertEqual( @@ -26,21 +48,21 @@ class TestCollectionTreeOperations(TestCase): def test_get_descendants(self): self.assertEqual( list(self.root_collection.get_descendants().order_by('path')), - [self.holiday_photos_collection, self.evil_plans_collection] + [self.evil_plans_collection, self.holiday_photos_collection] ) self.assertEqual( list(self.root_collection.get_descendants(inclusive=True).order_by('path')), [ self.root_collection, - self.holiday_photos_collection, - self.evil_plans_collection + self.evil_plans_collection, + self.holiday_photos_collection ] ) def test_get_siblings(self): self.assertEqual( list(self.holiday_photos_collection.get_siblings().order_by('path')), - [self.holiday_photos_collection, self.evil_plans_collection] + [self.evil_plans_collection, self.holiday_photos_collection] ) self.assertEqual( list(self.holiday_photos_collection.get_siblings(inclusive=False).order_by('path')), @@ -50,19 +72,19 @@ class TestCollectionTreeOperations(TestCase): def test_get_next_siblings(self): self.assertEqual( list( - self.holiday_photos_collection.get_next_siblings().order_by('path') + self.evil_plans_collection.get_next_siblings().order_by('path') ), - [self.evil_plans_collection] + [self.holiday_photos_collection] ) self.assertEqual( list( self.holiday_photos_collection.get_next_siblings(inclusive=True).order_by('path') ), - [self.holiday_photos_collection, self.evil_plans_collection] + [self.holiday_photos_collection] ) self.assertEqual( list( - self.evil_plans_collection.get_next_siblings().order_by('path') + self.holiday_photos_collection.get_next_siblings().order_by('path') ), [] ) @@ -72,17 +94,17 @@ class TestCollectionTreeOperations(TestCase): list( self.holiday_photos_collection.get_prev_siblings().order_by('path') ), - [] + [self.evil_plans_collection] ) self.assertEqual( list( self.evil_plans_collection.get_prev_siblings().order_by('path') ), - [self.holiday_photos_collection] + [] ) self.assertEqual( list( self.evil_plans_collection.get_prev_siblings(inclusive=True).order_by('path') ), - [self.holiday_photos_collection, self.evil_plans_collection] + [self.evil_plans_collection] ) diff --git a/wagtail/documents/tests/test_admin_views.py b/wagtail/documents/tests/test_admin_views.py index 5d0a28a328..4b62b65006 100644 --- a/wagtail/documents/tests/test_admin_views.py +++ b/wagtail/documents/tests/test_admin_views.py @@ -97,6 +97,15 @@ class TestDocumentIndexView(TestCase, WagtailTestUtils): [collection.name for collection in response.context['collections']], ['Root', 'Evil plans', 'Good plans']) + def test_collection_nesting(self): + root_collection = Collection.get_first_root_node() + evil_plans = root_collection.add_child(name="Evil plans") + evil_plans.add_child(name="Eviler plans") + + response = self.client.get(reverse('wagtaildocs:index')) + # "Eviler Plans" should be prefixed with ↳ (↳) and 4 non-breaking spaces. + self.assertContains(response, '    ↳ Eviler plans') + class TestDocumentAddView(TestCase, WagtailTestUtils): def setUp(self): @@ -128,6 +137,15 @@ class TestDocumentAddView(TestCase, WagtailTestUtils): self.assertContains(response, '