Back to index

plone3  3.1.7
test_negotiation.py
Go to the documentation of this file.
00001 import os, sys
00002 if __name__ == '__main__':
00003     execfile(os.path.join(sys.path[0], 'framework.py'))
00004 
00005 from Products.PloneTestCase import PloneTestCase
00006 from Products.PloneTestCase.PloneTestCase import default_user
00007 from Products.PloneTestCase.PloneTestCase import default_password
00008 
00009 PloneTestCase.setupPloneSite()
00010 from Products.PloneLanguageTool import LanguageTool
00011 
00012 
00013 class LanguageNegotiationTestCase(PloneTestCase.FunctionalTestCase):
00014 
00015     def afterSetUp(self):
00016         self.basic_auth = '%s:%s' % (default_user, default_password)
00017         self.portal_path = self.portal.absolute_url(1)
00018         self.tool = self.portal[LanguageTool.id]
00019 
00020 
00021 class TestDefaultLanguageNegotiation(LanguageNegotiationTestCase):
00022 
00023     def testLanguageNegotiation(self):
00024         response = self.publish(self.portal_path, self.basic_auth,
00025                                 env={'HTTP_ACCEPT_LANGUAGE': 'pt'})
00026 
00027         self.assertEquals(response.getStatus(), 200)
00028         # Once PLT is installed only English is allowed as a language
00029         self.assertEquals(response.headers['content-language'], 'en')
00030 
00031 
00032 class TestNoCombinedLanguageNegotiation(LanguageNegotiationTestCase):
00033 
00034     def afterSetUp(self):
00035         LanguageNegotiationTestCase.afterSetUp(self)
00036         # set some allowed languages and make sure we don't use combined
00037         # language codes
00038         self.tool.supported_langs = ['en', 'pt', 'de']
00039         self.tool.use_combined_language_codes = 0
00040         self.tool.display_flags = 0
00041 
00042     def testLanguageNegotiation(self):
00043         # Test simple supported codes
00044         response = self.publish(self.portal_path, self.basic_auth,
00045                                 env={'HTTP_ACCEPT_LANGUAGE': 'pt'})
00046 
00047         self.assertEquals(response.getStatus(), 200)
00048         self.failUnless('<option selected="selected" value="pt">'
00049             in response.getBody())
00050 
00051         response = self.publish(self.portal_path, self.basic_auth,
00052                                 env={'HTTP_ACCEPT_LANGUAGE': 'de'})
00053 
00054         self.assertEquals(response.getStatus(), 200)
00055         self.failUnless('<option selected="selected" value="de">'
00056             in response.getBody())
00057 
00058         # Test combined unsupported codes, should fall back
00059         response = self.publish(self.portal_path, self.basic_auth,
00060                                 env={'HTTP_ACCEPT_LANGUAGE': 'pt-br'})
00061 
00062         self.assertEquals(response.getStatus(), 200)
00063         self.failUnless('<option selected="selected" value="pt">'
00064             in response.getBody())
00065 
00066 
00067 class TestCombinedLanguageNegotiation(LanguageNegotiationTestCase):
00068 
00069     def afterSetUp(self):
00070         LanguageNegotiationTestCase.afterSetUp(self)
00071         # set some allowed languages and make sure we don't use combined
00072         # language codes
00073         self.tool.supported_langs = ['en', 'pt', 'de', 'pt-br']
00074         self.tool.use_combined_language_codes = 1
00075         self.tool.display_flags = 0
00076 
00077     def testLanguageNegotiation(self):
00078         # Test simple supported codes
00079         response = self.publish(self.portal_path, self.basic_auth,
00080                                 env={'HTTP_ACCEPT_LANGUAGE': 'pt'})
00081 
00082         self.assertEquals(response.getStatus(), 200)
00083         self.failUnless('<option selected="selected" value="pt">'
00084             in response.getBody())
00085 
00086         response = self.publish(self.portal_path, self.basic_auth,
00087                                 env={'HTTP_ACCEPT_LANGUAGE': 'de'})
00088 
00089         self.assertEquals(response.getStatus(), 200)
00090         self.failUnless('<option selected="selected" value="de">'
00091             in response.getBody())
00092 
00093         # Test combined supported codes
00094         response = self.publish(self.portal_path, self.basic_auth,
00095                                 env={'HTTP_ACCEPT_LANGUAGE': 'pt-br'})
00096 
00097         self.assertEquals(response.getStatus(), 200)
00098         self.failUnless('<option selected="selected" value="pt-br">'
00099             in response.getBody())
00100 
00101         # Test combined unsupported codes, should fall back
00102         response = self.publish(self.portal_path, self.basic_auth,
00103                                 env={'HTTP_ACCEPT_LANGUAGE': 'de-de'})
00104 
00105         self.assertEquals(response.getStatus(), 200)
00106         self.failUnless('<option selected="selected" value="de">'
00107             in response.getBody())
00108 
00109 
00110 class TestCcTLDLanguageNegotiation(LanguageNegotiationTestCase):
00111     def afterSetUp(self):
00112         LanguageNegotiationTestCase.afterSetUp(self)
00113         self.tool.supported_langs = ['en', 'nl', 'fr']
00114         self.tool.use_cctld_negotiation = 1
00115         self.tool.display_flags = 0
00116 
00117     def checkLanguage(self, response, language):
00118         self.assertEquals(response.getStatus(), 200)
00119         self.failUnless('<option selected="selected" value="%s">' % language
00120             in response.getBody())
00121 
00122     def testSimpleHostname(self):
00123         # For a simple hostname without ccTLD the canonical language is used
00124         response = self.publish(self.portal_path, self.basic_auth,
00125                                 env={'HTTP_HOST': 'localhost'})
00126         self.checkLanguage(response, "en")
00127 
00128     def testIPAddress(self):
00129         response = self.publish(self.portal_path, self.basic_auth,
00130                                 env={'HTTP_HOST': '127.0.0.1'})
00131         self.checkLanguage(response, "en")
00132 
00133     def testDutchDomain(self):
00134         response = self.publish(self.portal_path, self.basic_auth,
00135                                 env={'HTTP_HOST': 'plone.nl'})
00136         self.checkLanguage(response, "nl")
00137 
00138     def testAcceptedLanguages(self):
00139         # Brazil uses Portugese, which is not in the accepted languages list
00140         response = self.publish(self.portal_path, self.basic_auth,
00141                                 env={'HTTP_HOST': 'plone.br'})
00142         self.checkLanguage(response, "en")
00143 
00144     def testMultiLingualCountries(self):
00145         # Some countries refuse to pick a single language. Belgium
00146         # uses both Dutch and French, with a preference for Dutch.
00147 
00148         response = self.publish(self.portal_path, self.basic_auth,
00149                                 env={'HTTP_HOST': 'plone.be'})
00150         self.checkLanguage(response, "nl")
00151 
00152         # If we stop allowing Dutch we should now fall back to French
00153         self.tool.supported_langs = ['en', 'fr']
00154         response = self.publish(self.portal_path, self.basic_auth,
00155                                 env={'HTTP_HOST': 'plone.be'})
00156         self.checkLanguage(response, "fr")
00157 
00158 
00159 def test_suite():
00160     from unittest import TestSuite, makeSuite
00161     suite = TestSuite()
00162     suite.addTest(makeSuite(TestDefaultLanguageNegotiation))
00163     suite.addTest(makeSuite(TestNoCombinedLanguageNegotiation))
00164     suite.addTest(makeSuite(TestCombinedLanguageNegotiation))
00165     suite.addTest(makeSuite(TestCcTLDLanguageNegotiation))
00166     return suite
00167 
00168 if __name__ == '__main__':
00169     framework()
00170