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 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: # BBB for Zope 2.12 >>> try: ... from Zope2.App import zcml ... except ImportError: ... from Products.Five 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: # BBB for Zope 2.12 >>> try: ... from Testing.testbrowser import Browser ... except ImportError: ... from Products.Five.testbrowser import Browser >>> browser = Browser() >>> browser.addHeader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7') >>> 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: >>> browser.open("http://localhost/test_folder_1_/ftf/content_1/@@edit_content") >>> ni_hao = '\xe4\xbd\xa0\xe5\xa5\xbd' >>> ctl = browser.getControl(name="form.title") >>> ctl.value = ni_hao >>> browser.getControl(name="form.actions.apply").click() >>> isinstance(folder.content_1.title, unicode) True >>> folder.content_1.title == ni_hao.decode('utf-8') 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() >>> print folder.test123.somelist [u'a nice list item', u'a nice list item'] Clean up -------- Finally, we need to clean up: >>> from zope.component.testing import tearDown >>> tearDown()