Ignore:
Timestamp:
11 Mar 2012, 18:47:20 (13 years ago)
Author:
uli
Message:

Some better suited phone widget. Yet not finished (validation missing).

File:
1 edited

Legend:

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

    r7811 r7840  
    2626# XXX: could we possibly get rid of zope.app.testing?
    2727from zope.app.testing.setup import placefulSetUp, placefulTearDown
    28 from zope.component import provideAdapter
     28from zope.component import provideAdapter, getGlobalSiteManager
     29from zope.formlib import form
    2930from zope.formlib.exception import WidgetInputErrorView
    3031from zope.formlib.interfaces import (
     
    4849        title = u'Phone',
    4950        description = u'Phone number (not required)',
    50         required = False,)
     51        required = False,
     52        missing_value = u'')
    5153
    5254    baz = schema.TextLine(
     
    5456        description = u'Required phone with a default',
    5557        required = True,
    56         default=u'234--')
     58        #default=u'234--'
     59        )
    5760
    5861class SampleContent:
    5962    implements(ISampleContent)
     63
     64    foo = None
     65    bar = 'bar'
     66    baz = None
    6067
    6168class PhoneWidgetTests(unittest.TestCase):
     
    497504             u' value=""  />\n'))
    498505        return
     506
     507import zope.formlib
     508from zope import schema
     509from zope.formlib.tests.test_functional_objectwidget import (
     510    Test, PlacelessSetup, VerifyResults, traversingSetUp, xmlconfig,
     511    PageTemplate, os, DefaultTraversable
     512    )
     513from zope.formlib.tests.test_functional_textwidget import(
     514    FunctionalWidgetTestCase, patternExists)
     515from zope.formlib.widgets import TextWidget
     516from zope.component import adapts
     517from zope.interface import implements
     518from zope.traversing.interfaces import ITraversable
     519from waeup.kofa.widgets.phonewidget import PhoneWidget2, PhonePrefixes
     520
     521class SampleForm(form.EditForm):
     522    form_fields = form.fields(ISampleContent)
     523    form_fields['foo'].custom_widget = PhoneWidget2
     524    form_fields['bar'].custom_widget = PhoneWidget2
     525    form_fields['baz'].custom_widget = PhoneWidget2
     526
     527class PhoneWidget2Tests(FunctionalWidgetTestCase):
     528
     529    widgets = [
     530        (ITextLine, TextWidget),
     531        ]
     532
     533    def setUp(self):
     534        super(PhoneWidget2Tests, self).setUp()
     535        self.gsm = getGlobalSiteManager()
     536        self.reg_prefixes = PhonePrefixes()
     537        self.gsm.registerUtility(self.reg_prefixes)
     538        return
     539
     540    def tearDown(self):
     541        self.gsm.unregisterUtility(self.reg_prefixes)
     542        return
     543
     544    def test_display_editform(self):
     545        content = SampleContent()
     546        request = TestRequest()
     547        html = SampleForm(content, request)()
     548        # foo.country, foo.area and foo.ext exist
     549        self.assert_(patternExists(
     550            '<select .* name="form.foo.country".*>', html))
     551        self.assert_(patternExists(
     552            '<input .* name="form.foo.area".* value="".*>', html))
     553        self.assert_(patternExists(
     554            '<input .* name="form.foo.ext".* value="".*>', html))
     555        return
     556
     557    def test_submit_editform(self):
     558        # we can submit an edit form
     559        content = SampleContent()
     560        request = TestRequest()
     561
     562        # submit edit view
     563        request.form['form.foo.country'] = u'123'
     564        request.form['form.foo.area'] = u'456'
     565        request.form['form.foo.ext'] = u'7890'
     566        request.form['form.actions.apply'] = u''
     567        SampleForm(content, request)()
     568
     569        # check new values in object
     570        self.assertEqual(content.foo, u'123-456-7890')
     571        return
     572
     573    def test_invalid_type(self):
     574        # there is no invalid type for textline-based input
     575        content = SampleContent()
     576        request = TestRequest()
     577
     578        # submit invalid type for text line
     579        request.form['form.foo.country'] = '123'
     580        request.form['form.foo.area'] = '456'
     581        request.form['form.foo.ext'] = '7890'
     582        request.form['form.actions.apply'] = u''
     583        html = SampleForm(content, request)()
     584
     585        # We don't have a invalid field value
     586        # since we convert the value to unicode
     587        self.assert_('Object is of wrong type.' not in html)
     588        return
     589
     590    def test_missing_value(self):
     591        content = SampleContent()
     592        request = TestRequest()
     593
     594        request.form['form.foo.country'] = u'123'
     595        request.form['form.foo.area'] = u'456'
     596        request.form['form.foo.ext'] = u'7890'
     597        request.form['form.bar.country'] = u''
     598        request.form['form.bar.area'] = u''
     599        request.form['form.bar.ext'] = u''
     600        request.form['form.baz.country'] = u''
     601        request.form['form.baz.area'] = u''
     602        request.form['form.baz.ext'] = u''
     603        request.form['form.actions.apply'] = u''
     604        SampleForm(content, request)()
     605
     606        # check new values in object
     607        self.assertEqual(content.foo, u'123-456-7890')
     608        self.assertEqual(content.bar, u'--') # default missing value
     609        self.assertEqual(content.baz, u'--')
     610        return
Note: See TracChangeset for help on using the changeset viewer.