'Verify documents are sorted by display_order and number of helpful votes.'

def test_document_listing_order(self):

    p = ProductFactory()
    t = TopicFactory(product=p)
    docs = []
    for i in range(3):
        doc = DocumentFactory(products=[p], topics=[t])
        ApprovedRevisionFactory(document=doc)
        docs.append(doc)
    docs[1].display_order = 0
    docs[1].save()
    self.refresh()
    url = reverse('products.documents', args=[p.slug, t.slug])
    r = self.client.get(url, follow=True)
    eq_(200, r.status_code)
    doc = pq(r.content)
    eq_(doc('#document-list    >    ul    >    li:first-child    >    a').text(), docs[1].title)
    rev = docs[2].current_revision
    HelpfulVoteFactory(revision=rev, helpful=True)
    docs[2].save()
    self.refresh()
    cache.clear()
    url = reverse('products.documents', args=[p.slug, t.slug])
    r = self.client.get(url, follow=True)
    eq_(200, r.status_code)
    doc = pq(r.content)
    eq_(doc('#document-list    >    ul    >    li:nth-child(2)    >    a').text(), docs[2].title)
    rev = docs[0].current_revision
    HelpfulVoteFactory(revision=rev, helpful=True)
    HelpfulVoteFactory(revision=rev, helpful=True)
    docs[0].save()
    self.refresh()
    cache.clear()
    r = self.client.get(url, follow=True)
    eq_(200, r.status_code)
    doc = pq(r.content)
    eq_(doc('#document-list    >    ul    >    li:nth-child(2)    >    a').text(), docs[0].title)
