diff --git a/wiki/wiki/doctype/wiki_page/test_wiki_page.py b/wiki/wiki/doctype/wiki_page/test_wiki_page.py index 02888d697..6cc77176d 100644 --- a/wiki/wiki/doctype/wiki_page/test_wiki_page.py +++ b/wiki/wiki/doctype/wiki_page/test_wiki_page.py @@ -69,6 +69,74 @@ def test_wiki_page_lifecycle(self): 2, ) + def test_get_context_orders_revisions_by_creation_not_modified(self): + """`current_revision` is the newest revision, `previous_revision` the next. + + Frappe supplies a default `ORDER BY` from the doctype's `sort_field`, + which for Wiki Page Revision is `modified DESC`. That is not the same + thing as newest-first: touching an old revision's row moves it to the + front and it is then presented as the current one. This gives `modified` + the opposite order to `creation` so the two orderings disagree, and + asserts the chronological answer. + """ + space = frappe.new_doc("Wiki Space") + space.route = "wiki-revision-order-space" + space.append("wiki_sidebars", {"parent_label": "Group", "wiki_page": self.wiki_page.name}) + space.insert() + self.addCleanup(lambda: frappe.delete_doc("Wiki Space", space.name, force=True)) + + # `after_insert` already made one revision; add two more, oldest first. + ordered = frappe.db.get_all( + "Wiki Page Revision", filters={"wiki_page": self.wiki_page.name}, pluck="name" + ) + for content in ("second", "third"): + revision = frappe.new_doc("Wiki Page Revision") + revision.append("wiki_pages", {"wiki_page": self.wiki_page.name}) + revision.content = content + revision.message = content + revision.insert() + ordered.append(revision.name) + + self.assertEqual(len(ordered), 3) + oldest, middle, newest = ordered + + # `creation` ascends with insertion; `modified` runs the other way, so + # the default `modified DESC` ordering puts the *oldest* revision first. + for name, created, modified in ( + (oldest, "2024-01-01 10:00:00", "2024-06-03 10:00:00"), + (middle, "2024-01-02 10:00:00", "2024-06-02 10:00:00"), + (newest, "2024-01-03 10:00:00", "2024-06-01 10:00:00"), + ): + frappe.db.set_value( + "Wiki Page Revision", name, {"creation": created, "modified": modified}, + update_modified=False, + ) + + context = frappe._dict() + frappe.get_doc("Wiki Page", self.wiki_page.name).get_context(context) + + self.assertEqual(context.current_revision.name, newest) + self.assertEqual(context.previous_revision.name, middle) + self.assertNotIn(oldest, [context.current_revision.name, context.previous_revision.name]) + + def test_get_context_handles_a_page_with_one_revision(self): + """A single revision still yields the placeholder for the previous one.""" + space = frappe.new_doc("Wiki Space") + space.route = "wiki-single-revision-space" + space.append("wiki_sidebars", {"parent_label": "Group", "wiki_page": self.wiki_page.name}) + space.insert() + self.addCleanup(lambda: frappe.delete_doc("Wiki Space", space.name, force=True)) + + self.assertEqual( + len(frappe.db.get_all("Wiki Page Revision", filters={"wiki_page": self.wiki_page.name})), 1 + ) + + context = frappe._dict() + frappe.get_doc("Wiki Page", self.wiki_page.name).get_context(context) + + self.assertEqual(context.previous_revision["name"], "") + self.assertEqual(context.previous_revision["content"], "