web/test/unittest_form.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 30 Nov 2009 10:24:01 +0100
branchstable
changeset 3947 8d06bce45c02
parent 3772 316d0b524db8
child 3777 3ef8cdb5fb1c
child 3953 19aefd78f61b
permissions -rw-r--r--
when one is adding an inline entity for a relation of a single card, the 'add a new xxx' link disappears. If the user then cancel the addition, we have to make this link appears back. This is done by giving add new link id to removeInlineForm.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     1
"""
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     2
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     3
:organization: Logilab
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1947
diff changeset
     7
"""
2286
5dd33f9b7b84 comment out with_statement to avoid broken install if 2.4 is installed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2198
diff changeset
     8
#from __future__ import with_statement
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
     9
2145
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
    10
from xml.etree.ElementTree import fromstring
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
    11
866
6fdb029663ca use EnvBasedTC since we need to access the schema
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 848
diff changeset
    12
from logilab.common.testlib import unittest_main, mock_object
3757
122a01751d59 2.4 compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3515
diff changeset
    13
from logilab.common.compat import any
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    14
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 894
diff changeset
    15
from cubicweb import Binary
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    16
from cubicweb.devtools.testlib import WebTest
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    17
from cubicweb.web.formfields import (IntField, StringField, RichTextField,
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    18
                                     DateTimeField, DateTimePicker,
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    19
                                     FileField, EditableFileField)
2145
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
    20
from cubicweb.web.formwidgets import PasswordInput, Input
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1995
diff changeset
    21
from cubicweb.web.views.forms import EntityFieldsForm, FieldsForm
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
    22
from cubicweb.web.views.workflow import ChangeStateForm
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1983
diff changeset
    23
from cubicweb.web.views.formrenderers import FormRenderer
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    25
1359
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    26
class FieldsFormTC(WebTest):
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    27
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    28
    def test_form_field_format(self):
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    29
        form = FieldsForm(self.request(), None)
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    30
        self.assertEquals(form.form_field_format(None), 'text/html')
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1393
diff changeset
    31
        self.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')
1359
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    32
        self.commit()
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    33
        self.assertEquals(form.form_field_format(None), 'text/rest')
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    34
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    35
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    36
class EntityFieldsFormTC(WebTest):
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
847
27c4ebe90d03 prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents: 845
diff changeset
    38
    def setUp(self):
866
6fdb029663ca use EnvBasedTC since we need to access the schema
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 848
diff changeset
    39
        super(EntityFieldsFormTC, self).setUp()
868
5d993a0c794c update test, use a DateTimePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 866
diff changeset
    40
        self.req = self.request()
5d993a0c794c update test, use a DateTimePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 866
diff changeset
    41
        self.entity = self.user(self.req)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    42
1345
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    43
    def test_form_field_vocabulary_unrelated(self):
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    44
        b = self.add_entity('BlogEntry', title=u'di mascii code', content=u'a best-seller')
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    45
        t = self.add_entity('Tag', name=u'x')
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    46
        form1 = EntityFieldsForm(self.request(), None, entity=t)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    47
        unrelated = [reid for rview, reid in form1.subject_relation_vocabulary('tags')]
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    48
        self.failUnless(b.eid in unrelated, unrelated)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    49
        form2 = EntityFieldsForm(self.request(), None, entity=b)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    50
        unrelated = [reid for rview, reid in form2.object_relation_vocabulary('tags')]
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    51
        self.failUnless(t.eid in unrelated, unrelated)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    52
        self.execute('SET X tags Y WHERE X is Tag, Y is BlogEntry')
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    53
        unrelated = [reid for rview, reid in form1.subject_relation_vocabulary('tags')]
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    54
        self.failIf(b.eid in unrelated, unrelated)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    55
        unrelated = [reid for rview, reid in form2.object_relation_vocabulary('tags')]
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    56
        self.failIf(t.eid in unrelated, unrelated)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    57
3176
b94703b131ad hopefully correctly fix the breadcrumbs escaping
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3002
diff changeset
    58
1345
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    59
    def test_form_field_vocabulary_new_entity(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1393
diff changeset
    60
        e = self.etype_instance('CWUser')
1345
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    61
        form = EntityFieldsForm(self.request(), None, entity=e)
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    62
        unrelated = [rview for rview, reid in form.subject_relation_vocabulary('in_group')]
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    63
        # should be default groups but owners, i.e. managers, users, guests
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    64
        self.assertEquals(unrelated, [u'guests', u'managers', u'users'])
9f086baa2537 backport tests
sylvain.thenault@logilab.fr
parents: 1300
diff changeset
    65
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    66
    # def test_subject_in_state_vocabulary(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    67
    #     # on a new entity
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    68
    #     e = self.etype_instance('CWUser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    69
    #     form = EntityFieldsForm(self.request(), None, entity=e)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    70
    #     states = list(form.subject_in_state_vocabulary('in_state'))
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    71
    #     self.assertEquals(len(states), 1)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    72
    #     self.assertEquals(states[0][0], u'activated') # list of (combobox view, state eid)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    73
    #     # on an existant entity
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    74
    #     e = self.user()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    75
    #     form = EntityFieldsForm(self.request(), None, entity=e)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    76
    #     states = list(form.subject_in_state_vocabulary('in_state'))
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    77
    #     self.assertEquals(len(states), 1)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
    78
    #     self.assertEquals(states[0][0], u'deactivated') # list of (combobox view, state eid)
1359
bef6891393d7 backport some tests
sylvain.thenault@logilab.fr
parents: 1345
diff changeset
    79
1843
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    80
    def test_consider_req_form_params(self):
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    81
        e = self.etype_instance('CWUser')
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    82
        e.eid = 'A'
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    83
        form = EntityFieldsForm(self.request(login=u'toto'), None, entity=e)
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    84
        field = StringField(name='login', eidparam=True)
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    85
        form.append_field(field)
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3176
diff changeset
    86
        form.build_context({})
1843
646c2dd1f03e #343290: inputs don't use value specified in URL
sylvain.thenault@logilab.fr
parents: 1793
diff changeset
    87
        self.assertEquals(form.form_field_display_value(field, {}), 'toto')
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    88
1947
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    89
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    90
    def test_linkto_field_duplication(self):
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    91
        e = self.etype_instance('CWUser')
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    92
        e.eid = 'A'
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    93
        e.req = self.req
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    94
        geid = self.execute('CWGroup X WHERE X name "users"')[0][0]
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    95
        self.req.form['__linkto'] = 'in_group:%s:subject' % geid
2661
f8df42c9da6b [vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    96
        form = self.vreg['forms'].select('edition', self.req, entity=e)
1947
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    97
        form.content_type = 'text/html'
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    98
        pageinfo = self._check_html(form.form_render(), form, template=None)
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
    99
        inputs = pageinfo.find_tag('select', False)
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
   100
        self.failUnless(any(attrs for t, attrs in inputs if attrs.get('name') == 'in_group:A'))
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
   101
        inputs = pageinfo.find_tag('input', False)
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
   102
        self.failIf(any(attrs for t, attrs in inputs if attrs.get('name') == '__linkto'))
8696403e5324 test and fix __linkto handling (#343421)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1843
diff changeset
   103
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   104
    def test_reledit_composite_field(self):
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   105
        rset = self.execute('INSERT BlogEntry X: X title "cubicweb.org", X content "hop"')
3772
316d0b524db8 fix reledit related test breakage
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3757
diff changeset
   106
        form = self.vreg['views'].select('doreledit', self.request(),
2661
f8df42c9da6b [vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
   107
                                         rset=rset, row=0, rtype='content')
2198
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   108
        data = form.render(row=0, rtype='content')
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   109
        self.failUnless('edits-content' in data)
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   110
        self.failUnless('edits-content_format' in data)
ac45d4dbaf76 close #344264, pb with composite fields / renderer.display_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2174
diff changeset
   111
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   112
    # form view tests #########################################################
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   113
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   114
    def test_massmailing_formview(self):
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   115
        self.execute('INSERT EmailAddress X: X address L + "@cubicweb.org", '
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1393
diff changeset
   116
                     'U use_email X WHERE U is CWUser, U login L')
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1393
diff changeset
   117
        rset = self.execute('CWUser X')
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   118
        self.view('massmailing', rset, template=None)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   119
1181
620ec8e6ae19 cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents: 1147
diff changeset
   120
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   121
    # form tests ##############################################################
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   122
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   123
    def test_form_inheritance(self):
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   124
        class CustomChangeStateForm(ChangeStateForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   125
            hello = IntField(name='youlou')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   126
            creation_date = DateTimeField(widget=DateTimePicker)
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
   127
        form = CustomChangeStateForm(self.req, redirect_path='perdu.com',
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
   128
                                     entity=self.entity)
2755
e69a4077ffb4 [web test] fix unittest form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   129
        form.form_render(state=123, trcomment=u'',
e69a4077ffb4 [web test] fix unittest form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   130
                         trcomment_format=u'text/plain')
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   131
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
   132
    def test_change_state_form(self):
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
   133
        form = ChangeStateForm(self.req, redirect_path='perdu.com',
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
   134
                               entity=self.entity)
2755
e69a4077ffb4 [web test] fix unittest form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   135
        form.form_render(state=123, trcomment=u'',
e69a4077ffb4 [web test] fix unittest form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   136
                         trcomment_format=u'text/plain')
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   137
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   138
    # fields tests ############################################################
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   139
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   140
    def _render_entity_field(self, name, form):
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3176
diff changeset
   141
        form.build_context({})
1995
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1983
diff changeset
   142
        renderer = FormRenderer(self.req)
ec95eaa2b711 turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1983
diff changeset
   143
        return form.field_by_name(name).render(form, renderer)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   144
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   145
    def _test_richtextfield(self, expected):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   146
        class RTFForm(EntityFieldsForm):
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   147
            description = RichTextField()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2755
diff changeset
   148
        state = self.execute('State X WHERE X name "activated", X state_of WF, WF workflow_of ET, ET name "CWUser"').get_entity(0, 0)
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   149
        form = RTFForm(self.req, redirect_path='perdu.com', entity=state)
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   150
        # make it think it can use fck editor anyway
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   151
        form.form_field_format = lambda x: 'text/html'
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   152
        self.assertTextEquals(self._render_entity_field('description', form),
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   153
                              expected % {'eid': state.eid})
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   154
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   155
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   156
    def test_richtextfield_1(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   157
        self.req.use_fckeditor = lambda: False
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   158
        self._test_richtextfield('''<select id="description_format:%(eid)s" name="description_format:%(eid)s" size="1" style="display: block" tabindex="1">
1300
62d2b890a980 move tests where they belong
sylvain.thenault@logilab.fr
parents: 1181
diff changeset
   159
<option value="text/cubicweb-page-template">text/cubicweb-page-template</option>
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   160
<option value="text/html">text/html</option>
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   161
<option value="text/plain">text/plain</option>
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   162
<option selected="selected" value="text/rest">text/rest</option>
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   163
</select><textarea cols="80" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="2"></textarea>''')
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   164
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   165
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   166
    def test_richtextfield_2(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   167
        self.req.use_fckeditor = lambda: True
2755
e69a4077ffb4 [web test] fix unittest form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2661
diff changeset
   168
        self._test_richtextfield('<input name="description_format:%(eid)s" type="hidden" value="text/rest" /><textarea cols="80" cubicweb:type="wysiwyg" id="description:%(eid)s" name="description:%(eid)s" onkeyup="autogrow(this)" rows="2" tabindex="1"></textarea>')
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   169
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   170
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   171
    def test_filefield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   172
        class FFForm(EntityFieldsForm):
1983
5edd22d0e61a fix test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   173
            data = FileField(format_field=StringField(name='data_format', max_length=50),
5edd22d0e61a fix test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   174
                             encoding_field=StringField(name='data_encoding', max_length=20))
3515
3edebe9414fa update to new file schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3510
diff changeset
   175
        file = self.add_entity('File', data_name=u"pouet.txt", data_encoding=u'UTF-8',
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   176
                               data=Binary('new widgets system'))
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   177
        form = FFForm(self.req, redirect_path='perdu.com', entity=file)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   178
        self.assertTextEquals(self._render_entity_field('data', form),
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   179
                              '''<input id="data:%(eid)s" name="data:%(eid)s" tabindex="1" type="file" value="" />
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   180
<a href="javascript: toggleVisibility(&#39;data:%(eid)s-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   181
<div id="data:%(eid)s-advanced" class="hidden">
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   182
<label for="data_format:%(eid)s">data_format</label><input id="data_format:%(eid)s" maxlength="50" name="data_format:%(eid)s" size="45" tabindex="2" type="text" value="text/plain" /><br/>
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   183
<label for="data_encoding:%(eid)s">data_encoding</label><input id="data_encoding:%(eid)s" maxlength="20" name="data_encoding:%(eid)s" size="20" tabindex="3" type="text" value="UTF-8" /><br/>
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   184
</div>
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   185
<br/>
2439
77d8dd77acb3 [cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2381
diff changeset
   186
<input name="data:%(eid)s__detach" type="checkbox" />
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   187
detach attached file
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   188
''' % {'eid': file.eid})
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   189
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   190
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   191
    def test_editablefilefield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   192
        class EFFForm(EntityFieldsForm):
1983
5edd22d0e61a fix test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   193
            data = EditableFileField(format_field=StringField(name='data_format', max_length=50),
5edd22d0e61a fix test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   194
                                     encoding_field=StringField(name='data_encoding', max_length=20))
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   195
            def form_field_encoding(self, field):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   196
                return 'ascii'
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   197
            def form_field_format(self, field):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   198
                return 'text/plain'
3515
3edebe9414fa update to new file schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3510
diff changeset
   199
        file = self.add_entity('File', data_name=u"pouet.txt", data_encoding=u'UTF-8',
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   200
                               data=Binary('new widgets system'))
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   201
        form = EFFForm(self.req, redirect_path='perdu.com', entity=file)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   202
        self.assertTextEquals(self._render_entity_field('data', form),
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   203
                              '''<input id="data:%(eid)s" name="data:%(eid)s" tabindex="1" type="file" value="" />
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   204
<a href="javascript: toggleVisibility(&#39;data:%(eid)s-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   205
<div id="data:%(eid)s-advanced" class="hidden">
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   206
<label for="data_format:%(eid)s">data_format</label><input id="data_format:%(eid)s" maxlength="50" name="data_format:%(eid)s" size="45" tabindex="2" type="text" value="text/plain" /><br/>
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   207
<label for="data_encoding:%(eid)s">data_encoding</label><input id="data_encoding:%(eid)s" maxlength="20" name="data_encoding:%(eid)s" size="20" tabindex="3" type="text" value="UTF-8" /><br/>
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   208
</div>
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   209
<br/>
2439
77d8dd77acb3 [cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2381
diff changeset
   210
<input name="data:%(eid)s__detach" type="checkbox" />
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   211
detach attached file
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   212
<p><b>You can either submit a new file using the browse button above, or choose to remove already uploaded file by checking the "detach attached file" check-box, or edit file content online with the widget below.</b></p>
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   213
<textarea cols="80" name="data:%(eid)s" onkeyup="autogrow(this)" rows="3" tabindex="4">new widgets system</textarea>''' % {'eid': file.eid})
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   214
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   215
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   216
    def test_passwordfield(self):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   217
        class PFForm(EntityFieldsForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   218
            upassword = StringField(widget=PasswordInput)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   219
        form = PFForm(self.req, redirect_path='perdu.com', entity=self.entity)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   220
        self.assertTextEquals(self._render_entity_field('upassword', form),
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   221
                              '''<input id="upassword:%(eid)s" name="upassword:%(eid)s" tabindex="1" type="password" value="__cubicweb_internal_field__" />
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   222
<br/>
2637
07103211e511 R [test] update and fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2439
diff changeset
   223
<input name="upassword-confirm:%(eid)s" tabindex="1" type="password" value="__cubicweb_internal_field__" />
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2755
diff changeset
   224
&#160;
1111
bcb81e7c41bc fix test: form_add_entity_hiddens *must* be called before build context, resulting in slightly different outputs...
sylvain.thenault@logilab.fr
parents: 1074
diff changeset
   225
<span class="emphasis">confirm password</span>''' % {'eid': self.entity.eid})
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   226
1570
5c40d9fb4e8d stop using card in test, update form tests
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   227
2145
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   228
    def test_datefield(self):
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   229
        class DFForm(EntityFieldsForm):
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   230
            creation_date = DateTimeField(widget=Input)
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   231
        form = DFForm(self.req, entity=self.entity)
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   232
        init, cur = (fromstring(self._render_entity_field(attr, form)).get('value')
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   233
                     for attr in ('edits-creation_date', 'creation_date'))
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   234
        self.assertEquals(init, cur)
529867c301fc add datetime form test
Florent <florent@secondweb.fr>
parents: 2005
diff changeset
   235
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   236
if __name__ == '__main__':
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   237
    unittest_main()