1 | # |
---|
2 | # Tests for qPloneGoogleSitemaps |
---|
3 | # |
---|
4 | |
---|
5 | from urllib import urlencode |
---|
6 | from StringIO import StringIO |
---|
7 | |
---|
8 | from Products.Five import zcml |
---|
9 | from Products.Five import fiveconfigure |
---|
10 | from Products.PloneTestCase import PloneTestCase |
---|
11 | |
---|
12 | from Products.CMFPlone.utils import _createObjectByType |
---|
13 | |
---|
14 | import Products.qPloneGoogleSitemaps |
---|
15 | from Products.qPloneGoogleSitemaps.config import ping_googlesitemap |
---|
16 | |
---|
17 | from XMLParser import parse, hasURL |
---|
18 | |
---|
19 | PRODUCT = 'qPloneGoogleSitemaps' |
---|
20 | PRODUCTS = (PRODUCT,) |
---|
21 | |
---|
22 | # Import configure.zcml for qPloneGoogleSitemaps |
---|
23 | fiveconfigure.debug_mode = True |
---|
24 | zcml.load_config('configure.zcml', Products.qPloneGoogleSitemaps) |
---|
25 | fiveconfigure.debug_mode = False |
---|
26 | |
---|
27 | PloneTestCase.installProduct(PRODUCT) |
---|
28 | PloneTestCase.setupPloneSite(extension_profiles=("Products.%s:default" % PRODUCT,)) |
---|
29 | |
---|
30 | class TestqPloneGoogleSitemapsInstallation(PloneTestCase.PloneTestCase): |
---|
31 | |
---|
32 | def afterSetUp(self): |
---|
33 | self.loginAsPortalOwner() |
---|
34 | |
---|
35 | def testType(self): |
---|
36 | pt = self.portal.portal_types |
---|
37 | self.assert_('Sitemap' in pt.objectIds(), |
---|
38 | 'No "Sitemap" type after installation') |
---|
39 | #Test views |
---|
40 | views = pt.getTypeInfo('Sitemap').view_methods |
---|
41 | self.assert_('sitemap.xml' in views, |
---|
42 | 'No "sitemap.xml" view for Sitemap type') |
---|
43 | self.assert_('mobile-sitemap.xml' in views, |
---|
44 | 'No "mobile-sitemap.xml" view for Sitemap type') |
---|
45 | self.assert_('news-sitemap.xml' in views, |
---|
46 | 'No "news-sitemap.xml" view for Sitemap type') |
---|
47 | |
---|
48 | def testGSMProperties(self): |
---|
49 | pp = self.portal.portal_properties |
---|
50 | |
---|
51 | # Test types_not_searched |
---|
52 | self.assert_("Sitemap" in pp['site_properties'].getProperty('types_not_searched'), |
---|
53 | 'No "Sitemap" added to types not searched on installation') |
---|
54 | # Test metaTypesNotToList |
---|
55 | self.assert_("Sitemap" in pp['navtree_properties'].getProperty('metaTypesNotToList'), |
---|
56 | 'No "Sitemap" added to types not to list on installation') |
---|
57 | |
---|
58 | # Test 'googlesitemap_properties' |
---|
59 | self.assert_('googlesitemap_properties' in pp.objectIds(), |
---|
60 | 'No "googlesitemap_properties" after installation') |
---|
61 | qsmprops = pp['googlesitemap_properties'] |
---|
62 | self.assert_(qsmprops.hasProperty('content_default'), |
---|
63 | 'No "content_default" property added on installation') |
---|
64 | self.assert_(qsmprops.hasProperty('mobile_default'), |
---|
65 | 'No "mobile_default" property added on installation') |
---|
66 | self.assert_(qsmprops.hasProperty('news_default'), |
---|
67 | 'No "news_default" property added on installation') |
---|
68 | self.assert_(qsmprops.hasProperty('verification_filename'), |
---|
69 | 'No "verification_filename" property added on installation') |
---|
70 | |
---|
71 | def testGSMProperties(self): |
---|
72 | pp = self.portal.portal_properties |
---|
73 | |
---|
74 | self.assert_('googlesitemap_properties' in pp.objectIds(), |
---|
75 | 'No "googlesitemap_properties" after installation') |
---|
76 | qsmprops = pp['googlesitemap_properties'] |
---|
77 | self.assert_(qsmprops.hasProperty('content_default'), |
---|
78 | 'No "content_default" property added on installation') |
---|
79 | self.assert_(qsmprops.hasProperty('mobile_default'), |
---|
80 | 'No "mobile_default" property added on installation') |
---|
81 | self.assert_(qsmprops.hasProperty('news_default'), |
---|
82 | 'No "news_default" property added on installation') |
---|
83 | self.assert_(qsmprops.hasProperty('verification_filename'), |
---|
84 | 'No "verification_filename" property added on installation') |
---|
85 | |
---|
86 | def testSkins(self): |
---|
87 | ps = self.portal.portal_skins |
---|
88 | self.assert_('qPloneGoogleSitemaps' in ps.objectIds(), |
---|
89 | 'No "qPloneGoogleSitemaps" skin layer in portal_skins') |
---|
90 | self.assert_('qPloneGoogleSitemaps' in ps.getSkinPath(ps.getDefaultSkin()), |
---|
91 | 'No "qPloneGoogleSitemaps" skin layer in default skin') |
---|
92 | |
---|
93 | def testConfiglet(self): |
---|
94 | cp = self.portal.portal_controlpanel |
---|
95 | self.assert_([1 for ai in cp.listActionInfos() if ai['id']=='qPloneGoogleSitemaps'], |
---|
96 | 'No "qPloneGoogleSitemaps" configlet added to plone control panel') |
---|
97 | |
---|
98 | def testCatalog(self): |
---|
99 | catalog = self.portal.portal_catalog |
---|
100 | self.assert_('hasMobileContent' in catalog.indexes(), |
---|
101 | 'No "hasMobileContent" index in portal_catalog') |
---|
102 | |
---|
103 | |
---|
104 | class TestSitemapType(PloneTestCase.FunctionalTestCase): |
---|
105 | |
---|
106 | def afterSetUp(self): |
---|
107 | self.loginAsPortalOwner() |
---|
108 | self.auth = 'admin:admin' |
---|
109 | self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
110 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
111 | |
---|
112 | def testFields(self): |
---|
113 | field_ids = map(lambda x:x.getName(), self.contentSM.Schema().fields()) |
---|
114 | # test old Sitemap settings fields |
---|
115 | self.assert_('id' in field_ids) |
---|
116 | self.assert_('portalTypes' in field_ids) |
---|
117 | self.assert_('states' in field_ids) |
---|
118 | self.assert_('blackout_list' in field_ids) |
---|
119 | self.assert_('urls' in field_ids) |
---|
120 | self.assert_('pingTransitions' in field_ids) |
---|
121 | # test new sitemap type field |
---|
122 | self.assert_('sitemapType' in field_ids) |
---|
123 | |
---|
124 | def testSitemapTypes(self): |
---|
125 | sitemap_types = self.contentSM.getField('sitemapType').Vocabulary().keys() |
---|
126 | self.assert_('content' in sitemap_types) |
---|
127 | self.assert_('mobile' in sitemap_types) |
---|
128 | self.assert_('news' in sitemap_types) |
---|
129 | |
---|
130 | def testAutoSetLayout(self): |
---|
131 | response = self.publish('/%s/createObject?type_name=Sitemap' % \ |
---|
132 | self.portal.absolute_url(1), basic=self.auth) |
---|
133 | location = response.getHeader('location') |
---|
134 | newurl = location[location.find('/'+self.portal.absolute_url(1)):] |
---|
135 | |
---|
136 | msm_id = 'mobile_sitemap' |
---|
137 | form = {'id': msm_id, |
---|
138 | 'sitemapType':'mobile', |
---|
139 | 'portalTypes':['Document',], |
---|
140 | 'states':['published'], |
---|
141 | 'form_submit':'Save', |
---|
142 | 'form.submitted':1, |
---|
143 | } |
---|
144 | post_data = StringIO(urlencode(form)) |
---|
145 | response = self.publish(newurl, request_method='POST', stdin=post_data, basic=self.auth) |
---|
146 | msitemap = getattr(self.portal, msm_id) |
---|
147 | |
---|
148 | self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml') |
---|
149 | |
---|
150 | def txestPingSetting(self): |
---|
151 | pwf = self.portal.portal_workflow['plone_workflow'] |
---|
152 | self.assertEqual(self.contentSM.getPingTransitions(), ()) |
---|
153 | |
---|
154 | self.contentSM.setPingTransitions(('plone_workflow#publish',)) |
---|
155 | self.assertEqual(self.contentSM.getPingTransitions(), ('plone_workflow#publish',)) |
---|
156 | self.assert_(ping_googlesitemap in pwf.scripts.keys(),"Not add wf script") |
---|
157 | |
---|
158 | |
---|
159 | class TestqPloneGoogleSitemaps(PloneTestCase.FunctionalTestCase): |
---|
160 | |
---|
161 | def afterSetUp(self): |
---|
162 | self.loginAsPortalOwner() |
---|
163 | |
---|
164 | self.workflow = self.portal.portal_workflow |
---|
165 | self.auth = 'admin:admin' |
---|
166 | _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
167 | self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps' |
---|
168 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
169 | |
---|
170 | # Add testing document to portal |
---|
171 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
172 | self.my_doc = self.portal['my_doc'] |
---|
173 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | def testSitemap(self): |
---|
178 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
179 | parsed_sitemap = parse(sitemap) |
---|
180 | start = parsed_sitemap['start'] |
---|
181 | data = parsed_sitemap['data'] |
---|
182 | self.assertEqual(len(start.keys()), 1) |
---|
183 | self.assert_('urlset' in start.keys()) |
---|
184 | |
---|
185 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
186 | |
---|
187 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
188 | parsed_sitemap = parse(sitemap) |
---|
189 | start = parsed_sitemap['start'] |
---|
190 | data = parsed_sitemap['data'] |
---|
191 | self.assertEqual(len(start.keys()), 4) |
---|
192 | self.assert_('urlset' in start.keys()) |
---|
193 | self.assert_('url' in start.keys()) |
---|
194 | self.assert_('loc' in start.keys()) |
---|
195 | self.assert_('lastmod' in start.keys()) |
---|
196 | |
---|
197 | self.assert_(data[0] == self.my_doc.absolute_url(0), 'Incorect url') |
---|
198 | |
---|
199 | def testVerificationFile(self): |
---|
200 | self.portal.gsm_create_verify_file('verif_file') |
---|
201 | |
---|
202 | vf_created = hasattr(self.portal, 'verif_file') |
---|
203 | self.assert_(vf_created, 'Verification file not created') |
---|
204 | |
---|
205 | self.portal.gsm_delete_verify_file() |
---|
206 | vf_created = hasattr(self.portal, 'verif_file') |
---|
207 | self.assert_(not vf_created, 'Verification file not removed') |
---|
208 | |
---|
209 | |
---|
210 | class TestSettings(PloneTestCase.FunctionalTestCase): |
---|
211 | |
---|
212 | def afterSetUp(self): |
---|
213 | self.loginAsPortalOwner() |
---|
214 | |
---|
215 | self.workflow = self.portal.portal_workflow |
---|
216 | self.gsm_props = self.portal.portal_properties['googlesitemap_properties'] |
---|
217 | self.auth = 'admin:admin' |
---|
218 | self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
219 | |
---|
220 | self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps' |
---|
221 | |
---|
222 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
223 | |
---|
224 | # Add testing document to portal |
---|
225 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
226 | self.my_doc = self.portal['my_doc'] |
---|
227 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
228 | self.my_doc_url = self.my_doc.absolute_url() |
---|
229 | |
---|
230 | def testMetaTypeToDig(self): |
---|
231 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
232 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
233 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
234 | |
---|
235 | self.contentSM.setPortalTypes([]) |
---|
236 | |
---|
237 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
238 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
239 | |
---|
240 | self.contentSM.setPortalTypes(['Document']) |
---|
241 | |
---|
242 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
243 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
244 | |
---|
245 | def testStates(self): |
---|
246 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
247 | self.contentSM.setStates(['visible']) |
---|
248 | |
---|
249 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
250 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
251 | |
---|
252 | self.contentSM.setStates(['published']) |
---|
253 | |
---|
254 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
255 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
256 | |
---|
257 | def test_blackout_entries(self): |
---|
258 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
259 | self.contentSM.setBlackout_list((self.my_doc.getId(),)) |
---|
260 | |
---|
261 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
262 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
263 | |
---|
264 | self.contentSM.setBlackout_list([]) |
---|
265 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
266 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
267 | |
---|
268 | def test_regexp(self): |
---|
269 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
270 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
271 | self.assert_(not hasURL(sitemap, self.portal.absolute_url())) |
---|
272 | |
---|
273 | regexp = "s/\/%s//"%self.my_doc.getId() |
---|
274 | self.contentSM.setReg_exp([regexp]) |
---|
275 | |
---|
276 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
277 | self.assert_(hasURL(sitemap, self.portal.absolute_url())) |
---|
278 | |
---|
279 | def test_add_urls(self): |
---|
280 | self.contentSM.setUrls(['http://w1', 'w2', '/w3']) |
---|
281 | w1_url = 'http://w1' |
---|
282 | w2_url = self.portal.absolute_url() + '/w2' |
---|
283 | w3_url = self.portal.getPhysicalRoot().absolute_url() + '/w3' |
---|
284 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
285 | |
---|
286 | self.assert_(hasURL(sitemap, w1_url)) |
---|
287 | self.assert_(hasURL(sitemap, w2_url)) |
---|
288 | self.assert_(hasURL(sitemap, w3_url)) |
---|
289 | |
---|
290 | def test_suite(): |
---|
291 | from unittest import TestSuite, makeSuite |
---|
292 | suite = TestSuite() |
---|
293 | suite.addTest(makeSuite(TestqPloneGoogleSitemapsInstallation)) |
---|
294 | suite.addTest(makeSuite(TestSitemapType)) |
---|
295 | suite.addTest(makeSuite(TestqPloneGoogleSitemaps)) |
---|
296 | suite.addTest(makeSuite(TestSettings)) |
---|
297 | |
---|
298 | return suite |
---|
299 | |
---|
300 | if __name__ == '__main__': |
---|
301 | framework() |
---|