def test_get_content_tag_bug(self): """ Make sure that {% get_content %} use the "lang" context variable if no language string is provided. """ page_data = {'title': 'test', 'slug': 'english'} page = self.new_page(page_data) Content(page=page, language='fr-ch', type='title', body='french').save() Content(page=page, language='fr-ch', type='slug', body='french').save() self.assertEqual(page.slug(language='fr-ch'), 'french') self.assertEqual(page.slug(language='en-us'), 'english') # default context = {'page': page} template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(render(template, context), 'english') # french specified context = {'page': page, 'lang': 'fr'} template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(render(template, context), 'french') # english specified context = {'page': page, 'lang': 'en-us'} template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(render(template, context), 'english')
def test_get_page_ids_by_slug(self): """ Test that get_page_ids_by_slug work as intented. """ page_data = {'title': 'test1', 'slug': 'test1'} page1 = self.new_page(page_data) self.assertEqual(Content.objects.get_page_ids_by_slug('test1'), [page1.id]) page_data = {'title': 'test1', 'slug': 'test1'} page2 = self.new_page(page_data) self.assertEqual(Content.objects.get_page_ids_by_slug('test1'), [page1.id, page2.id]) Content(page=page1, language='en-us', type='slug', body='test2').save() self.assertEqual(Content.objects.get_page_ids_by_slug('test1'), [page1.id, page2.id]) Content(page=page1, language='en-us', type='slug', body='test1').save() self.assertEqual(Content.objects.get_page_ids_by_slug('test1'), [page1.id, page2.id])
def edit_page_content(request, page_id, lang): lang_form = LanguageForm({'lang': lang}) if not lang_form.is_valid(): return HttpResponse(_(u'Language is not registered in system.') + _(u" Language code: ") + lang) page = get_object_or_404(Page, id=page_id) try: content = Content.objects.get(page=page_id, lang=lang) except Content.DoesNotExist: content = Content(page=page, lang=lang) ContentForm = get_content_form(('page', 'lang')) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form', instance=content) if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('pages:administration:edit_page', id=page_id) else: content_form = ContentForm(prefix='content_form', instance=content) return render(request, 'pages/administration/edit_page_content.html', { 'content': content, 'content_form': content_form, })
def test_01_set_body_pagelink(self): """Test the get_body_pagelink_ids and set_body_pagelink functions.""" self.set_setting("PAGE_LINK_FILTER", True) page1 = self.create_new_page() page2 = self.create_new_page() # page2 has a link on page1 content_string = 'test <a href="%s" class="page_%d">hello</a>' content = Content( page=page2, language='en-us', type='body', body=content_string % ('#', page1.id) ) content.save() self.assertEqual( Content.objects.get_content(page2, 'en-us', 'body'), content_string % (page1.get_url_path(), page1.id) ) self.assertFalse(page2.has_broken_link()) page1.delete() self.assertEqual( Content.objects.get_content(page2, 'en-us', 'body'), 'test <a href="#" class="pagelink_broken">hello</a>' ) self.assertTrue(page2.has_broken_link())
def test_01_set_body_pagelink(self): """Test the get_body_pagelink_ids and set_body_pagelink functions.""" self.set_setting("PAGE_LINK_FILTER", True) try: import BeautifulSoup except ImportError: raise unittest.SkipTest("BeautifulSoup is not installed") page1 = self.create_new_page() page2 = self.create_new_page() # page2 has a link on page1 content_string = 'test <a href="%s" class="page_%d">hello</a>' content = Content(page=page2, language='en-us', type='body', body=content_string % ('#', page1.id)) content.save() self.assertEqual(Content.objects.get_content(page2, 'en-us', 'body'), content_string % (page1.get_url_path(), page1.id)) self.assertFalse(page2.has_broken_link()) page1.delete() self.assertEqual(Content.objects.get_content(page2, 'en-us', 'body'), 'test <a href="#" class="pagelink_broken">hello</a>') self.assertTrue(page2.has_broken_link())
def test_str_method(self): """Problem with encoding __str__ method""" page = self.new_page({"title": u"АБВГДЕЖ"}) content = Content(page=page, type="title", language="fr-ch", body=u"АБВГДЕЖ") content.save() try: str(content) except: self.fail("Cyrilic characters in content should not raise any error")
def test_language_fallback_bug(self): """Language fallback doesn't work properly.""" page = self.create_new_page() c = Content(page=page, type="new_type", body="toto", language="en-us") c.save() self.assertEqual(Content.objects.get_content(page, "en-us", "new_type"), "toto") self.assertEqual(Content.objects.get_content(page, "fr-ch", "new_type"), "") self.assertEqual(Content.objects.get_content(page, "fr-ch", "new_type", True), "toto")
def test_str_method(self): """Problem with encoding __str__ method""" page = self.new_page({'title': u'АБВГДЕЖ'}) content = Content(page=page, type='title', language='fr-ch', body=u"АБВГДЕЖ") content.save() try: str(content) except: self.fail("Cyrilic characters in content should not raise any error")
def test_get_page_from_id_context_variable(self): """Test get_page_from_string_or_id with an id context variable.""" page = self.new_page({"slug": "test"}) self.assertEqual(get_page_from_string_or_id(str(page.id)), page) content = Content(page=page, language="en-us", type="test_id", body=page.id) content.save() context = Context({"current_page": page}) context = RequestContext(MockRequest(), context) template = Template( "{% load pages_tags %}" "{% placeholder test_id as str %}" "{% get_page str as p %}" "{{ p.slug }}" ) self.assertEqual(template.render(context), "test")
def test_language_fallback_bug(self): """Language fallback doesn't work properly.""" page = self.create_new_page() c = Content(page=page, type='new_type', body='toto', language='en') c.save() self.assertEqual(Content.objects.get_content(page, 'en', 'new_type'), 'toto') self.assertEqual(Content.objects.get_content(page, 'fr', 'new_type'), '') self.assertEqual( Content.objects.get_content(page, 'fr', 'new_type', True), 'toto')
def test_get_page_from_id_context_variable(self): """Test get_page_from_string_or_id with an id context variable.""" page = self.new_page({'slug': 'test'}) self.assertEqual(get_page_from_string_or_id(str(page.id)), page) content = Content(page=page, language='en-us', type='test_id', body=page.id) content.save() context = {'current_page': page} template = Template('{% load pages_tags %}' '{% placeholder test_id as str %}' '{% get_page str as p %}' '{{ p.slug }}') self.assertEqual(render(template, context), 'test')
def test_automatic_slug_renaming(self): """Test a slug renaming.""" self.set_setting("PAGE_AUTOMATIC_SLUG_RENAMING", True) c = self.get_admin_client() page_data = self.get_new_page_data() page_data['slug'] = "slug" response = c.post('/admin/pages/page/add/', page_data) self.assertRedirects(response, '/admin/pages/page/') response = c.post('/admin/pages/page/add/', page_data) self.assertRedirects(response, '/admin/pages/page/') response = c.post('/admin/pages/page/add/', page_data) self.assertRedirects(response, '/admin/pages/page/') slug = page_data['slug'] page1 = Content.objects.get_content_slug_by_slug(slug).page page2 = Content.objects.get_content_slug_by_slug(slug+'-2').page page3 = Content.objects.get_content_slug_by_slug(slug+'-3').page self.assertNotEqual(page1.id, page2.id) self.assertNotEqual(page2.id, page3.id) self.assertEqual(Content.objects.filter(type="slug").count(), 3) # post again on page 3 doesn't change the slug page_3_slug = page3.slug() page_data['slug'] = page_3_slug response = c.post('/admin/pages/page/%d/' % page3.id, page_data) self.assertRedirects(response, '/admin/pages/page/') self.assertEqual(Content.objects.filter(type="slug").count(), 3) content = Content.objects.get_content_slug_by_slug(page_3_slug) self.assertEqual(page3.id, content.page.id) # change an old slug of another page and see that it doesn't # influence the current slug of this page old_slug = Content.objects.filter(page=page1).latest("creation_date") new_slug = Content(page=page1, body=page_3_slug, type="slug") new_slug.creation_date = old_slug.creation_date - datetime.timedelta(seconds=5) new_slug.save() self.assertEqual(Content.objects.filter(type="slug").count(), 4) # check than the old slug doesn't trigger a new slug for page 3 response = c.post('/admin/pages/page/%d/' % page3.id, page_data) content = Content.objects.get_content_slug_by_slug(page_3_slug) self.assertEqual(page3.id, content.page.id) self.assertEqual(Content.objects.filter(type="slug").count(), 4) new_slug.creation_date = old_slug.creation_date + datetime.timedelta(seconds=5) new_slug.save() # check than the new slug does trigger a new slug for page 3 response = c.post('/admin/pages/page/%d/' % page3.id, page_data) content = Content.objects.get_content_slug_by_slug(page_3_slug) self.assertEqual(page1.id, content.page.id) content = Content.objects.get_content_slug_by_slug(page_3_slug+'-2') self.assertEqual(page3.id, content.page.id)
def test_bug_172(self): """Test bug 167 http://code.google.com/p/django-page-cms/issues/detail?id=172""" c = self.get_admin_client() c.login(username='******', password='******') page_data = self.get_new_page_data() page_data['title'] = 'title-en-us' page_data['slug'] = 'slug' response = c.post('/admin/pages/page/add/', page_data) page = Content.objects.get_content_slug_by_slug('slug').page Content(page=page, type='title', language='fr-ch', body="title-fr-ch").save() from pages.utils import get_request_mock request = get_request_mock() temp = loader.get_template('pages/tests/test3.html') render = temp.render(RequestContext(request, {'page': page})) self.assertTrue('title-en-us' in render) render = temp.render( RequestContext(request, { 'page': page, 'lang': 'fr-ch' })) self.assertTrue('title-fr-ch' in render)
def test_po_file_imoprt_export(self): """Test the po files export and import.""" page1 = self.new_page(content={'slug':'page1', 'title':'english title'}) page1.save() #Content(page=page1, language='en-us', type='title', body='toto').save() Content(page=page1, language='fr-ch', type='title', body='french title').save() page1.invalidate() import StringIO stdout = StringIO.StringIO() # TODO: might be nice to use a temp dir for this test export_po_files(path='potests', stdout=stdout) self.assertTrue("Export language fr-ch" in stdout.getvalue()) f = open("potests/fr-ch.po", "r+") old = f.read().replace('french title', 'translated') f.seek(0) f.write(old) f.close() stdout = StringIO.StringIO() import_po_files(path='potests', stdout=stdout) self.assertTrue("Update language fr-ch" in stdout.getvalue()) self.assertTrue(("Update page %d" % page1.id) in stdout.getvalue()) self.assertTrue(page1.title(language='fr-ch'), 'translated')
def populate_pages(self, parent=None, child=5, depth=5): """Create a population of :class:`Page <pages.models.Page>` for testing purpose.""" from pages.models import Content author = User.objects.all()[0] if depth == 0: return p = self.model(parent=parent, author=author, status=self.model.PUBLISHED) p.save() p = self.get(id=p.id) Content(body='page-' + str(p.id), type='title', language=settings.PAGE_DEFAULT_LANGUAGE, page=p).save() Content(body='page-' + str(p.id), type='slug', language=settings.PAGE_DEFAULT_LANGUAGE, page=p).save() for child in range(1, child + 1): self.populate_pages(parent=p, child=child, depth=(depth - 1))
def test_language_fallback_bug(self): """Language fallback doesn't work properly.""" page = self.create_new_page() c = Content(page=page, type='new_type', body='toto', language='en-us') c.save() self.assertEqual( Content.objects.get_content(page, 'en-us', 'new_type'), 'toto' ) self.assertEqual( Content.objects.get_content(page, 'fr-ch', 'new_type'), '' ) self.assertEqual( Content.objects.get_content(page, 'fr-ch', 'new_type', True), 'toto' )
def new_page(self, content={'title': 'test-page'}, language='en-us'): author = User.objects.all()[0] page = Page.objects.create(author=author, status=Page.PUBLISHED, template='pages/examples/index.html') if pages_settings.PAGE_USE_SITE_ID: page.sites.add(Site.objects.get(id=1)) # necessary to clear old URL cache page.invalidate() for key, value in content.items(): Content(page=page, language='en-us', type=key, body=value).save() return page
def test_show_absolute_url_with_language(self): """ Test a {% show_absolute_url %} template tag bug. """ page_data = {'title': 'english', 'slug': 'english'} page = self.new_page(page_data) Content(page=page, language='fr-ch', type='title', body='french').save() Content(page=page, language='fr-ch', type='slug', body='french').save() self.assertEqual(page.get_url_path(language='fr-ch'), self.get_page_url(u'french')) self.assertEqual(page.get_url_path(language='en-us'), self.get_page_url(u'english')) context = RequestContext(MockRequest, {'page': page}) template = Template('{% load pages_tags %}' '{% show_absolute_url page "en-us" %}') self.assertEqual(template.render(context), self.get_page_url(u'english')) template = Template('{% load pages_tags %}' '{% show_absolute_url page "fr-ch" %}') self.assertEqual(template.render(context), self.get_page_url('french'))
def edit_page_content(request, page_id, lang): lang_form = LanguageForm({'lang': lang}) if not lang_form.is_valid(): return HttpResponse( _(u'Language is not registered in system.') + _(u" Language code: ") + lang) page = get_object_or_404(Page, id=page_id) try: content = Content.objects.get(page=page_id, lang=lang) except Content.DoesNotExist: content = Content(page=page, lang=lang) ContentForm = get_content_form(('page', 'lang')) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form', instance=content) if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('pages:administration:edit_page', id=page_id) else: content_form = ContentForm(prefix='content_form', instance=content) return render(request, 'pages/administration/edit_page_content.html', { 'page': page, 'content': content, 'content_form': content_form, })
def test_bug_172(self): """Test bug 167 http://code.google.com/p/django-page-cms/issues/detail?id=172""" c = self.get_admin_client() page_data = self.get_new_page_data() page_data['title'] = 'title-en-us' page_data['slug'] = 'slug' response = c.post(add_url, page_data) page = Content.objects.get_content_slug_by_slug('slug').page Content(page=page, type='title', language='fr-ch', body="title-fr-ch").save() request = get_request_mock() temp = loader.get_template('pages/tests/test3.html') render = temp.render({'page': page}) self.assertTrue('title-en-us' in render) render = temp.render({'page': page, 'lang': 'fr-ch'}) self.assertTrue('title-fr-ch' in render)
def test_page_sitemap(self): """ Test the sitemap class """ c = self.get_admin_client() page1 = self.new_page(content={'slug': 'english-slug'}) page1.save() Content(page=page1, language='fr-ch', type='slug', body='french-slug').save() response = c.get('/sitemap.xml') self.assertContains(response, 'english-slug') self.assertNotContains(response, 'french-slug') response = c.get('/sitemap2.xml') self.assertContains(response, 'english-slug') self.assertContains(response, 'french-slug')
def new_page(content={ 'title': 'test-page', 'slug': 'test-page-slug' }, parent=None, delegate_to=None, language='en', template='pages/examples/index.html'): author = get_user_model().objects.all()[0] page = Page.objects.create(author=author, status=Page.PUBLISHED, template=template, parent=parent, delegate_to=delegate_to) if pages_settings.PAGE_USE_SITE_ID: page.sites.add(Site.objects.get(id=1)) # necessary to clear old URL cache page.invalidate() for key, value in list(content.items()): Content(page=page, language='en', type=key, body=value).save() return page