diff -r 49075f57cf2c -r aa09e20dd8c0 web/test/unittest_form.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/unittest_form.py Thu May 14 12:48:11 2009 +0200 @@ -0,0 +1,183 @@ +from logilab.common.testlib import unittest_main, mock_object +from cubicweb import Binary +from cubicweb.devtools.testlib import WebTest +from cubicweb.web.form import EntityFieldsForm, FieldsForm +from cubicweb.web.formrenderers import FormRenderer +from cubicweb.web.formfields import (IntField, StringField, RichTextField, + DateTimeField, DateTimePicker, + FileField, EditableFileField) +from cubicweb.web.formwidgets import PasswordInput +from cubicweb.web.views.workflow import ChangeStateForm + + +class FieldsFormTC(WebTest): + + def test_form_field_format(self): + form = FieldsForm(self.request(), None) + self.assertEquals(form.form_field_format(None), 'text/html') + self.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"') + self.commit() + self.assertEquals(form.form_field_format(None), 'text/rest') + + +class EntityFieldsFormTC(WebTest): + + def setUp(self): + super(EntityFieldsFormTC, self).setUp() + self.req = self.request() + self.entity = self.user(self.req) + self.renderer = FormRenderer() + + def test_form_field_vocabulary_unrelated(self): + b = self.add_entity('BlogEntry', title=u'di mascii code', content=u'a best-seller') + t = self.add_entity('Tag', name=u'x') + form1 = EntityFieldsForm(self.request(), None, entity=t) + unrelated = [reid for rview, reid in form1.subject_relation_vocabulary('tags')] + self.failUnless(b.eid in unrelated, unrelated) + form2 = EntityFieldsForm(self.request(), None, entity=b) + unrelated = [reid for rview, reid in form2.object_relation_vocabulary('tags')] + self.failUnless(t.eid in unrelated, unrelated) + self.execute('SET X tags Y WHERE X is Tag, Y is BlogEntry') + unrelated = [reid for rview, reid in form1.subject_relation_vocabulary('tags')] + self.failIf(b.eid in unrelated, unrelated) + unrelated = [reid for rview, reid in form2.object_relation_vocabulary('tags')] + self.failIf(t.eid in unrelated, unrelated) + + def test_form_field_vocabulary_new_entity(self): + e = self.etype_instance('CWUser') + form = EntityFieldsForm(self.request(), None, entity=e) + unrelated = [rview for rview, reid in form.subject_relation_vocabulary('in_group')] + # should be default groups but owners, i.e. managers, users, guests + self.assertEquals(unrelated, [u'guests', u'managers', u'users']) + + def test_subject_in_state_vocabulary(self): + # on a new entity + e = self.etype_instance('CWUser') + form = EntityFieldsForm(self.request(), None, entity=e) + states = list(form.subject_in_state_vocabulary('in_state')) + self.assertEquals(len(states), 1) + self.assertEquals(states[0][0], u'activated') # list of (combobox view, state eid) + # on an existant entity + e = self.user() + form = EntityFieldsForm(self.request(), None, entity=e) + states = list(form.subject_in_state_vocabulary('in_state')) + self.assertEquals(len(states), 1) + self.assertEquals(states[0][0], u'deactivated') # list of (combobox view, state eid) + + + + # form view tests ######################################################### + + def test_massmailing_formview(self): + self.execute('INSERT EmailAddress X: X address L + "@cubicweb.org", ' + 'U use_email X WHERE U is CWUser, U login L') + rset = self.execute('CWUser X') + self.view('massmailing', rset, template=None) + + + # form tests ############################################################## + + def test_form_inheritance(self): + class CustomChangeStateForm(ChangeStateForm): + hello = IntField(name='youlou') + creation_date = DateTimeField(widget=DateTimePicker) + form = CustomChangeStateForm(self.req, redirect_path='perdu.com', + entity=self.entity) + form.form_render(state=123, trcomment=u'') + + def test_change_state_form(self): + form = ChangeStateForm(self.req, redirect_path='perdu.com', + entity=self.entity) + form.form_render(state=123, trcomment=u'') + + # fields tests ############################################################ + + def _render_entity_field(self, name, form): + form.form_build_context({}) + return form.field_by_name(name).render(form, self.renderer) + + def _test_richtextfield(self, expected): + class RTFForm(EntityFieldsForm): + description = RichTextField() + state = self.execute('State X WHERE X name "activated", X state_of ET, ET name "CWUser"').get_entity(0, 0) + form = RTFForm(self.req, redirect_path='perdu.com', entity=state) + # make it think it can use fck editor anyway + form.form_field_format = lambda x: 'text/html' + self.assertTextEquals(self._render_entity_field('description', form), + expected % {'eid': state.eid}) + + + def test_richtextfield_1(self): + self.req.use_fckeditor = lambda: False + self._test_richtextfield('''''' % {'eid': file.eid}) + + + def test_passwordfield(self): + class PFForm(EntityFieldsForm): + upassword = StringField(widget=PasswordInput) + form = PFForm(self.req, redirect_path='perdu.com', entity=self.entity) + self.assertTextEquals(self._render_entity_field('upassword', form), + ''' +
+ +  +confirm password''' % {'eid': self.entity.eid}) + + +if __name__ == '__main__': + unittest_main()