|
25 | 25 | generate_short_url,
|
26 | 26 | get_featured_articles,
|
27 | 27 | get_kb_visited,
|
| 28 | + get_visible_document_or_404, |
28 | 29 | has_visited_kb,
|
29 | 30 | num_active_contributors,
|
30 | 31 | remove_expired_from_kb_visited,
|
31 | 32 | update_kb_visited,
|
32 | 33 | )
|
| 34 | +from django.http import Http404 |
33 | 35 |
|
34 | 36 |
|
35 | 37 | class ActiveContributorsTestCase(TestCase):
|
@@ -700,3 +702,135 @@ def test_with_product_and_topic_2(self):
|
700 | 702 | },
|
701 | 703 | )
|
702 | 704 | self.assertFalse(self.session.modified)
|
| 705 | + |
| 706 | + |
| 707 | +class GetVisibleDocumentTests(TestCase): |
| 708 | + def setUp(self): |
| 709 | + self.user = UserFactory() |
| 710 | + |
| 711 | + self.en_doc = ApprovedRevisionFactory( |
| 712 | + document__locale="en-US", |
| 713 | + document__slug="test-document", |
| 714 | + is_ready_for_localization=True, |
| 715 | + ).document |
| 716 | + |
| 717 | + self.de_rev = TranslatedRevisionFactory( |
| 718 | + document__locale="de", |
| 719 | + document__slug="test-document-de", |
| 720 | + document__parent=self.en_doc, |
| 721 | + based_on=self.en_doc.current_revision, |
| 722 | + ) |
| 723 | + self.de_doc = self.de_rev.document |
| 724 | + |
| 725 | + self.fr_rev = TranslatedRevisionFactory( |
| 726 | + document__locale="fr", |
| 727 | + document__slug="test-document-fr", |
| 728 | + document__parent=self.en_doc, |
| 729 | + based_on=self.en_doc.current_revision, |
| 730 | + ) |
| 731 | + self.fr_doc = self.fr_rev.document |
| 732 | + |
| 733 | + def test_get_document_by_locale_and_slug_direct_match(self): |
| 734 | + """Test getting a document when there's a direct match for locale and slug.""" |
| 735 | + doc = get_visible_document_or_404(self.user, locale="en-US", slug="test-document") |
| 736 | + self.assertEqual(doc, self.en_doc) |
| 737 | + |
| 738 | + doc = get_visible_document_or_404(self.user, locale="de", slug="test-document-de") |
| 739 | + self.assertEqual(doc, self.de_doc) |
| 740 | + |
| 741 | + doc = get_visible_document_or_404(self.user, locale="fr", slug="test-document-fr") |
| 742 | + self.assertEqual(doc, self.fr_doc) |
| 743 | + |
| 744 | + def test_get_document_via_translation_for_nondefault_locale(self): |
| 745 | + """Test getting a document via its parent when no direct match in the requested locale.""" |
| 746 | + with self.assertRaises(Http404): |
| 747 | + # This should raise a 404 because we're explicitly |
| 748 | + # setting look_for_translation_via_parent=False |
| 749 | + get_visible_document_or_404( |
| 750 | + self.user, |
| 751 | + locale="en-US", |
| 752 | + slug="test-document-de", |
| 753 | + look_for_translation_via_parent=False, |
| 754 | + ) |
| 755 | + |
| 756 | + # This should also raise a 404 because cross-locale lookups should only work when |
| 757 | + # look_for_translation_via_parent is True |
| 758 | + with self.assertRaises(Http404): |
| 759 | + get_visible_document_or_404(self.user, locale="en-US", slug="test-document-de") |
| 760 | + |
| 761 | + def test_cross_locale_lookup(self): |
| 762 | + """Test the new cross-locale lookup functionality for language switcher.""" |
| 763 | + doc = get_visible_document_or_404( |
| 764 | + self.user, |
| 765 | + locale="en-US", |
| 766 | + slug="test-document-de", |
| 767 | + look_for_translation_via_parent=True, |
| 768 | + ) |
| 769 | + self.assertEqual(doc, self.en_doc) |
| 770 | + |
| 771 | + doc = get_visible_document_or_404( |
| 772 | + self.user, |
| 773 | + locale="en-US", |
| 774 | + slug="test-document-fr", |
| 775 | + look_for_translation_via_parent=True, |
| 776 | + ) |
| 777 | + self.assertEqual(doc, self.en_doc) |
| 778 | + |
| 779 | + doc = get_visible_document_or_404( |
| 780 | + self.user, locale="de", slug="test-document", look_for_translation_via_parent=True |
| 781 | + ) |
| 782 | + self.assertEqual(doc, self.de_doc) |
| 783 | + |
| 784 | + doc = get_visible_document_or_404( |
| 785 | + self.user, locale="de", slug="test-document-fr", look_for_translation_via_parent=True |
| 786 | + ) |
| 787 | + self.assertEqual(doc, self.de_doc) |
| 788 | + |
| 789 | + def test_fallback_to_parent(self): |
| 790 | + """Test falling back to parent document when translation doesn't exist.""" |
| 791 | + no_es_doc = ApprovedRevisionFactory( |
| 792 | + document__locale="en-US", document__slug="no-spanish" |
| 793 | + ).document |
| 794 | + |
| 795 | + with self.assertRaises(Http404): |
| 796 | + get_visible_document_or_404( |
| 797 | + self.user, locale="es", slug="no-spanish", look_for_translation_via_parent=True |
| 798 | + ) |
| 799 | + |
| 800 | + doc = get_visible_document_or_404( |
| 801 | + self.user, |
| 802 | + locale="es", |
| 803 | + slug="no-spanish", |
| 804 | + look_for_translation_via_parent=True, |
| 805 | + return_parent_if_no_translation=True, |
| 806 | + ) |
| 807 | + self.assertEqual(doc, no_es_doc) |
| 808 | + |
| 809 | + def test_nonexistent_document(self): |
| 810 | + """Test that we get a 404 for documents that don't exist in any locale.""" |
| 811 | + with self.assertRaises(Http404): |
| 812 | + get_visible_document_or_404( |
| 813 | + self.user, |
| 814 | + locale="en-US", |
| 815 | + slug="doesnt-exist", |
| 816 | + look_for_translation_via_parent=True, |
| 817 | + ) |
| 818 | + |
| 819 | + with self.assertRaises(Http404): |
| 820 | + get_visible_document_or_404( |
| 821 | + self.user, locale="de", slug="doesnt-exist", look_for_translation_via_parent=True |
| 822 | + ) |
| 823 | + |
| 824 | + def test_unapproved_revision(self): |
| 825 | + """Test that users can't see documents without approved revisions.""" |
| 826 | + unapproved_doc = RevisionFactory( |
| 827 | + document__locale="en-US", document__slug="unapproved", is_approved=False |
| 828 | + ).document |
| 829 | + |
| 830 | + random_user = UserFactory() |
| 831 | + with self.assertRaises(Http404): |
| 832 | + get_visible_document_or_404(random_user, locale="en-US", slug="unapproved") |
| 833 | + |
| 834 | + creator = unapproved_doc.revisions.first().creator |
| 835 | + doc = get_visible_document_or_404(creator, locale="en-US", slug="unapproved") |
| 836 | + self.assertEqual(doc, unapproved_doc) |
0 commit comments