Integration tests
=================

    >>> browser = self.browser

Standalone form
---------------

Add a new EasyForm::

    >>> browser.open(self.portal_url)
    >>> browser.getLink('EasyForm').click()
    >>> browser.getControl('Title').value = 'testform'
    >>> browser.getControl('Save').click()

We'll want to test the save data adapter later.
Let's add one now::

    >>> browser.getLink('Actions').click()
    >>> browser.getControl('Add new action…').click()
    >>> browser.getControl('Title').value = 'saver'
    >>> browser.getControl('Short Name').value = 'saver'
    >>> browser.getControl('Save Data').selected = True
    >>> browser.getControl('Add').click()
    >>> 'http://nohost/plone/testform/actions/saver' in browser.contents
    True

Return to form and confirm that it renders properly::

    >>> browser.open(self.portal_url + '/testform')
    >>> browser.url
    'http://nohost/plone/testform'
    >>> browser.contents
    '...<form class="rowlike enableUnloadProtection  easyformForm kssattr-formname-view" action="http://nohost/plone/testform" method="post" id="form" enctype="multipart/form-data">...'
    >>> """<form class="rowlike enableUnloadProtection  easyformForm kssattr-formname-view" action="http://nohost/plone/testform" method="post" id="form" enctype="multipart/form-data">""" in browser.contents
    True
    >>> """<input id="form-widgets-replyto" name="form.widgets.replyto" class="text-widget required textline-field" value="" type="text" />""" in browser.contents
    True
    >>> """<input id="form-widgets-topic" name="form.widgets.topic" class="text-widget required textline-field" value="" type="text" />""" in browser.contents
    True
    >>> """<textarea id="form-widgets-comments" name="form.widgets.comments" class="textarea-widget required text-field"></textarea>""" in browser.contents
    True
    >>> """<input id="form-buttons-submit" name="form.buttons.submit" class="submit-widget button-field" value="Submit" type="submit" />""" in browser.contents
    True

Submit the form.  An incomplete submission should give validation errors::

    >>> browser.open(self.portal_url + '/testform/actions/mailer')
    >>> browser.getControl(name='form.widgets.recipient_email').value = 'mdummy@address.com'
    >>> browser.getControl('Save').click()
    >>> browser.open(self.portal_url + '/testform')
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'test'
    >>> browser.getControl('Submit').click()
    >>> browser.url
    'http://nohost/plone/testform'
    >>> 'There were some errors.' in browser.contents
    True

Now let's try a complete submission and confirm that it displays the default
thank you page.  The default form has a mailer, so we'll programmatically set
a recipient so that it doesn't complain.  (The mailer is mocked in the doctest
base class)::

    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    <sent mail from ...to ['mdummy@address.com']>
    >>> browser.url
    'http://nohost/plone/testform'
    >>> 'Thanks for your input.' in browser.contents
    True
    >>> browser.getControl('Reset').click()
    >>> browser.url
    'http://nohost/plone/testform'
    >>> 'Thanks for your input.' in browser.contents
    False

Add a new fieldset::

    >>> browser.getLink('Fields').click()
    >>> browser.getControl('Add new fieldset…').click()
    >>> browser.getControl('Title').value = 'New fieldset'
    >>> browser.getControl('Short Name').value = 'new_fieldset'
    >>> browser.getControl('Add').click()

Change fieldset of Comments field::

    >>> browser.open(self.portal_url + '/testform/fields/comments/@@changefieldset?fieldset_index=1')

Submit the form::

    >>> browser.open(self.portal_url + '/testform')
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'Test Subject'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    <sent mail from ...to ['mdummy@address.com']>
    >>> 'test@example.com' in browser.contents
    True
    >>> 'Test Subject' in browser.contents
    True
    >>> 'PFG rocks!' in browser.contents
    True
    >>> 'Thanks for your input.' in browser.contents
    True

Disable Mailer::

    >>> browser.getLink('Actions').click()
    >>> browser.getControl('Mailer').click()
    >>> browser.getControl('Save').click()
    >>> browser.open(self.portal_url + '/testform')
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'test'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    >>> 'Thanks for your input.' in browser.contents
    True

Set Execution Condition for Mailer::

    >>> browser.getLink('Actions').click()
    >>> browser.getControl('Mailer').click()
    >>> browser.getControl('Save').click()
    >>> browser.open(self.portal_url + '/testform/actions/mailer')
    >>> browser.getControl(name='form.widgets.IActionExtender.execCondition').value = 'python:False'
    >>> browser.getControl('Save').click()
    >>> browser.open(self.portal_url + '/testform')
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'test'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    >>> 'Thanks for your input.' in browser.contents
    True

Don't show all fields on thanks page::

    >>> browser.open(self.portal_url + '/testform/edit')
    >>> browser.getControl('Show All Fields').click()
    >>> browser.getControl('Save').click()
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'Test Subject'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    >>> 'test@example.com' in browser.contents
    False
    >>> 'Test Subject' in browser.contents
    False
    >>> 'PFG rocks!' in browser.contents
    False
    >>> 'Thanks for your input.' in browser.contents
    True

Check redirect to portal::

    >>> browser.open(self.portal_url + '/testform/edit')
    >>> browser.getControl(name='form.widgets.thanksPageOverride').value = "portal_url"
    >>> browser.getControl('Save').click()
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'Test Subject'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    >>> browser.url
    'http://nohost/plone'

Check traverse to portal::

    >>> browser.open(self.portal_url + '/testform/edit')
    >>> browser.getControl('Traverse to').selected = True
    >>> browser.getControl(name='form.widgets.thanksPageOverride').value = "string:news"
    >>> browser.getControl('Save').click()
    >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
    >>> browser.getControl('Subject').value = 'Test Subject'
    >>> browser.getControl('Comments').value = 'PFG rocks!'
    >>> browser.getControl('Submit').click()
    >>> browser.url
    'http://nohost/plone/testform'
    >>> 'Thanks for your input.' in browser.contents
    False

We should be able to view an individual field::

    >>> browser.open(self.portal_url + '/testform/fields/comments')
    >>> browser.url
    'http://nohost/plone/testform/fields/comments'

    >>> print browser.contents # doctest: +SKIP
    <!DOCTYPE...
    ...
     <div class="pfg-form formid-comments">
          ...
          <textarea...name="comments"...></textarea>
          ...
          <div class="formControls">
            ...
            <input type="hidden" name="form.submitted"
                   value="1" />
            ...
            <input class="context" type="submit"
                   name="form_submit" value="Submit" />
          </div>
      </form>
    </div>
    ...

Let's take a look at the save-data adapter::

    >>> browser.open(self.portal_url + '/testform/actions/saver')
    >>> browser.url
    'http://nohost/plone/testform/actions/saver'

It ought to tell us we have a saved input::

    >>> print browser.contents # doctest: +SKIP
    <!DOCTYPE...
    ...
    <span>1</span> input(s) saved
    ...

And a form offering to clear input::

    >>> print browser.contents # doctest: +SKIP
    <!DOCTYPE...
    ...
    <form action="http://nohost/plone/testform/saver/clearSavedFormInput" method="post"...>
        <input type="hidden" name="_authenticator" value="..."/>
        <input type="submit" ... value="Clear Saved Input" ... />
          ...
              Clear all saved input?
          ...
    </form>
    ...

    Let's try it out; we should redirect back to form::

    >>> browser.getControl('Clear Saved Input').click() # doctest: +SKIP
    >>> browser.url
    'http://nohost/plone/testform/actions/saver'

Attempts to use clearSavedFormInput without auth should be forbidden::

    >>> browser.open(self.portal_url + '/testform/saver/clearSavedFormInput') # doctest: +SKIP
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 403: Forbidden


Attempts to use gpg_services TTW should be fruitless::

    >>> browser.open(self.portal_url + '/testform/@@gpg_services/encrypt?data=XXX&recipient_key_id=yyy')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to read the success action TTW should be fruitless::

    >>> browser.open(self.portal_url + '/testform/fgGetSuccessAction')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

That should also be true for fields::

    >>> browser.open(self.portal_url + '/testform/comments/fgGetSuccessAction')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to set mailer body TTW should fail
    >>> browser.open(self.portal_url + '/testform/mailer/setBody_pt?value=stuff')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to read mailer body TTW should fail
    >>> browser.open(self.portal_url + '/testform/mailer/body_pt')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

We want to test security on the custom script adapter. Let's add one::

    >>> browser.open(self.portal_url + '/testform')
    >>> browser.getLink('Actions').click()
    >>> browser.getControl('Add new action…').click()
    >>> browser.getControl('Title').value = 'Test Script Adapter'
    >>> browser.getControl('Short Name').value = 'test_script_adapter'
    >>> browser.getControl('Custom Script').selected = True
    >>> browser.getControl('Add').click()
    >>> browser.open(self.portal_url + '/testform/actions/test_script_adapter')
    >>> browser.url
    'http://nohost/plone/testform/actions/test_script_adapter'

Attempts to set script body TTW should fail::

    >>> browser.open(self.portal_url + '/testform/test-script-adapter/updateScript?body=raise%2010&role=none')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to run the script TTW should fail::

    >>> browser.open(self.portal_url + '/testform/test-script-adapter/onSuccess?fields=')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> browser.open(self.portal_url + '/testform/test-script-adapter/scriptBody?fields=')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> browser.open(self.portal_url + '/testform/test-script-adapter/executeCustomScript?fields=&form=&req=')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to use onSuccess TTW should fail::

    >>> browser.open(self.portal_url + '/testform/saver/onSuccess?fields=&request=')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

Attempts to read our special member attributes TTW should fail::

    >>> browser.open(self.portal_url + '/testform/memberId')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> browser.open(self.portal_url + '/testform/memberFullName')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found

    >>> browser.open(self.portal_url + '/testform/memberEmail')
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 404: Not Found


# Embedded form
# -------------
#
# Now let's make sure PloneFormGen forms work properly when embedded in some other
# context.  First let's load some test-only ZCML which registers a @@form-wrapper
# view that will render the form we created above::
#
#     >>> from Products.Five import zcml
#     >>> import Products.PloneFormGen
#     >>> zcml.load_config('tests/fixtures/tests.zcml', Products.PloneFormGen)
#
# Now we should be able to render the same form as above, but located at @@form-wrapper::
#
#     >>> browser.open(self.portal_url + '/@@form-wrapper')
#     >>> print browser.contents
#     <!DOCTYPE...
#     ...
#     <div class="pfg-embedded">
#         ...
#         <form...action="http://nohost/plone/@@form-wrapper"...>
#         ...
#         </form>
#         ...
#     </div>
#     ...
#
# If we submit an incomplete form, it should get validated in-place and render with error
# messages::
#
#     >>> browser.getControl('Submit').click()
#     >>> browser.url
#     'http://nohost/plone/@@form-wrapper'
#     >>> 'This field is required.' in browser.contents
#     True
#
# If we successfully complete the form, we should get pseudo-redirected to the thank you page::
#
#     >>> browser.getControl('Your E-Mail Address').value = 'test@example.com'
#     >>> browser.getControl('Subject').value = 'test'
#     >>> browser.getControl('Comments').value = 'Now with double the rockage...'
#     >>> browser.getControl('Submit').click()
#     <sent mail from ...to ['mdummy@address.com']>
#     >>> browser.url
#     'http://nohost/plone/@@form-wrapper'
#     >>> 'Thanks for your input.' in browser.contents
#     True
#
# Make sure that the thank you page was rendered standalone, rather than embedded in the
# wrapping template::
#
#     >>> 'Embedded form' in browser.contents
#     False
#
# We used some trickery to retry the request with a new URL without doing a real redirect,
# in order to preserve the request variables.  So let's make sure they made it::
#
#     >>> 'Now with double the rockage' in browser.contents
#     True
