| 1 | ## $Id: test_phonewidget.py 8168 2012-04-16 06:51:20Z 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 |
|---|
| 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 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 |
|---|
| 96 | request.form['form.foo.country'] = u'+123' |
|---|
| 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 |
|---|
| 103 | self.assertEqual(content.foo, u'+123-456-7890') |
|---|
| 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 |
|---|
| 112 | request.form['form.foo.country'] = '+123' |
|---|
| 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 | |
|---|
| 127 | request.form['form.foo.country'] = u'+123' |
|---|
| 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'' |
|---|
| 137 | |
|---|
| 138 | SampleForm(content, request)() |
|---|
| 139 | |
|---|
| 140 | # check new values in object |
|---|
| 141 | self.assertEqual(content.foo, u'+123-456-7890') |
|---|
| 142 | self.assertEqual(content.bar, u'') # default missing value |
|---|
| 143 | self.assertEqual(content.baz, None) |
|---|
| 144 | return |
|---|
| 145 | |
|---|
| 146 | def test_partial_values(self): |
|---|
| 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. |
|---|
| 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' |
|---|
| 157 | request.form['form.bar.area'] = u'12' |
|---|
| 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 |
|---|
| 167 | # as there were errors in the form, no value was set at all |
|---|
| 168 | self.assertEqual(content.foo, None) |
|---|
| 169 | self.assertEqual(content.bar, 'bar') |
|---|
| 170 | self.assertEqual(content.baz, None) |
|---|
| 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 |
|---|