Phil Gyford
Phil Gyford

Reputation: 14594

How to test order of Pages in Wagtail?

I have a Wagtail site and I have a parent/child structure between a couple of page types:

class FrontPage(Page):
    subpage_types = ["InnerPage"]

    def get_next_page(self):
        return self.get_children().live().first()

class InnerPage(Page):
    parent_page_types = ["FrontPage"]

    def get_next_page(self):
        return self.get_next_siblings().live().first()

The Admin user can drag the InnerPages into their chosen order in the Wagtail Admin, which is great. FrontPage.get_children() then fetches the inner pages in that order, and the get_next_page() methods return the next pages in the same order.

I'd like to write tests to check that the get_next_page() methods do return the correct page. But I can't work out how to assign an order to pages when creating them for tests. I can see that when doing the SQL queries Wagtail orders by wagtailcore_page.path, but I'm not sure how to affect this when programmatically creating InnerPage objects.

Upvotes: 0

Views: 340

Answers (1)

gasman
gasman

Reputation: 25237

Assuming you're using parent_page.add_child(instance=new_page) when creating pages in your test - the add_child method will create the new page as the last child in the ordering, so you just need to make sure to create them in the order you want them to appear in.

For more control over where new pages are inserted in the ordering, you can use the add_sibling method instead:

parent_page = FrontPage.objects.first()
last_child = InnerPage(title='last child')
parent_page.add_child(instance=last_child
first_child = InnerPage(title='first child')
# insert first_child immediately before last_child
last_child.add_sibling(pos='left', instance=first_child)

Upvotes: 1

Related Questions