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