1 | ## $Id: test_phonewidget.py 7855 2012-03-12 17:03:53Z 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 | """ |
---|
19 | Tests for PhoneWidget. |
---|
20 | |
---|
21 | Most tests are reimplementation of similar tests in zope.formlib. |
---|
22 | """ |
---|
23 | from zope import schema |
---|
24 | from zope.component import getGlobalSiteManager |
---|
25 | from zope.formlib import form |
---|
26 | from zope.formlib.tests.test_functional_textwidget import( |
---|
27 | FunctionalWidgetTestCase, patternExists) |
---|
28 | from zope.formlib.textwidgets import TextWidget |
---|
29 | from zope.interface import Interface, implements |
---|
30 | from zope.publisher.browser import TestRequest |
---|
31 | from zope.schema.interfaces import ITextLine |
---|
32 | from waeup.kofa.widgets.phonewidget import PhoneWidget, PhonePrefixes |
---|
33 | |
---|
34 | # Dummy content |
---|
35 | class ISampleContent(Interface): |
---|
36 | foo = schema.TextLine( |
---|
37 | title = u'Phone', |
---|
38 | description = u'Phone number', |
---|
39 | required = True, |
---|
40 | default=u'+234-1-1') |
---|
41 | |
---|
42 | bar = schema.TextLine( |
---|
43 | title = u'Phone', |
---|
44 | description = u'Phone number (not required)', |
---|
45 | required = False, |
---|
46 | missing_value = u'') |
---|
47 | |
---|
48 | baz = schema.TextLine( |
---|
49 | title = u'Phone', |
---|
50 | description = u'Required phone with a default', |
---|
51 | required = False, |
---|
52 | #default=u'+234--' |
---|
53 | ) |
---|
54 | |
---|
55 | class SampleContent: |
---|
56 | implements(ISampleContent) |
---|
57 | |
---|
58 | def __init__(self): |
---|
59 | self.foo = None |
---|
60 | self.bar = 'bar' |
---|
61 | self.baz = None |
---|
62 | |
---|
63 | class SampleForm(form.EditForm): |
---|
64 | form_fields = form.fields(ISampleContent) |
---|
65 | form_fields['foo'].custom_widget = PhoneWidget |
---|
66 | form_fields['bar'].custom_widget = PhoneWidget |
---|
67 | form_fields['baz'].custom_widget = PhoneWidget |
---|
68 | |
---|
69 | class PhoneWidgetTests(FunctionalWidgetTestCase): |
---|
70 | |
---|
71 | widgets = [ |
---|
72 | (ITextLine, TextWidget), |
---|
73 | (ITextLine, TextWidget), |
---|
74 | (ITextLine, TextWidget), |
---|
75 | ] |
---|
76 | |
---|
77 | def setUp(self): |
---|
78 | super(PhoneWidgetTests, self).setUp() |
---|
79 | # register the phone prefixes utility |
---|
80 | self.gsm = getGlobalSiteManager() |
---|
81 | self.reg_prefixes = PhonePrefixes() |
---|
82 | self.gsm.registerUtility(self.reg_prefixes) |
---|
83 | return |
---|
84 | |
---|
85 | def tearDown(self): |
---|
86 | self.gsm.unregisterUtility(self.reg_prefixes) |
---|
87 | return |
---|
88 | |
---|
89 | def test_display_editform(self): |
---|
90 | content = SampleContent() |
---|
91 | request = TestRequest() |
---|
92 | html = SampleForm(content, request)() |
---|
93 | # foo.country, foo.area and foo.ext exist |
---|
94 | self.assert_(patternExists( |
---|
95 | '<select .* name="form.foo.country".*>', html)) |
---|
96 | self.assert_(patternExists( |
---|
97 | '<input .* name="form.foo.area".* value="".*>', html)) |
---|
98 | self.assert_(patternExists( |
---|
99 | '<input .* name="form.foo.ext".* value="".*>', html)) |
---|
100 | return |
---|
101 | |
---|
102 | def test_submit_editform(self): |
---|
103 | # we can submit an edit form |
---|
104 | content = SampleContent() |
---|
105 | request = TestRequest() |
---|
106 | |
---|
107 | # submit edit view |
---|
108 | request.form['form.foo.country'] = u'+123' |
---|
109 | request.form['form.foo.area'] = u'456' |
---|
110 | request.form['form.foo.ext'] = u'7890' |
---|
111 | request.form['form.actions.apply'] = u'' |
---|
112 | SampleForm(content, request)() |
---|
113 | |
---|
114 | # check new values in object |
---|
115 | self.assertEqual(content.foo, u'+123-456-7890') |
---|
116 | return |
---|
117 | |
---|
118 | def test_invalid_type(self): |
---|
119 | # there is no invalid type for textline-based input |
---|
120 | content = SampleContent() |
---|
121 | request = TestRequest() |
---|
122 | |
---|
123 | # submit invalid type for text line |
---|
124 | request.form['form.foo.country'] = '+123' |
---|
125 | request.form['form.foo.area'] = '456' |
---|
126 | request.form['form.foo.ext'] = '7890' |
---|
127 | request.form['form.actions.apply'] = u'' |
---|
128 | html = SampleForm(content, request)() |
---|
129 | |
---|
130 | # We don't have a invalid field value |
---|
131 | # since we convert the value to unicode |
---|
132 | self.assert_('Object is of wrong type.' not in html) |
---|
133 | return |
---|
134 | |
---|
135 | def test_missing_value(self): |
---|
136 | content = SampleContent() |
---|
137 | request = TestRequest() |
---|
138 | |
---|
139 | request.form['form.foo.country'] = u'+123' |
---|
140 | request.form['form.foo.area'] = u'456' |
---|
141 | request.form['form.foo.ext'] = u'7890' |
---|
142 | request.form['form.bar.country'] = u'' |
---|
143 | request.form['form.bar.area'] = u'' |
---|
144 | request.form['form.bar.ext'] = u'' |
---|
145 | request.form['form.baz.country'] = u'' |
---|
146 | request.form['form.baz.area'] = u'' |
---|
147 | request.form['form.baz.ext'] = u'' |
---|
148 | request.form['form.actions.apply'] = u'' |
---|
149 | |
---|
150 | SampleForm(content, request)() |
---|
151 | |
---|
152 | # check new values in object |
---|
153 | self.assertEqual(content.foo, u'+123-456-7890') |
---|
154 | self.assertEqual(content.bar, u'') # default missing value |
---|
155 | self.assertEqual(content.baz, None) |
---|
156 | return |
---|