Commit 7f267e23 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[test] fake_form is a cw backport

parent 2c0bc426a545
......@@ -96,3 +96,44 @@ def create_entity(self, etype, **kwargs):
self._nb_inserted_entities += 1
return entity
try:
from cubicweb.devtools.testlib import CubicWebTC
except ImportError:
# devtools not available
pass
else:
# #4985805
from cubicweb.web import eid_param
@monkeypatch(CubicWebTC, methodname='fake_form')
@staticmethod
def fake_form(formid, field_dict, entity_field_dicts):
"""Build _cw.form dictionnary to fake posting of some standard cubicweb form
* `formid`, the form id, usually form's __regid__
* `field_dict`, dictionary of name:value for fields that are not tied to an entity
* `entity_field_dicts`, list of (entity, dictionary) where dictionary contains name:value
for fields that are not tied to the given entity
"""
form = {'__form_id': formid}
fields = []
for field, value in field_dict.items():
fields.append(field)
form[field] = value
def _add_entity_field(entity, field, value):
entity_fields.append(field)
form[eid_param(field, entity.eid)] = value
for entity, field_dict in entity_field_dicts:
if '__maineid' not in form:
form['__maineid'] = entity.eid
entity_fields = []
form.setdefault('eid', []).append(entity.eid)
_add_entity_field(entity, '__type', entity.cw_etype)
for field, value in field_dict.items():
_add_entity_field(entity, field, value)
if entity_fields:
form[eid_param('_cw_entity_fields', entity.eid)] = ','.join(entity_fields)
if fields:
form['_cw_fields'] = ','.join(fields)
return form
......@@ -20,35 +20,6 @@ from StringIO import StringIO
from cubicweb.web import Redirect, eid_param
from cubicweb.devtools.testlib import CubicWebTC
def fake_form(formid, field_dict, entity_field_dicts):
form = {'__form_id': formid}
fields = []
for field, value in field_dict.items():
fields.append(field)
form[field] = value
def _add_entity_field(entity, field, value):
entity_fields.append(field)
form[eid_param(field, entity.eid)] = value
for entity, field_dict in entity_field_dicts:
if '__maineid' not in form:
form['__maineid'] = entity.eid
entity_fields = []
form.setdefault('eid', []).append(entity.eid)
_add_entity_field(entity, '__type', entity.cw_etype)
for field, value in field_dict.items():
_add_entity_field(entity, field, value)
if entity_fields:
form[eid_param('_cw_entity_fields', entity.eid)] = ','.join(entity_fields)
if fields:
form['_cw_fields'] = ','.join(fields)
return form
# input class="form-control" name="__type:858" type="hidden" value="ConceptScheme" />
# <input class="form-control" name="eid" type="hidden" value="858" />
# <input class="form-control" name="__form_generation_time:858" type="hidden" value="1423044545.187907" />
# <input class="form-control" name="__maineid" type="hidden" value="858" />
# <input class="form-control" name="_cw_fields" type="hidden" value="__maineid,eid,language,file,encoding" />
# <input class="form-control" name="_cw_entity_fields:858" type="hidden" value="__type,__form_generation_time" />
class ViewsTC(CubicWebTC):
......@@ -60,7 +31,7 @@ class ViewsTC(CubicWebTC):
scheme = req.entity_from_eid(scheme.eid)
# simply test the form properly render and is well formed
self.view('skos.scheme.import', rset=scheme.as_rset(), req=req, template=None)
req.form = fake_form('skos.scheme.import', {
req.form = self.fake_form('skos.scheme.import', {
'file': ('filename.txt', StringIO('\n\nélectro\nhip-hop\nrap\njazz\nclassique\n')),
'encoding': u'utf-8',
'language': u'fr',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment