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