Changeset 7852 for main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests
- Timestamp:
- 12 Mar 2012, 16:49:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_phonewidget.py
r7849 r7852 39 39 from waeup.kofa.widgets.phonewidget import PhoneWidget 40 40 41 import zope.formlib 42 from zope import schema 43 from zope.formlib.tests.test_functional_objectwidget import ( 44 Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig, 45 PageTemplate, os, DefaultTraversable 46 ) 47 from zope.formlib.tests.test_functional_textwidget import( 48 FunctionalWidgetTestCase, patternExists) 49 from zope.formlib.widgets import TextWidget 50 from zope.component import adapts 51 from zope.interface import implements 52 from zope.traversing.interfaces import ITraversable 53 from waeup.kofa.widgets.phonewidget import PhoneWidget, PhonePrefixes 54 41 55 # Dummy content 42 56 class ISampleContent(Interface): … … 68 82 self.baz = None 69 83 70 class PhoneWidgetTests(unittest.TestCase):71 # Tests for phone widget72 84 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 forms80 provideAdapter(81 WidgetInputErrorView, (IWidgetInputError, IDefaultBrowserLayer),82 IWidgetInputErrorView)83 84 # setup some sample content to bind to (only neccessary for85 # 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 widget95 self.request = TestRequest()96 self.widget = PhoneWidget(self.field, self.request)97 self.widget_nonreq = PhoneWidget(self.field_nonreq, self.request)98 return99 100 def tearDown(self):101 placefulTearDown()102 return103 104 def test_ifaces(self):105 # make sure we fullfill interface contracts106 verify.verifyClass(IBrowserWidget, PhoneWidget)107 verify.verifyClass(IInputWidget, PhoneWidget)108 verify.verifyObject(IBrowserWidget, self.widget)109 verify.verifyObject(IInputWidget, self.widget)110 return111 112 def test_attribs_set(self):113 # make sure the public attributes are set correctly114 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 return121 122 def test_subwidgets_exist(self):123 # make sure we have three sub widgets124 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 return131 132 def test_set_rendered_value(self):133 # make sure we render values correctly134 self.widget.setRenderedValue(u'123-456-1234567')135 self.assertEqual(136 self.widget(),137 (u'+ <input class="span2 textType" id="field.foo.country"'138 u' name="field.foo.country" size="20" type="text" value="123" '139 u' />'140 u' -\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' -\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 return149 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 return160 161 def test_set_prefix_changed(self):162 # when we set the prefix, widget names change163 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 return172 173 def test_set_prefix_changed_no_dot(self):174 # when we try to set a name without trailing dot, one will be175 # inserted automatically176 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 return185 186 def test_get_input_value(self):187 # we get a string when we want the input value188 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 return198 199 def test_get_input_value_invalid(self):200 # we get errors when valid data is sent201 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 return212 213 def test_apply_changes(self):214 # we can apply passed-in numbers to content objects. The215 # widget is smart enough to detect whether the values really216 # changed.217 class Content(object):218 field = None219 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 return231 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 return262 263 def test_has_valid_input(self):264 # we can check for input also including validation265 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 return293 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 return311 312 def test_error_extension(self):313 # the widget can handle input errors in extension subwidget314 315 # extension field too short316 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 pass326 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 return334 335 336 def test_error_area(self):337 # the widget can handle input errors in area code subwidget338 339 # area code contains illegal chars340 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 pass350 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 return358 359 def test_error_country(self):360 # the widget can handle input errors in country code subwidget361 362 # invalid country code363 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 pass373 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 return381 382 def test_render_wo_data(self):383 # when no data was submitted, the content data will be displayed384 request = TestRequest(form={})385 self.content.foo = u'11-222-3333'386 widget = PhoneWidget(self.field, request)387 self.assertEqual(388 widget(),389 (u'+ <input class="span2 textType" id="field.foo.country"'390 u' name="field.foo.country" size="20" type="text" value="11" '391 u' />'392 u' -\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' -\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 return401 402 def test_render_with_data(self):403 # when data was in the request, it will override any content value404 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'+ <input class="span2 textType" id="field.foo.country"'413 u' name="field.foo.country" size="20" type="text" value="11" '414 u' />'415 u' -\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' -\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 return424 425 def test_render_changed_prefix(self):426 # when the prefix is changed, the subwidgets will reflect that427 # properly. This tests a regression: if setPrefix was called428 # on the phone widget, the subwidgets were already assuming429 # that there is no data in the request430 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), subwidgets437 # will display the content values assuming (correctly) there438 # 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 the442 # sent form values and not the content values.443 widget.setPrefix('field.')444 self.assertTrue('value="3333"' in widget())445 return446 447 def test_render_changed_prefix(self):448 # when the prefix is changed, the subwidgets will reflect that449 # properly. This tests a regression: if setPrefix was called450 # on the phone widget, the subwidgets were already assuming451 # that there is no data in the request452 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), subwidgets459 # will display the content values assuming (correctly) there460 # 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 the464 # sent form values and not the content values.465 widget.setPrefix('field.')466 self.assertTrue('value="3333"' in widget())467 return468 469 def test_non_required_no_input(self):470 # if the bound field requires no input phone widget will cope471 # with that.472 class Content(object):473 field = None474 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 None486 self.assertTrue(content.bar is None)487 self.assertTrue(result2 is None)488 return489 490 def test_set_rendered_with_empty_context_value(self):491 # if we set empty string as rendered value, empty fields are492 # displayed.493 self.widget.setRenderedValue(u'')494 self.assertEqual(495 self.widget(),496 (u'+ <input class="span2 textType" id="field.foo.country"'497 u' name="field.foo.country" size="20" type="text" value="" '498 u' />'499 u' -\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' -\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 return508 509 import zope.formlib510 from zope import schema511 from zope.formlib.tests.test_functional_objectwidget import (512 Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig,513 PageTemplate, os, DefaultTraversable514 )515 from zope.formlib.tests.test_functional_textwidget import(516 FunctionalWidgetTestCase, patternExists)517 from zope.formlib.widgets import TextWidget518 from zope.component import adapts519 from zope.interface import implements520 from zope.traversing.interfaces import ITraversable521 from waeup.kofa.widgets.phonewidget import PhoneWidget2, PhonePrefixes522 85 523 86 class SampleForm(form.EditForm): 524 87 form_fields = form.fields(ISampleContent) 525 form_fields['foo'].custom_widget = PhoneWidget 2526 form_fields['bar'].custom_widget = PhoneWidget 2527 form_fields['baz'].custom_widget = PhoneWidget 288 form_fields['foo'].custom_widget = PhoneWidget 89 form_fields['bar'].custom_widget = PhoneWidget 90 form_fields['baz'].custom_widget = PhoneWidget 528 91 529 class PhoneWidget 2Tests(FunctionalWidgetTestCase):92 class PhoneWidgetTests(FunctionalWidgetTestCase): 530 93 531 94 widgets = [ … … 536 99 537 100 def setUp(self): 538 super(PhoneWidget 2Tests, self).setUp()101 super(PhoneWidgetTests, self).setUp() 539 102 # register the phone prefixes utility 540 103 self.gsm = getGlobalSiteManager()
Note: See TracChangeset for help on using the changeset viewer.