|
1 """cubicweb.common.widget unit tests |
|
2 |
|
3 """ |
|
4 |
|
5 from mx.DateTime import now |
|
6 NOW = now() |
|
7 from logilab.common.testlib import unittest_main |
|
8 from cubicweb.devtools.apptest import EnvBasedTC |
|
9 |
|
10 from cubicweb.web.widgets import widget, AutoCompletionWidget |
|
11 |
|
12 |
|
13 class WidgetsTC(EnvBasedTC): |
|
14 |
|
15 def get_widget(self, etype, rname, rtype): |
|
16 rschema = self.schema[rname] |
|
17 return widget(self.vreg, etype, rschema, rtype, role='subject') |
|
18 |
|
19 |
|
20 def test_hidden_widget(self): |
|
21 w = self.get_widget('State', 'eid', 'Int') |
|
22 self.assertEquals(w.name, 'eid') |
|
23 self.assertEquals(w.render_example(self.request()), '') |
|
24 self.assertDictEquals(w.attrs, {}) |
|
25 entity = self.etype_instance('State') |
|
26 entity.eid = 'X' |
|
27 self.assertEquals(w.required(entity), True) |
|
28 self.assertEquals(w.render(entity), '') |
|
29 self.assertEquals(w.edit_render(entity), |
|
30 u'<input type="hidden" name="eid" value="X" />') |
|
31 |
|
32 def test_textarea_widget(self): |
|
33 self.add_entity('EProperty', pkey=u'ui.fckeditor', value=u'') |
|
34 self.commit() |
|
35 w = self.get_widget('State', 'description', 'String') |
|
36 self.assertEquals(w.name, 'description') |
|
37 self.assertEquals(w.render_example(self.request()), '') |
|
38 self.assertDictEquals(w.attrs, {'accesskey': 'd'}) |
|
39 entity = self.etype_instance('State') |
|
40 entity.eid = 'X' |
|
41 entity |
|
42 self.assertEquals(w.required(entity), False) |
|
43 self.assertEquals(w.render(entity), '') |
|
44 self.assertTextEquals(w.edit_render(entity), |
|
45 u'''<input type="hidden" name="edits-description:X" value="__cubicweb_internal_field__"/> |
|
46 <input type="hidden" name="edits-description_format:X" value="__cubicweb_internal_field__"/> |
|
47 |
|
48 <select name="description_format:X" id="description_format:X" tabindex="0"> |
|
49 <option value="text/rest" >text/rest</option> |
|
50 <option value="text/html" selected="selected">text/html</option> |
|
51 <option value="text/plain" >text/plain</option> |
|
52 <option value="text/cubicweb-page-template" >text/cubicweb-page-template</option> |
|
53 </select><br/><textarea onkeypress="autogrow(this)" name="description:X" accesskey="d" cols="80" id="description:X" rows="20" tabindex="1"></textarea>''') |
|
54 |
|
55 def test_textarea_widget_previous_value(self): |
|
56 self.add_entity('EProperty', pkey=u'ui.fckeditor', value=u'') |
|
57 self.commit() |
|
58 w = self.get_widget('State', 'description', 'String') |
|
59 req = self.request() |
|
60 req.data['formvalues'] = {'description:X': 'a description'} |
|
61 entity = self.etype_instance('State', req) |
|
62 entity.eid = 'X' |
|
63 self.assertEquals(w.required(entity), False) |
|
64 self.assertEquals(w.render(entity), '') |
|
65 self.assertTextEquals(w.edit_render(entity), |
|
66 u'''<input type="hidden" name="edits-description:X" value="__cubicweb_internal_field__"/> |
|
67 <input type="hidden" name="edits-description_format:X" value="__cubicweb_internal_field__"/> |
|
68 |
|
69 <select name="description_format:X" id="description_format:X" tabindex="0"> |
|
70 <option value="text/rest" >text/rest</option> |
|
71 <option value="text/html" selected="selected">text/html</option> |
|
72 <option value="text/plain" >text/plain</option> |
|
73 <option value="text/cubicweb-page-template" >text/cubicweb-page-template</option> |
|
74 </select><br/><textarea onkeypress="autogrow(this)" name="description:X" accesskey="d" cols="80" id="description:X" rows="20" tabindex="1">a description</textarea>''') |
|
75 |
|
76 def test_fckeditor_widget(self): |
|
77 w = self.get_widget('State', 'description', 'String') |
|
78 req = self.request() |
|
79 entity = self.etype_instance('State', req) |
|
80 entity.eid = 'X' |
|
81 self.assertEquals(w.required(entity), False) |
|
82 self.assertEquals(w.render(entity), '') |
|
83 self.assertTextEquals(w.edit_render(entity), |
|
84 u'''<input type="hidden" name="edits-description:X" value="__cubicweb_internal_field__"/> |
|
85 <input type="hidden" name="edits-description_format:X" value=""/> |
|
86 <input type="hidden" name="description_format:X" value="text/html"/> |
|
87 <textarea cubicweb:type="wysiwyg" onkeypress="autogrow(this)" name="description:X" accesskey="d" cols="80" id="description:X" rows="20" tabindex="0"></textarea>''') |
|
88 |
|
89 def test_string_widget(self): |
|
90 w = self.get_widget('Personne', 'nom', 'String') |
|
91 self.assertEquals(w.name, 'nom') |
|
92 self.assertEquals(w.render_example(self.request()), '') |
|
93 self.assertDictEquals(w.attrs, {'accesskey': 'n', 'maxlength': 64, 'size': 40}) |
|
94 entity = self.etype_instance('Personne') |
|
95 entity.eid = 'X' |
|
96 self.assertEquals(w.required(entity), True) |
|
97 self.assertEquals(w.render(entity), '') |
|
98 self.assertEquals(w.edit_render(entity), |
|
99 u'<input type="hidden" name="edits-nom:X" value="__cubicweb_internal_field__"/>\n' |
|
100 '<input type="text" name="nom:X" value="" accesskey="n" id="nom:X" maxlength="64" size="40" tabindex="0"/>') |
|
101 |
|
102 def test_string_widget_previous_value(self): |
|
103 w = self.get_widget('Personne', 'nom', 'String') |
|
104 self.assertEquals(w.name, 'nom') |
|
105 self.assertEquals(w.render_example(self.request()), '') |
|
106 self.assertDictEquals(w.attrs, {'accesskey': 'n', 'maxlength': 64, 'size': 40}) |
|
107 req = self.request() |
|
108 req.data['formvalues'] = {'nom:X': 'a name'} |
|
109 entity = self.etype_instance('Personne', req) |
|
110 entity.eid = 'X' |
|
111 self.assertEquals(w.required(entity), True) |
|
112 self.assertEquals(w.render(entity), '') |
|
113 self.assertEquals(w.edit_render(entity), |
|
114 u'<input type="hidden" name="edits-nom:X" value="__cubicweb_internal_field__"/>\n' |
|
115 '<input type="text" name="nom:X" value="a name" accesskey="n" id="nom:X" maxlength="64" size="40" tabindex="0"/>') |
|
116 |
|
117 def test_static_combo_widget(self): |
|
118 w = self.get_widget('Personne', 'promo', 'String') |
|
119 self.assertEquals(w.name, 'promo') |
|
120 self.assertEquals(w.render_example(self.request()), '') |
|
121 self.assertDictEquals(w.attrs, {}) |
|
122 entity = self.etype_instance('Personne') |
|
123 entity.eid = 'X' |
|
124 self.assertEquals(w.required(entity), False) |
|
125 self.assertEquals(w.render(entity), '') |
|
126 self.assertTextEquals(w.edit_render(entity), |
|
127 u'<input type="hidden" name="edits-promo:X" value="__cubicweb_internal_field__"/>\n\n' |
|
128 '<select name="promo:X" id="promo:X" tabindex="0">\n' |
|
129 '<option value="bon" >bon</option>\n' |
|
130 '<option value="pasbon" >pasbon</option>\n' |
|
131 '</select>') |
|
132 |
|
133 def test_static_combo_widget_previous_value(self): |
|
134 w = self.get_widget('Personne', 'promo', 'String') |
|
135 self.assertEquals(w.name, 'promo') |
|
136 self.assertEquals(w.render_example(self.request()), '') |
|
137 self.assertDictEquals(w.attrs, {}) |
|
138 req = self.request() |
|
139 req.data['formvalues'] = {'promo:X': 'pasbon'} |
|
140 entity = self.etype_instance('Personne', req) |
|
141 entity.eid = 'X' |
|
142 self.assertEquals(w.required(entity), False) |
|
143 self.assertEquals(w.render(entity), '') |
|
144 self.assertTextEquals(w.edit_render(entity), |
|
145 u'<input type="hidden" name="edits-promo:X" value="__cubicweb_internal_field__"/>\n\n' |
|
146 '<select name="promo:X" id="promo:X" tabindex="0">\n' |
|
147 '<option value="bon" >bon</option>\n' |
|
148 '<option value="pasbon" selected="selected">pasbon</option>\n' |
|
149 '</select>') |
|
150 |
|
151 def test_integer_widget(self): |
|
152 w = self.get_widget('Personne', 'tel', 'Int') |
|
153 self.assertEquals(w.name, 'tel') |
|
154 self.assertEquals(w.render_example(self.request()), '23') |
|
155 self.assertDictEquals(w.attrs, {'accesskey': 't', 'maxlength': 15, 'size': 5}) |
|
156 entity = self.etype_instance('Personne') |
|
157 entity.eid = 'X' |
|
158 self.assertEquals(w.required(entity), False) |
|
159 self.assertEquals(w.render(entity), '') |
|
160 self.assertEquals(w.edit_render(entity), |
|
161 u'<input type="hidden" name="edits-tel:X" value="__cubicweb_internal_field__"/>\n' |
|
162 '<input type="text" name="tel:X" value="" accesskey="t" id="tel:X" maxlength="15" size="5" tabindex="0"/>') |
|
163 |
|
164 def test_integer_widget_previous_value(self): |
|
165 w = self.get_widget('Personne', 'tel', 'Int') |
|
166 self.assertEquals(w.name, 'tel') |
|
167 self.assertEquals(w.render_example(self.request()), '23') |
|
168 self.assertDictEquals(w.attrs, {'accesskey': 't', 'maxlength': 15, 'size': 5}) |
|
169 req = self.request() |
|
170 req.data['formvalues'] = {'tel:X': '0123456789'} |
|
171 entity = self.etype_instance('Personne', req) |
|
172 entity.eid = 'X' |
|
173 self.assertEquals(w.required(entity), False) |
|
174 self.assertEquals(w.render(entity), '') |
|
175 self.assertEquals(w.edit_render(entity), |
|
176 u'<input type="hidden" name="edits-tel:X" value="__cubicweb_internal_field__"/>\n' |
|
177 '<input type="text" name="tel:X" value="0123456789" accesskey="t" id="tel:X" maxlength="15" size="5" tabindex="0"/>') |
|
178 |
|
179 def test_datetime_widget(self): |
|
180 w = self.get_widget('Personne', 'datenaiss', 'Datetime') |
|
181 self.assertEquals(w.name, 'datenaiss') |
|
182 now_ = now() |
|
183 example = '%s, or without time: %s' % ( |
|
184 now_.strftime(self.vreg.property_value('ui.datetime-format')), |
|
185 now_.strftime(self.vreg.property_value('ui.date-format'))) |
|
186 self.assertEquals(w.render_example(self.request()), example) |
|
187 self.assertDictEquals(w.attrs, {'accesskey': 'd', 'maxlength': 16, 'size': 16}) |
|
188 entity = self.etype_instance('Personne') |
|
189 entity.eid = 'X' |
|
190 self.assertEquals(w.required(entity), False) |
|
191 self.assertEquals(w.render(entity), '') |
|
192 self.assertEquals(w.edit_render(entity), |
|
193 u'<input type="hidden" name="edits-datenaiss:X" value="__cubicweb_internal_field__"/>\n' |
|
194 '<input type="text" name="datenaiss:X" value="" accesskey="d" id="datenaiss:X" maxlength="16" size="16" tabindex="0"/>' |
|
195 '<a onclick="toggleCalendar(\'datenaiss:Xhelper\', \'datenaiss:X\', %s, %s);" class="calhelper">\n<img src="http://testing.fr/cubicweb/data/calendar.gif" title="calendar" alt="" /></a><div class="calpopup hidden" id="datenaiss:Xhelper"></div>' % (NOW.year, NOW.month)) |
|
196 |
|
197 def test_datetime_widget_previous_value(self): |
|
198 w = self.get_widget('Personne', 'datenaiss', 'Datetime') |
|
199 self.assertEquals(w.name, 'datenaiss') |
|
200 self.assertDictEquals(w.attrs, {'accesskey': 'd', 'maxlength': 16, 'size': 16}) |
|
201 req = self.request() |
|
202 req.data['formvalues'] = {'datenaiss:X': '2000/01/01'} |
|
203 entity = self.etype_instance('Personne', req) |
|
204 entity.eid = 'X' |
|
205 self.assertEquals(w.required(entity), False) |
|
206 self.assertEquals(w.render(entity), '') |
|
207 self.assertEquals(w.edit_render(entity), |
|
208 u'<input type="hidden" name="edits-datenaiss:X" value="__cubicweb_internal_field__"/>\n' |
|
209 '<input type="text" name="datenaiss:X" value="2000/01/01" accesskey="d" id="datenaiss:X" maxlength="16" size="16" tabindex="0"/>' |
|
210 '<a onclick="toggleCalendar(\'datenaiss:Xhelper\', \'datenaiss:X\', %s, %s);" class="calhelper">\n<img src="http://testing.fr/cubicweb/data/calendar.gif" title="calendar" alt="" /></a><div class="calpopup hidden" id="datenaiss:Xhelper"></div>' % (NOW.year, NOW.month)) |
|
211 |
|
212 |
|
213 |
|
214 def test_float_widget(self): |
|
215 w = self.get_widget('Personne', 'salary', 'Float') |
|
216 self.assertEquals(w.name, 'salary') |
|
217 format = now().strftime(self.vreg.property_value('ui.float-format')) |
|
218 self.assertEquals(w.render_example(self.request()), format % 1.23) |
|
219 self.assertDictEquals(w.attrs, {'accesskey': 's', 'maxlength': 15, 'size': 5}) |
|
220 entity = self.etype_instance('Personne') |
|
221 entity.eid = 'X' |
|
222 self.assertEquals(w.required(entity), False) |
|
223 self.assertEquals(w.render(entity), '') |
|
224 self.assertEquals(w.edit_render(entity), |
|
225 u'<input type="hidden" name="edits-salary:X" value="__cubicweb_internal_field__"/>\n' |
|
226 '<input type="text" name="salary:X" value="" accesskey="s" id="salary:X" maxlength="15" size="5" tabindex="0"/>') |
|
227 |
|
228 |
|
229 def test_float_widget_previous_value(self): |
|
230 w = self.get_widget('Personne', 'salary', 'Float') |
|
231 self.assertEquals(w.name, 'salary') |
|
232 format = now().strftime(self.vreg.property_value('ui.float-format')) |
|
233 self.assertEquals(w.render_example(self.request()), format % 1.23) |
|
234 self.assertDictEquals(w.attrs, {'accesskey': 's', 'maxlength': 15, 'size': 5}) |
|
235 req = self.request() |
|
236 req.data['formvalues'] = {'salary:X': 7.89} |
|
237 entity = self.etype_instance('Personne', req) |
|
238 entity.eid = 'X' |
|
239 self.assertEquals(w.required(entity), False) |
|
240 self.assertEquals(w.render(entity), '') |
|
241 self.assertEquals(w.edit_render(entity), |
|
242 u'<input type="hidden" name="edits-salary:X" value="__cubicweb_internal_field__"/>\n' |
|
243 '<input type="text" name="salary:X" value="7.89" accesskey="s" id="salary:X" maxlength="15" size="5" tabindex="0"/>') |
|
244 |
|
245 |
|
246 def test_bool_widget(self): |
|
247 w = self.get_widget('Personne', 'test', 'Boolean') |
|
248 self.assertEquals(w.name, 'test') |
|
249 self.assertEquals(w.render_example(self.request()), '') |
|
250 self.assertDictEquals(w.attrs, {'accesskey': 't'}) |
|
251 entity = self.etype_instance('Personne') |
|
252 entity.eid = 'X' |
|
253 self.assertEquals(w.required(entity), False) |
|
254 self.assertEquals(w.render(entity), '') |
|
255 self.assertEquals(w.edit_render(entity), |
|
256 u'''<input type="hidden" name="edits-test:X" value="__cubicweb_internal_field__"/> |
|
257 |
|
258 <input type="radio" name="test:X" value="1" accesskey="t" id="test:X" tabindex="0"/>yes<br/> |
|
259 <input type="radio" name="test:X" value="" accesskey="t" tabindex="0" checked="checked"/>no<br/>''') |
|
260 |
|
261 def test_bool_widget_previous_value(self): |
|
262 w = self.get_widget('Personne', 'test', 'Boolean') |
|
263 self.assertEquals(w.name, 'test') |
|
264 self.assertEquals(w.render_example(self.request()), '') |
|
265 self.assertDictEquals(w.attrs, {'accesskey': 't'}) |
|
266 req = self.request() |
|
267 req.data['formvalues'] = {'test:X': 'checked'} |
|
268 entity = self.etype_instance('Personne', req) |
|
269 entity.eid = 'X' |
|
270 self.assertEquals(w.required(entity), False) |
|
271 self.assertEquals(w.render(entity), '') |
|
272 self.assertEquals(w.edit_render(entity), |
|
273 u'''<input type="hidden" name="edits-test:X" value="__cubicweb_internal_field__"/> |
|
274 |
|
275 <input type="radio" name="test:X" value="1" accesskey="t" id="test:X" tabindex="0" checked="checked"/>yes<br/> |
|
276 <input type="radio" name="test:X" value="" accesskey="t" tabindex="0"/>no<br/>''') |
|
277 |
|
278 |
|
279 def test_password_widget(self): |
|
280 w = self.get_widget('EUser', 'upassword', 'Password') |
|
281 self.assertEquals(w.name, 'upassword') |
|
282 self.assertEquals(w.render_example(self.request()), '') |
|
283 self.assertDictEquals(w.attrs, {'accesskey': 'u'}) |
|
284 entity = self.etype_instance('EUser') |
|
285 entity.eid = 'X' |
|
286 self.assertEquals(w.required(entity), True) |
|
287 self.assertEquals(w.render(entity), '') |
|
288 self.assertEquals(w.edit_render(entity), |
|
289 u'<input type="hidden" name="edits-upassword:X" value="__cubicweb_internal_field__"/>\n' |
|
290 '<input type="password" name="upassword:X" value="" accesskey="u" id="upassword:X" tabindex="0"/><br/>\n' |
|
291 '<input type="password" name="upassword-confirm:X" id="upassword-confirm:X" tabindex="1"/> <span class="emphasis">(confirm password)</span>') |
|
292 |
|
293 def test_autocompletion_widget(self): |
|
294 entity = self.etype_instance('Personne') |
|
295 entity.widgets['nom'] = 'AutoCompletionWidget' |
|
296 entity.autocomplete_initfuncs = {'nom' : 'getnames'} |
|
297 try: |
|
298 w = self.get_widget(entity, 'nom', 'String') |
|
299 self.failUnless(isinstance(w, AutoCompletionWidget)) |
|
300 self.assertEquals(w.name, 'nom') |
|
301 self.assertEquals(w.render_example(self.request()), '') |
|
302 self.assertDictEquals(w.attrs, {'accesskey': 'n', 'maxlength': 64, 'size': 40}) |
|
303 entity.eid = 'X' |
|
304 self.assertEquals(w.required(entity), True) |
|
305 self.assertEquals(w.render(entity), '') |
|
306 |
|
307 self.assertTextEquals(w.edit_render(entity), |
|
308 u'<input type="hidden" name="edits-nom:X" value="__cubicweb_internal_field__"/>\n' |
|
309 u'<input type="text" name="nom:X" value="" cubicweb:dataurl="http://testing.fr/cubicweb/json?pageid=None&mode=remote&fname=getnames" class="widget required" id="nom:X" tabindex="0" cubicweb:loadtype="auto" cubicweb:wdgtype="SuggestField" cubicweb:accesskey="n" cubicweb:maxlength="64" cubicweb:size="40" />') |
|
310 |
|
311 finally: |
|
312 del entity.widgets['nom'] |
|
313 |
|
314 |
|
315 def test_autocompletion_widget_previous_value(self): |
|
316 req = self.request() |
|
317 req.data['formvalues'] = {'nom:X': 'a name'} |
|
318 entity = self.etype_instance('Personne', req) |
|
319 entity.widgets['nom'] = 'AutoCompletionWidget' |
|
320 entity.autocomplete_initfuncs = {'nom' : 'getnames'} |
|
321 try: |
|
322 w = self.get_widget(entity, 'nom', 'String') |
|
323 self.failUnless(isinstance(w, AutoCompletionWidget)) |
|
324 self.assertEquals(w.name, 'nom') |
|
325 self.assertEquals(w.render_example(self.request()), '') |
|
326 self.assertDictEquals(w.attrs, {'accesskey': 'n', 'maxlength': 64, 'size': 40}) |
|
327 entity.eid = 'X' |
|
328 self.assertEquals(w.required(entity), True) |
|
329 self.assertEquals(w.render(entity), '') |
|
330 self.assertTextEquals(w.edit_render(entity), |
|
331 u'<input type="hidden" name="edits-nom:X" value="__cubicweb_internal_field__"/>\n' |
|
332 u'<input type="text" name="nom:X" value="a name" cubicweb:dataurl="http://testing.fr/cubicweb/json?pageid=None&mode=remote&fname=getnames" class="widget required" id="nom:X" tabindex="0" cubicweb:loadtype="auto" cubicweb:wdgtype="SuggestField" cubicweb:accesskey="n" cubicweb:maxlength="64" cubicweb:size="40" />') |
|
333 |
|
334 finally: |
|
335 del entity.widgets['nom'] |
|
336 |
|
337 |
|
338 def test_nonregr_float_widget_with_none(self): |
|
339 w = self.get_widget('Personne', 'salary', 'Float') |
|
340 self.assertEquals(w.name, 'salary') |
|
341 format = now().strftime(self.vreg.property_value('ui.float-format')) |
|
342 self.assertEquals(w.render_example(self.request()), format % 1.23) |
|
343 self.assertDictEquals(w.attrs, {'accesskey': 's', 'maxlength': 15, 'size': 5}) |
|
344 req = self.request() |
|
345 entity = self.etype_instance('Personne', req) |
|
346 entity.eid = 'X' |
|
347 entity.salary = None |
|
348 self.assertEquals(w.required(entity), False) |
|
349 self.assertEquals(w.render(entity), '') |
|
350 self.assertEquals(w.edit_render(entity), |
|
351 u'<input type="hidden" name="edits-salary:X" value="__cubicweb_internal_field__"/>\n' |
|
352 '<input type="text" name="salary:X" value="" accesskey="s" id="salary:X" maxlength="15" size="5" tabindex="0"/>') |
|
353 |
|
354 |
|
355 def test_custom_widget_for_non_final_relation(self): |
|
356 entity = self.etype_instance('Personne', self.request()) |
|
357 entity.widgets['travaille'] = 'AutoCompletionWidget' |
|
358 entity.autocomplete_initfuncs = {'nom' : 'getnames'} |
|
359 w = self.get_widget(entity, 'travaille', 'Societe') |
|
360 self.failUnless(isinstance(w, AutoCompletionWidget)) |
|
361 |
|
362 |
|
363 if __name__ == '__main__': |
|
364 unittest_main() |