[7416] | 1 | ## $Id: test_phonewidget.py 8168 2012-04-16 06:51:20Z uli $ |
---|
[7345] | 2 | ## |
---|
[7416] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[7345] | 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. |
---|
[7416] | 8 | ## |
---|
[7345] | 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. |
---|
[7416] | 13 | ## |
---|
[7345] | 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 | |
---|
[7855] | 21 | Most tests are reimplementation of similar tests in zope.formlib. |
---|
[7345] | 22 | """ |
---|
| 23 | from zope import schema |
---|
[7854] | 24 | from zope.component import getGlobalSiteManager |
---|
[7840] | 25 | from zope.formlib import form |
---|
[7854] | 26 | from zope.formlib.tests.test_functional_textwidget import( |
---|
| 27 | FunctionalWidgetTestCase, patternExists) |
---|
[7345] | 28 | from zope.formlib.textwidgets import TextWidget |
---|
[7854] | 29 | from zope.interface import Interface, implements |
---|
[7345] | 30 | from zope.publisher.browser import TestRequest |
---|
| 31 | from zope.schema.interfaces import ITextLine |
---|
[7874] | 32 | from waeup.kofa.widgets.phonewidget import PhoneWidget |
---|
[7852] | 33 | |
---|
[7345] | 34 | # Dummy content |
---|
| 35 | class ISampleContent(Interface): |
---|
| 36 | foo = schema.TextLine( |
---|
| 37 | title = u'Phone', |
---|
| 38 | description = u'Phone number', |
---|
[7849] | 39 | required = True, |
---|
| 40 | default=u'+234-1-1') |
---|
[7345] | 41 | |
---|
[7410] | 42 | bar = schema.TextLine( |
---|
| 43 | title = u'Phone', |
---|
| 44 | description = u'Phone number (not required)', |
---|
[7840] | 45 | required = False, |
---|
| 46 | missing_value = u'') |
---|
[7410] | 47 | |
---|
[7440] | 48 | baz = schema.TextLine( |
---|
| 49 | title = u'Phone', |
---|
| 50 | description = u'Required phone with a default', |
---|
[7849] | 51 | required = False, |
---|
| 52 | #default=u'+234--' |
---|
[7840] | 53 | ) |
---|
[7440] | 54 | |
---|
[7345] | 55 | class SampleContent: |
---|
| 56 | implements(ISampleContent) |
---|
| 57 | |
---|
[7849] | 58 | def __init__(self): |
---|
| 59 | self.foo = None |
---|
| 60 | self.bar = 'bar' |
---|
| 61 | self.baz = None |
---|
[7840] | 62 | |
---|
| 63 | class SampleForm(form.EditForm): |
---|
| 64 | form_fields = form.fields(ISampleContent) |
---|
[7852] | 65 | form_fields['foo'].custom_widget = PhoneWidget |
---|
| 66 | form_fields['bar'].custom_widget = PhoneWidget |
---|
| 67 | form_fields['baz'].custom_widget = PhoneWidget |
---|
[7840] | 68 | |
---|
[7852] | 69 | class PhoneWidgetTests(FunctionalWidgetTestCase): |
---|
[7840] | 70 | |
---|
| 71 | widgets = [ |
---|
| 72 | (ITextLine, TextWidget), |
---|
[7849] | 73 | (ITextLine, TextWidget), |
---|
| 74 | (ITextLine, TextWidget), |
---|
[7840] | 75 | ] |
---|
| 76 | |
---|
| 77 | def test_display_editform(self): |
---|
| 78 | content = SampleContent() |
---|
| 79 | request = TestRequest() |
---|
| 80 | html = SampleForm(content, request)() |
---|
| 81 | # foo.country, foo.area and foo.ext exist |
---|
| 82 | self.assert_(patternExists( |
---|
| 83 | '<select .* name="form.foo.country".*>', html)) |
---|
| 84 | self.assert_(patternExists( |
---|
| 85 | '<input .* name="form.foo.area".* value="".*>', html)) |
---|
| 86 | self.assert_(patternExists( |
---|
| 87 | '<input .* name="form.foo.ext".* value="".*>', html)) |
---|
| 88 | return |
---|
| 89 | |
---|
| 90 | def test_submit_editform(self): |
---|
| 91 | # we can submit an edit form |
---|
| 92 | content = SampleContent() |
---|
| 93 | request = TestRequest() |
---|
| 94 | |
---|
| 95 | # submit edit view |
---|
[7849] | 96 | request.form['form.foo.country'] = u'+123' |
---|
[7840] | 97 | request.form['form.foo.area'] = u'456' |
---|
| 98 | request.form['form.foo.ext'] = u'7890' |
---|
| 99 | request.form['form.actions.apply'] = u'' |
---|
| 100 | SampleForm(content, request)() |
---|
| 101 | |
---|
| 102 | # check new values in object |
---|
[7849] | 103 | self.assertEqual(content.foo, u'+123-456-7890') |
---|
[7840] | 104 | return |
---|
| 105 | |
---|
| 106 | def test_invalid_type(self): |
---|
| 107 | # there is no invalid type for textline-based input |
---|
| 108 | content = SampleContent() |
---|
| 109 | request = TestRequest() |
---|
| 110 | |
---|
| 111 | # submit invalid type for text line |
---|
[7849] | 112 | request.form['form.foo.country'] = '+123' |
---|
[7840] | 113 | request.form['form.foo.area'] = '456' |
---|
| 114 | request.form['form.foo.ext'] = '7890' |
---|
| 115 | request.form['form.actions.apply'] = u'' |
---|
| 116 | html = SampleForm(content, request)() |
---|
| 117 | |
---|
| 118 | # We don't have a invalid field value |
---|
| 119 | # since we convert the value to unicode |
---|
| 120 | self.assert_('Object is of wrong type.' not in html) |
---|
| 121 | return |
---|
| 122 | |
---|
| 123 | def test_missing_value(self): |
---|
| 124 | content = SampleContent() |
---|
| 125 | request = TestRequest() |
---|
| 126 | |
---|
[7849] | 127 | request.form['form.foo.country'] = u'+123' |
---|
[7840] | 128 | request.form['form.foo.area'] = u'456' |
---|
| 129 | request.form['form.foo.ext'] = u'7890' |
---|
| 130 | request.form['form.bar.country'] = u'' |
---|
| 131 | request.form['form.bar.area'] = u'' |
---|
| 132 | request.form['form.bar.ext'] = u'' |
---|
| 133 | request.form['form.baz.country'] = u'' |
---|
| 134 | request.form['form.baz.area'] = u'' |
---|
| 135 | request.form['form.baz.ext'] = u'' |
---|
| 136 | request.form['form.actions.apply'] = u'' |
---|
[7849] | 137 | |
---|
[7840] | 138 | SampleForm(content, request)() |
---|
| 139 | |
---|
| 140 | # check new values in object |
---|
[7849] | 141 | self.assertEqual(content.foo, u'+123-456-7890') |
---|
| 142 | self.assertEqual(content.bar, u'') # default missing value |
---|
| 143 | self.assertEqual(content.baz, None) |
---|
[7840] | 144 | return |
---|
[7883] | 145 | |
---|
| 146 | def test_partial_values(self): |
---|
[8168] | 147 | # make sure partial numbers will not be enough |
---|
| 148 | # XXX: we have to test each single field alone as an error |
---|
| 149 | # in one field will stop setting the other ones. |
---|
[7883] | 150 | content = SampleContent() |
---|
| 151 | request = TestRequest() |
---|
| 152 | |
---|
| 153 | request.form['form.foo.country'] = u'+123' |
---|
| 154 | request.form['form.foo.area'] = u'456' |
---|
| 155 | request.form['form.foo.ext'] = u'' |
---|
| 156 | request.form['form.bar.country'] = u'+123' |
---|
[8168] | 157 | request.form['form.bar.area'] = u'12' |
---|
[7883] | 158 | request.form['form.bar.ext'] = u'789' |
---|
| 159 | request.form['form.baz.country'] = u'+123' |
---|
| 160 | request.form['form.baz.area'] = u'456' |
---|
| 161 | request.form['form.baz.ext'] = u'789' |
---|
| 162 | request.form['form.actions.apply'] = u'' |
---|
| 163 | |
---|
| 164 | SampleForm(content, request)() |
---|
| 165 | |
---|
| 166 | # check new values in object |
---|
[8168] | 167 | # as there were errors in the form, no value was set at all |
---|
[7883] | 168 | self.assertEqual(content.foo, None) |
---|
[8168] | 169 | self.assertEqual(content.bar, 'bar') |
---|
| 170 | self.assertEqual(content.baz, None) |
---|
[7883] | 171 | return |
---|
| 172 | |
---|
| 173 | def test_no_values(self): |
---|
| 174 | # if the last two subfields contain no value, no phone will be set |
---|
| 175 | content = SampleContent() |
---|
| 176 | request = TestRequest() |
---|
| 177 | |
---|
| 178 | request.form['form.bar.country'] = u'+123' |
---|
| 179 | request.form['form.bar.area'] = u'' |
---|
| 180 | request.form['form.bar.ext'] = u'' |
---|
| 181 | request.form['form.baz.country'] = u'+124' |
---|
| 182 | request.form['form.baz.area'] = u'' |
---|
| 183 | request.form['form.baz.ext'] = u'' |
---|
| 184 | request.form['form.actions.apply'] = u'' |
---|
| 185 | |
---|
| 186 | SampleForm(content, request)() |
---|
| 187 | |
---|
| 188 | # check new values in object |
---|
| 189 | self.assertEqual(content.bar, u'') # default missing value |
---|
| 190 | self.assertEqual(content.baz, None) |
---|
| 191 | return |
---|