Repository: wagtail/wagtail
File: wagtail/documents/tests/test_admin_views.py (Python)

Code before:
    def test_search_and_order_by_created_at(self):
        # Create Documents
        doc1 = models.Document.objects.create(title="recent good Document")
        doc1.created_at = local_datetime(2024, 1, 1)
        doc1.save()

        doc2 = models.Document.objects.create(title="latest good Document")
        doc2.created_at = local_datetime(2025, 1, 1)
        doc2.save()

        old_doc = models.Document.objects.create(title="Oldest good document")
        old_doc.created_at = local_datetime(2023, 1, 1)
        old_doc.save()

        response = self.get({"q": "good", "ordering": "created_at"})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context["query_string"], "good")

        # Check that the documents are ordered by created_at (oldest first)
        documents = list(response.context["page_obj"].object_list)
        self.assertEqual(documents, [old_doc, doc1, doc2])

        # Verify that the 'ordering' filter has the correct value in the response
        self.assertIn("ordering", response.context)
        self.assertEqual(response.context["ordering"], "created_at")
        self.assertTrue(documents[0], old_doc)


class TestDocumentIndexResultsView(WagtailTestUtils, TransactionTestCase):
    def setUp(self):
        Collection.add_root(name="Root")
        self.login()

    def get(self, params={}):
        return self.client.get(reverse("wagtaildocs:index_results"), params)

    def test_search(self):
        doc = models.Document.objects.create(title="A boring report")

        response = self.get({"q": "boring"})
        params = urlencode({"next": "/admin/documents/?q=boring"})
        self.assertEqual(response.status_code, 200)
        # 'next' param on edit page link should point back to the documents index, not the results view
        self.assertContains(response, f"/admin/documents/edit/{doc.pk}/?{params}")
        self.assertNotContains(response, "<th>Collection</th>", html=True)
        self.assertNotContains(response, "<td>Root</td>", html=True)

    def test_search_with_collection(self):
        root_collection = Collection.get_first_root_node()
        root_collection.add_child(name="Evil plans")
        doc = models.Document.objects.create(title="A boring report")

Review comment (suggestion):
This should be `assertEqual`. The `self.assertEqual(documents, [old_doc, doc1, doc2])` a few lines above already ensures the first element is the `old_doc` though, so there's no need to check this

```suggestion
```

It would be more useful if we have another `get()` that uses the descending order (`-created_at`) to ensure it works too

Code after:
    def test_search_and_order_by_created_at(self):
        # Create Documents, change their created_at dates after creation as
        # the field has auto_now_add=True
        doc1 = models.Document.objects.create(title="recent good Document")
        doc1.created_at = local_datetime(2024, 1, 1)
        doc1.save()

        doc2 = models.Document.objects.create(title="latest ok Document")
        doc2.created_at = local_datetime(2025, 1, 1)
        doc2.save()

        doc3 = models.Document.objects.create(title="oldest good document")
        doc3.created_at = local_datetime(2023, 1, 1)
        doc3.save()

        cases = [
            ("created_at", [doc3, doc1]),
            ("-created_at", [doc1, doc3]),
        ]

        for ordering, expected_docs in cases:
            with self.subTest(ordering=ordering):
                response = self.get({"q": "good", "ordering": ordering})
                self.assertEqual(response.status_code, 200)
                self.assertEqual(response.context["query_string"], "good")

                # Check that the documents are filtered by the search query
                # and are in the correct order
                documents = list(response.context["page_obj"].object_list)
                self.assertEqual(documents, expected_docs)
                self.assertIn("ordering", response.context)
                self.assertEqual(response.context["ordering"], ordering)


class TestDocumentIndexResultsView(WagtailTestUtils, TransactionTestCase):
    def setUp(self):
        Collection.add_root(name="Root")
        self.login()

    def get(self, params={}):
        return self.client.get(reverse("wagtaildocs:index_results"), params)

    def test_search(self):
        doc = models.Document.objects.create(title="A boring report")

        response = self.get({"q": "boring"})
        params = urlencode({"next": "/admin/documents/?q=boring"})
        self.assertEqual(response.status_code, 200)
        # 'next' param on edit page link should point back to the documents index, not the results view
        self.assertContains(response, f"/admin/documents/edit/{doc.pk}/?{params}")
        self.assertNotContains(response, "<th>Collection</th>", html=True)