1 | import unittest |
---|
2 | |
---|
3 | from zope.testing import doctestunit |
---|
4 | from zope.component import testing |
---|
5 | from zope.component import queryAdapter, queryMultiAdapter |
---|
6 | from zope.schema.interfaces import InvalidValue |
---|
7 | from plone.indexer.interfaces import IIndexableObject |
---|
8 | from Testing import ZopeTestCase as ztc |
---|
9 | |
---|
10 | from Products.Five import zcml |
---|
11 | from Products.Five import fiveconfigure |
---|
12 | from Products.CMFCore.utils import getToolByName |
---|
13 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
14 | from Products.PloneTestCase.layer import PloneSite |
---|
15 | |
---|
16 | from Products.Archetypes.tests.utils import makeContent |
---|
17 | |
---|
18 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
19 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
20 | from quintagroup.canonicalpath.adapters import PROPERTY_PATH |
---|
21 | from quintagroup.canonicalpath.adapters import PROPERTY_LINK |
---|
22 | |
---|
23 | class TestCase(ptc.PloneTestCase): |
---|
24 | class layer(PloneSite): |
---|
25 | @classmethod |
---|
26 | def setUp(cls): |
---|
27 | import quintagroup.canonicalpath |
---|
28 | fiveconfigure.debug_mode = True |
---|
29 | zcml.load_config('configure.zcml', quintagroup.canonicalpath) |
---|
30 | fiveconfigure.debug_mode = False |
---|
31 | |
---|
32 | ptc.setupPloneSite() |
---|
33 | |
---|
34 | class TestIndexerRegistration(TestCase): |
---|
35 | |
---|
36 | def afterSetUp(self): |
---|
37 | self.loginAsPortalOwner() |
---|
38 | self.catalog = getToolByName(self.portal, 'portal_catalog') |
---|
39 | self.my_doc = makeContent(self.portal, portal_type='Document', id='my_doc') |
---|
40 | self.logout() |
---|
41 | |
---|
42 | def testForPortal(self): |
---|
43 | wrapper = queryMultiAdapter((self.portal, self.catalog), IIndexableObject) |
---|
44 | self.assertFalse(wrapper is None, "No indexer registered for portal object") |
---|
45 | |
---|
46 | def testForAT(self): |
---|
47 | wrapper = queryMultiAdapter((self.my_doc, self.catalog), IIndexableObject) |
---|
48 | self.assertFalse(wrapper is None, "No indexer registered for document object") |
---|
49 | |
---|
50 | def testCanonicalPathForPortal(self): |
---|
51 | wrapper = queryMultiAdapter((self.portal, self.catalog), IIndexableObject) |
---|
52 | self.assertTrue(hasattr(wrapper, 'canonical_path'), |
---|
53 | "'canonical_path' attribute not accessible with indexer wrapper for portal object") |
---|
54 | |
---|
55 | def testCanonicalPathForAT(self): |
---|
56 | wrapper = queryMultiAdapter((self.my_doc, self.catalog), IIndexableObject) |
---|
57 | self.assertTrue(hasattr(wrapper, 'canonical_path'), |
---|
58 | "'canonical_path' attribute not accessible with indexer wrapper for Document object") |
---|
59 | |
---|
60 | def testCanonicalLinkForPortal(self): |
---|
61 | wrapper = queryMultiAdapter((self.portal, self.catalog), IIndexableObject) |
---|
62 | self.assertTrue(hasattr(wrapper, 'canonical_link'), |
---|
63 | "'canonical_link' attribute not accessible with indexer wrapper for portal object") |
---|
64 | |
---|
65 | def testCanonicalLinkForAT(self): |
---|
66 | wrapper = queryMultiAdapter((self.my_doc, self.catalog), IIndexableObject) |
---|
67 | self.assertTrue(hasattr(wrapper, 'canonical_link'), |
---|
68 | "'canonical_link' attribute not accessible with indexer wrapper for Document object") |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | class TestDefaultCanonicalPathAdapter(TestCase): |
---|
73 | |
---|
74 | |
---|
75 | def afterSetUp(self): |
---|
76 | self.loginAsPortalOwner() |
---|
77 | self.purl = getToolByName(self.portal, 'portal_url') |
---|
78 | self.my_doc = makeContent(self.portal, portal_type='Document', id='my_doc') |
---|
79 | self.logout() |
---|
80 | |
---|
81 | self.mydoc_cp = '/'+'/'.join(self.purl.getRelativeContentPath(self.my_doc)) |
---|
82 | self.portal_cp = '/'+'/'.join(self.purl.getRelativeContentPath(self.portal)) |
---|
83 | |
---|
84 | def testRegistration4Portal(self): |
---|
85 | cpadapter = queryAdapter(self.portal, ICanonicalPath) |
---|
86 | self.assertFalse(cpadapter is None, |
---|
87 | "Can't get canonical path adapter for the plone site object") |
---|
88 | |
---|
89 | def testRegistration4AT(self): |
---|
90 | cpadapter = queryAdapter(self.my_doc, ICanonicalPath) |
---|
91 | self.assertFalse(cpadapter is None, |
---|
92 | "Can't get canonical path adapter for the Document object") |
---|
93 | |
---|
94 | |
---|
95 | def testGetDefault4Portal(self): |
---|
96 | cpadapter = queryAdapter(self.portal, ICanonicalPath) |
---|
97 | self.assertTrue(cpadapter.canonical_path == self.portal_cp, |
---|
98 | "Canonical path adapter return '%s' for portal, must be: '%s'" % ( |
---|
99 | cpadapter.canonical_path, self.portal_cp) ) |
---|
100 | |
---|
101 | |
---|
102 | def testGetDefault4AT(self): |
---|
103 | cpadapter = queryAdapter(self.my_doc, ICanonicalPath) |
---|
104 | self.assertTrue(cpadapter.canonical_path == self.mydoc_cp, |
---|
105 | "Canonical path adapter return '%s' for document, must be: '%s'" % ( |
---|
106 | cpadapter.canonical_path, self.mydoc_cp) ) |
---|
107 | |
---|
108 | |
---|
109 | def testSet4Portal(self): |
---|
110 | cpadapter = queryAdapter(self.portal, ICanonicalPath) |
---|
111 | newcp = self.portal_cp + '/new_portal_canonical' |
---|
112 | |
---|
113 | cpadapter.canonical_path = newcp |
---|
114 | prop = self.portal.getProperty(PROPERTY_PATH, None) |
---|
115 | self.assertTrue(prop == newcp, |
---|
116 | "Canonical path adapter setter NOT SET new '%s' value to '%s' " \ |
---|
117 | "propery for the portal" % (newcp, PROPERTY_PATH) ) |
---|
118 | |
---|
119 | self.assertTrue(cpadapter.canonical_path == newcp, |
---|
120 | "Canonical path adapter GET '%s' canonical_path, for portal, " \ |
---|
121 | "must be: '%s'" % (cpadapter.canonical_path, newcp) ) |
---|
122 | |
---|
123 | |
---|
124 | def testSet4AT(self): |
---|
125 | cpadapter = queryAdapter(self.my_doc, ICanonicalPath) |
---|
126 | newcp = self.mydoc_cp + '/new_mydoc_canonical' |
---|
127 | |
---|
128 | cpadapter.canonical_path = newcp |
---|
129 | prop = self.my_doc.getProperty(PROPERTY_PATH, None) |
---|
130 | self.assertTrue(prop == newcp, |
---|
131 | "Canonical path adapter setter NOT SET new '%s' value to '%s' " \ |
---|
132 | "propery for the Document" % (newcp, PROPERTY_PATH) ) |
---|
133 | |
---|
134 | self.assertTrue(cpadapter.canonical_path == newcp, |
---|
135 | "Canonical path adapter GET '%s' canonical_path, for Document, " \ |
---|
136 | "must be: '%s'" % (cpadapter.canonical_path, newcp) ) |
---|
137 | |
---|
138 | |
---|
139 | def testValidationWrong(self): |
---|
140 | cpadapter = queryAdapter(self.my_doc, ICanonicalPath) |
---|
141 | for wrong in ['new\nline','s p a c e','with\ttabs']: |
---|
142 | try: |
---|
143 | cpadapter.canonical_path = wrong |
---|
144 | except InvalidValue: |
---|
145 | continue |
---|
146 | else: |
---|
147 | raise self.failureException, "InvalidValue not raised when '%s' wrong value try to set" % wrong |
---|
148 | |
---|
149 | def testValidationGood(self): |
---|
150 | cpadapter = queryAdapter(self.my_doc, ICanonicalPath) |
---|
151 | for good in ['./good','../good','/good', 'good']: |
---|
152 | cpadapter.canonical_path = good |
---|
153 | |
---|
154 | |
---|
155 | class TestDefaultCanonicalLinkAdapter(TestCase): |
---|
156 | |
---|
157 | |
---|
158 | def afterSetUp(self): |
---|
159 | self.loginAsPortalOwner() |
---|
160 | self.purl = getToolByName(self.portal, 'portal_url') |
---|
161 | self.my_doc = makeContent(self.portal, portal_type='Document', id='my_doc') |
---|
162 | self.logout() |
---|
163 | |
---|
164 | self.mydoc_cl = self.my_doc.absolute_url() |
---|
165 | |
---|
166 | def testRegistration4Portal(self): |
---|
167 | cladapter = queryAdapter(self.portal, ICanonicalLink) |
---|
168 | self.assertFalse(cladapter is None, |
---|
169 | "Can't get canonical link adapter for the plone site object") |
---|
170 | |
---|
171 | def testRegistration4AT(self): |
---|
172 | cladapter = queryAdapter(self.my_doc, ICanonicalLink) |
---|
173 | self.assertFalse(cladapter is None, |
---|
174 | "Can't get canonical link adapter for the Document object") |
---|
175 | |
---|
176 | |
---|
177 | def testGetDefault4Portal(self): |
---|
178 | cladapter = queryAdapter(self.portal, ICanonicalLink) |
---|
179 | self.assertTrue(cladapter.canonical_link == self.purl(), |
---|
180 | "Canonical link adapter return '%s' for portal, must be: '%s'" % ( |
---|
181 | cladapter.canonical_link, self.purl()) ) |
---|
182 | |
---|
183 | |
---|
184 | def testGetDefault4AT(self): |
---|
185 | cladapter = queryAdapter(self.my_doc, ICanonicalLink) |
---|
186 | self.assertTrue(cladapter.canonical_link == self.mydoc_cl, |
---|
187 | "Canonical link adapter return '%s' for document, must be: '%s'" % ( |
---|
188 | cladapter.canonical_link, self.mydoc_cl) ) |
---|
189 | |
---|
190 | |
---|
191 | def testSet4Portal(self): |
---|
192 | cladapter = queryAdapter(self.portal, ICanonicalLink) |
---|
193 | newcl = self.purl() + '/new_portal_canonical' |
---|
194 | |
---|
195 | cladapter.canonical_link = newcl |
---|
196 | prop = self.portal.getProperty(PROPERTY_LINK, None) |
---|
197 | self.assertTrue(prop == newcl, |
---|
198 | "Canonical link adapter setter NOT SET new '%s' value to '%s' " \ |
---|
199 | "propery for the portal" % (newcl, PROPERTY_LINK) ) |
---|
200 | |
---|
201 | self.assertTrue(cladapter.canonical_link == newcl, |
---|
202 | "Canonical link adapter GET '%s' canonical_link, for portal, " \ |
---|
203 | "must be: '%s'" % (cladapter.canonical_link, newcl) ) |
---|
204 | |
---|
205 | |
---|
206 | def testSet4AT(self): |
---|
207 | cladapter = queryAdapter(self.my_doc, ICanonicalLink) |
---|
208 | newcl = self.mydoc_cl + '/new_mydoc_canonical' |
---|
209 | |
---|
210 | cladapter.canonical_link = newcl |
---|
211 | prop = self.my_doc.getProperty(PROPERTY_LINK, None) |
---|
212 | self.assertTrue(prop == newcl, |
---|
213 | "Canonical link adapter setter NOT SET new '%s' value to '%s' " \ |
---|
214 | "propery for the Document" % (newcl, PROPERTY_LINK) ) |
---|
215 | |
---|
216 | self.assertTrue(cladapter.canonical_link == newcl, |
---|
217 | "Canonical link adapter GET '%s' canonical_link, for Document, " \ |
---|
218 | "must be: '%s'" % (cladapter.canonical_link, newcl) ) |
---|
219 | |
---|
220 | def testValidationWrong(self): |
---|
221 | cladapter = queryAdapter(self.my_doc, ICanonicalLink) |
---|
222 | for wrong in ['http://new\nline','s p a c e','with\ttabs']: |
---|
223 | try: |
---|
224 | cladapter.canonical_link = wrong |
---|
225 | except InvalidValue: |
---|
226 | continue |
---|
227 | else: |
---|
228 | raise self.failureException, "InvalidValue not raised when " \ |
---|
229 | "'%s' wrong value try to set" % wrong |
---|
230 | |
---|
231 | def testValidationGood(self): |
---|
232 | cladapter = queryAdapter(self.my_doc, ICanonicalLink) |
---|
233 | for good in ['http://', './good','../good','/good', 'good']: |
---|
234 | cladapter.canonical_link = good |
---|
235 | |
---|
236 | |
---|
237 | def test_suite(): |
---|
238 | return unittest.TestSuite([ |
---|
239 | unittest.makeSuite(TestIndexerRegistration), |
---|
240 | unittest.makeSuite(TestDefaultCanonicalPathAdapter), |
---|
241 | unittest.makeSuite(TestDefaultCanonicalLinkAdapter), |
---|
242 | ]) |
---|
243 | |
---|
244 | if __name__ == '__main__': |
---|
245 | unittest.main(defaultTest='test_suite') |
---|