Back to index

plone3  3.1.7
test_cookie.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 
00012 from zope.testing import doctest
00013 from zope.testing.doctestunit import DocTestSuite
00014 
00015 
00016 class TestRequest(dict):
00017 
00018     def __init__(self, cookie):
00019         self.cookies = {}
00020         self.cookies['I18N_LANGUAGE'] = cookie
00021 
00022 
00023 def configurationSetUp(self):
00024     setUp()
00025     XMLConfig('meta.zcml', zope.component)()
00026 
00027 
00028 def testCookieNegotiator():
00029     """
00030       >>> from plone.i18n.negotiator.negotiator import Negotiator
00031       >>> negotiator = Negotiator()
00032       >>> from plone.i18n.negotiator.cookie import CookieLanguage
00033       >>> negotiator[0] = CookieLanguage
00034 
00035       >>> from plone.i18n.locales.interfaces import ILanguageAvailability
00036       >>> from plone.i18n.locales.languages import LanguageAvailability
00037 
00038       >>> request = TestRequest('de')
00039       >>> negotiator.getLanguage(('de', ), request) is None
00040       True
00041 
00042       >>> from zope.component import getSiteManager
00043       >>> lang_avail = LanguageAvailability()
00044 
00045       >>> sm = getSiteManager()
00046       >>> sm.registerUtility(lang_avail, provided=ILanguageAvailability)
00047 
00048       >>> data = [
00049       ...    (('de', 'en'),  'de'),
00050       ...    (('en', 'fi'), 'fi'),
00051       ...    (('ca', 'pt'), 'pt_BR'),
00052       ... ]
00053 
00054       >>> for langs, cookie in data:
00055       ...    request = TestRequest(cookie)
00056       ...    negotiator.getLanguage(langs, request)
00057       'de'
00058       'fi'
00059     """
00060 
00061 
00062 def test_suite():
00063     return unittest.TestSuite((
00064         DocTestSuite(setUp=configurationSetUp,
00065                      tearDown=tearDown,
00066                      optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE),
00067         ))
00068 
00069 if __name__ == '__main__':
00070     unittest.main(defaultTest="test_suite")