Ignore:
Timestamp:
12 Mar 2012, 16:49:10 (13 years ago)
Author:
uli
Message:

Enable new phone widget and remove the old one.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_phonewidget.py

    r7849 r7852  
    3939from waeup.kofa.widgets.phonewidget import PhoneWidget
    4040
     41import zope.formlib
     42from zope import schema
     43from zope.formlib.tests.test_functional_objectwidget import (
     44    Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig,
     45    PageTemplate, os, DefaultTraversable
     46    )
     47from zope.formlib.tests.test_functional_textwidget import(
     48    FunctionalWidgetTestCase, patternExists)
     49from zope.formlib.widgets import TextWidget
     50from zope.component import adapts
     51from zope.interface import implements
     52from zope.traversing.interfaces import ITraversable
     53from waeup.kofa.widgets.phonewidget import PhoneWidget, PhonePrefixes
     54
    4155# Dummy content
    4256class ISampleContent(Interface):
     
    6882        self.baz = None
    6983
    70 class PhoneWidgetTests(unittest.TestCase):
    71     # Tests for phone widget
    7284
    73     def setUp(self):
    74         placefulSetUp()
    75 
    76         # register TextLine views (so that subwidgets can be rendered)
    77         provideAdapter(
    78             TextWidget, (ITextLine, IDefaultBrowserLayer), IInputWidget)
    79         # errors in forms
    80         provideAdapter(
    81             WidgetInputErrorView, (IWidgetInputError, IDefaultBrowserLayer),
    82             IWidgetInputErrorView)
    83 
    84         # setup some sample content to bind to (only neccessary for
    85         # some of the tests)
    86         self.content = SampleContent()
    87         self.field = ISampleContent['foo']
    88         self.field = self.field.bind(self.content)
    89         self.field_nonreq = ISampleContent['bar']
    90         self.field_nonreq = self.field_nonreq.bind(self.content)
    91         self.field_w_default = ISampleContent['baz']
    92         self.field_w_default = self.field_w_default.bind(self.content)
    93 
    94         # create an empty request. We need one to create a widget
    95         self.request = TestRequest()
    96         self.widget = PhoneWidget(self.field, self.request)
    97         self.widget_nonreq = PhoneWidget(self.field_nonreq, self.request)
    98         return
    99 
    100     def tearDown(self):
    101         placefulTearDown()
    102         return
    103 
    104     def test_ifaces(self):
    105         # make sure we fullfill interface contracts
    106         verify.verifyClass(IBrowserWidget, PhoneWidget)
    107         verify.verifyClass(IInputWidget, PhoneWidget)
    108         verify.verifyObject(IBrowserWidget, self.widget)
    109         verify.verifyObject(IInputWidget, self.widget)
    110         return
    111 
    112     def test_attribs_set(self):
    113         # make sure the public attributes are set correctly
    114         self.assertEqual(self.widget.name, 'field.foo')
    115         self.assertEqual(self.widget.label, u'Phone')
    116         self.assertEqual(self.widget.hint, u'Phone number')
    117         self.assertEqual(self.widget.visible, True)
    118         self.assertEqual(self.widget.required, True)
    119         self.assertEqual(self.widget_nonreq.required, False)
    120         return
    121 
    122     def test_subwidgets_exist(self):
    123         # make sure we have three sub widgets
    124         sub_widget1 = self.widget.widgets['country']
    125         sub_widget2 = self.widget.widgets['area']
    126         sub_widget3 = self.widget.widgets['extension']
    127         self.assertTrue(isinstance(sub_widget1, TextWidget))
    128         self.assertTrue(isinstance(sub_widget2, TextWidget))
    129         self.assertTrue(isinstance(sub_widget3, TextWidget))
    130         return
    131 
    132     def test_set_rendered_value(self):
    133         # make sure we render values correctly
    134         self.widget.setRenderedValue(u'123-456-1234567')
    135         self.assertEqual(
    136             self.widget(),
    137             (u'+&nbsp;<input class="span2 textType" id="field.foo.country"'
    138              u' name="field.foo.country" size="20" type="text" value="123" '
    139              u' />'
    140              u'&nbsp;-\n'
    141              u'<input class="span2 textType" id="field.foo.area"'
    142              u' name="field.foo.area" size="20" type="text" value="456" '
    143              u' />'
    144              u'&nbsp;-\n'
    145              u'<input class="span3 textType" id="field.foo.extension"'
    146              u' name="field.foo.extension" size="20" type="text"'
    147              u' value="1234567"  />\n'))
    148         return
    149 
    150     def test_set_prefix_default(self):
    151         # prefix determines the name of widget and its subwidgets.
    152         self.assertEqual(self.widget.name, 'field.foo')
    153         self.assertEqual(self.widget.widgets['country'].name,
    154                          'field.foo.country')
    155         self.assertEqual(self.widget.widgets['area'].name,
    156                          'field.foo.area')
    157         self.assertEqual(self.widget.widgets['extension'].name,
    158                          'field.foo.extension')
    159         return
    160 
    161     def test_set_prefix_changed(self):
    162         # when we set the prefix, widget names change
    163         self.widget.setPrefix('test.')
    164         self.assertEqual(self.widget.name, 'test.foo')
    165         self.assertEqual(self.widget.widgets['country'].name,
    166                          'test.foo.country')
    167         self.assertEqual(self.widget.widgets['area'].name,
    168                          'test.foo.area')
    169         self.assertEqual(self.widget.widgets['extension'].name,
    170                          'test.foo.extension')
    171         return
    172 
    173     def test_set_prefix_changed_no_dot(self):
    174         # when we try to set a name without trailing dot, one will be
    175         # inserted automatically
    176         self.widget.setPrefix('test')
    177         self.assertEqual(self.widget.name, 'test.foo')
    178         self.assertEqual(self.widget.widgets['country'].name,
    179                          'test.foo.country')
    180         self.assertEqual(self.widget.widgets['area'].name,
    181                          'test.foo.area')
    182         self.assertEqual(self.widget.widgets['extension'].name,
    183                          'test.foo.extension')
    184         return
    185 
    186     def test_get_input_value(self):
    187         # we get a string when we want the input value
    188         request = TestRequest(
    189             form={
    190                 'field.foo.country': '123',
    191                 'field.foo.area': '456',
    192                 'field.foo.extension': '12345'
    193                 })
    194         widget = PhoneWidget(self.field, request)
    195         value = widget.getInputValue()
    196         self.assertEqual(value, '123-456-12345')
    197         return
    198 
    199     def test_get_input_value_invalid(self):
    200         # we get errors when valid data is sent
    201         request = TestRequest(
    202             form={
    203                 'field.foo.country': 'not-a-number',
    204                 'field.foo.area': '456',
    205                 'field.foo.extension': '12345',
    206                 })
    207         widget = PhoneWidget(self.field, request)
    208         self.assertRaises(
    209             WidgetInputError,
    210             widget.getInputValue)
    211         return
    212 
    213     def test_apply_changes(self):
    214         # we can apply passed-in numbers to content objects. The
    215         # widget is smart enough to detect whether the values really
    216         # changed.
    217         class Content(object):
    218             field = None
    219         content = Content()
    220         request = TestRequest(form={
    221                 'field.foo.country': '123',
    222                 'field.foo.area': '456',
    223                 'field.foo.extension': '7890'})
    224         widget = PhoneWidget(self.field, request)
    225         result1 = widget.applyChanges(content)
    226         result2 = widget.applyChanges(content)
    227         self.assertEqual(result1, True)
    228         self.assertEqual(content.foo, u'123-456-7890')
    229         self.assertEqual(result2, False)
    230         return
    231 
    232     def test_has_input(self):
    233         # we can check whether there is input without actually validating.
    234         request = TestRequest(form={
    235                 'field.foo.country': '123'})
    236         widget = PhoneWidget(self.field, request)
    237         result = widget.hasInput()
    238         self.assertEqual(result, False)
    239 
    240         request = TestRequest(form={
    241                 'field.foo.area': '123'})
    242         widget = PhoneWidget(self.field, request)
    243         result = widget.hasInput()
    244         self.assertEqual(result, False)
    245 
    246         request = TestRequest(form={
    247                 'field.foo.extension': '123567'})
    248         widget = PhoneWidget(self.field, request)
    249         result = widget.hasInput()
    250         self.assertEqual(result, False)
    251 
    252         request = TestRequest(form={
    253                 'field.foo.country': '123',
    254                 'field.foo.area': '456',
    255                 'field.foo.extension': '7890'})
    256         widget = PhoneWidget(self.field, request)
    257         result = widget.hasInput()
    258         self.assertEqual(result, True)
    259 
    260         self.assertEqual(self.widget.hasInput(), False)
    261         return
    262 
    263     def test_has_valid_input(self):
    264         # we can check for input also including validation
    265         request = TestRequest(form={
    266                 'field.foo.country': '123'})
    267         widget = PhoneWidget(self.field, request)
    268         result = widget.hasValidInput()
    269         self.assertEqual(result, False)
    270 
    271         request = TestRequest(form={
    272                 'field.foo.area': '123'})
    273         widget = PhoneWidget(self.field, request)
    274         result = widget.hasValidInput()
    275         self.assertEqual(result, False)
    276 
    277         request = TestRequest(form={
    278                 'field.foo.extension': '123567'})
    279         widget = PhoneWidget(self.field, request)
    280         result = widget.hasValidInput()
    281         self.assertEqual(result, False)
    282 
    283         request = TestRequest(form={
    284                 'field.foo.country': '123',
    285                 'field.foo.area': '456',
    286                 'field.foo.extension': '7890'})
    287         widget = PhoneWidget(self.field, request)
    288         result = widget.hasValidInput()
    289         self.assertEqual(result, True)
    290 
    291         self.assertEqual(self.widget.hasValidInput(), False)
    292         return
    293 
    294     def test_hidden(self):
    295         # we can render output as hidden field(s)
    296         self.widget.setRenderedValue(u'123-456-1234567')
    297         self.assertEqual(
    298             self.widget.hidden(),
    299             (u'<input class="span2 hiddenType" id="field.foo.country"'
    300              u' name="field.foo.country" type="hidden" value="123" '
    301              u' />'
    302              u'\n'
    303              u'<input class="span2 hiddenType" id="field.foo.area"'
    304              u' name="field.foo.area" type="hidden" value="456" '
    305              u' />'
    306              u'\n'
    307              u'<input class="span3 hiddenType" id="field.foo.extension"'
    308              u' name="field.foo.extension" type="hidden"'
    309              u' value="1234567"  />'))
    310         return
    311 
    312     def test_error_extension(self):
    313         # the widget can handle input errors in extension subwidget
    314 
    315         # extension field too short
    316         request = TestRequest(form={
    317                 'field.foo.country': '123',
    318                 'field.foo.area': '456',
    319                 'field.foo.extension': '0'})
    320         widget = PhoneWidget(self.field, request)
    321         try:
    322             widget.getInputValue()
    323             self.fail('No error raised')
    324         except WidgetInputError, e:
    325             pass
    326         self.assertEqual(
    327             e.__repr__(),
    328             ("WidgetInputError("
    329              "'extension', u'Direct Line', ConstraintNotSatisfied(u'0'))"))
    330         self.assertEqual(
    331             widget.error(),
    332             u'<span class="error">Constraint not satisfied</span>')
    333         return
    334 
    335 
    336     def test_error_area(self):
    337         # the widget can handle input errors in area code subwidget
    338 
    339         # area code contains illegal chars
    340         request = TestRequest(form={
    341                 'field.foo.country': '123',
    342                 'field.foo.area': 'no-number',
    343                 'field.foo.extension': '12345'})
    344         widget = PhoneWidget(self.field, request)
    345         try:
    346             widget.getInputValue()
    347             self.fail('No error raised')
    348         except WidgetInputError, e:
    349             pass
    350         self.assertEqual(
    351             e.__repr__(),
    352             ("WidgetInputError("
    353              "'area', u'Area Code', ConstraintNotSatisfied(u'no-number'))"))
    354         self.assertEqual(
    355             widget.error(),
    356             u'<span class="error">Constraint not satisfied</span>')
    357         return
    358 
    359     def test_error_country(self):
    360         # the widget can handle input errors in country code subwidget
    361 
    362         # invalid country code
    363         request = TestRequest(form={
    364                 'field.foo.country': 'XXX',
    365                 'field.foo.area': '456',
    366                 'field.foo.extension': '12345'})
    367         widget = PhoneWidget(self.field, request)
    368         try:
    369             widget.getInputValue()
    370             self.fail('No error raised')
    371         except WidgetInputError, e:
    372             pass
    373         self.assertEqual(
    374             e.__repr__(),
    375             ("WidgetInputError("
    376              "'country', u'Country Code', ConstraintNotSatisfied(u'XXX'))"))
    377         self.assertEqual(
    378             widget.error(),
    379             u'<span class="error">Constraint not satisfied</span>')
    380         return
    381 
    382     def test_render_wo_data(self):
    383         # when no data was submitted, the content data will be displayed
    384         request = TestRequest(form={})
    385         self.content.foo = u'11-222-3333'
    386         widget = PhoneWidget(self.field, request)
    387         self.assertEqual(
    388             widget(),
    389             (u'+&nbsp;<input class="span2 textType" id="field.foo.country"'
    390              u' name="field.foo.country" size="20" type="text" value="11" '
    391              u' />'
    392              u'&nbsp;-\n'
    393              u'<input class="span2 textType" id="field.foo.area"'
    394              u' name="field.foo.area" size="20" type="text" value="222" '
    395              u' />'
    396              u'&nbsp;-\n'
    397              u'<input class="span3 textType" id="field.foo.extension"'
    398              u' name="field.foo.extension" size="20" type="text"'
    399              u' value="3333"  />\n'))
    400         return
    401 
    402     def test_render_with_data(self):
    403         # when data was in the request, it will override any content value
    404         request = TestRequest(form={
    405                 'field.foo.country': '11',
    406                 'field.foo.area': '222',
    407                 'field.foo.extension': '3333'})
    408         self.content.foo = u'22-333-4444'
    409         widget = PhoneWidget(self.field, request)
    410         self.assertEqual(
    411             widget(),
    412             (u'+&nbsp;<input class="span2 textType" id="field.foo.country"'
    413              u' name="field.foo.country" size="20" type="text" value="11" '
    414              u' />'
    415              u'&nbsp;-\n'
    416              u'<input class="span2 textType" id="field.foo.area"'
    417              u' name="field.foo.area" size="20" type="text" value="222" '
    418              u' />'
    419              u'&nbsp;-\n'
    420              u'<input class="span3 textType" id="field.foo.extension"'
    421              u' name="field.foo.extension" size="20" type="text"'
    422              u' value="3333"  />\n'))
    423         return
    424 
    425     def test_render_changed_prefix(self):
    426         # when the prefix is changed, the subwidgets will reflect that
    427         # properly. This tests a regression: if setPrefix was called
    428         # on the phone widget, the subwidgets were already assuming
    429         # that there is no data in the request
    430         request = TestRequest(form={
    431                 'field.foo.country': '11',
    432                 'field.foo.area': '222',
    433                 'field.foo.extension': '3333'})
    434         self.content.foo = u'22-333-4444'
    435         widget = PhoneWidget(self.field, request)
    436         # when setting a wrong prefix (not in the form), subwidgets
    437         # will display the content values assuming (correctly) there
    438         # is no data sent for them in the request.
    439         widget.setPrefix('not-existent.')
    440         self.assertTrue('value="4444"' in widget())
    441         # when setting the right prefix, subwidgets will display the
    442         # sent form values and not the content values.
    443         widget.setPrefix('field.')
    444         self.assertTrue('value="3333"' in widget())
    445         return
    446 
    447     def test_render_changed_prefix(self):
    448         # when the prefix is changed, the subwidgets will reflect that
    449         # properly. This tests a regression: if setPrefix was called
    450         # on the phone widget, the subwidgets were already assuming
    451         # that there is no data in the request
    452         request = TestRequest(form={
    453                 'field.foo.country': '11',
    454                 'field.foo.area': '222',
    455                 'field.foo.extension': '3333'})
    456         self.content.foo = u'22-333-4444'
    457         widget = PhoneWidget(self.field, request)
    458         # when setting a wrong prefix (not in the form), subwidgets
    459         # will display the content values assuming (correctly) there
    460         # is no data sent for them in the request.
    461         widget.setPrefix('not-existent.')
    462         self.assertTrue('value="4444"' in widget())
    463         # when setting the right prefix, subwidgets will display the
    464         # sent form values and not the content values.
    465         widget.setPrefix('field.')
    466         self.assertTrue('value="3333"' in widget())
    467         return
    468 
    469     def test_non_required_no_input(self):
    470         # if the bound field requires no input phone widget will cope
    471         # with that.
    472         class Content(object):
    473             field = None
    474 
    475         content = Content()
    476         request = TestRequest(form={
    477                 'field.bar.country': '',
    478                 'field.bar.area': '',
    479                 'field.bar.extension': '',
    480                 })
    481         widget = PhoneWidget(self.field_nonreq, request)
    482         result1 = widget.applyChanges(content)
    483         result2 = widget.getInputValue()
    484         self.assertEqual(result1, True)
    485         # without input we get None
    486         self.assertTrue(content.bar is None)
    487         self.assertTrue(result2 is None)
    488         return
    489 
    490     def test_set_rendered_with_empty_context_value(self):
    491         # if we set empty string as rendered value, empty fields are
    492         # displayed.
    493         self.widget.setRenderedValue(u'')
    494         self.assertEqual(
    495             self.widget(),
    496             (u'+&nbsp;<input class="span2 textType" id="field.foo.country"'
    497              u' name="field.foo.country" size="20" type="text" value="" '
    498              u' />'
    499              u'&nbsp;-\n'
    500              u'<input class="span2 textType" id="field.foo.area"'
    501              u' name="field.foo.area" size="20" type="text" value="" '
    502              u' />'
    503              u'&nbsp;-\n'
    504              u'<input class="span3 textType" id="field.foo.extension"'
    505              u' name="field.foo.extension" size="20" type="text"'
    506              u' value=""  />\n'))
    507         return
    508 
    509 import zope.formlib
    510 from zope import schema
    511 from zope.formlib.tests.test_functional_objectwidget import (
    512     Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig,
    513     PageTemplate, os, DefaultTraversable
    514     )
    515 from zope.formlib.tests.test_functional_textwidget import(
    516     FunctionalWidgetTestCase, patternExists)
    517 from zope.formlib.widgets import TextWidget
    518 from zope.component import adapts
    519 from zope.interface import implements
    520 from zope.traversing.interfaces import ITraversable
    521 from waeup.kofa.widgets.phonewidget import PhoneWidget2, PhonePrefixes
    52285
    52386class SampleForm(form.EditForm):
    52487    form_fields = form.fields(ISampleContent)
    525     form_fields['foo'].custom_widget = PhoneWidget2
    526     form_fields['bar'].custom_widget = PhoneWidget2
    527     form_fields['baz'].custom_widget = PhoneWidget2
     88    form_fields['foo'].custom_widget = PhoneWidget
     89    form_fields['bar'].custom_widget = PhoneWidget
     90    form_fields['baz'].custom_widget = PhoneWidget
    52891
    529 class PhoneWidget2Tests(FunctionalWidgetTestCase):
     92class PhoneWidgetTests(FunctionalWidgetTestCase):
    53093
    53194    widgets = [
     
    53699
    537100    def setUp(self):
    538         super(PhoneWidget2Tests, self).setUp()
     101        super(PhoneWidgetTests, self).setUp()
    539102        # register the phone prefixes utility
    540103        self.gsm = getGlobalSiteManager()
Note: See TracChangeset for help on using the changeset viewer.