1 | import unittest |
---|
2 | import os.path |
---|
3 | import email |
---|
4 | |
---|
5 | from zope.interface import classProvides, implements |
---|
6 | import transaction |
---|
7 | |
---|
8 | from Products.Five import zcml |
---|
9 | from Products.Five import fiveconfigure |
---|
10 | from Testing import ZopeTestCase as ztc |
---|
11 | |
---|
12 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
13 | from Products.PloneTestCase.layer import onsetup |
---|
14 | |
---|
15 | from collective.transmogrifier.interfaces import ISectionBlueprint, ISection, ITransmogrifier |
---|
16 | |
---|
17 | class Source(object): |
---|
18 | classProvides(ISectionBlueprint) |
---|
19 | implements(ISection) |
---|
20 | |
---|
21 | def __init__(self, transmogrifier, name, options, previous): |
---|
22 | self.previous = previous |
---|
23 | |
---|
24 | def __iter__(self): |
---|
25 | for item in self.previous: |
---|
26 | yield item |
---|
27 | |
---|
28 | fname = os.path.join(os.path.dirname(__file__), 'complex_form.xml') |
---|
29 | xml = file(fname).read() |
---|
30 | item = dict( |
---|
31 | _type='PloneFormMailer', |
---|
32 | _path='complex-form', |
---|
33 | _files=dict( |
---|
34 | marshall=dict( |
---|
35 | name='.marshall.xml', |
---|
36 | data=xml |
---|
37 | ) |
---|
38 | ) |
---|
39 | ) |
---|
40 | yield item |
---|
41 | |
---|
42 | fname = os.path.join(os.path.dirname(__file__), 'fieldset_form.xml') |
---|
43 | xml = file(fname).read() |
---|
44 | item = dict( |
---|
45 | _type='PloneFormMailer', |
---|
46 | _path='fieldset-form', |
---|
47 | _files=dict( |
---|
48 | marshall=dict( |
---|
49 | name='.marshall.xml', |
---|
50 | data=xml |
---|
51 | ) |
---|
52 | ) |
---|
53 | ) |
---|
54 | yield item |
---|
55 | |
---|
56 | ztc.installProduct('PloneFormGen') |
---|
57 | |
---|
58 | @onsetup |
---|
59 | def setup_product(): |
---|
60 | fiveconfigure.debug_mode = True |
---|
61 | import quintagroup.transmogrifier.pfm2pfg |
---|
62 | zcml.load_config('configure.zcml', quintagroup.transmogrifier.pfm2pfg) |
---|
63 | import quintagroup.transmogrifier.pfm2pfg.tests |
---|
64 | zcml.load_config('test_import.zcml', quintagroup.transmogrifier.pfm2pfg.tests) |
---|
65 | fiveconfigure.debug_mode = False |
---|
66 | |
---|
67 | setup_product() |
---|
68 | ptc.setupPloneSite(products=['PloneFormGen']) |
---|
69 | |
---|
70 | class TestImport(ptc.PloneTestCase): |
---|
71 | """ Test importing of PloneFormGen content. |
---|
72 | """ |
---|
73 | |
---|
74 | def afterSetUp(self): |
---|
75 | if 'complex-form' not in self.portal: |
---|
76 | # run transmogrifier pipeline |
---|
77 | transmogrifier = ITransmogrifier(self.portal) |
---|
78 | transmogrifier('test_import') |
---|
79 | |
---|
80 | def beforeTearDown(self): |
---|
81 | transaction.commit() |
---|
82 | |
---|
83 | def test_form_folder(self): |
---|
84 | self.failUnless('complex-form' in self.portal) |
---|
85 | form = self.portal['complex-form'] |
---|
86 | |
---|
87 | self.assertEqual(form['title'], 'Test form') |
---|
88 | self.assertEqual(form.getFormPrologue(), '<p>form prologue</p>') |
---|
89 | self.assertEqual(form.getFormEpilogue(), '<p>form epilogue</p>') |
---|
90 | self.assertEqual(form['thanksPageOverride'], 'redirect_to:string:test') |
---|
91 | self.assertEqual(form['afterValidationOverride'], 'string:script') |
---|
92 | |
---|
93 | def test_mailer(self): |
---|
94 | form = self.portal['complex-form'] |
---|
95 | self.failUnless('mailer' in form) |
---|
96 | mailer = form['mailer'] |
---|
97 | |
---|
98 | self.assertEqual(mailer['subjectOverride'], 'string:Test form submit') |
---|
99 | self.assertEqual(mailer['recipient_name'], 'Test') |
---|
100 | self.assertEqual(mailer['recipientOverride'], 'string:recipient@mail.org') |
---|
101 | self.assertEqual(mailer['additional_headers'], ('Header1: value1', 'Header2: value2')) |
---|
102 | self.assertEqual(mailer['body_type'], 'html') |
---|
103 | self.assertEqual(mailer.getBody_pre(), 'Next are input fields') |
---|
104 | self.assertEqual(mailer.getBody_post(), 'You have filled all necessary fields') |
---|
105 | self.assertEqual(mailer.getBody_footer(), 'It\'s built on Plone') |
---|
106 | self.assertEqual(mailer['cc_recipients'], ('cc1@mail.org', 'cc2@mail.org')) |
---|
107 | self.assertEqual(mailer['bcc_recipients'], ('bcc1@mail.org', 'bcc2@mail.org')) |
---|
108 | |
---|
109 | self.assertEqual(mailer.created(), form.created()) |
---|
110 | self.assertEqual(mailer.modified(), form.modified()) |
---|
111 | |
---|
112 | def test_thanks_page(self): |
---|
113 | form = self.portal['complex-form'] |
---|
114 | self.failUnless('thank-you' in form) |
---|
115 | page = form['thank-you'] |
---|
116 | |
---|
117 | self.assertEqual(page.Title(), 'The form was sent.') |
---|
118 | self.assertEqual(page.getThanksPrologue(), |
---|
119 | '<p>Thank you for submiting it.<br /></p>') |
---|
120 | |
---|
121 | self.assertEqual(page.created(), form.created()) |
---|
122 | self.assertEqual(page.modified(), form.modified()) |
---|
123 | |
---|
124 | def test_string_field(self): |
---|
125 | form = self.portal['complex-form'] |
---|
126 | self.failIf('field1' not in form) |
---|
127 | field = form['field1'] |
---|
128 | |
---|
129 | self.assertEqual(field.getPortalTypeName(), 'FormStringField') |
---|
130 | self.assertEqual(field['title'], 'String field') |
---|
131 | self.assertEqual(field.Description(), 'field description') |
---|
132 | self.assertEqual(field['required'], True) |
---|
133 | self.assertEqual(field['hidden'], False) |
---|
134 | self.assertEqual(field['fgDefault'], '') |
---|
135 | self.assertEqual(field['fgmaxlength'], 255) |
---|
136 | self.assertEqual(field['fgsize'], 20) |
---|
137 | |
---|
138 | self.assertEqual(field.created(), form.created()) |
---|
139 | self.assertEqual(field.modified(), form.modified()) |
---|
140 | |
---|
141 | def test_email_field(self): |
---|
142 | form = self.portal['complex-form'] |
---|
143 | self.failIf('field2' not in form) |
---|
144 | field = form['field2'] |
---|
145 | |
---|
146 | self.assertEqual(field.getPortalTypeName(), 'FormStringField') |
---|
147 | self.assertEqual(field['title'], 'Email field') |
---|
148 | self.assertEqual(field.Description(), '') |
---|
149 | self.assertEqual(field['required'], True) |
---|
150 | self.assertEqual(field['hidden'], False) |
---|
151 | self.assertEqual(field['fgDefault'], 'test@mail.com') |
---|
152 | self.assertEqual(field['fgmaxlength'], 255) |
---|
153 | self.assertEqual(field['fgsize'], 20) |
---|
154 | self.assertEqual(field['fgStringValidator'], 'isEmail') |
---|
155 | |
---|
156 | self.assertEqual(field.created(), form.created()) |
---|
157 | self.assertEqual(field.modified(), form.modified()) |
---|
158 | |
---|
159 | def test_link_field(self): |
---|
160 | form = self.portal['complex-form'] |
---|
161 | self.failIf('field3' not in form) |
---|
162 | field = form['field3'] |
---|
163 | |
---|
164 | self.assertEqual(field.getPortalTypeName(), 'FormStringField') |
---|
165 | self.assertEqual(field['title'], 'Link field') |
---|
166 | self.assertEqual(field.Description(), '') |
---|
167 | self.assertEqual(field['required'], False) |
---|
168 | self.assertEqual(field['hidden'], False) |
---|
169 | self.assertEqual(field['fgDefault'], '') |
---|
170 | self.assertEqual(field['fgmaxlength'], 15) |
---|
171 | self.assertEqual(field['fgsize'], 20) |
---|
172 | self.assertEqual(field['fgStringValidator'], 'isURL') |
---|
173 | |
---|
174 | self.assertEqual(field.created(), form.created()) |
---|
175 | self.assertEqual(field.modified(), form.modified()) |
---|
176 | |
---|
177 | def test_pattern_field(self): |
---|
178 | form = self.portal['complex-form'] |
---|
179 | self.failIf('field4' not in form) |
---|
180 | field = form['field4'] |
---|
181 | |
---|
182 | self.assertEqual(field.getPortalTypeName(), 'FormStringField') |
---|
183 | self.assertEqual(field['title'], 'Pattern field') |
---|
184 | self.assertEqual(field.Description(), '') |
---|
185 | self.assertEqual(field['required'], True) |
---|
186 | self.assertEqual(field['hidden'], True) |
---|
187 | self.assertEqual(field['fgDefault'], '') |
---|
188 | self.assertEqual(field['fgmaxlength'], 255) |
---|
189 | self.assertEqual(field['fgsize'], 20) |
---|
190 | |
---|
191 | self.assertEqual(field.created(), form.created()) |
---|
192 | self.assertEqual(field.modified(), form.modified()) |
---|
193 | |
---|
194 | def test_textarea_field(self): |
---|
195 | form = self.portal['complex-form'] |
---|
196 | self.failIf('field5' not in form) |
---|
197 | field = form['field5'] |
---|
198 | |
---|
199 | self.assertEqual(field.getPortalTypeName(), 'FormTextField') |
---|
200 | self.assertEqual(field['title'], 'Text area field') |
---|
201 | self.assertEqual(field.Description(), '') |
---|
202 | self.assertEqual(field['required'], True) |
---|
203 | self.assertEqual(field['hidden'], False) |
---|
204 | self.assertEqual(field.getFgDefault(), '') |
---|
205 | self.assertEqual(field['fgmaxlength'], 1000) |
---|
206 | self.assertEqual(field['fgRows'], 5) |
---|
207 | |
---|
208 | self.assertEqual(field.created(), form.created()) |
---|
209 | self.assertEqual(field.modified(), form.modified()) |
---|
210 | |
---|
211 | def test_rawtextarea_field(self): |
---|
212 | form = self.portal['complex-form'] |
---|
213 | self.failIf('field6' not in form) |
---|
214 | field = form['field6'] |
---|
215 | |
---|
216 | self.assertEqual(field.getPortalTypeName(), 'FormTextField') |
---|
217 | self.assertEqual(field['title'], 'Raw text area field') |
---|
218 | self.assertEqual(field.Description(), '') |
---|
219 | self.assertEqual(field['required'], True) |
---|
220 | self.assertEqual(field['hidden'], False) |
---|
221 | self.assertEqual(field.getFgDefault(), '') |
---|
222 | self.assertEqual(field['fgmaxlength'], '0') |
---|
223 | self.assertEqual(field['fgRows'], 5) |
---|
224 | |
---|
225 | self.assertEqual(field.created(), form.created()) |
---|
226 | self.assertEqual(field.modified(), form.modified()) |
---|
227 | |
---|
228 | def test_password_field(self): |
---|
229 | form = self.portal['complex-form'] |
---|
230 | self.failIf('field7' not in form) |
---|
231 | field = form['field7'] |
---|
232 | |
---|
233 | self.assertEqual(field.getPortalTypeName(), 'FormPasswordField') |
---|
234 | self.assertEqual(field['title'], 'Password field') |
---|
235 | self.assertEqual(field.Description(), '') |
---|
236 | self.assertEqual(field['required'], True) |
---|
237 | self.assertEqual(field['fgDefault'], '') |
---|
238 | self.assertEqual(field['fgmaxlength'], 255) |
---|
239 | self.assertEqual(field['fgsize'], 20) |
---|
240 | |
---|
241 | self.assertEqual(field.created(), form.created()) |
---|
242 | self.assertEqual(field.modified(), form.modified()) |
---|
243 | |
---|
244 | def test_label_field(self): |
---|
245 | form = self.portal['complex-form'] |
---|
246 | self.failIf('field8' not in form) |
---|
247 | field = form['field8'] |
---|
248 | |
---|
249 | self.assertEqual(field.getPortalTypeName(), 'FormRichLabelField') |
---|
250 | self.assertEqual(field['title'], 'Label field') |
---|
251 | self.assertEqual(field.Description(), '') |
---|
252 | self.assertEqual(field['fgDefault'], 'label text') |
---|
253 | |
---|
254 | self.assertEqual(field.created(), form.created()) |
---|
255 | self.assertEqual(field.modified(), form.modified()) |
---|
256 | |
---|
257 | def test_integer_field(self): |
---|
258 | form = self.portal['complex-form'] |
---|
259 | self.failIf('field9' not in form) |
---|
260 | field = form['field9'] |
---|
261 | |
---|
262 | self.assertEqual(field.getPortalTypeName(), 'FormIntegerField') |
---|
263 | self.assertEqual(field['title'], 'Integer field') |
---|
264 | self.assertEqual(field.Description(), '') |
---|
265 | self.assertEqual(field['required'], True) |
---|
266 | self.assertEqual(field['fgDefault'], '55') |
---|
267 | self.assertEqual(field['fgmaxlength'], 40) |
---|
268 | self.assertEqual(field['fgsize'], 20) |
---|
269 | self.assertEqual(field['minval'], 10) |
---|
270 | self.assertEqual(field['maxval'], 1000) |
---|
271 | |
---|
272 | self.assertEqual(field.created(), form.created()) |
---|
273 | self.assertEqual(field.modified(), form.modified()) |
---|
274 | |
---|
275 | def test_float_field(self): |
---|
276 | form = self.portal['complex-form'] |
---|
277 | self.failIf('field10' not in form) |
---|
278 | field = form['field10'] |
---|
279 | |
---|
280 | self.assertEqual(field.getPortalTypeName(), 'FormFixedPointField') |
---|
281 | self.assertEqual(field['title'], 'Float field') |
---|
282 | self.assertEqual(field.Description(), '') |
---|
283 | self.assertEqual(field['required'], True) |
---|
284 | self.assertEqual(field['fgDefault'], '') |
---|
285 | self.assertEqual(field['fgmaxlength'], 10) |
---|
286 | self.assertEqual(field['fgsize'], 20) |
---|
287 | |
---|
288 | self.assertEqual(field.created(), form.created()) |
---|
289 | self.assertEqual(field.modified(), form.modified()) |
---|
290 | |
---|
291 | def test_datetime_field(self): |
---|
292 | form = self.portal['complex-form'] |
---|
293 | self.failIf('field11' not in form) |
---|
294 | field = form['field11'] |
---|
295 | |
---|
296 | self.assertEqual(field.getPortalTypeName(), 'FormDateField') |
---|
297 | self.assertEqual(field['title'], 'Date time field') |
---|
298 | self.assertEqual(field.Description(), '') |
---|
299 | self.assertEqual(field['required'], True) |
---|
300 | self.assertEqual(field['fgDefault'], '2008/10/10 10:10:00 GMT+3') |
---|
301 | self.assertEqual(field['fgStartingYear'], 2006) |
---|
302 | self.assertEqual(field['fgEndingYear'], 2010) |
---|
303 | |
---|
304 | self.assertEqual(field.created(), form.created()) |
---|
305 | self.assertEqual(field.modified(), form.modified()) |
---|
306 | |
---|
307 | def test_file_field(self): |
---|
308 | form = self.portal['complex-form'] |
---|
309 | self.failIf('field12' not in form) |
---|
310 | field = form['field12'] |
---|
311 | |
---|
312 | self.assertEqual(field.getPortalTypeName(), 'FormFileField') |
---|
313 | self.assertEqual(field['title'], 'File field') |
---|
314 | self.assertEqual(field.Description(), '') |
---|
315 | |
---|
316 | self.assertEqual(field.created(), form.created()) |
---|
317 | self.assertEqual(field.modified(), form.modified()) |
---|
318 | |
---|
319 | def test_lines_field(self): |
---|
320 | form = self.portal['complex-form'] |
---|
321 | self.failIf('field13' not in form) |
---|
322 | field = form['field13'] |
---|
323 | |
---|
324 | self.assertEqual(field.getPortalTypeName(), 'FormLinesField') |
---|
325 | self.assertEqual(field['title'], 'Lines field') |
---|
326 | self.assertEqual(field.Description(), '') |
---|
327 | self.assertEqual(field['required'], True) |
---|
328 | self.assertEqual(field['fgDefault'], ('first', 'second', 'third')) |
---|
329 | self.assertEqual(field['fgRows'], 5) |
---|
330 | |
---|
331 | self.assertEqual(field.created(), form.created()) |
---|
332 | self.assertEqual(field.modified(), form.modified()) |
---|
333 | |
---|
334 | def test_checkbox_field(self): |
---|
335 | form = self.portal['complex-form'] |
---|
336 | self.failIf('field14' not in form) |
---|
337 | field = form['field14'] |
---|
338 | |
---|
339 | self.assertEqual(field.getPortalTypeName(), 'FormBooleanField') |
---|
340 | self.assertEqual(field['title'], 'Checkbox field') |
---|
341 | self.assertEqual(field.Description(), '') |
---|
342 | self.assertEqual(field['fgDefault'], True) |
---|
343 | |
---|
344 | self.assertEqual(field.created(), form.created()) |
---|
345 | self.assertEqual(field.modified(), form.modified()) |
---|
346 | |
---|
347 | def test_list_field(self): |
---|
348 | form = self.portal['complex-form'] |
---|
349 | self.failIf('field15' not in form) |
---|
350 | field = form['field15'] |
---|
351 | |
---|
352 | self.assertEqual(field.getPortalTypeName(), 'FormSelectionField') |
---|
353 | self.assertEqual(field['title'], 'List field') |
---|
354 | self.assertEqual(field.Description(), '') |
---|
355 | self.assertEqual(field['fgDefault'], 'first') |
---|
356 | self.assertEqual(field['fgVocabulary'], ('first|First', 'second|Second', 'third|Third')) |
---|
357 | self.assertEqual(field['fgFormat'], 'select') |
---|
358 | |
---|
359 | self.assertEqual(field.created(), form.created()) |
---|
360 | self.assertEqual(field.modified(), form.modified()) |
---|
361 | |
---|
362 | def test_radio_field(self): |
---|
363 | form = self.portal['complex-form'] |
---|
364 | self.failIf('field16' not in form) |
---|
365 | field = form['field16'] |
---|
366 | |
---|
367 | self.assertEqual(field.getPortalTypeName(), 'FormSelectionField') |
---|
368 | self.assertEqual(field['title'], 'Radio field') |
---|
369 | self.assertEqual(field.Description(), '') |
---|
370 | self.assertEqual(field['fgDefault'], 'first') |
---|
371 | self.assertEqual(field['fgVocabulary'], ('first|First', 'second|Second', 'third|Third')) |
---|
372 | self.assertEqual(field['fgFormat'], 'radio') |
---|
373 | |
---|
374 | self.assertEqual(field.created(), form.created()) |
---|
375 | self.assertEqual(field.modified(), form.modified()) |
---|
376 | |
---|
377 | def test_multilist_field(self): |
---|
378 | form = self.portal['complex-form'] |
---|
379 | self.failIf('field17' not in form) |
---|
380 | field = form['field17'] |
---|
381 | |
---|
382 | self.assertEqual(field.getPortalTypeName(), 'FormMultiSelectionField') |
---|
383 | self.assertEqual(field['title'], 'Multi list field') |
---|
384 | self.assertEqual(field.Description(), '') |
---|
385 | self.assertEqual(field['required'], True) |
---|
386 | self.assertEqual(field['fgDefault'], ('first', 'third')) |
---|
387 | self.assertEqual(field['fgVocabulary'], ('first|First', 'second|Second', 'third|Third')) |
---|
388 | self.assertEqual(field['fgFormat'], 'select') |
---|
389 | self.assertEqual(field['fgRows'], 5) |
---|
390 | |
---|
391 | self.assertEqual(field.created(), form.created()) |
---|
392 | self.assertEqual(field.modified(), form.modified()) |
---|
393 | |
---|
394 | def test_multicheckbox_field(self): |
---|
395 | form = self.portal['complex-form'] |
---|
396 | self.failIf('field18' not in form) |
---|
397 | field = form['field18'] |
---|
398 | |
---|
399 | self.assertEqual(field.getPortalTypeName(), 'FormMultiSelectionField') |
---|
400 | self.assertEqual(field['title'], 'Multi checkbox field') |
---|
401 | self.assertEqual(field.Description(), '') |
---|
402 | self.assertEqual(field['required'], True) |
---|
403 | self.assertEqual(field['fgDefault'], ('first', 'third')) |
---|
404 | self.assertEqual(field['fgVocabulary'], ('first|First', 'second|Second', 'third|Third')) |
---|
405 | self.assertEqual(field['fgFormat'], 'checkbox') |
---|
406 | |
---|
407 | self.assertEqual(field.created(), form.created()) |
---|
408 | self.assertEqual(field.modified(), form.modified()) |
---|
409 | |
---|
410 | def test_fieldset(self): |
---|
411 | self.failIf('fieldset-form' not in self.portal) |
---|
412 | form = self.portal['fieldset-form'] |
---|
413 | self.assertEqual(form.objectIds(), ['mailer', 'thank-you', 'checkbox', 'datetime', 'Other']) |
---|
414 | |
---|
415 | fieldset = form['Other'] |
---|
416 | self.assertEqual(fieldset['title'], 'Other') |
---|
417 | self.assertEqual(fieldset.objectIds(), ['email', 'float', 'string']) |
---|
418 | |
---|
419 | class FakeRequest(dict): |
---|
420 | |
---|
421 | def __init__(self, **kwargs): |
---|
422 | self.form = kwargs |
---|
423 | |
---|
424 | class TestSubmit(ptc.PloneTestCase): |
---|
425 | """ test ya_gpg.py """ |
---|
426 | |
---|
427 | def dummy_send(self, mfrom, mto, messageText): |
---|
428 | self.mfrom = mfrom |
---|
429 | self.mto = mto |
---|
430 | self.messageText = messageText |
---|
431 | |
---|
432 | def afterSetUp(self): |
---|
433 | transmogrifier = ITransmogrifier(self.portal) |
---|
434 | transmogrifier('test_import') |
---|
435 | self.form = getattr(self.folder, 'complex-form') |
---|
436 | for i in self.form.contentValues(): |
---|
437 | field = i.getField('required') |
---|
438 | if field is not None: |
---|
439 | field.getMutator(i)(False) |
---|
440 | self.mailhost = self.folder.MailHost |
---|
441 | self.mailhost._send = self.dummy_send |
---|
442 | |
---|
443 | def test_submitting(self): |
---|
444 | """ Test submitting of form """ |
---|
445 | request = FakeRequest(field2='field2@mail.org') |
---|
446 | errors = self.form.fgvalidate(REQUEST=request) |
---|
447 | msg = email.message_from_string(self.messageText) |
---|
448 | self.assertEqual(msg.get('To'), 'Test <recipient@mail.org>') |
---|
449 | self.assertEqual(msg.get('Subject'), '=?utf-8?q?Test_form_submit?=') |
---|
450 | |
---|
451 | def test_suite(): |
---|
452 | suite = unittest.TestSuite() |
---|
453 | suite.addTest(unittest.makeSuite(TestImport)) |
---|
454 | suite.addTest(unittest.makeSuite(TestSubmit)) |
---|
455 | return suite |
---|