-*- coding: utf-8 -*-

==============================================================================
Test SearchableTranslatedText index for smart folders
==============================================================================

Create the browser object we'll be using.

    >>> from DateTime.DateTime import DateTime
    >>> from Products.Five.testbrowser import Browser
    >>> browser = Browser()
    >>> portal_url = self.portal.absolute_url()
    >>> browser.handleErrors = False
    >>> self.portal.error_log._ignored_exceptions = ()

Import a variable to decide if we are in Plone 3 or not.

    >>> from icsemantic.catalog.config import HAS_PLONE3

Hide the recent changes portlet.

    >>> self.portal.left_slots = ()

Log in into the site as manager.

    >>> from Products.PloneTestCase.setup import portal_owner, default_user, default_password
    >>> login_url = portal_url + '/login_form'
    >>> logout_url = portal_url + '/logout'
    >>> browser.open(login_url)

We have the login portlet, so let's use that.

    >>> browser.getControl(name='__ac_name').value = portal_owner
    >>> browser.getControl(name='__ac_password').value = default_password
    >>> browser.getControl(name='submit').click()
    >>> browser.open(portal_url)
    >>> browser.url == portal_url
    True

Let's make three languages available in the site: English, Spanish and French.
The default language is English.

    >>> browser.getLink('Site Setup').click()
    >>> browser.getLink('Language Settings').click()
    >>> browser.getControl(name='supportedLanguages:list').getControl(value='en').selected = True
    >>> browser.getControl(name='supportedLanguages:list').getControl(value='es').selected = True
    >>> browser.getControl(name='supportedLanguages:list').getControl(value='fr').selected = True
    >>> browser.getControl('Save').click()
    >>> prefs_languages_url = portal_url + '/portal_languages/prefs_languages'
    >>> browser.open(prefs_languages_url)
    >>> browser.getControl(name='defaultLanguage').value = ('en',)
    >>> browser.getControl('Save').click()

Create a new user with the "icsemantic.langfallback" property set.

    >>> member1 = portal.portal_registration.addMember('test1', 'test1')
    >>> member1.setMemberProperties({'language': 'en', 'icsemantic.preferred_languages': ('es', 'fr',)})

First, let's create a folder to put a few documents.

    >>> if HAS_PLONE3:
    ...     browser.getLink('Add new').click()
    ...     browser.getControl('Folder').click()
    ...     browser.getControl(name='form.button.Add').click()
    ... else:
    ...     browser.getLink('Add to folder').click()
    ...     browser.getControl('Add Folder').click()
    >>> browser.getControl(name='title').value = 'My Folder'
    >>> browser.getControl('Save').click()
    >>> browser.getLink('View').click()
    >>> browser.url == portal_url + '/my-folder/'
    True

Now let's create two pages according to these tables:
Language      Title             Contains
en            First Page        soccer
es            Primera página    fútbol

Language      Title             Contains
en            Second Page       football
fr            Deuxième page     football

So, if the user test1 is seeing the site in English and searches for documents
containig translations of the word 'soccer' to French, she will get only:
Deuxième page.

If she is seeing the site in Spanish, she will get empty results because
'soccer' is not an Spanish word.

We are going to make sure that the first page we create includes the word
'soccer' in the body.

    >>> if HAS_PLONE3:
    ...     browser.getLink('Add new').click()
    ...     browser.getControl('Page').click()
    ...     browser.getControl(name='form.button.Add').click()
    ... else:
    ...     browser.getLink('Add item').click()
    ...     browser.getControl('Page').click()
    >>> browser.getControl(name='title').value = 'First Page'
    >>> browser.getControl(name='description').value = 'First page description'
    >>> browser.getControl(name='text').value = 'Juan is good at playing soccer'
    >>> browser.getControl('Save').click()

Let's translate the page into Spanish.

    >>> browser.open(portal_url+'/my-folder/first-page/createTranslation?set_language=es&current=en')
    >>> browser.getControl('Title').value = 'Primera página'
    >>> browser.getControl('Description').value = 'Descripción de la primera página'
    >>> browser.getControl('Body Text').value = "Juan es bueno jugando al fútbol."
    >>> browser.getControl('Save').click()
    >>> 'Your changes have been saved.' in browser.contents
    True

We are creating another page. This time around, we are making sure that the page
contains the word 'football'.

    >>> browser.open(portal_url+'/my-folder?set_language=en')
    >>> browser.getLink('My Folder').click()
    >>> browser.url == portal_url + '/my-folder'
    True
    >>> if HAS_PLONE3:
    ...     browser.getLink('Add new').click()
    ...     browser.getControl('Page').click()
    ...     browser.getControl(name='form.button.Add').click()
    ... else:
    ...     browser.getLink('Add item').click()
    ...     browser.getControl('Page').click()
    >>> browser.getControl(name='title').value = 'Second Page'
    >>> browser.getControl(name='description').value = 'This is another test description'
    >>> browser.getControl(name='text').value = 'Football.'
    >>> browser.getControl('Save').click()

Let's translate the page into French.

    >>> browser.open(portal_url+'/my-folder/second-page/createTranslation?set_language=fr&current=en')
    >>> browser.getControl('Title').value = 'Deuxsième page'
    >>> browser.getControl('Description').value = ''
    >>> browser.getControl('Body Text').value = 'Football.'
    >>> browser.getControl('Save').click()
    >>> 'Your changes have been saved.' in browser.contents
    True

Go back to the site root and create a collection.

    >>> browser.open(portal_url+'/my-folder?set_language=en')
    >>> browser.open(portal_url)
    >>> browser.url == portal_url
    True
    >>> if HAS_PLONE3:
    ...     browser.getLink('Add new').click()
    ...     browser.getControl('Collection').click()
    ...     browser.getControl(name='form.button.Add').click()
    ... else:
    ...     browser.getLink('Add to folder').click() # different in Plone 3
    ...     browser.getControl('Smart Folder').click() # different in Plone 3
    >>> browser.getControl(name='title').value = 'Translations of soccer'
    >>> browser.getControl('Save').click()

With an empty criteria, none of the pages created linked from the collection
view. But first we must login with the test user we created.

    >>> logout_url = portal_url + '/logout'
    >>> browser.open(logout_url)
    >>> browser.open(login_url)
    >>> browser.getControl(name='__ac_name').value = 'test1'
    >>> browser.getControl(name='__ac_password').value = 'test1'
    >>> browser.getControl(name='submit').click()
    >>> browser.open(portal_url)
    >>> browser.url == portal_url
    True

    >>> browser.open(portal_url+'?set_language=en')
    >>> browser.open(portal_url)
    >>> browser.getLink('Translations of soccer').click()
    >>> 'First Page' in browser.contents
    False
    >>> 'Second Page' in browser.contents
    False
    >>> 'Primera página' in browser.contents
    False
    >>> 'Deuxsième page' in browser.contents
    False


Set the collection criteria to query for content items that contains
translations of 'pared'.

    >>> browser.open(logout_url)
    >>> browser.open(login_url)
    >>> browser.getControl(name='__ac_name').value = portal_owner
    >>> browser.getControl(name='__ac_password').value = default_password
    >>> browser.getControl(name='submit').click()
    >>> browser.open(portal_url)
    >>> browser.url == portal_url
    True

    >>> browser.getLink('Translations of soccer').click()
    >>> browser.getLink('Criteria').click()
    >>> browser.getControl(name='field', index=0).value = ('SearchableTranslatedText',)
    >>> browser.getControl('Criteria type').value = ('ATTranslationCriterion',)
    >>> browser.getControl('Add').click()
    >>> browser.getControl(name='crit__SearchableTranslatedText_ATTranslationCriterion_value').value = 'soccer'
    >>> browser.getControl(name='crit__SearchableTranslatedText_ATTranslationCriterion_language:list').value = ('fr',)
    >>> browser.getControl('Save', index=0).click()

Let's check that the second page previously created is among the
results of the collection when seeing the site in English.

    >>> browser.open(logout_url)
    >>> browser.open(login_url)
    >>> browser.getControl(name='__ac_name').value = 'test1'
    >>> browser.getControl(name='__ac_password').value = 'test1'
    >>> browser.getControl(name='submit').click()
    >>> browser.open(portal_url)
    >>> browser.url == portal_url
    True

    >>> browser.open(portal_url+'?set_language=en')
    >>> browser.open(portal_url)
    >>> browser.getLink('Translations of soccer').click()
    >>> 'First Page' in browser.contents
    False
    >>> 'Second Page' in browser.contents
    False
    >>> 'Primera página' in browser.contents
    False
    >>> 'Deuxsième page' in browser.contents
    True

