source: products/quintagroup.transmogrifier.pfm2pfg/trunk/quintagroup/transmogrifier/pfm2pfg/tests/test_export.py

Last change on this file was 1496, checked in by koval, 10 years ago

updated package to work with a lastest version of quintagroup.transmogrifier

File size: 3.7 KB
Line 
1import time
2import unittest
3from xml.dom import minidom
4
5from Testing import ZopeTestCase
6from Products.CMFPlone.tests.PloneTestCase import PloneTestCase
7from Products.Marshall.registry import getComponent
8
9try:
10    import Products.PloneFormMailer
11    HAS_PFM = True
12except ImportError:
13    HAS_PFM = False
14
15# next condition is required, because a test preparation
16# raises errors on Plone 3+
17if HAS_PFM:
18    from quintagroup.transmogrifier.pfm2pfg.exporting import PloneFormMailerExporter
19
20    ZopeTestCase.installProduct('Formulator')
21    ZopeTestCase.installProduct('MimetypesRegistry')
22    ZopeTestCase.installProduct('PortalTransforms')
23    ZopeTestCase.installProduct('Archetypes')
24    ZopeTestCase.installProduct('PloneFormMailer')
25    ZopeTestCase.installProduct('TALESField')
26
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,))
36
37    app = ZopeTestCase.app()
38    setupPloneFormMailerTestCase(app, quiet=True)
39    ZopeTestCase.close(app)
40
41class 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
104def test_suite():
105    suite = unittest.TestSuite()
106    suite.addTest(unittest.makeSuite(TestExport))
107    return suite
Note: See TracBrowser for help on using the repository browser.