=============================
Lingua Plone Functional Tests
=============================

Environment
-----------

First, we are going to setup an environment:

  Use standard username and password from PloneTestCase
  >>> from Products.PloneTestCase.PloneTestCase import default_user
  >>> from Products.PloneTestCase.PloneTestCase import default_password

  Use the member's home folder as playground for the tests
  >>> folder = self.folder
  >>> folder_path = '/'.join(folder.getPhysicalPath())

  We need the portal, too
  >>> portal = self.portal
  >>> portal_path = '/'.join(portal.getPhysicalPath())

  Plone's default error message template is too verbose and far too long
  >>> portal.default_error_message = None

Then, we set german and french as additional available languages:

  English is initially the only available language
  >>> language_tool = portal.portal_languages
  >>> language_tool.getSupportedLanguages()
  ['en']

  So, we add another two
  >>> language_tool.addSupportedLanguage('de')
  >>> language_tool.addSupportedLanguage('fr')
  >>> language_tool.getSupportedLanguages()
  ['en', 'de', 'fr']

  English is the current language
  >>> language_tool.getPreferredLanguage()
  'en'


API Basics
----------

Now, time to create our english content object:

  >>> _ = folder.invokeFactory('SimpleType', 'doc')
  >>> english = folder.doc
  >>> english.setLanguage('en')
  >>> english.setBody('__ENGLISH_CONTENT__')
  >>> en_path = '/'.join(english.getPhysicalPath())

  Content is created with the current language
  >>> english.Language()
  'en'

And add a german translation:

  >>> english.addTranslation('de')
  >>> german = english.getTranslation('de')
  >>> german.setBody('__GERMAN_CONTENT__')
  >>> de_path = '/'.join(german.getPhysicalPath())
  >>> german.Language()
  'de'

  Note that we don't have a french translation
  >>> english.hasTranslation('fr')
  False
  >>> english.getTranslationLanguages()
  ['de', 'en']

  Both english and german objects are considered 'translations'
  >>> english.isTranslation()
  True
  >>> german.isTranslation()
  True

  But only english is canonical
  >>> english.isCanonical()
  True
  >>> german.isCanonical()
  False

Finally, add some stuff inside portal root:

  Raise privileges
  >>> self.setRoles(['Manager'])

  Add content & translation
  >>> _ = portal.invokeFactory('SimpleType', 'root')
  >>> root = portal.root
  >>> root.setLanguage('en')
  >>> root.setBody('__ENGLISH_CONTENT__')
  >>> root.addTranslation('de')
  >>> root.getTranslation('de').setBody('__GERMAN_CONTENT__')

  We also make the newly content the actual portal default page
  >>> portal.setDefaultPage(root.getId())

  Back to normal
  >>> self.setRoles(['Member'])


First time access: no cookie
----------------------------

When you go to the page, and have a browser language that
is set (no overriding cookie yet), it should pick up the
translation if it exists and set a cookie with the language.

XXX We are not going to show a different page than the
one you ask for - the magic only happens with browserdefault

  a) Single page
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: de,en;q=0.7,en-us;q=0.3
  ... """ % en_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

  b) Default page in folder
  >>> folder.setDefaultPage(english.getId())
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: de,en;q=0.7,en-us;q=0.3
  ... """ % folder_path)
  HTTP/1.1 200 OK
  Content-Language: de
  ...
  Set-Cookie: I18N_LANGUAGE="de"; Path=/
  ...__GERMAN_CONTENT__...

  c) Default page in portal
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: de,en;q=0.7,en-us;q=0.3
  ... """ % portal_path)
  HTTP/1.1 200 OK
  Content-Language: de
  ...
  Set-Cookie: I18N_LANGUAGE="de"; Path=/
  ...__GERMAN_CONTENT__...

If the languages accepted by the browser doesn't exist, the
page in the URL should be shown and a cookie with its language
should be set.

  a) Single page
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: fr,no;q=0.7,en-us;q=0.3
  ... """ % en_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

  b) Default page in folder
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: fr,no;q=0.7,en-us;q=0.3
  ... """ % folder_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

  c) Default page in portal
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: fr,no;q=0.7,en-us;q=0.3
  ... """ % portal_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

If the browser language is the same from the page in the URL,
then we still have to set a cookie.

  a) Single page
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... """ % en_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

  b) Default page in folder
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... """ % folder_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...

  c) Default page in portal
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... """ % portal_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...
  Set-Cookie: I18N_LANGUAGE="en"; Path=/
  ...__ENGLISH_CONTENT__...


After first access: cookie
--------------------------

If you have a cookie with language override set, it should pick up
the correct translation. There's no need to set another cookie. The
rule is that if a language is explicitly set (cookie), we respect
that until the user decides to change it.

XXX If the user somehow goes to an English page, we have to show it
in English. The user should never go there by browsing, because
there is no mixing of languages.
XXX Maybe we should get an English cookie? Not sure...

  a) Single page
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=de
  ... """ % en_path)
  HTTP/1.1 200 OK
  Content-Language: en
  ...__ENGLISH_CONTENT__...

  b) Default page in folder
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=de
  ... """ % folder_path)
  HTTP/1.1 200 OK
  Content-Language: de
  ...__GERMAN_CONTENT__...

  c) Default page in portal
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=de
  ... """ % portal_path)
  HTTP/1.1 200 OK
  Content-Language: de
  ...__GERMAN_CONTENT__...

If the translation doesn't exist, it should show a placeholder
page, which informs you that the language is still the same,
but the content hasn't a translation.

  a) Single page
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=fr
  ... """ % en_path)
  HTTP/1.1 200 OK
  Content-Language: fr
  ...This content does not exist in your current language...
  ...<strong>Français</strong>...

  b) Default page in folder
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=fr
  ... """ % folder_path)
  HTTP/1.1 200 OK
  Content-Language: fr
  ...This content does not exist in your current language...
  ...<strong>Français</strong>...

  c) Default page in portal
  >>> print http(r"""
  ... GET /%s HTTP/1.1
  ... Accept-Language: en,de;q=0.7,en-us;q=0.3
  ... Cookie: I18N_LANGUAGE=fr
  ... """ % portal_path)
  HTTP/1.1 200 OK
  Content-Language: fr
  ...This content does not exist in your current language...
  ...<strong>Français</strong>...
