1 | import time |
---|
2 | import unittest |
---|
3 | from xml.dom import minidom |
---|
4 | |
---|
5 | from Testing import ZopeTestCase |
---|
6 | from Products.CMFPlone.tests.PloneTestCase import PloneTestCase |
---|
7 | from Products.Marshall.registry import getComponent |
---|
8 | |
---|
9 | from quintagroup.transmogrifier.pfm2pfg.exporting import PloneFormMailerExporter |
---|
10 | |
---|
11 | ZopeTestCase.installProduct('Formulator') |
---|
12 | ZopeTestCase.installProduct('MimetypesRegistry') |
---|
13 | ZopeTestCase.installProduct('PortalTransforms') |
---|
14 | ZopeTestCase.installProduct('Archetypes') |
---|
15 | ZopeTestCase.installProduct('PloneFormMailer') |
---|
16 | ZopeTestCase.installProduct('TALESField') |
---|
17 | |
---|
18 | def setupPloneFormMailerTestCase(app, quiet=0): |
---|
19 | get_transaction().begin() |
---|
20 | _start = time.time() |
---|
21 | if not quiet: |
---|
22 | ZopeTestCase._print('Adding PloneFormMailer ... ') |
---|
23 | app.portal.portal_quickinstaller.installProduct('PloneFormMailer') |
---|
24 | get_transaction().commit() |
---|
25 | if not quiet: |
---|
26 | ZopeTestCase._print('done (%.3fs)\n' % (time.time()-_start,)) |
---|
27 | |
---|
28 | app = ZopeTestCase.app() |
---|
29 | setupPloneFormMailerTestCase(app, quiet=True) |
---|
30 | ZopeTestCase.close(app) |
---|
31 | |
---|
32 | class TestExport(PloneTestCase): |
---|
33 | """Test case for proping up a test portal.""" |
---|
34 | def afterSetUp(self): |
---|
35 | self.loginPortalOwner() |
---|
36 | self.portal.invokeFactory('PloneFormMailer', id='form') |
---|
37 | form = self.portal.form |
---|
38 | ['recipient_name', 'bcc_recipients', 'cc_recipients'] |
---|
39 | form.setRecipientName("string:Recipient") |
---|
40 | form.setCCRecipients([ |
---|
41 | 'string:cc1@mail.org', |
---|
42 | 'string:cc2@mail.org' |
---|
43 | ]) |
---|
44 | form.setBCCRecipients([ |
---|
45 | 'string:bcc1@mail.org', |
---|
46 | 'string:bcc2@mail.org' |
---|
47 | ]) |
---|
48 | form.setSubject('string:Email subject') |
---|
49 | |
---|
50 | def _testField(self, xml, name, values): |
---|
51 | """ Check whether marshalled in xml format field has some values. |
---|
52 | """ |
---|
53 | doc = minidom.parseString(xml) |
---|
54 | elems = [i for i in doc.getElementsByTagName('field') if i.getAttribute('name') == name] |
---|
55 | if type(values) not in (list, tuple): |
---|
56 | values = [values] |
---|
57 | fields = [] |
---|
58 | for i in elems: |
---|
59 | fields.append(i.firstChild.nodeValue.strip()) |
---|
60 | return values == fields |
---|
61 | |
---|
62 | def test_data_corrector(self): |
---|
63 | _, _, xml = getComponent('atxml').marshall(self.portal.form) |
---|
64 | data = {'name': '', 'data': xml} |
---|
65 | #import pdb; pdb.set_trace() |
---|
66 | adapter = PloneFormMailerExporter(self.portal.form) |
---|
67 | adapter(data) |
---|
68 | xml = data['data'] |
---|
69 | self.assertEqual(self._testField(xml, 'recipient_name', 'Recipient'), True) |
---|
70 | self.assertEqual( |
---|
71 | self._testField(xml, |
---|
72 | 'cc_recipients', |
---|
73 | ['cc1@mail.org', 'cc2@mail.org']), |
---|
74 | True |
---|
75 | ) |
---|
76 | self.assertEqual( |
---|
77 | self._testField(xml, |
---|
78 | 'bcc_recipients', |
---|
79 | ['bcc1@mail.org', 'bcc2@mail.org']), |
---|
80 | True |
---|
81 | ) |
---|
82 | self.assertEqual(self._testField(xml, 'subject', 'string:Email subject'), True) |
---|
83 | |
---|
84 | def test_bad_tales_field(self): |
---|
85 | self.portal.form.setRecipientName("bad tales expression") |
---|
86 | _, _, xml = getComponent('atxml').marshall(self.portal.form) |
---|
87 | data = {'name': '', 'data': xml} |
---|
88 | #import pdb; pdb.set_trace() |
---|
89 | adapter = PloneFormMailerExporter(self.portal.form) |
---|
90 | adapter(data) |
---|
91 | xml = data['data'] |
---|
92 | #import pdb; pdb.set_trace() |
---|
93 | self.assertEqual(self._testField(xml, 'recipient_name', 'bad tales expression'), False) |
---|
94 | |
---|
95 | def test_suite(): |
---|
96 | suite = unittest.TestSuite() |
---|
97 | suite.addTest(unittest.makeSuite(TestExport)) |
---|
98 | return suite |
---|