How to test an edit controller
The documentation for edit controller is here.
They are used to process forms server-side (both during creation and edit).
I'm unable to find a consistent way (test and real usage) to retrieve the eid of the entity edited.
The following code works in real case (edit and creation), but fails in test :
class MyController(EditController):
# […]
def publish(self, rset=None):
# […]
eid = self._edited_entity.eid
In the test, I create a form :
def _send_form(self, entity_eid, entity_type, field, value):
"""Send a form for an entity with a single field filled"""
with self.new_access('WR').web_request() as req:
form = {
'__form_id': u'edition',
'__domid': u'entityForm',
'__type:%s' % entity_eid: entity_type,
'__maineid': entity_eid,
'eid': str(entity_eid),
'_cw_fields': u'resend_mail-subject',
'_cw_entity_fields:%s' % entity_eid: u',',
field: value,
}
req.form = form
controller = self.vreg['controllers'].select('edit', req=req,
appli=self.app)
controller.publish()
The test fails because in the publish self._edited_eid
is not defined.
The following code in the publish function works but this is only a workaround to get the test to pass:
class MyController(EditController):
# […]
def publish(self, rset=None):
eid = self._edited_entity.eid if self._edited_entity else int(self._cw.form['__maineid'])
Relying only on self._cw.form['__maineid']
is not a solution, because this works only for edition and fails for creation.
Another workaround is to use :
try:
eid = int(self._cw.form['__maineid'])
except ValueError:
eid = self._cw.form['__maineid']
eid = self._cw.data['eidmap'][fake_eid]