1 | # |
---|
2 | # Tests related to general Sitemap type. |
---|
3 | # |
---|
4 | from quintagroup.plonegooglesitemaps.tests.base \ |
---|
5 | import FunctionalTestCase, TestCase, IMobileMarker |
---|
6 | from StringIO import StringIO |
---|
7 | from urllib import urlencode |
---|
8 | import sys |
---|
9 | from XMLParser import hasURL |
---|
10 | import unittest |
---|
11 | |
---|
12 | from DateTime import DateTime |
---|
13 | from zope.interface import alsoProvides |
---|
14 | from zope.publisher.browser import TestRequest |
---|
15 | |
---|
16 | from Products.Archetypes import atapi |
---|
17 | from Products.CMFPlone.utils import _createObjectByType |
---|
18 | |
---|
19 | from quintagroup.plonegooglesitemaps.browser.sitemapview import SitemapView |
---|
20 | from quintagroup.plonegooglesitemaps.browser.newssitemapview \ |
---|
21 | import NewsSitemapView |
---|
22 | from quintagroup.plonegooglesitemaps.browser.mobilesitemapview \ |
---|
23 | import MobileSitemapView |
---|
24 | |
---|
25 | |
---|
26 | class TestSitemapType(FunctionalTestCase): |
---|
27 | |
---|
28 | def afterSetUp(self): |
---|
29 | super(TestSitemapType, self).afterSetUp() |
---|
30 | self.contentSM = _createObjectByType('Sitemap', self.portal, |
---|
31 | id='google-sitemaps') |
---|
32 | |
---|
33 | def testFields(self): |
---|
34 | field_ids = map(lambda x: x.getName(), |
---|
35 | self.contentSM.Schema().fields()) |
---|
36 | # test old Sitemap settings fields |
---|
37 | self.assert_('id' in field_ids) |
---|
38 | self.assert_('portalTypes' in field_ids) |
---|
39 | self.assert_('states' in field_ids) |
---|
40 | self.assert_('blackout_list' in field_ids) |
---|
41 | self.assert_('urls' in field_ids) |
---|
42 | self.assert_('pingTransitions' in field_ids) |
---|
43 | # test new sitemap type field |
---|
44 | self.assert_('sitemapType' in field_ids) |
---|
45 | |
---|
46 | def testSitemapTypes(self): |
---|
47 | sm_vocabulary = self.contentSM.getField('sitemapType').Vocabulary() |
---|
48 | sitemap_types = sm_vocabulary.keys() |
---|
49 | self.assert_('content' in sitemap_types) |
---|
50 | self.assert_('mobile' in sitemap_types) |
---|
51 | self.assert_('news' in sitemap_types) |
---|
52 | |
---|
53 | def testAutoSetLayout(self): |
---|
54 | response = self.publish('/%s/createObject?type_name=Sitemap' |
---|
55 | % self.portal.absolute_url(1), basic=self.auth) |
---|
56 | location = response.getHeader('location') |
---|
57 | newurl = location[location.find('/' + self.portal.absolute_url(1)):] |
---|
58 | |
---|
59 | msm_id = 'mobile_sitemap' |
---|
60 | form = {'id': msm_id, |
---|
61 | 'sitemapType': 'mobile', |
---|
62 | 'portalTypes': ['Document', ], |
---|
63 | 'states': ['published'], |
---|
64 | 'form_submit': 'Save', |
---|
65 | 'form.submitted': 1, |
---|
66 | } |
---|
67 | post_data = StringIO(urlencode(form)) |
---|
68 | response = self.publish(newurl, request_method='POST', stdin=post_data, |
---|
69 | basic=self.auth) |
---|
70 | msitemap = getattr(self.portal, msm_id) |
---|
71 | |
---|
72 | self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml') |
---|
73 | |
---|
74 | def testPingSetting(self): |
---|
75 | self.assertEqual(self.contentSM.getPingTransitions(), ()) |
---|
76 | |
---|
77 | self.contentSM.setPingTransitions(('plone_workflow#publish',)) |
---|
78 | self.assertEqual(self.contentSM.getPingTransitions(), |
---|
79 | ('plone_workflow#publish',)) |
---|
80 | |
---|
81 | def testWorkflowStates(self): |
---|
82 | wfstates = self.contentSM.getWorkflowStates() |
---|
83 | self.assertEqual(isinstance(wfstates, atapi.DisplayList), True) |
---|
84 | self.assertEqual("published" in wfstates.keys(), True) |
---|
85 | |
---|
86 | def testWorkflowTransitions(self): |
---|
87 | wftrans = self.contentSM.getWorkflowTransitions() |
---|
88 | self.assertEqual(isinstance(wftrans, atapi.DisplayList), True) |
---|
89 | self.assertEqual("simple_publication_workflow#publish" in |
---|
90 | wftrans.keys(), True) |
---|
91 | |
---|
92 | def testSettingBlackout(self): |
---|
93 | bolist = ["path:./el1 ", " ", "", " id:index.html ", "index_html"] |
---|
94 | expect = ("path:./el1", "id:index.html", "index_html") |
---|
95 | self.contentSM.edit(blackout_list=bolist) |
---|
96 | value = self.contentSM.getBlackout_list() |
---|
97 | self.assertTrue(value == expect, "Blackout list was not cleaned " |
---|
98 | "up from whitespaces: %s" |
---|
99 | % str(value)) |
---|
100 | |
---|
101 | |
---|
102 | class TestSettings(FunctionalTestCase): |
---|
103 | |
---|
104 | def afterSetUp(self): |
---|
105 | super(TestSettings, self).afterSetUp() |
---|
106 | gsm_properties = 'googlesitemap_properties' |
---|
107 | self.gsm_props = self.portal.portal_properties[gsm_properties] |
---|
108 | self.contentSM = _createObjectByType('Sitemap', self.portal, |
---|
109 | id='google-sitemaps') |
---|
110 | self.sitemapUrl = '/' + self.portal.absolute_url(1) + \ |
---|
111 | '/google-sitemaps' |
---|
112 | # Add testing document to portal |
---|
113 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
---|
114 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
115 | self.my_doc_url = self.my_doc.absolute_url() |
---|
116 | |
---|
117 | def testMetaTypeToDig(self): |
---|
118 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
119 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
120 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
121 | |
---|
122 | self.contentSM.setPortalTypes([]) |
---|
123 | |
---|
124 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
125 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
126 | |
---|
127 | self.contentSM.setPortalTypes(['Document']) |
---|
128 | |
---|
129 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
130 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
131 | |
---|
132 | def testStates(self): |
---|
133 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
134 | self.contentSM.setStates(['visible']) |
---|
135 | |
---|
136 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
137 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
138 | |
---|
139 | self.contentSM.setStates(['published']) |
---|
140 | |
---|
141 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
142 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
143 | |
---|
144 | def test_blackout_entries(self): |
---|
145 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
146 | self.contentSM.setBlackout_list((self.my_doc.getId(),)) |
---|
147 | |
---|
148 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
149 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
150 | |
---|
151 | self.contentSM.setBlackout_list([]) |
---|
152 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
153 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
154 | |
---|
155 | def test_regexp(self): |
---|
156 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
157 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
158 | self.assert_(not hasURL(sitemap, self.portal.absolute_url())) |
---|
159 | |
---|
160 | regexp = "s/\/%s//" % self.my_doc.getId() |
---|
161 | self.contentSM.setReg_exp([regexp]) |
---|
162 | |
---|
163 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
164 | self.assert_(hasURL(sitemap, self.portal.absolute_url())) |
---|
165 | |
---|
166 | def test_add_urls(self): |
---|
167 | self.contentSM.setUrls(['http://w1', 'w2', '/w3']) |
---|
168 | w1_url = 'http://w1' |
---|
169 | w2_url = self.portal.absolute_url() + '/w2' |
---|
170 | w3_url = self.portal.getPhysicalRoot().absolute_url() + '/w3' |
---|
171 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
172 | |
---|
173 | self.assert_(hasURL(sitemap, w1_url)) |
---|
174 | self.assert_(hasURL(sitemap, w2_url)) |
---|
175 | self.assert_(hasURL(sitemap, w3_url)) |
---|
176 | |
---|
177 | |
---|
178 | class TestPinging(FunctionalTestCase): |
---|
179 | |
---|
180 | def afterSetUp(self): |
---|
181 | super(TestPinging, self).afterSetUp() |
---|
182 | workflow = "simple_publication_workflow" |
---|
183 | self.workflow.setChainForPortalTypes(pt_names=('News Item', |
---|
184 | 'Document'), |
---|
185 | chain=workflow) |
---|
186 | gsm_properties = 'googlesitemap_properties' |
---|
187 | self.gsm_props = self.portal.portal_properties[gsm_properties] |
---|
188 | # Add sitemaps |
---|
189 | self.contentSM = _createObjectByType('Sitemap', self.portal, |
---|
190 | id='google-sitemaps') |
---|
191 | spw_publish = 'simple_publication_workflow#publish' |
---|
192 | self.contentSM.setPingTransitions((spw_publish,)) |
---|
193 | self.newsSM = _createObjectByType('Sitemap', self.portal, |
---|
194 | id='news-sitemaps') |
---|
195 | self.newsSM.setPortalTypes(('News Item', 'Document')) |
---|
196 | self.newsSM.setPingTransitions((spw_publish,)) |
---|
197 | self.sitemapUrl = '/' + self.portal.absolute_url(1) + \ |
---|
198 | '/google-sitemaps' |
---|
199 | # Add testing document to portal |
---|
200 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
---|
201 | self.my_news = _createObjectByType('News Item', self.portal, |
---|
202 | id='my_news') |
---|
203 | |
---|
204 | def testAutomatePinging(self): |
---|
205 | # 1. Check for pinging both sitemaps |
---|
206 | back_out, myout = sys.stdout, StringIO() |
---|
207 | sys.stdout = myout |
---|
208 | try: |
---|
209 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
210 | myout.seek(0) |
---|
211 | data = myout.read() |
---|
212 | finally: |
---|
213 | sys.stdout = back_out |
---|
214 | |
---|
215 | self.assert_('Pinged %s sitemap to Google' |
---|
216 | % self.contentSM.absolute_url() in data, |
---|
217 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
---|
218 | self.assert_('Pinged %s sitemap to Google' |
---|
219 | % self.newsSM.absolute_url() in data, |
---|
220 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
221 | |
---|
222 | # 2. Check for pinging only news-sitemap sitemaps |
---|
223 | back_out, myout = sys.stdout, StringIO() |
---|
224 | sys.stdout = myout |
---|
225 | try: |
---|
226 | self.workflow.doActionFor(self.my_news, 'publish') |
---|
227 | myout.seek(0) |
---|
228 | data = myout.read() |
---|
229 | finally: |
---|
230 | sys.stdout = back_out |
---|
231 | |
---|
232 | self.assert_('Pinged %s sitemap to Google' |
---|
233 | % self.newsSM.absolute_url() in data, |
---|
234 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
235 | self.assert_(not 'Pinged %s sitemap to Google' |
---|
236 | % self.contentSM.absolute_url() in data, |
---|
237 | "Pinged %s on news: '%s'" % (self.contentSM.id, data)) |
---|
238 | |
---|
239 | def testPingingWithSetupForm(self): |
---|
240 | # Ping news and content sitemaps |
---|
241 | formUrl = '/' + self.portal.absolute_url(1) + '/prefs_gsm_settings' |
---|
242 | qs = 'smselected:list=%s&smselected:list=%s&form.button.Ping=1' \ |
---|
243 | '&form.submitted=1' % (self.contentSM.id, self.newsSM.id) |
---|
244 | |
---|
245 | back_out, myout = sys.stdout, StringIO() |
---|
246 | sys.stdout = myout |
---|
247 | try: |
---|
248 | self.publish("%s?%s" % (formUrl, qs), basic=self.auth) |
---|
249 | myout.seek(0) |
---|
250 | data = myout.read() |
---|
251 | finally: |
---|
252 | sys.stdout = back_out |
---|
253 | |
---|
254 | self.assert_('Pinged %s sitemap to Google' |
---|
255 | % self.contentSM.absolute_url() in data, |
---|
256 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
---|
257 | self.assert_('Pinged %s sitemap to Google' |
---|
258 | % self.newsSM.absolute_url() in data, |
---|
259 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
260 | |
---|
261 | |
---|
262 | class TestContextSearch(TestCase): |
---|
263 | """ Test if sitemaps collect objects from the container, |
---|
264 | where it added to, but not from the portal root. |
---|
265 | """ |
---|
266 | def prepareTestContent(self, smtype, ptypes, ifaces=()): |
---|
267 | # Create test folder |
---|
268 | tfolder = _createObjectByType("Folder", self.portal, id="test-folder") |
---|
269 | # Add SiteMap in the test folder |
---|
270 | self.sm = _createObjectByType("Sitemap", tfolder, id='sitemap', |
---|
271 | sitemapType=smtype, portalTypes=ptypes) |
---|
272 | self.sm.at_post_create_script() |
---|
273 | # Add content in root and in the test folder |
---|
274 | pubdate = (DateTime() + 1).strftime("%Y-%m-%d") |
---|
275 | root_content = _createObjectByType(ptypes[0], self.portal, |
---|
276 | id='root-content') |
---|
277 | inner_content = _createObjectByType(ptypes[0], tfolder, |
---|
278 | id='inner-content') |
---|
279 | for obj in (root_content, inner_content): |
---|
280 | self.workflow.doActionFor(obj, 'publish') |
---|
281 | if ifaces: |
---|
282 | alsoProvides(obj, ifaces) |
---|
283 | obj.edit(effectiveDate=pubdate) # this also reindex object |
---|
284 | self.inner_path = '/'.join(inner_content.getPhysicalPath()) |
---|
285 | |
---|
286 | def testGoogleSitemap(self): |
---|
287 | self.prepareTestContent("content", ("Document",)) |
---|
288 | filtered = SitemapView(self.sm, TestRequest()).getFilteredObjects() |
---|
289 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
---|
290 | [self.inner_path, ]) |
---|
291 | |
---|
292 | def testNewsSitemap(self): |
---|
293 | self.prepareTestContent("news", ("News Item",)) |
---|
294 | filtered = NewsSitemapView(self.sm, TestRequest()).getFilteredObjects() |
---|
295 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
---|
296 | [self.inner_path, ]) |
---|
297 | |
---|
298 | def testMobileSitemap(self): |
---|
299 | self.patchMobile() |
---|
300 | self.prepareTestContent("content", ("Document",), (IMobileMarker,)) |
---|
301 | filtered = MobileSitemapView(self.sm, |
---|
302 | TestRequest()).getFilteredObjects() |
---|
303 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
---|
304 | [self.inner_path, ]) |
---|
305 | |
---|
306 | |
---|
307 | class TestSitemapDate(TestCase): |
---|
308 | """ Method dedicated to test index (sitemap_date) in portal_catalog |
---|
309 | """ |
---|
310 | def afterSetUp(self): |
---|
311 | super(TestSitemapDate, self).afterSetUp() |
---|
312 | |
---|
313 | from time import sleep |
---|
314 | |
---|
315 | # sequence is important for testing |
---|
316 | # ("test-folder1", "test-folder2", "index_html") |
---|
317 | self.folder1 = _createObjectByType("Folder", self.portal, |
---|
318 | id="test-folder1") |
---|
319 | |
---|
320 | # create objects with different sitemap_date |
---|
321 | sleep(1) |
---|
322 | self.folder2 = _createObjectByType("Folder", self.folder1, |
---|
323 | id="test-folder2") |
---|
324 | sleep(1) |
---|
325 | self.page = _createObjectByType("Document", self.folder2, |
---|
326 | id="index_html") |
---|
327 | |
---|
328 | def getCatalogSitemapDate(self, obj): |
---|
329 | """ Method gets sitemap_date from portal_catalog """ |
---|
330 | return self.portal.portal_catalog(id=obj.id)[0].sitemap_date |
---|
331 | |
---|
332 | def getIndexerSitemapDate(self, obj): |
---|
333 | """ Method gets modification date from |
---|
334 | function sitemap_date (indexer) |
---|
335 | """ |
---|
336 | from quintagroup.plonegooglesitemaps.indexers import sitemap_date |
---|
337 | |
---|
338 | modification_date = sitemap_date(obj) |
---|
339 | # you have had to use '__call__' since Plone 3.3 |
---|
340 | if callable(modification_date): |
---|
341 | modification_date = modification_date() |
---|
342 | return modification_date |
---|
343 | |
---|
344 | def testReindexParentObjects(self): |
---|
345 | """ Method tests handler (reindexParentObjects) """ |
---|
346 | from quintagroup.plonegooglesitemaps.handlers \ |
---|
347 | import reindexParentObjects |
---|
348 | |
---|
349 | # set default page |
---|
350 | self.folder2.setDefaultPage("index_html") |
---|
351 | reindexParentObjects(self.page, None) |
---|
352 | |
---|
353 | self.assertEqual(self.getCatalogSitemapDate(self.page), |
---|
354 | self.getCatalogSitemapDate(self.folder2)) |
---|
355 | self.assertNotEqual(self.getCatalogSitemapDate(self.page), |
---|
356 | self.getCatalogSitemapDate(self.folder1)) |
---|
357 | |
---|
358 | # set default page |
---|
359 | self.folder1.setDefaultPage("test-folder2") |
---|
360 | reindexParentObjects(self.folder2, None) |
---|
361 | self.assertEqual(self.getCatalogSitemapDate(self.page), |
---|
362 | self.getCatalogSitemapDate(self.folder1)) |
---|
363 | |
---|
364 | def testSitemapDateIndexer(self): |
---|
365 | """ Method tests index (sitemap_date) """ |
---|
366 | last_date = self.getCatalogSitemapDate(self.folder1) |
---|
367 | self.assertEqual(last_date, self.getIndexerSitemapDate(self.folder1)) |
---|
368 | self.folder1.setDefaultPage("test-folder2") |
---|
369 | self.assertNotEqual(last_date, |
---|
370 | self.getIndexerSitemapDate(self.folder1)) |
---|
371 | |
---|
372 | |
---|
373 | def test_suite(): |
---|
374 | suite = unittest.TestSuite() |
---|
375 | suite.addTest(unittest.makeSuite(TestSitemapType)) |
---|
376 | suite.addTest(unittest.makeSuite(TestSettings)) |
---|
377 | suite.addTest(unittest.makeSuite(TestPinging)) |
---|
378 | suite.addTest(unittest.makeSuite(TestContextSearch)) |
---|
379 | suite.addTest(unittest.makeSuite(TestSitemapDate)) |
---|
380 | return suite |
---|
381 | |
---|
382 | if __name__ == '__main__': |
---|
383 | unittest.main(defaultTest='test_suite') |
---|
384 | # framework() |
---|