web/test/unittest_formwidgets.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """unittests for cw.web.formwidgets"""
       
    19 
       
    20 from logilab.common.testlib import unittest_main, mock_object as mock
       
    21 
       
    22 from cubicweb.devtools import fake
       
    23 from cubicweb.devtools.testlib import CubicWebTC
       
    24 from cubicweb.web import formwidgets, formfields
       
    25 from cubicweb.web.views.forms import FieldsForm
       
    26 
       
    27 
       
    28 class WidgetsTC(CubicWebTC):
       
    29 
       
    30     def test_editableurl_widget(self):
       
    31         field = formfields.guess_field(self.schema['Bookmark'], self.schema['path'])
       
    32         widget = formwidgets.EditableURLWidget()
       
    33         req = fake.FakeRequest(form={'path-subjectfqs:A': 'param=value&vid=view'})
       
    34         form = mock(_cw=req, formvalues={}, edited_entity=mock(eid='A'))
       
    35         self.assertEqual(widget.process_field_data(form, field),
       
    36                          '?param=value%26vid%3Dview')
       
    37 
       
    38     def test_bitselect_widget(self):
       
    39         field = formfields.guess_field(self.schema['CWAttribute'], self.schema['ordernum'])
       
    40         field.choices = [('un', '1',), ('deux', '2',)]
       
    41         widget = formwidgets.BitSelect(settabindex=False)
       
    42         req = fake.FakeRequest(form={'ordernum-subject:A': ['1', '2']})
       
    43         form = mock(_cw=req, formvalues={}, edited_entity=mock(eid='A'),
       
    44                     form_previous_values=())
       
    45         self.assertMultiLineEqual(widget._render(form, field, None),
       
    46                              '''\
       
    47 <select id="ordernum-subject:A" multiple="multiple" name="ordernum-subject:A" size="2">
       
    48 <option selected="selected" value="2">deux</option>
       
    49 <option selected="selected" value="1">un</option>
       
    50 </select>''')
       
    51         self.assertEqual(widget.process_field_data(form, field),
       
    52                          3)
       
    53 
       
    54     def test_xml_escape_checkbox(self):
       
    55         class TestForm(FieldsForm):
       
    56             bool = formfields.BooleanField(ignore_req_params=True,
       
    57                 choices=[('python >> others', '1')],
       
    58                 widget=formwidgets.CheckBox())
       
    59         with self.admin_access.web_request() as req:
       
    60             form = TestForm(req, None)
       
    61             form.build_context()
       
    62             field = form.field_by_name('bool')
       
    63             widget = field.widget
       
    64             self.assertMultiLineEqual(widget._render(form, field, None),
       
    65                 '<label><input id="bool" name="bool" tabindex="1" '
       
    66                 'type="checkbox" value="1" />&#160;'
       
    67                 'python &gt;&gt; others</label>')
       
    68 
       
    69 
       
    70 if __name__ == '__main__':
       
    71     unittest_main()