Back to index

plone3  3.1.7
test_negotiator.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     Negotiator tests.
00004 """
00005 
00006 import unittest
00007 
00008 import zope.component
00009 from zope.component.testing import setUp, tearDown
00010 from zope.configuration.xmlconfig import XMLConfig
00011 from zope.i18n.interfaces import IUserPreferredLanguages
00012 from zope.interface import implements
00013 
00014 from zope.testing import doctest
00015 from zope.testing.doctestunit import DocTestSuite
00016 
00017 import plone.i18n.negotiator
00018 from plone.i18n.negotiator.negotiator import Negotiator
00019 
00020 
00021 class Env(object):
00022     implements(IUserPreferredLanguages)
00023 
00024     def __init__(self, langs=()):
00025         self.langs = langs
00026 
00027     def getPreferredLanguages(self):
00028         return self.langs
00029 
00030 
00031 class TestRequest(dict):
00032 
00033     def __init__(self, languages):
00034         self["HTTP_ACCEPT_LANGUAGE"] = languages
00035 
00036 
00037 def configurationSetUp(self):
00038     setUp()
00039     XMLConfig('meta.zcml', zope.component)()
00040     XMLConfig('configure.zcml', plone.i18n.negotiator)()
00041 
00042 def testDefaultNegotiator():
00043     """
00044       >>> negotiator = Negotiator()
00045 
00046       >>> _cases = (
00047       ...    (('ca-es',), ('ca_ES', ), 'ca_ES'),
00048       ...    (('en','de'), ('en','de','fr'),  'en'),
00049       ...    (('en'),      ('it','de','fr'),  None),
00050       ...    (('pt-br','de'), ('pt_BR','de','fr'),  'pt_BR'),
00051       ...    (('pt-br','en-us', 'de'), ('de', 'en', 'fr'),  'en'),
00052       ...    )
00053 
00054       >>> for user_pref_langs, obj_langs, expected in _cases:
00055       ...     env = Env(user_pref_langs)
00056       ...     negotiator.getLanguage(obj_langs, env) is None
00057       True
00058       True
00059       True
00060       True
00061       True
00062     """
00063 
00064 
00065 def testFallbackOnDefaultNegotiator():
00066     """
00067       >>> negotiator = Negotiator()
00068 
00069       >>> from zope.publisher.browser import BrowserLanguages
00070       >>> negotiator[0] = BrowserLanguages
00071 
00072       >>> data = [
00073       ...    (('pt', 'en'),  ('pt_BR, en')),
00074       ...    (('en-us', 'da'), ('da, en;q=.9, en-gb;q=1.0, en-us')),
00075       ...    (('en', 'pt-br'), ('pt_BR; q=0.6, pt_PT; q = .7, en-gb')),
00076       ...    (('en-gb', 'en-us'), ('en-us, en_GB;q=0.9, en, pt_BR; q=1.0')),
00077       ...    ]
00078 
00079       >>> for langs, req in data:
00080       ...    request = TestRequest(req)
00081       ...    negotiator.getLanguage(langs, request)
00082       'en'
00083       'da'
00084       'en'
00085       'en-us'
00086     """
00087 
00088 
00089 def testBrowserNegotiator():
00090     """
00091       >>> negotiator = Negotiator()
00092       >>> from zope.publisher.browser import BrowserLanguages
00093       >>> negotiator[0] = BrowserLanguages
00094 
00095       >>> data = [
00096       ...    (('da', 'en'),  ('da, en, pt')),
00097       ...    (('en-us', 'da'), ('da, en;q=.9, en-gb;q=1.0, en-us')),
00098       ...    (('en', 'pt-br'), ('pt_BR; q=0.6, pt_PT; q = .7, en-gb')),
00099       ...    (('en-gb', 'en-us'), ('en-us, en_GB;q=0.9, en, pt_BR; q=1.0')),
00100       ...    (('ro', 'fr'), ('ro,en-us;q=0.8,es;q=0.5,fr;q=0.3')),
00101       ...    (('es', 'ro'), ('ro,en-us;q=0,es;q=0.5,fr;q=0,ru;q=1,it'))
00102       ...    ]
00103 
00104       >>> for langs, req in data:
00105       ...    request = TestRequest(req)
00106       ...    negotiator.getLanguage(langs, request)
00107       'da'
00108       'da'
00109       'en'
00110       'en-us'
00111       'ro'
00112       'ro'
00113     """
00114 
00115 
00116 def testAvailableLanguagesBrowserNegotiator():
00117     """
00118       >>> negotiator = Negotiator()
00119       >>> from zope.publisher.browser import BrowserLanguages
00120       >>> negotiator[0] = BrowserLanguages
00121 
00122       >>> from plone.i18n.locales.interfaces import IContentLanguageAvailability
00123       >>> from plone.i18n.locales.languages import LanguageAvailability
00124 
00125       >>> from zope.component import getSiteManager
00126       >>> lang_avail = LanguageAvailability()
00127 
00128       >>> sm = getSiteManager()
00129       >>> sm.registerUtility(lang_avail, provided=IContentLanguageAvailability)
00130 
00131       >>> data = [
00132       ...    (('xy', ),  ('da, en, pt')),
00133       ...    (('xy', ), ('da, en;q=.9, en-gb;q=1.0, en-us')),
00134       ...    (('xy', ), ('pt_BR; q=0.6, pt_PT; q = .7, en-gb')),
00135       ... ]
00136 
00137       >>> for langs, req in data:
00138       ...    request = TestRequest(req)
00139       ...    negotiator.getLanguage(langs, request)
00140       'da'
00141       'da'
00142       'en'
00143     """
00144 
00145 
00146 def test_suite():
00147     return unittest.TestSuite((
00148         DocTestSuite(setUp=configurationSetUp,
00149                      tearDown=tearDown,
00150                      optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE),
00151         ))
00152 
00153 if __name__ == '__main__':
00154     unittest.main(defaultTest="test_suite")