SendAsPDF functional test cases
===============================

Setup and check
---------------

First step, we have to set up the system::

    >>> self.setup_data()

Let's first test if our pages are there (there is some problem with
Plone 3 when displaying the ``folder_contents`` view due to the page
containing non-ASCII characters in the title, so we use the
``folder_listing`` view)::

    >>> self.login_as_manager()
    >>> self.browser.open('http://nohost/plone/selectViewTemplate?templateId=folder_listing')
    >>> ['Plone (%s)' % lang in self.browser.contents for lang in self.data.keys()]
    [True, True, True]

And check if they contain what they are supposed to contain::

    >>> for lang, text in self.data.items():
    ...     self.browser.open('http://nohost/plone/plone-%s' % lang)
    ...     print text in self.browser.contents
    True
    True
    True

We switch back to the default view for the home page::

    >>> self.browser.open('http://nohost/plone/select_default_page')
    >>> self.browser.getControl(name='objectId').value = ['front-page']
    >>> self.browser.getControl(name='form.button.Save').click()

Ok, now we can test that we can send those pages as PDF.


1 - Links availability
----------------------

``collective.sendaspdf`` adds link at the bottom of the page to allow
sending the page as a PDF file or to download it::

    >>> self.browser.open('http://nohost/plone/')
    >>> self.get_sendaspdf_actions()
    [{'text': 'Send as PDF',
      'href': '.../plone/front-page/send_as_pdf?page_url=http://nohost/plone/'},
     {'text': 'Download as PDF',
     'href': '.../plone/front-page/download_as_pdf?page_url=http://nohost/plone/'}]

If we open another page, the link URL changes (well, makes sense ...)::

    >>> self.browser.open('http://nohost/plone/plone-fr')
    >>> self.get_sendaspdf_actions()
    [{'text': 'Send as PDF',
      'href': '.../plone/plone-fr/send_as_pdf?page_url=http://nohost/plone/plone-fr'},
     {'text': 'Download as PDF',
     'href': '.../plone/plone-fr/download_as_pdf?page_url=http://nohost/plone/plone-fr'}]

If we open the page with GET parameters, they will also be kept for
the PDF generation (not in the same order, but that is not a real problem)::

    >>> self.browser.open('http://nohost/plone/plone-fr?first=1&second=two&third=trois')
    >>> self.get_sendaspdf_actions()
    [{'text': 'Send as PDF',
      'href': '.../plone/plone-fr/send_as_pdf?page_url=http://nohost/plone/plone-fr?second=two&third=trois&first=1'},
     {'text': 'Download as PDF',
     'href': '.../plone/plone-fr/download_as_pdf?page_url=http://nohost/plone/plone-fr?second=two&third=trois&first=1'}]

Parameters posted as ``POST`` will not be taken into account when
computing the page URL::

    >>> self.browser.open('http://nohost/plone/post_form/')
    >>> self.browser.getControl(name='text').value='Some text here'
    >>> self.browser.getControl(name='submit').click()
    >>> self.get_sendaspdf_actions()
    [{'text': 'Send as PDF',
      'href': '.../plone/front-page/send_as_pdf?page_url=http://nohost/plone/'},
     {'text': 'Download as PDF',
      'href': '.../plone/front-page/download_as_pdf?page_url=http://nohost/plone/'}]


Theoretically, we should be able to split ``GET`` and ``POST`` parameters, in
order to use the ``GET`` parameters when computing the new URI.
In practice it does not work yet::

    >>> self.browser.open('http://nohost/plone/mixed_form/')
    >>> self.browser.getControl(name='text').value='Some text here'
    >>> self.browser.getControl(name='submit').click()
    >>> self.get_sendaspdf_actions()
    [{'text': 'Send as PDF',
      'href': '.../plone/front-page/send_as_pdf?page_url=http://nohost/plone/'},
     {'text': 'Download as PDF',
      'href': '.../plone/front-page/download_as_pdf?page_url=http://nohost/plone/'}]

This should be the extracted links::

    [{'text': 'Send as PDF',
      'href': '.../plone/front-page/send_as_pdf?page_url=http://nohost/plone/?a_get_param=12},
     {'text': 'Download as PDF',
      'href': '.../plone/front-page/download_as_pdf?page_url=http://nohost/plone/?a_get_param=12}]


2 - Tool setup
--------------

``collective.sendaspdf`` allows some settings in the Plone control panel::

    >>> self.browser.open('http://nohost/plone/')
    >>> self.browser.getLink('Site Setup').click()

The URL differs in Plone 3 and 4, but this should be OK to check we
really are in the control panel::

    >>> self.browser.url in ['http://nohost/plone/plone_control_panel',
    ...                      'http://nohost/plone/@@overview-controlpanel']
    True

    >>> 'Send as PDF' in self.browser.contents
    True

    >>> self.browser.getLink('Send as PDF').click()
    >>> self.browser.url
    'http://nohost/plone/portal_sendaspdf'

Now we'll check that the default settings are there and correct::

    >>> self.list_available_controls(form_name='edit_form',
    ...                              hidden=['mail_content_text_format:default'])
    id: portal_sendaspdf (hidden)
    pdf_generator: ['wk'] (select)
    [<ItemControl name='pdf_generator' type='select' optionValue='pisa' selected=False>,
     <ItemControl name='pdf_generator' type='select' optionValue='wk' selected=True>]
    tempdir: /tmp/ (text)
    excluded_browser_attachment:lines:  (textarea)
    auto_cleanup_pdf:boolean: True (checkbox)
    [<ItemControl name='auto_cleanup_pdf:boolean' type='checkbox' optionValue='on' selected=True>]
    auto_cleanup_pdf:boolean:default:  (hidden)
    rewrite_https_links:boolean: False (checkbox)
    [<ItemControl name='rewrite_https_links:boolean' type='checkbox' optionValue='on' selected=False>]
    rewrite_https_links:boolean:default:  (hidden)
    salt: salt_as_pdf (text)
    mail_title:  (text)
    mail_content_text_format: text/html (hidden)
    mail_content:  (textarea)
    mail_content_file: None (file)
    filename_in_mail: attachment.pdf (text)
    always_print_css:boolean: True (checkbox)
    [<ItemControl name='always_print_css:boolean' type='checkbox' optionValue='on' selected=True>]
    always_print_css:boolean:default:  (hidden)
    allow_cookie:boolean: False (checkbox)
    [<ItemControl name='allow_cookie:boolean' type='checkbox' optionValue='on' selected=False>]
    allow_cookie:boolean:default:  (hidden)
    print_css_types:lines:  (textarea)
    ...

We will not test if we can correctly change settings etc., that's
handled by Archetypes which works quite well for this kind of thing.
We'll edit them later for some tests (for example the email generation).

3 - PDF download
----------------

First, we will download the PDF generated by the English page about Plone::

    >>> self.browser.open('http://nohost/plone/')
    >>> self.browser.getLink('Download as PDF').click()

    >>> from collective.sendaspdf.tests.pdf2txt import display_pdf
    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    Welcome to Plone
    ...

Let's test the pages we've created previously (the output sucks a bit,
there's a space appearing after Plone's "p", but the thing to test
here is how we handle non-ASCII characters)::

    >>> self.browser.open('http://nohost/plone/plone-en')
    >>> self.browser.getLink('Download as PDF').click()
    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    Plone is a free and open source...
    ...

We'll also test that the filename is correct::

    >>> self.browser.headers.get('content-disposition')
    'attachment; filename="Plone (en).pdf"'

Same thing for the French page::

    >>> self.browser.open('http://nohost/plone/plone-fr')
    >>> self.browser.getLink('Download as PDF').click()
    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    Plone est un système de gestion de contenu Web...
    ...considéré comme étant...
    ...

    >>> self.browser.headers.get('content-disposition')
    'attachment; filename="Plone (fr).pdf"'

And the Japanese one.  We would want to find 'PloneはZopeアプリケーショ
ンサーバ上に構築されたフリ' in the text, which works locally, but it
does not happen on Jenkins, so I am giving up on that, sorry.  :-(
But we can find other stuff::

    >>> self.browser.open('http://nohost/plone/plone-jp')
    >>> self.browser.getLink('Download as PDF').click()
    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    ...Monobook [JP]...
    ...

    >>> self.browser.headers.get('content-disposition')
    'attachment; filename="Plone (jp).pdf"'

Now the one with Cyrilic in the title::

    >>> self.browser.open(self.cyrilic_title_page_url)
    >>> self.browser.getLink('Download as PDF').click()

There is a problem with setting headers containing non-ASCII
characters. So instead we use the id of the page (note that the 
page id is quite different from the title, see
``collective.sendaspdf/tests/base.cfg``,
around line 172 for more explanations)::

    >>> self.cyrilic_title_page_url
    'http://nohost/plone/cyrilic-title-page'

    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    You are here: Home ... 3цдса
    ...

    >>> self.browser.headers.get('content-disposition')
    'attachment; filename="cyrilic-title-page.pdf"'
