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/')
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)
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.')
def test_set_url_path(self): """ Assert translation URL Paths are correctly set in page and descendants for a slug change and page move operations """ from wagtail.wagtailcore.models import Site # Create a test Site with a root page root = models.TestRootPage.objects.create(title='url paths', depth=1, path='0006', slug='url-path-slug') Site.objects.create(root_page=root) # 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() # check everything is as expected 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/') # PAGE SLUG CHANGE 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/') # Retrieve grandchild from DB: grandchild_new = models.TestSlugPage1.objects.get(id=grandchild.id) self.assertEqual(grandchild_new.url_path_en, '/child_en/grandchild1_en/') self.assertEqual(grandchild_new.url_path_de, '/child/grandchild1/') # Add 2nd child to the root child2 = root.add_child( instance=models.TestSlugPage1(title='child2', slug='child2', depth=2, path='00060002') ) child2.save() # Add grandchildren grandchild2 = child2.add_child( instance=models.TestSlugPage1(title='grandchild2', slug='grandchild2', depth=3, path='000600020001') ) grandchild2.save() # PAGE MOVE child2.move(child, pos='last-child') # re-fetch child2 to confirm db fields have been updated child2 = models.TestSlugPage1.objects.get(id=child2.id) self.assertEqual(child2.depth, 3) self.assertEqual(child2.get_parent().id, child.id) self.assertEqual(child2.url_path_de, '/child/child2/') self.assertEqual(child2.url_path_en, '/child_en/child2/') # children of child2 should also have been updated grandchild2 = child2.get_children().get(slug='grandchild2').specific self.assertEqual(grandchild2.depth, 4) self.assertEqual(grandchild2.url_path_de, '/child/child2/grandchild2/') self.assertEqual(grandchild2.url_path_en, '/child_en/child2/grandchild2/')