Exemplo n.º 1
0
    def test_duplicate_slug(self):
        from wagtail.wagtailcore.models import Site
        # Create a test Site with a root page
        root = models.TestRootPage(title='title', depth=1, path='0001', slug_en='slug_en', slug_de='slug_de')
        root.save()

        site = Site(root_page=root)
        site.save()

        # Add children to the root
        child = root.add_child(
            instance=models.TestSlugPage1(title='child1', slug_de='child', slug_en='child-en', depth=2, path='00010001')
        )

        child2 = root.add_child(
            instance=models.TestSlugPage2(title='child2', slug_de='child-2', slug_en='child2-en', depth=2,
                                          path='00010002')
        )

        # Clean should work fine as the two slugs are different
        child2.clean()

        # Make the slug equal to test if the duplicate is detected
        child2.slug_de = 'child'

        self.assertRaises(ValidationError, child2.clean)
Exemplo n.º 2
0
    def test_relative_url(self):
        try:
            from wagtail.core.models import Site
        except ImportError:
            from wagtail.wagtailcore.models import Site
        # Create a test Site with a root page
        root = models.TestRootPage(title='title slugurl',
                                   depth=1,
                                   path='0004',
                                   slug_en='title_slugurl_en',
                                   slug_de='title_slugurl_de')
        root.save()
        site = Site(root_page=root)
        site.save()

        # Add children to the root
        child = root.add_child(
            instance=models.TestSlugPage1(title='child1 slugurl',
                                          slug_en='child-slugurl-en',
                                          slug_de='child-slugurl-de',
                                          depth=2,
                                          path='00040001'))
        child.save_revision().publish()

        url_1_de = child.relative_url(site)
        self.assertEqual(
            url_1_de, '/de/child-slugurl-de/',
            'When using the default language, slugurl produces the wrong url.')

        trans_real.activate('en')

        url_1_en = child.relative_url(site)
        self.assertEqual(
            url_1_en, '/en/child-slugurl-en/',
            'When using non-default language, slugurl produces the wrong url.')

        # Add children using non-default language
        child2 = root.add_child(
            instance=models.TestSlugPage2(title='child2 slugurl',
                                          title_de='child2 slugurl DE',
                                          slug_de='child2-slugurl-de',
                                          slug_en='child2-slugurl-en',
                                          depth=2,
                                          path='00040002'))
        child2.save_revision().publish()

        url_2_en = child2.relative_url(site)
        self.assertEqual(
            url_2_en, '/en/child2-slugurl-en/',
            'When using non-default language, slugurl produces the wrong url.')

        trans_real.activate('de')

        url_2_de = child2.relative_url(site)
        self.assertEqual(
            url_2_de, '/de/child2-slugurl-de/',
            'When using non-default language, slugurl produces the wrong url.')
Exemplo n.º 3
0
    def test_set_url_path(self):
        from wagtail.wagtailcore.models import Site
        # Create a test Site with a root page
        root = models.TestRootPage(title='url paths', depth=1, path='0006', slug='url-path-slug')
        root.save()

        site = Site(root_page=root)
        site.save()

        # Add children to the root
        child = root.add_child(
            instance=models.TestSlugPage1(title='child', slug='child', depth=2, path='00060001')
        )
        child.save()

        # Add grandchildren to the root
        grandchild = child.add_child(
            instance=models.TestSlugPage1(title='grandchild', slug='grandchild', depth=2, path='000600010001')
        )
        grandchild.save()

        self.assertEqual(child.url_path_de, '/child/')
        self.assertEqual(child.url_path_en, '/child/')
        self.assertEqual(grandchild.url_path_de, '/child/grandchild/')
        self.assertEqual(grandchild.url_path_en, '/child/grandchild/')

        grandchild.slug_de = 'grandchild1'
        grandchild.save()

        self.assertEqual(grandchild.url_path_de, '/child/grandchild1/')
        self.assertEqual(grandchild.url_path_en, '/child/grandchild1/')

        grandchild.slug_en = 'grandchild1_en'
        grandchild.save()

        self.assertEqual(grandchild.url_path_de, '/child/grandchild1/')
        self.assertEqual(grandchild.url_path_en, '/child/grandchild1_en/')

        # Children url paths should update when parent changes
        child.slug_en = 'child_en'
        child.save()

        self.assertEqual(child.url_path_de, '/child/')
        self.assertEqual(child.url_path_en, '/child_en/')

        # We should retrieve grandchild with the below command:
        # grandchild_new = models.TestSlugPage1.objects.get(id=grandchild.id)
        # but it's exhibiting strange behaviour during tests. See:
        # https://github.com/infoportugal/wagtail-modeltranslation/issues/103#issuecomment-352006610
        grandchild_new = models.TestSlugPage1._default_manager.raw("""
            SELECT page_ptr_id, url_path_en, url_path_de FROM {}
            WHERE page_ptr_id=%s LIMIT 1
        """.format(models.TestSlugPage1._meta.db_table), [grandchild.page_ptr_id])[0]
        self.assertEqual(grandchild_new.url_path_en, '/child_en/grandchild1_en/')
        self.assertEqual(grandchild_new.url_path_de, '/child/grandchild1/')
Exemplo n.º 4
0
    def test_original_slug_update(self):
        from wagtail.wagtailcore.models import Page
        # save the page in the default language
        root = models.TestRootPage(title='original slug', title_de='originalschnecke', depth=1, path='0002',
                                   slug_en='test-slug-en', slug_de='test-slug-de')
        root.save()

        # some control checks, we don't expect any surprises here
        self.assertEqual(root.slug, 'test-slug-de', 'slug has the wrong value.')
        self.assertEqual(root.slug_de, 'test-slug-de', 'slug_de has the wrong value.')
        self.assertEqual(root.slug_en, 'test-slug-en', 'slug_en has the wrong value.')

        # fetches the correct Page using slug
        page = Page.objects.filter(slug='test-slug-de').first()
        self.assertEqual(page.specific, root, 'The wrong page was retrieved from DB.')

        trans_real.activate('en')

        # fetches the correct Page using slug using non-default language
        page = Page.objects.filter(slug='test-slug-de').first()
        self.assertEqual(page.specific, root, 'The wrong page was retrieved from DB.')

        # save the page 2 in the non-default language
        root2 = models.TestRootPage(title='original slug 2', title_de='originalschnecke 2', depth=1, path='0003',
                                    slug_en='test-slug2-en', slug_de='test-slug2-de')
        root2.save()

        # sanity checks
        self.assertEqual(root2.slug, 'test-slug2-en', 'slug has the wrong value.')
        self.assertEqual(root2.slug_de, 'test-slug2-de', 'slug_de has the wrong value.')
        self.assertEqual(root2.slug_en, 'test-slug2-en', 'slug_en has the wrong value.')

        # fetches the correct Page using slug using non-default language
        page = Page.objects.filter(slug='test-slug2-de').first()
        self.assertEqual(page.specific, root2, 'The wrong page was retrieved from DB.')

        trans_real.activate('de')

        # fetches the correct Page using slug using default language
        page = Page.objects.filter(slug='test-slug2-de').first()
        self.assertEqual(page.specific, root2, 'The wrong page was retrieved from DB.')