web/test/unittest_form.py
author sylvain.thenault@logilab.fr
Wed, 11 Mar 2009 19:54:30 +0100
branchtls-sprint
changeset 1074 c07f3accf04a
parent 1057 6636c75c4aa9
child 1111 bcb81e7c41bc
permissions -rw-r--r--
new style massmailing view
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
866
6fdb029663ca use EnvBasedTC since we need to access the schema
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 848
diff changeset
     1
from logilab.common.testlib import unittest_main, mock_object
907
192800415f59 FileField backport
sylvain.thenault@logilab.fr
parents: 894
diff changeset
     2
from cubicweb import Binary
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
     3
from cubicweb.devtools.testlib import WebTest
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
     4
from cubicweb.web.form import *
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
     5
from cubicweb.web.views.baseforms import ChangeStateForm
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
     7
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
     8
class EntityFieldsFormTC(WebTest):
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
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
    10
    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
    11
        super(EntityFieldsFormTC, self).setUp()
868
5d993a0c794c update test, use a DateTimePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 866
diff changeset
    12
        self.req = self.request()
5d993a0c794c update test, use a DateTimePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 866
diff changeset
    13
        self.entity = self.user(self.req)
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    14
        self.renderer = FormRenderer()
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
    15
        
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    16
    # form tests ##############################################################
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    17
    
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    18
    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
    19
        class CustomChangeStateForm(ChangeStateForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    20
            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
    21
            creation_date = DateTimeField(widget=DateTimePicker)
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
    22
        form = CustomChangeStateForm(self.req, redirect_path='perdu.com',
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
    23
                                     entity=self.entity)
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    24
        self.assertTextEquals(form.form_render(state=123, trcomment=u''),
1052
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    25
                              ''' ''')
869
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    26
168ad6d424d1 form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents: 868
diff changeset
    27
    def test_change_state_form(self):
894
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
    28
        form = ChangeStateForm(self.req, redirect_path='perdu.com',
e1f848785e3d test and fixes
sylvain.thenault@logilab.fr
parents: 869
diff changeset
    29
                               entity=self.entity)
1052
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    30
        self.assertTextEquals(form.form_render(state=123, trcomment=u''),
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    31
                              ''' ''')
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    32
        
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    33
    def test_delete_conf_form_multi(self):
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    34
        rset = self.execute('EGroup X')
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    35
        self.assertTextEquals(self.view('deleteconf', rset, template=None).source,
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    36
                              '')
1074
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    37
        
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    38
    def test_massmailing_form(self):
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    39
        self.execute('INSERT EmailAddress X: X address L + "@cubicweb.org", '
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    40
                     'U use_email X WHERE U is EUser, U login L')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    41
        rset = self.execute('EUser X')
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    42
        self.assertTextEquals(self.view('massmailing', rset, template=None).source,
c07f3accf04a new style massmailing view
sylvain.thenault@logilab.fr
parents: 1057
diff changeset
    43
                              '')
1052
4f746935e6b1 use assertTextEquals
sylvain.thenault@logilab.fr
parents: 907
diff changeset
    44
1057
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    45
    # fields tests ############################################################
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    46
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    47
    def _render_entity_field(self, name, form):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    48
        form.form_build_context({})
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    49
        form.form_add_entity_hiddens(form.entity.e_schema)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    50
        return form.field_by_name(name).render(form, self.renderer)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    51
    
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    52
    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
    53
        class RTFForm(EntityFieldsForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    54
            content = RichTextField()
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    55
        card = self.add_entity('Card', title=u"tls sprint fev 2009",
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    56
                               content=u'<h1>new widgets system</h1>',
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    57
                               content_format=u'text/html')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    58
        form = RTFForm(self.req, redirect_path='perdu.com', entity=card)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    59
        self.assertTextEquals(self._render_entity_field('content', form), expected)
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    60
        
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    61
    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
    62
        self.req.use_fckeditor = lambda: False
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    63
        self._test_richtextfield('''<select name="content_format" id="content_format" tabindex="0">
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    64
<option value="text/rest">text/rest</option>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    65
<option selected="selected" value="text/html">text/html</option>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    66
<option value="text/plain">text/plain</option>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    67
<option value="text/cubicweb-page-template">text/cubicweb-page-template</option>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    68
</select><textarea tabindex="1" id="content" name="content" onkeypress="autogrow(this)"/>''')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    69
    
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    70
    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
    71
        self.req.use_fckeditor = lambda: True
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    72
        self._test_richtextfield('''<input type="hidden" name="content_format" value="text/html"/><textarea tabindex="0" cubicweb:type="wysiwyg" id="content" name="content" onkeypress="autogrow(this)"/>''')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    73
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    74
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    75
    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
    76
        class FFForm(EntityFieldsForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    77
            data = FileField(format_field=StringField(name='data_format'),
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    78
                             encoding_field=StringField(name='data_encoding'))
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    79
        file = self.add_entity('File', name=u"pouet.txt",
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    80
                               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
    81
        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
    82
        self.assertTextEquals(self._render_entity_field('data', form),
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    83
                              '''<input id="data" type="file" name="data" value="" tabindex="0"/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    84
<a href="javascript: toggleVisibility(&#39;data-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    85
<div id="data-advanced" class="hidden">
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    86
<label for="data_format">data_format</label><input id="data_format" type="text" name="data_format" value="" tabindex="1"/><br/><br/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    87
<label for="data_encoding">data_encoding</label><input id="data_encoding" type="text" name="data_encoding" value="" tabindex="2"/><br/><br/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    88
</div>''')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    89
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    90
        
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    91
    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
    92
        class EFFForm(EntityFieldsForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    93
            data = EditableFileField(format_field=StringField(name='data_format'),
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    94
                                     encoding_field=StringField(name='data_encoding'))
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    95
            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
    96
                return 'ascii'
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    97
            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
    98
                return 'text/plain'
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
    99
        file = self.add_entity('File', name=u"pouet.txt",
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   100
                               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
   101
        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
   102
        self.assertTextEquals(self._render_entity_field('data', form),
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   103
                              '''<input id="data" type="file" name="data" value="" tabindex="0"/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   104
<a href="javascript: toggleVisibility(&#39;data-advanced&#39;)" title="show advanced fields"><img src="http://testing.fr/cubicweb/data/puce_down.png" alt="show advanced fields"/></a>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   105
<div id="data-advanced" class="hidden">
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   106
<label for="data_format">data_format</label><input id="data_format" type="text" name="data_format" value="" tabindex="1"/><br/><br/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   107
<label for="data_encoding">data_encoding</label><input id="data_encoding" type="text" name="data_encoding" value="" tabindex="2"/><br/><br/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   108
</div>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   109
<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>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   110
<textarea tabindex="3" name="data" onkeypress="autogrow(this)">new widgets system</textarea>''')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   111
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   112
    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
   113
        class PFForm(EntityFieldsForm):
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   114
            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
   115
        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
   116
        self.assertTextEquals(self._render_entity_field('upassword', form),
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   117
                              '''<input id="upassword" type="password" name="upassword" value="" tabindex="0"/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   118
<br/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   119
<input type="password" id="upassword" name="upassword-confirm" tabindex="0"/>
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   120
&nbsp;
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   121
<span class="emphasis">confirm password</span>''')
6636c75c4aa9 actual fields test, though it's still to early for form tests
sylvain.thenault@logilab.fr
parents: 1052
diff changeset
   122
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
        
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
if __name__ == '__main__':
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
   125
    unittest_main()