Skip to content
Browse files

Add MultilingualTestCase.test_create_page ensuring some testing on th…

…e admin's multilingual features.
  • Loading branch information...
1 parent 0cb4af0 commit 59bfedd41dda73e26ef261e12daa9be47f4fa6b8 @benjaoming benjaoming committed Jan 26, 2013
Showing with 140 additions and 6 deletions.
  1. +6 −3 cms/admin/forms.py
  2. +2 −2 cms/admin/pageadmin.py
  3. +2 −0 cms/models/pagemodel.py
  4. +33 −0 cms/test_utils/testcases.py
  5. +97 −1 cms/tests/multilingual.py
View
9 cms/admin/forms.py
@@ -3,7 +3,7 @@
from cms.apphook_pool import apphook_pool
from cms.forms.widgets import UserSelectAdminWidget
from cms.models import (Page, PagePermission, PageUser, ACCESS_PAGE,
- PageUserGroup)
+ PageUserGroup, titlemodels)
from cms.utils.conf import get_cms_setting
from cms.utils.i18n import get_language_tuple, get_language_list
from cms.utils.mail import mail_page_user_change
@@ -116,8 +116,11 @@ def clean(self):
#Check for titles attached to the page makes sense only because
#AdminFormsTests.test_clean_overwrite_url validates the form with when no page instance available
#Looks like just a theoretical corner case
- title = page.get_title_obj(lang)
- if title and slug:
+ try:
+ title = page.get_title_obj(lang, fallback=False)
+ except titlemodels.Title.DoesNotExist:
+ title = None
+ if title and not isinstance(title, titlemodels.EmptyTitle) and slug:
oldslug = title.slug
title.slug = slug
title.save()
View
4 cms/admin/pageadmin.py
@@ -34,7 +34,7 @@
from cms.exceptions import NoPermissionsException
from cms.forms.widgets import PluginEditor
from cms.models import (Page, Title, CMSPlugin, PagePermission,
- PageModeratorState, EmptyTitle, GlobalPagePermission)
+ PageModeratorState, EmptyTitle, GlobalPagePermission, titlemodels)
from cms.models.managers import PagePermissionsPermissionManager
from cms.models.placeholdermodel import Placeholder
from cms.plugin_pool import plugin_pool
@@ -342,7 +342,7 @@ def get_form(self, request, obj=None, **kwargs):
try:
title_obj = obj.get_title_obj(language=language, fallback=False, version_id=version_id, force_reload=True)
- except:
+ except titlemodels.Title.DoesNotExist:
title_obj = EmptyTitle()
if form.base_fields['site'].initial is None:
form.base_fields['site'].initial = obj.site
View
2 cms/models/pagemodel.py
@@ -603,6 +603,8 @@ def get_cached_ancestors(self, ascending=True):
def get_title_obj(self, language=None, fallback=True, version_id=None, force_reload=False):
"""Helper function for accessing wanted / current title.
If wanted title doesn't exists, EmptyTitle instance will be returned.
+ If fallback=False is used, titlemodels.Title.DoesNotExist will be raised
+ when a language does not exist.
"""
language = self._get_title_cache(language, fallback, version_id, force_reload)
if language in self.title_cache:
View
33 cms/test_utils/testcases.py
@@ -21,6 +21,7 @@
URL_CMS_PAGE = "/en/admin/cms/page/"
URL_CMS_PAGE_ADD = urljoin(URL_CMS_PAGE, "add/")
URL_CMS_PAGE_CHANGE = urljoin(URL_CMS_PAGE, "%d/")
+URL_CMS_PAGE_CHANGE_LANGUAGE = URL_CMS_PAGE_CHANGE + "?language=%s"
URL_CMS_PAGE_DELETE = urljoin(URL_CMS_PAGE_CHANGE, "delete/")
URL_CMS_PLUGIN_ADD = urljoin(URL_CMS_PAGE_CHANGE, "add-plugin/")
URL_CMS_PLUGIN_EDIT = urljoin(URL_CMS_PAGE_CHANGE, "edit-plugin/")
@@ -129,6 +130,38 @@ def get_new_page_data(self, parent_id=''):
self.counter = self.counter + 1
return page_data
+
+ def get_new_page_data_dbfields(self, parent=None, site=None,
+ language=None,
+ template='nav_playground.html',):
+ page_data = {
+ 'title': 'test page %d' % self.counter,
+ 'slug': 'test-page-%d' % self.counter,
+ 'language': settings.LANGUAGES[0][0] if not language else language,
+ 'template': template,
+ 'parent': parent if parent else None,
+ 'site': site if site else Site.objects.get_current(),
+ }
+ self.counter = self.counter + 1
+ return page_data
+
+
+ def get_pagedata_from_dbfields(self, page_data):
+ """Converts data created by get_new_page_data_dbfields to data
+ created from get_new_page_data so you can switch between test cases
+ in api.create_page and client.post"""
+ page_data['site'] = page_data['site'].id
+ page_data['parent'] = page_data['parent'].id if page_data['parent'] else ''
+ # required only if user haves can_change_permission
+ page_data['pagepermission_set-TOTAL_FORMS'] = 0
+ page_data['pagepermission_set-INITIAL_FORMS'] = 0
+ page_data['pagepermission_set-MAX_NUM_FORMS'] = 0
+ page_data['pagepermission_set-2-TOTAL_FORMS'] = 0
+ page_data['pagepermission_set-2-INITIAL_FORMS'] = 0
+ page_data['pagepermission_set-2-MAX_NUM_FORMS'] = 0
+ return page_data
+
+
def print_page_structure(self, qs):
"""Just a helper to see the page struct.
"""
View
98 cms/tests/multilingual.py
@@ -2,11 +2,17 @@
from __future__ import with_statement
import copy
from cms.api import create_page, create_title, publish_page, add_plugin
-from cms.test_utils.testcases import SettingsOverrideTestCase
+from cms.models import Title
+from cms.test_utils.testcases import (CMSTestCase, SettingsOverrideTestCase,
+ URL_CMS_PAGE_ADD,
+ URL_CMS_PAGE, URL_CMS_PAGE_CHANGE,
+ URL_CMS_PAGE_CHANGE_LANGUAGE)
from cms.test_utils.util.context_managers import SettingsOverride
from cms.test_utils.util.mock import AttributeObject
from cms.utils import get_cms_setting
+from cms.utils.conf import get_languages
from django.conf import settings
+from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect
@@ -21,6 +27,96 @@ class MultilingualTestCase(SettingsOverrideTestCase):
}
+ def test_create_page(self):
+ """
+ Test that a page can be created
+ and that a new language can be created afterwards in the admin pages
+
+ TODO: Use get_languages as done in test_create_page in the other
+ methods of MultilingualTestCase ?
+ """
+
+ site = Site.objects.get_current()
+
+ # Create a new page
+
+ # Use the very first language in the list of languages
+ # for the current site
+ TESTLANG = get_languages()[site.pk][0]['code']
+ page_data = self.get_new_page_data_dbfields(
+ site=site,
+ language=TESTLANG
+ )
+
+ page = create_page(**page_data)
+ title = page.get_title_obj()
+
+ # A title is set?
+ self.assertNotEqual(title, None)
+
+ # Publish and unpublish the page
+ page.published = True
+ page.save()
+ page.published = False
+ page.save()
+
+ # Has correct title and slug after calling save()?
+ self.assertEqual(page.get_title(), page_data['title'])
+ self.assertEqual(page.get_slug(), page_data['slug'])
+ self.assertEqual(page.placeholders.all().count(), 2)
+
+ # Were public instances created?
+ title = Title.objects.drafts().get(slug=page_data['slug'])
+
+ # Test that it's the default language
+ self.assertEqual(title.language, TESTLANG)
+
+ # Do stuff using admin pages
+ superuser = self.get_superuser()
+ with self.login_user_context(superuser):
+
+ page_data = self.get_pagedata_from_dbfields(page_data)
+
+ # Publish page using the admin
+ page_data['published'] = True
+ response = self.client.post(URL_CMS_PAGE_CHANGE_LANGUAGE % (page.pk, TESTLANG),
+ page_data)
+
+ page = page.reload()
+ self.assertTrue(page.published)
+
+ # Create a different language using the edit admin page
+ # This test case is bound in actual experience...
+ # pull#1604
+ page_data2 = page_data.copy()
+ page_data2['title'] = 'ein Titel'
+ page_data2['slug'] = 'ein-slug'
+ TESTLANG2 = 'de'
+ page_data2['language'] = TESTLANG2
+
+ # Ensure that the language version is not returned
+ # since it does not exist
+ self.assertRaises(Title.DoesNotExist,
+ page.get_title_obj,
+ language=TESTLANG2, fallback=False)
+
+ # Now create it
+ response = self.client.post(URL_CMS_PAGE_CHANGE_LANGUAGE % (page.pk, TESTLANG2),
+ page_data2)
+
+ page = page.reload()
+
+ # Test the new language version
+ self.assertEqual(page.get_title(language=TESTLANG2), page_data2['title'])
+ self.assertEqual(page.get_slug(language=TESTLANG2), page_data2['slug'])
+
+ # Test the default language version (TESTLANG)
+ self.assertEqual(page.get_slug(language=TESTLANG, fallback=False), page_data['slug'])
+ self.assertEqual(page.get_title(language=TESTLANG, fallback=False), page_data['title'])
+ self.assertEqual(page.get_slug(fallback=False), page_data['slug'])
+ self.assertEqual(page.get_title(fallback=False), page_data['title'])
+
+
def test_multilingual_page(self):
page = create_page("mlpage", "nav_playground.html", "en")
create_title("de", page.get_title(), page, slug=page.get_slug())

0 comments on commit 59bfedd

Please sign in to comment.
Something went wrong with that request. Please try again.