source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_phonewidget.py @ 7854

Last change on this file since 7854 was 7854, checked in by uli, 13 years ago

Clean up imports.

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1## $Id: test_phonewidget.py 7854 2012-03-12 17:01:12Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Tests for PhoneWidget.
20
21Tests in here are an extended reformulation of the tests done in
22z3.widget as doctests for the usphone widget.
23"""
24from zope import schema
25from zope.component import getGlobalSiteManager
26from zope.formlib import form
27from zope.formlib.tests.test_functional_textwidget import(
28    FunctionalWidgetTestCase, patternExists)
29from zope.formlib.textwidgets import TextWidget
30from zope.interface import Interface, implements
31from zope.publisher.browser import TestRequest
32from zope.schema.interfaces import ITextLine
33from waeup.kofa.widgets.phonewidget import PhoneWidget, PhonePrefixes
34
35# Dummy content
36class ISampleContent(Interface):
37    foo = schema.TextLine(
38        title = u'Phone',
39        description = u'Phone number',
40        required = True,
41        default=u'+234-1-1')
42
43    bar = schema.TextLine(
44        title = u'Phone',
45        description = u'Phone number (not required)',
46        required = False,
47        missing_value = u'')
48
49    baz = schema.TextLine(
50        title = u'Phone',
51        description = u'Required phone with a default',
52        required = False,
53        #default=u'+234--'
54        )
55
56class SampleContent:
57    implements(ISampleContent)
58
59    def __init__(self):
60        self.foo = None
61        self.bar = 'bar'
62        self.baz = None
63
64class SampleForm(form.EditForm):
65    form_fields = form.fields(ISampleContent)
66    form_fields['foo'].custom_widget = PhoneWidget
67    form_fields['bar'].custom_widget = PhoneWidget
68    form_fields['baz'].custom_widget = PhoneWidget
69
70class PhoneWidgetTests(FunctionalWidgetTestCase):
71
72    widgets = [
73        (ITextLine, TextWidget),
74        (ITextLine, TextWidget),
75        (ITextLine, TextWidget),
76        ]
77
78    def setUp(self):
79        super(PhoneWidgetTests, self).setUp()
80        # register the phone prefixes utility
81        self.gsm = getGlobalSiteManager()
82        self.reg_prefixes = PhonePrefixes()
83        self.gsm.registerUtility(self.reg_prefixes)
84        return
85
86    def tearDown(self):
87        self.gsm.unregisterUtility(self.reg_prefixes)
88        return
89
90    def test_display_editform(self):
91        content = SampleContent()
92        request = TestRequest()
93        html = SampleForm(content, request)()
94        # foo.country, foo.area and foo.ext exist
95        self.assert_(patternExists(
96            '<select .* name="form.foo.country".*>', html))
97        self.assert_(patternExists(
98            '<input .* name="form.foo.area".* value="".*>', html))
99        self.assert_(patternExists(
100            '<input .* name="form.foo.ext".* value="".*>', html))
101        return
102
103    def test_submit_editform(self):
104        # we can submit an edit form
105        content = SampleContent()
106        request = TestRequest()
107
108        # submit edit view
109        request.form['form.foo.country'] = u'+123'
110        request.form['form.foo.area'] = u'456'
111        request.form['form.foo.ext'] = u'7890'
112        request.form['form.actions.apply'] = u''
113        SampleForm(content, request)()
114
115        # check new values in object
116        self.assertEqual(content.foo, u'+123-456-7890')
117        return
118
119    def test_invalid_type(self):
120        # there is no invalid type for textline-based input
121        content = SampleContent()
122        request = TestRequest()
123
124        # submit invalid type for text line
125        request.form['form.foo.country'] = '+123'
126        request.form['form.foo.area'] = '456'
127        request.form['form.foo.ext'] = '7890'
128        request.form['form.actions.apply'] = u''
129        html = SampleForm(content, request)()
130
131        # We don't have a invalid field value
132        # since we convert the value to unicode
133        self.assert_('Object is of wrong type.' not in html)
134        return
135
136    def test_missing_value(self):
137        content = SampleContent()
138        request = TestRequest()
139
140        request.form['form.foo.country'] = u'+123'
141        request.form['form.foo.area'] = u'456'
142        request.form['form.foo.ext'] = u'7890'
143        request.form['form.bar.country'] = u''
144        request.form['form.bar.area'] = u''
145        request.form['form.bar.ext'] = u''
146        request.form['form.baz.country'] = u''
147        request.form['form.baz.area'] = u''
148        request.form['form.baz.ext'] = u''
149        request.form['form.actions.apply'] = u''
150
151        SampleForm(content, request)()
152
153        # check new values in object
154        self.assertEqual(content.foo, u'+123-456-7890')
155        self.assertEqual(content.bar, u'') # default missing value
156        self.assertEqual(content.baz, None)
157        return
Note: See TracBrowser for help on using the repository browser.