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:

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

And 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

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/'}]


Theoritically, 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()
    >>> self.browser.url
    'http://nohost/plone/plone_control_panel'

    >>> '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 the default settings are there and correct:

    >>> self.list_available_controls(form_name='edit_form')
    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)
    salt: salt_as_pdf (text)
    mail_title:  (text)
    mail_content_text_format: text/html (hidden)
    mail_content:  (textarea)
    mail_content_text_format:default: text/html (hidden)
    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)
    print_css_types:lines:  (textarea)
    ...

We will not test if we can correctly change settings etc, that's
handled by Archetype which works quite well for this kind of things.
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)
    ------------------
    ...
    P lone 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)
    ------------------
    ...
    P lone est un système de gestion de contenu Web...
    ...

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

And the Japanese one:

    >>> self.browser.open('http://nohost/plone/plone-jp')
    >>> self.browser.getLink('Download as PDF').click()
    >>> display_pdf(self.browser.contents)
    ------------------
    ...
    P loneはZopeアプリケーションサーバ上に構築されたフリ...
    ...

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