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 = RequestContext(MockRequest, {'page': page}) template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(template.render(context), 'english') # french specified context = RequestContext(MockRequest, {'page': page, 'lang': 'fr'}) template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(template.render(context), 'french') # english specified context = RequestContext(MockRequest, {'page': page, 'lang': 'en-us'}) template = Template('{% load pages_tags %}' '{% get_content page "slug" as content %}' '{{ content }}') self.assertEqual(template.render(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 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_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_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/basic_cms/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 basic_cms.http 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_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 populate_pages(self, parent=None, child=5, depth=5): """Create a population of :class:`Page <pages.models.Page>` for testing purpose.""" from basic_cms.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_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 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 test_po_file_imoprt_export(self): """Test the po files export and import.""" try: import polib except ImportError: return unittest.skip("Polib is not installed") 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')