web/test/unittest_formwidgets.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Dec 2015 17:44:17 +0100
changeset 11001 37c5c9b3b3bf
parent 10902 41bd01ebebb4
permissions -rw-r--r--
[migration] handle the case where new final entity type is not yet in the schema This may occur on new cube addition. Related to #7569998.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# copyright 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
#
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
#
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# any later version.
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
#
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# details.
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
#
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
"""unittests for cw.web.formwidgets"""
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    20
from logilab.common.testlib import unittest_main, mock_object as mock
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    22
from cubicweb.devtools import fake
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    23
from cubicweb.devtools.testlib import CubicWebTC
8665
e65af61bde7d [uicfg] uicfg.py moves from web/ to web/views/ (prepares #2406609)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
    24
from cubicweb.web import formwidgets, formfields
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    25
from cubicweb.web.views.forms import FieldsForm
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    28
class WidgetsTC(CubicWebTC):
10596
3a7ce5c9fe35 [web/test] Use TestCase.setUpClass instead of setUpModule
Julien Cristau <julien.cristau@logilab.fr>
parents: 8665
diff changeset
    29
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    30
    def test_editableurl_widget(self):
10596
3a7ce5c9fe35 [web/test] Use TestCase.setUpClass instead of setUpModule
Julien Cristau <julien.cristau@logilab.fr>
parents: 8665
diff changeset
    31
        field = formfields.guess_field(self.schema['Bookmark'], self.schema['path'])
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
        widget = formwidgets.EditableURLWidget()
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
        req = fake.FakeRequest(form={'path-subjectfqs:A': 'param=value&vid=view'})
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
        form = mock(_cw=req, formvalues={}, edited_entity=mock(eid='A'))
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
        self.assertEqual(widget.process_field_data(form, field),
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
                         '?param=value%26vid%3Dview')
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    38
    def test_bitselect_widget(self):
10596
3a7ce5c9fe35 [web/test] Use TestCase.setUpClass instead of setUpModule
Julien Cristau <julien.cristau@logilab.fr>
parents: 8665
diff changeset
    39
        field = formfields.guess_field(self.schema['CWAttribute'], self.schema['ordernum'])
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    40
        field.choices = [('un', '1',), ('deux', '2',)]
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    41
        widget = formwidgets.BitSelect(settabindex=False)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    42
        req = fake.FakeRequest(form={'ordernum-subject:A': ['1', '2']})
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    43
        form = mock(_cw=req, formvalues={}, edited_entity=mock(eid='A'),
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    44
                    form_previous_values=())
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    45
        self.assertMultiLineEqual(widget._render(form, field, None),
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    46
                             '''\
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    47
<select id="ordernum-subject:A" multiple="multiple" name="ordernum-subject:A" size="2">
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    48
<option selected="selected" value="2">deux</option>
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    49
<option selected="selected" value="1">un</option>
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    50
</select>''')
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    51
        self.assertEqual(widget.process_field_data(form, field),
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    52
                         3)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6781
diff changeset
    53
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    54
    def test_xml_escape_checkbox(self):
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    55
        class TestForm(FieldsForm):
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    56
            bool = formfields.BooleanField(ignore_req_params=True,
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    57
                choices=[('python >> others', '1')],
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    58
                widget=formwidgets.CheckBox())
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    59
        with self.admin_access.web_request() as req:
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    60
            form = TestForm(req, None)
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    61
            form.build_context()
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    62
            field = form.field_by_name('bool')
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    63
            widget = field.widget
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    64
            self.assertMultiLineEqual(widget._render(form, field, None),
10902
41bd01ebebb4 [web] Add a <label> around choices on CheckBox and Radio form widgets
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10901
diff changeset
    65
                '<label><input id="bool" name="bool" tabindex="1" '
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    66
                'type="checkbox" value="1" />&#160;'
10902
41bd01ebebb4 [web] Add a <label> around choices on CheckBox and Radio form widgets
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10901
diff changeset
    67
                'python &gt;&gt; others</label>')
10901
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    68
ddeac3ecdd88 [web] xml_escape CheckBox and Radio labels (closes #7672764)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10596
diff changeset
    69
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
if __name__ == '__main__':
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
    unittest_main()