Testing formlib integration
===========================

This doctest is will test the Five formlib support and to provide some
examples.

It will not test the actual formlib functionality. See
zope/formlib/form.txt for tests and more explanations of zope.formlib

Before we can begin, we need to set up a few things.

We prepare for Python 2/3 compatibility:

  >>> import six

We need a manager account:

  >>> uf = self.folder.acl_users
  >>> _ignored = uf._doAddUser('manager', 'r00t', ['Manager'], [])

We need to configure all of Five and the necessary formlib components for
this test:

  >>> from Zope2.App import zcml
  >>> import Products.Five
  >>> zcml.load_config('meta.zcml', Products.Five)
  >>> import five.formlib
  >>> import five.formlib.tests
  >>> zcml.load_config('configure.zcml', package=Products.Five)
  >>> zcml.load_config('configure.zcml', package=five.formlib)
  >>> zcml.load_config('configure.zcml', package=five.formlib.tests)

Finally, we need to setup a traversable folder. Otherwise, Five won't get
to to do its view lookup:

  >>> from Products.Five.tests.testing import manage_addFiveTraversableFolder
  >>> manage_addFiveTraversableFolder(self.folder, 'ftf')

Let's set up a testbrowser:

  >>> from Testing.testbrowser import Browser
  >>> browser = Browser()
  >>> browser.addHeader('Accept-Language', 'en-US')
  >>> browser.addHeader('Authorization', 'Basic manager:r00t')

Let's 'manually' create a Content instance and add it to the folder:

  >>> from five.formlib.tests import content
  >>> folder = self.folder.ftf
  >>> obj = content.Content('content_1', 'Title', [])
  >>> folder._setObject('content_1', obj)
  'content_1'
  >>> print(folder.content_1.title)
  Title

Now we can edit this content object, e.g. changing the title. Formlib,
like the traditional AddView and EditView, supports unicode.
But we need some hackery for Python 2/3 compatibility. For Python 2,
the test framework expects utf-8 encoded binary strings; for Python 3,
it expects text. The function below converts as necessary.

  >>> def to_input(v):
  ...     if six.PY2:
  ...         return v.encode("utf-8")
  ...     else:
  ...         return v


  >>> browser.open("http://localhost/test_folder_1_/ftf/content_1/@@edit_content")
  >>> ni_hao = u'\u4f60\u597d'
  >>> ctl = browser.getControl(name="form.title")
  >>> ctl.value = to_input(ni_hao)
  >>> browser.getControl(name="form.actions.apply").click()
  >>> folder.content_1.title == ni_hao
  True

Adding a new content object, with two list items:

  >>> browser.open("http://localhost/test_folder_1_/ftf/@@add_content")
  >>> ctl = browser.getControl(name="form.id")
  >>> ctl.value = 'test123'
  >>> ctl = browser.getControl(name="form.title")
  >>> ctl.value = ni_hao
  >>> browser.getControl(name="form.somelist.add").click()
  >>> ctl = browser.getControl(name="form.somelist.0.")
  >>> ctl.value = 'a nice list item'
  >>> browser.getControl(name="form.somelist.add").click()
  >>> ctl = browser.getControl(name="form.somelist.1.")
  >>> ctl.value = 'a nice list item'
  >>> browser.getControl(name="form.actions.add").click()
  >>> folder.test123.somelist == [u'a nice list item', u'a nice list item']
  True

Clean up
--------

Finally, we need to clean up:

  >>> from zope.component.testing import tearDown
  >>> tearDown()
