1 | ## $Id: test_objectwidget.py 7196 2011-11-25 07:44:52Z henrik $ |
---|
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 | """Tests for objectwidget. |
---|
19 | """ |
---|
20 | import doctest |
---|
21 | import unittest |
---|
22 | import sys |
---|
23 | from zope.component import testing |
---|
24 | from zope.interface import Interface, implements |
---|
25 | from zope.interface.verify import verifyClass |
---|
26 | from zope.publisher.browser import TestRequest |
---|
27 | from zope.schema import Object, TextLine |
---|
28 | from zope.schema.interfaces import ITextLine |
---|
29 | from zope.traversing.adapters import DefaultTraversable |
---|
30 | from zope.traversing.interfaces import ITraversable |
---|
31 | from zope.publisher.interfaces.browser import IDefaultBrowserLayer |
---|
32 | from zope.component import provideAdapter |
---|
33 | |
---|
34 | from zope.formlib.interfaces import ( |
---|
35 | IInputWidget, IDisplayWidget,MissingInputError) |
---|
36 | from zope.formlib.widgets import TextWidget, DisplayWidget |
---|
37 | from zope.formlib.tests.test_browserwidget import BrowserWidgetTest |
---|
38 | from zope.formlib.interfaces import IWidgetInputErrorView |
---|
39 | |
---|
40 | from waeup.sirp.widgets.objectwidget import WAeUPObjectWidget as ObjectWidget |
---|
41 | from waeup.sirp.widgets.objectwidget import ( |
---|
42 | WAeUPObjectDisplayWidget as ObjectDisplayWidget) |
---|
43 | |
---|
44 | class ITestContact(Interface): |
---|
45 | name = TextLine() |
---|
46 | email = TextLine() |
---|
47 | |
---|
48 | class TestContact(object): |
---|
49 | implements(ITestContact) |
---|
50 | |
---|
51 | class ITestContent(Interface): |
---|
52 | foo = Object( |
---|
53 | ITestContact, |
---|
54 | title = u'Foo Title', |
---|
55 | description = u'', |
---|
56 | ) |
---|
57 | |
---|
58 | class ObjectWidgetInputErrorView(object): |
---|
59 | implements(IWidgetInputErrorView) |
---|
60 | |
---|
61 | def __init__(self, error, request): |
---|
62 | self.error = error |
---|
63 | self.request = request |
---|
64 | |
---|
65 | def snippet(self): |
---|
66 | return repr(self.error) |
---|
67 | |
---|
68 | class ObjectWidgetTest(BrowserWidgetTest): |
---|
69 | """Documents and tests the object widget. |
---|
70 | |
---|
71 | >>> from zope.interface.verify import verifyClass |
---|
72 | >>> verifyClass(IInputWidget, ObjectWidget) |
---|
73 | True |
---|
74 | """ |
---|
75 | |
---|
76 | _FieldFactory = Object |
---|
77 | def _WidgetFactory(self, context, request, **kw): |
---|
78 | kw.update({'factory': TestContact}) |
---|
79 | return ObjectWidget(context, request, **kw) |
---|
80 | |
---|
81 | def setUpContent(self, desc=u'', title=u'Foo Title'): |
---|
82 | provideAdapter(TextWidget, (ITextLine, IDefaultBrowserLayer), |
---|
83 | IInputWidget) |
---|
84 | |
---|
85 | class TestObject(object): |
---|
86 | implements(ITestContent) |
---|
87 | |
---|
88 | self.content = TestObject() |
---|
89 | self.field = ITestContent['foo'] |
---|
90 | self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl') |
---|
91 | self.request.form['field.foo'] = u'Foo Value' |
---|
92 | self._widget = self._WidgetFactory(self.field, self.request) |
---|
93 | |
---|
94 | def setUp(self): |
---|
95 | super(ObjectWidgetTest, self).setUp() |
---|
96 | self.field = Object(ITestContact, __name__=u'foo') |
---|
97 | provideAdapter(TextWidget, |
---|
98 | (ITextLine, IDefaultBrowserLayer), |
---|
99 | IInputWidget) |
---|
100 | |
---|
101 | def test_applyChanges(self): |
---|
102 | self.request.form['field.foo.name'] = u'Foo Name' |
---|
103 | self.request.form['field.foo.email'] = u'foo@foo.test' |
---|
104 | widget = self._WidgetFactory(self.field, self.request) |
---|
105 | |
---|
106 | self.assertEqual(widget.applyChanges(self.content), True) |
---|
107 | self.assertEqual(hasattr(self.content, 'foo'), True) |
---|
108 | self.assertEqual(isinstance(self.content.foo, TestContact), True) |
---|
109 | self.assertEqual(self.content.foo.name, u'Foo Name') |
---|
110 | self.assertEqual(self.content.foo.email, u'foo@foo.test') |
---|
111 | |
---|
112 | def test_error(self): |
---|
113 | provideAdapter( |
---|
114 | ObjectWidgetInputErrorView, |
---|
115 | (MissingInputError, TestRequest), |
---|
116 | IWidgetInputErrorView) |
---|
117 | |
---|
118 | widget = self._WidgetFactory(self.field, self.request) |
---|
119 | self.assertRaises(MissingInputError, widget.getInputValue) |
---|
120 | error_html = widget.error() |
---|
121 | if sys.version_info < (2, 5): |
---|
122 | self.failUnless("email: <zope.formlib.interfaces.Mis" |
---|
123 | in error_html) |
---|
124 | self.failUnless("name: <zope.formlib.interfaces.Miss" |
---|
125 | in error_html) |
---|
126 | else: |
---|
127 | self.failUnless( |
---|
128 | "email: MissingInputError(u'field.foo.email', u'', None)" |
---|
129 | in error_html) |
---|
130 | self.failUnless( |
---|
131 | "name: MissingInputError(u'field.foo.name', u'', None)" |
---|
132 | in error_html) |
---|
133 | |
---|
134 | def test_applyChangesNoChange(self): |
---|
135 | self.content.foo = TestContact() |
---|
136 | self.content.foo.name = u'Foo Name' |
---|
137 | self.content.foo.email = u'foo@foo.test' |
---|
138 | |
---|
139 | self.request.form['field.foo.name'] = u'Foo Name' |
---|
140 | self.request.form['field.foo.email'] = u'foo@foo.test' |
---|
141 | widget = self._WidgetFactory(self.field, self.request) |
---|
142 | widget.setRenderedValue(self.content.foo) |
---|
143 | |
---|
144 | self.assertEqual(widget.applyChanges(self.content), False) |
---|
145 | self.assertEqual(hasattr(self.content, 'foo'), True) |
---|
146 | self.assertEqual(isinstance(self.content.foo, TestContact), True) |
---|
147 | self.assertEqual(self.content.foo.name, u'Foo Name') |
---|
148 | self.assertEqual(self.content.foo.email, u'foo@foo.test') |
---|
149 | |
---|
150 | def test_respect_custom_widgets(self): |
---|
151 | # We can use our own subwidgets when creating ObjectWidgets |
---|
152 | class CustomTextWidget(TextWidget): |
---|
153 | pass |
---|
154 | |
---|
155 | # We create a custom widget for the `name` field of ITestContact |
---|
156 | name_widget = CustomTextWidget(ITestContact['name'], self.request) |
---|
157 | |
---|
158 | # Custom widgets are passed by <fieldname>_widget keyword: |
---|
159 | widget = self._WidgetFactory(self.field, self.request, |
---|
160 | name_widget=name_widget) |
---|
161 | widget1, widget2 = widget.subwidgets() |
---|
162 | self.assertTrue(widget1 is name_widget) |
---|
163 | |
---|
164 | |
---|
165 | class ObjectDisplayWidgetTest(BrowserWidgetTest): |
---|
166 | """Documents and tests the display variant of object widget. |
---|
167 | |
---|
168 | >>> from zope.interface.verify import verifyClass |
---|
169 | >>> verifyClass(IDisplayWidget, ObjectDisplayWidget) |
---|
170 | True |
---|
171 | |
---|
172 | """ |
---|
173 | _FieldFactory = Object |
---|
174 | def _WidgetFactory(self, context, request, **kw): |
---|
175 | kw.update({'factory': TestContact}) |
---|
176 | return ObjectDisplayWidget(context, request, **kw) |
---|
177 | |
---|
178 | def setUpContent(self, desc=u'', title=u'Foo Title'): |
---|
179 | provideAdapter(TextWidget, (ITextLine, IDefaultBrowserLayer), |
---|
180 | IInputWidget) |
---|
181 | provideAdapter(DisplayWidget, (ITextLine, IDefaultBrowserLayer), |
---|
182 | IDisplayWidget) |
---|
183 | # The widget must be traversable. We register the default |
---|
184 | # adapter that can turn nearly any object into an ITraversable. |
---|
185 | provideAdapter(DefaultTraversable, (None,), ITraversable) |
---|
186 | |
---|
187 | class TestObject(object): |
---|
188 | implements(ITestContent) |
---|
189 | |
---|
190 | self.content = TestObject() |
---|
191 | self.content.name = u'Foo Name' |
---|
192 | self.content.email = u'foo@foo.test' |
---|
193 | self.field = ITestContent['foo'] |
---|
194 | self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl') |
---|
195 | self.request.form['field.foo'] = u'Foo Value' |
---|
196 | self._widget = self._WidgetFactory(self.field, self.request) |
---|
197 | |
---|
198 | def setUp(self): |
---|
199 | super(ObjectDisplayWidgetTest, self).setUp() |
---|
200 | self.field = Object(ITestContact, __name__=u'foo') |
---|
201 | provideAdapter(TextWidget, |
---|
202 | (ITextLine, IDefaultBrowserLayer), |
---|
203 | IInputWidget) |
---|
204 | provideAdapter(DisplayWidget, |
---|
205 | (ITextLine, IDefaultBrowserLayer), |
---|
206 | IDisplayWidget) |
---|
207 | |
---|
208 | |
---|
209 | def test_interfaces(self): |
---|
210 | from zope.interface.verify import DoesNotImplement |
---|
211 | self.assertTrue(IDisplayWidget.providedBy(self._widget)) |
---|
212 | self.assertFalse(IInputWidget.providedBy(self._widget)) |
---|
213 | self.assertTrue(verifyClass(IDisplayWidget, ObjectDisplayWidget)) |
---|
214 | |
---|
215 | def test_render(self): |
---|
216 | widget = ObjectDisplayWidget( |
---|
217 | self.field, self.request, TestContact) |
---|
218 | widget.setRenderedValue(self.content) |
---|
219 | |
---|
220 | check_list = [ |
---|
221 | '<td', 'class=', 'row', |
---|
222 | 'Foo Name', |
---|
223 | '</td>', '<td', 'class=', 'row', |
---|
224 | 'foo@foo.test', |
---|
225 | '</td>'] |
---|
226 | self.verifyResult(widget(), check_list, inorder=True) |
---|
227 | |
---|
228 | def test_suite(): |
---|
229 | return unittest.TestSuite(( |
---|
230 | unittest.makeSuite(ObjectWidgetTest), |
---|
231 | unittest.makeSuite(ObjectDisplayWidgetTest), |
---|
232 | doctest.DocTestSuite(), |
---|
233 | )) |
---|
234 | |
---|
235 | if __name__=='__main__': |
---|
236 | unittest.main(defaultTest='test_suite') |
---|