1 | import re |
---|
2 | import string |
---|
3 | import unittest |
---|
4 | |
---|
5 | from Products.Five import zcml |
---|
6 | from Products.Five import fiveconfigure |
---|
7 | from Testing import ZopeTestCase as ztc |
---|
8 | from Products.PloneTestCase.layer import onsetup |
---|
9 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
10 | from Products.CMFCore.permissions import View |
---|
11 | from Products.Archetypes.atapi import StringField |
---|
12 | |
---|
13 | from quintagroup.pfg.captcha import CaptchaWidget |
---|
14 | from quintagroup.pfg.captcha import CaptchaValidator |
---|
15 | from quintagroup.pfg.captcha.field import CAPTCHA_ID, HIDDEN_FIELDS |
---|
16 | |
---|
17 | _marker = object() |
---|
18 | |
---|
19 | PRODUCTS = [ |
---|
20 | 'quintagroup.captcha.core', |
---|
21 | 'quintagroup.pfg.captcha', |
---|
22 | ] |
---|
23 | PROFILES = [p+':default' for p in PRODUCTS] |
---|
24 | |
---|
25 | @onsetup |
---|
26 | def setup_product(): |
---|
27 | fiveconfigure.debug_mode = True |
---|
28 | import quintagroup.pfg.captcha |
---|
29 | zcml.load_config('configure.zcml', quintagroup.pfg.captcha) |
---|
30 | fiveconfigure.debug_mode = False |
---|
31 | ztc.installProduct('PloneFormGen') |
---|
32 | ztc.installPackage('quintagroup.pfg.captcha') |
---|
33 | ztc.installPackage('quintagroup.captcha.core') |
---|
34 | |
---|
35 | setup_product() |
---|
36 | ptc.setupPloneSite(products=['PloneFormGen',], extension_profiles=PROFILES) |
---|
37 | |
---|
38 | |
---|
39 | class TestInstallations(ptc.PloneTestCase): |
---|
40 | |
---|
41 | def testInstalledProducts(self): |
---|
42 | qi = self.portal.portal_quickinstaller |
---|
43 | installed = [p['id'] for p in qi.listInstalledProducts()] |
---|
44 | for p in PRODUCTS: |
---|
45 | if p.startswith('Products'): |
---|
46 | p = p[9:] |
---|
47 | self.assertEqual(p in installed, True, |
---|
48 | '"%s" product not installed' % p) |
---|
49 | |
---|
50 | def testType(self): |
---|
51 | pt = self.portal.portal_types |
---|
52 | self.assertEqual("CaptchaField" in pt.objectIds(), True) |
---|
53 | |
---|
54 | def testPortalFactory(self): |
---|
55 | pf = self.portal.portal_factory |
---|
56 | self.assertEqual("CaptchaField" in pf.getFactoryTypes(), True) |
---|
57 | |
---|
58 | def testWorkflow(self): |
---|
59 | pw = self.portal.portal_workflow |
---|
60 | default_chain = pw.getDefaultChain() |
---|
61 | cf_chain = pw.getChainForPortalType('CaptchaField') |
---|
62 | self.assertNotEqual(cf_chain == default_chain , True) |
---|
63 | |
---|
64 | def testNotToList(self): |
---|
65 | navtree = self.portal.portal_properties.navtree_properties |
---|
66 | mtNotToList = navtree.getProperty("metaTypesNotToList") |
---|
67 | self.assertEqual('CaptchaField' in mtNotToList, True) |
---|
68 | |
---|
69 | def testSkins(self): |
---|
70 | ps = self.portal.portal_skins |
---|
71 | self.assertEqual("qplonecaptchafield" in ps.objectIds(), True) |
---|
72 | for sname, spath in ps.getSkinPaths(): |
---|
73 | paths = filter(None, map(string.strip, spath.split(','))) |
---|
74 | self.assertEqual("qplonecaptchafield" in paths, True, |
---|
75 | '"qplonecaptchafield" layer not present in "%s" skin' % sname) |
---|
76 | |
---|
77 | |
---|
78 | class TestCaptchaField(ptc.PloneTestCase): |
---|
79 | |
---|
80 | def afterSetUp(self): |
---|
81 | self.folder.invokeFactory('FormFolder', 'ff1') |
---|
82 | self.ff1 = getattr(self.folder, 'ff1') |
---|
83 | self.ff1.invokeFactory('CaptchaField', 'captcha_field') |
---|
84 | |
---|
85 | def testId(self): |
---|
86 | """CaptchaField has always CAPTCHA_ID id.""" |
---|
87 | self.assertEqual(CAPTCHA_ID in self.ff1, True) |
---|
88 | self.assertNotEqual('captcha_field' in self.ff1, True) |
---|
89 | |
---|
90 | def testSchema(self): |
---|
91 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
92 | schema = cf.Schema() |
---|
93 | for field in HIDDEN_FIELDS: |
---|
94 | visibility = schema[field].widget.visible |
---|
95 | self.assertEqual(visibility, {'view':'invisible','edit':'invisible'}, |
---|
96 | '"%s" field is not hidden, but %s:' % (field, visibility)) |
---|
97 | |
---|
98 | def testFGField(self): |
---|
99 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
100 | fgField = getattr(cf, 'fgField', _marker) |
---|
101 | self.assertNotEqual(fgField, _marker) |
---|
102 | # Test fgField properties |
---|
103 | self.assertEqual(type(fgField), StringField) |
---|
104 | self.assertEqual(bool(fgField.searchable), False ) |
---|
105 | self.assertEqual(fgField.write_permission, View,) |
---|
106 | self.assertEqual(type(fgField.widget), CaptchaWidget) |
---|
107 | validators = [v.__class__ for v in fgField.validators._chain] |
---|
108 | self.assertEqual(CaptchaValidator in validators, True) |
---|
109 | |
---|
110 | |
---|
111 | def test_suite(): |
---|
112 | suite = unittest.TestSuite() |
---|
113 | suite.addTest(unittest.makeSuite(TestInstallations)) |
---|
114 | suite.addTest(unittest.makeSuite(TestCaptchaField)) |
---|
115 | return suite |
---|