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