[1593] | 1 | # |
---|
[1619] | 2 | # Tests for quintagroup.plonegooglesitemaps |
---|
[1593] | 3 | # |
---|
[1619] | 4 | |
---|
[2382] | 5 | from base import * |
---|
[1593] | 6 | from Products.CMFPlone.utils import _createObjectByType |
---|
| 7 | |
---|
[1619] | 8 | class TestGoogleSitemapsInstallation(TestCase): |
---|
[1593] | 9 | |
---|
| 10 | def testType(self): |
---|
| 11 | pt = self.portal.portal_types |
---|
| 12 | self.assert_('Sitemap' in pt.objectIds(), |
---|
| 13 | 'No "Sitemap" type after installation') |
---|
| 14 | #Test views |
---|
| 15 | views = pt.getTypeInfo('Sitemap').view_methods |
---|
| 16 | self.assert_('sitemap.xml' in views, |
---|
| 17 | 'No "sitemap.xml" view for Sitemap type') |
---|
| 18 | self.assert_('mobile-sitemap.xml' in views, |
---|
| 19 | 'No "mobile-sitemap.xml" view for Sitemap type') |
---|
| 20 | self.assert_('news-sitemap.xml' in views, |
---|
| 21 | 'No "news-sitemap.xml" view for Sitemap type') |
---|
| 22 | |
---|
| 23 | def testGSMProperties(self): |
---|
| 24 | pp = self.portal.portal_properties |
---|
| 25 | |
---|
| 26 | # Test types_not_searched |
---|
| 27 | self.assert_("Sitemap" in pp['site_properties'].getProperty('types_not_searched'), |
---|
| 28 | 'No "Sitemap" added to types not searched on installation') |
---|
| 29 | # Test metaTypesNotToList |
---|
| 30 | self.assert_("Sitemap" in pp['navtree_properties'].getProperty('metaTypesNotToList'), |
---|
| 31 | 'No "Sitemap" added to types not to list on installation') |
---|
| 32 | |
---|
| 33 | # Test 'googlesitemap_properties' |
---|
| 34 | self.assert_('googlesitemap_properties' in pp.objectIds(), |
---|
| 35 | 'No "googlesitemap_properties" after installation') |
---|
| 36 | qsmprops = pp['googlesitemap_properties'] |
---|
| 37 | self.assert_(qsmprops.hasProperty('verification_filenames'), |
---|
| 38 | 'No "verification_filenames" property added on installation') |
---|
| 39 | |
---|
| 40 | def testSkins(self): |
---|
| 41 | ps = self.portal.portal_skins |
---|
[1619] | 42 | self.assert_('plonegooglesitemaps' in ps.objectIds(), |
---|
| 43 | 'No "plonegooglesitemaps" skin layer in portal_skins') |
---|
| 44 | self.assert_('plonegooglesitemaps' in ps.getSkinPath(ps.getDefaultSkin()), |
---|
| 45 | 'No "plonegooglesitemaps" skin layer in default skin') |
---|
[1593] | 46 | |
---|
| 47 | def testConfiglet(self): |
---|
| 48 | cp = self.portal.portal_controlpanel |
---|
[1619] | 49 | self.assert_([1 for ai in cp.listActionInfos() if ai['id']=='GoogleSitemaps'], |
---|
| 50 | 'No "GoogleSitemaps" configlet added to plone control panel') |
---|
[1593] | 51 | |
---|
| 52 | |
---|
[1619] | 53 | class TestSitemapType(FunctionalTestCase): |
---|
[1593] | 54 | |
---|
| 55 | def afterSetUp(self): |
---|
[1619] | 56 | super(TestSitemapType, self).afterSetUp() |
---|
[1593] | 57 | self.auth = 'admin:admin' |
---|
| 58 | self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
| 59 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
| 60 | |
---|
| 61 | def testFields(self): |
---|
| 62 | field_ids = map(lambda x:x.getName(), self.contentSM.Schema().fields()) |
---|
| 63 | # test old Sitemap settings fields |
---|
| 64 | self.assert_('id' in field_ids) |
---|
| 65 | self.assert_('portalTypes' in field_ids) |
---|
| 66 | self.assert_('states' in field_ids) |
---|
| 67 | self.assert_('blackout_list' in field_ids) |
---|
| 68 | self.assert_('urls' in field_ids) |
---|
| 69 | self.assert_('pingTransitions' in field_ids) |
---|
| 70 | # test new sitemap type field |
---|
| 71 | self.assert_('sitemapType' in field_ids) |
---|
| 72 | |
---|
| 73 | def testSitemapTypes(self): |
---|
| 74 | sitemap_types = self.contentSM.getField('sitemapType').Vocabulary().keys() |
---|
| 75 | self.assert_('content' in sitemap_types) |
---|
| 76 | self.assert_('mobile' in sitemap_types) |
---|
| 77 | self.assert_('news' in sitemap_types) |
---|
| 78 | |
---|
| 79 | def testAutoSetLayout(self): |
---|
| 80 | response = self.publish('/%s/createObject?type_name=Sitemap' % \ |
---|
| 81 | self.portal.absolute_url(1), basic=self.auth) |
---|
| 82 | location = response.getHeader('location') |
---|
| 83 | newurl = location[location.find('/'+self.portal.absolute_url(1)):] |
---|
| 84 | |
---|
| 85 | msm_id = 'mobile_sitemap' |
---|
| 86 | form = {'id': msm_id, |
---|
| 87 | 'sitemapType':'mobile', |
---|
| 88 | 'portalTypes':['Document',], |
---|
| 89 | 'states':['published'], |
---|
| 90 | 'form_submit':'Save', |
---|
| 91 | 'form.submitted':1, |
---|
| 92 | } |
---|
| 93 | post_data = StringIO(urlencode(form)) |
---|
| 94 | response = self.publish(newurl, request_method='POST', stdin=post_data, basic=self.auth) |
---|
| 95 | msitemap = getattr(self.portal, msm_id) |
---|
| 96 | |
---|
| 97 | self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml') |
---|
| 98 | |
---|
| 99 | def txestPingSetting(self): |
---|
| 100 | pwf = self.portal.portal_workflow['plone_workflow'] |
---|
| 101 | self.assertEqual(self.contentSM.getPingTransitions(), ()) |
---|
| 102 | |
---|
| 103 | self.contentSM.setPingTransitions(('plone_workflow#publish',)) |
---|
| 104 | self.assertEqual(self.contentSM.getPingTransitions(), ('plone_workflow#publish',)) |
---|
| 105 | self.assert_(ping_googlesitemap in pwf.scripts.keys(),"Not add wf script") |
---|
| 106 | |
---|
| 107 | |
---|
[1619] | 108 | class TestGoogleSitemaps(FunctionalTestCase): |
---|
[1593] | 109 | |
---|
| 110 | def afterSetUp(self): |
---|
[1619] | 111 | super(TestGoogleSitemaps, self).afterSetUp() |
---|
[1593] | 112 | |
---|
| 113 | self.workflow = self.portal.portal_workflow |
---|
| 114 | self.auth = 'admin:admin' |
---|
| 115 | _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
| 116 | self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps' |
---|
| 117 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
| 118 | self.gsm_props = self.portal.portal_properties['googlesitemap_properties'] |
---|
| 119 | |
---|
| 120 | # Add testing document to portal |
---|
| 121 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
| 122 | self.my_doc = self.portal['my_doc'] |
---|
| 123 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | def testSitemap(self): |
---|
| 127 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 128 | parsed_sitemap = parse(sitemap) |
---|
| 129 | start = parsed_sitemap['start'] |
---|
| 130 | data = parsed_sitemap['data'] |
---|
| 131 | self.assert_('urlset' in start.keys()) |
---|
[1621] | 132 | self.assertFalse(self.my_doc.absolute_url(0) in data, |
---|
| 133 | 'Wrong content present in the sitemap') |
---|
[1593] | 134 | |
---|
| 135 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 136 | |
---|
| 137 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 138 | parsed_sitemap = parse(sitemap) |
---|
| 139 | start = parsed_sitemap['start'] |
---|
| 140 | data = parsed_sitemap['data'] |
---|
| 141 | self.assertEqual(len(start.keys()), 4) |
---|
| 142 | self.assert_('urlset' in start.keys()) |
---|
| 143 | self.assert_('url' in start.keys()) |
---|
| 144 | self.assert_('loc' in start.keys()) |
---|
| 145 | self.assert_('lastmod' in start.keys()) |
---|
| 146 | |
---|
[1621] | 147 | self.assertTrue(self.my_doc.absolute_url(0) in data, 'Incorect url') |
---|
[1593] | 148 | |
---|
| 149 | def testVerificationFileCreation(self): |
---|
| 150 | self.portal.gsm_create_verify_file('verif_file') |
---|
| 151 | |
---|
| 152 | vf_created = hasattr(self.portal, 'verif_file') |
---|
| 153 | self.assert_(vf_created, 'Verification file not created') |
---|
| 154 | |
---|
| 155 | def testVerificationForm(self): |
---|
| 156 | verifyConfigUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_verification' |
---|
| 157 | verif_config = self.publish(verifyConfigUrl, self.auth).getBody() |
---|
| 158 | rexp_input_acitve = re.compile('<input\s+name="verify_filename"\s+([^>]*)>', re.I|re.S) |
---|
| 159 | rexp_button_acitve = re.compile('<input\s+name="form.button.CreateFile"\s+([^>]*)>', re.I|re.S) |
---|
| 160 | rexp_delete_button = re.compile('<input\s+name="form.button.DeleteFile"\s+[^>]*>', re.I|re.S) |
---|
| 161 | |
---|
| 162 | input_acitve = rexp_input_acitve.search(verif_config) |
---|
| 163 | button_acitve = rexp_button_acitve.search(verif_config) |
---|
| 164 | delete_button = rexp_delete_button.match(verif_config) |
---|
| 165 | |
---|
| 166 | self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1)) |
---|
| 167 | self.assert_(button_acitve and not 'disabled' in button_acitve.groups(1)) |
---|
| 168 | self.assert_(not delete_button) |
---|
| 169 | |
---|
| 170 | self.portal.gsm_create_verify_file('verif_file') |
---|
| 171 | |
---|
| 172 | input_acitve = rexp_input_acitve.search(verif_config) |
---|
| 173 | button_acitve = rexp_button_acitve.search(verif_config) |
---|
| 174 | delete_button = rexp_delete_button.match(verif_config) |
---|
| 175 | |
---|
| 176 | verif_config = self.publish(verifyConfigUrl, self.auth).getBody() |
---|
| 177 | self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1)) |
---|
| 178 | self.assert_(not delete_button) |
---|
| 179 | |
---|
| 180 | def testMultiplyVerificationFiles(self): |
---|
| 181 | verifyConfigUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_verification' |
---|
| 182 | |
---|
| 183 | form = {'verify_filename':'verif_file_1', |
---|
| 184 | 'form.button.CreateFile': 'Create verification file', |
---|
| 185 | 'form.submitted':1} |
---|
| 186 | post_data = StringIO(urlencode(form)) |
---|
| 187 | response = self.publish(verifyConfigUrl, request_method='POST', |
---|
| 188 | stdin=post_data, basic=self.auth) |
---|
| 189 | self.assertEqual(response.getStatus(), 200) |
---|
| 190 | self.assert_('verif_file_1' in self.gsm_props.getProperty('verification_filenames',[]), |
---|
| 191 | self.gsm_props.getProperty('verification_filenames',[])) |
---|
| 192 | |
---|
| 193 | form = {'verify_filename':'verif_file_2', |
---|
| 194 | 'form.button.CreateFile': 'Create verification file', |
---|
| 195 | 'form.submitted':1} |
---|
| 196 | post_data = StringIO(urlencode(form)) |
---|
| 197 | response = self.publish(verifyConfigUrl, request_method='POST', |
---|
| 198 | stdin=post_data, basic=self.auth) |
---|
| 199 | self.assertEqual(response.getStatus(), 200) |
---|
| 200 | self.assert_([1 for vf in ['verif_file','verif_file_2'] \ |
---|
| 201 | if vf in self.gsm_props.getProperty('verification_filenames',[])], |
---|
| 202 | self.gsm_props.getProperty('verification_filenames',[])) |
---|
| 203 | |
---|
| 204 | |
---|
[1619] | 205 | class TestSettings(FunctionalTestCase): |
---|
[1593] | 206 | |
---|
| 207 | def afterSetUp(self): |
---|
[1619] | 208 | super(TestSettings, self).afterSetUp() |
---|
[1593] | 209 | |
---|
| 210 | self.workflow = self.portal.portal_workflow |
---|
| 211 | self.gsm_props = self.portal.portal_properties['googlesitemap_properties'] |
---|
| 212 | self.auth = 'admin:admin' |
---|
| 213 | self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
| 214 | |
---|
| 215 | self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps' |
---|
| 216 | |
---|
| 217 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
| 218 | |
---|
| 219 | # Add testing document to portal |
---|
| 220 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
| 221 | self.my_doc = self.portal['my_doc'] |
---|
| 222 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
| 223 | self.my_doc_url = self.my_doc.absolute_url() |
---|
| 224 | |
---|
| 225 | def testMetaTypeToDig(self): |
---|
| 226 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 227 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 228 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
| 229 | |
---|
| 230 | self.contentSM.setPortalTypes([]) |
---|
| 231 | |
---|
| 232 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 233 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
| 234 | |
---|
| 235 | self.contentSM.setPortalTypes(['Document']) |
---|
| 236 | |
---|
| 237 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 238 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
| 239 | |
---|
| 240 | def testStates(self): |
---|
| 241 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 242 | self.contentSM.setStates(['visible']) |
---|
| 243 | |
---|
| 244 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 245 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
| 246 | |
---|
| 247 | self.contentSM.setStates(['published']) |
---|
| 248 | |
---|
| 249 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 250 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
| 251 | |
---|
| 252 | def test_blackout_entries(self): |
---|
| 253 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 254 | self.contentSM.setBlackout_list((self.my_doc.getId(),)) |
---|
| 255 | |
---|
| 256 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 257 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
---|
| 258 | |
---|
| 259 | self.contentSM.setBlackout_list([]) |
---|
| 260 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 261 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
---|
| 262 | |
---|
| 263 | def test_regexp(self): |
---|
| 264 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 265 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 266 | self.assert_(not hasURL(sitemap, self.portal.absolute_url())) |
---|
| 267 | |
---|
| 268 | regexp = "s/\/%s//"%self.my_doc.getId() |
---|
| 269 | self.contentSM.setReg_exp([regexp]) |
---|
| 270 | |
---|
| 271 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 272 | self.assert_(hasURL(sitemap, self.portal.absolute_url())) |
---|
| 273 | |
---|
| 274 | def test_add_urls(self): |
---|
| 275 | self.contentSM.setUrls(['http://w1', 'w2', '/w3']) |
---|
| 276 | w1_url = 'http://w1' |
---|
| 277 | w2_url = self.portal.absolute_url() + '/w2' |
---|
| 278 | w3_url = self.portal.getPhysicalRoot().absolute_url() + '/w3' |
---|
| 279 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
| 280 | |
---|
| 281 | self.assert_(hasURL(sitemap, w1_url)) |
---|
| 282 | self.assert_(hasURL(sitemap, w2_url)) |
---|
| 283 | self.assert_(hasURL(sitemap, w3_url)) |
---|
| 284 | |
---|
| 285 | |
---|
[1619] | 286 | class TestPinging(FunctionalTestCase): |
---|
[1593] | 287 | |
---|
| 288 | def afterSetUp(self): |
---|
[1619] | 289 | super(TestPinging, self).afterSetUp() |
---|
[1593] | 290 | |
---|
| 291 | self.workflow = self.portal.portal_workflow |
---|
[1622] | 292 | self.workflow.setChainForPortalTypes(pt_names=('News Item','Document'), |
---|
| 293 | chain="simple_publication_workflow") |
---|
[1593] | 294 | self.gsm_props = self.portal.portal_properties['googlesitemap_properties'] |
---|
| 295 | self.auth = 'admin:admin' |
---|
| 296 | self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), []) |
---|
| 297 | # Add sitemaps |
---|
| 298 | self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
[1622] | 299 | self.contentSM.setPingTransitions(('simple_publication_workflow#publish',)) |
---|
[1593] | 300 | self.newsSM = _createObjectByType('Sitemap', self.portal, id='news-sitemaps') |
---|
| 301 | self.newsSM.setPortalTypes(('News Item','Document')) |
---|
[1622] | 302 | self.newsSM.setPingTransitions(('simple_publication_workflow#publish',)) |
---|
[1593] | 303 | self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps' |
---|
| 304 | # Add testing document to portal |
---|
| 305 | my_doc = self.portal.invokeFactory('Document', id='my_doc') |
---|
| 306 | self.my_doc = self.portal['my_doc'] |
---|
| 307 | my_news = self.portal.invokeFactory('News Item', id='my_news') |
---|
| 308 | self.my_news = self.portal['my_news'] |
---|
| 309 | |
---|
| 310 | def testAutomatePinging(self): |
---|
| 311 | # 1. Check for pinging both sitemaps |
---|
| 312 | back_out, myout = sys.stdout, StringIO() |
---|
| 313 | sys.stdout = myout |
---|
[1622] | 314 | try: |
---|
| 315 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
| 316 | myout.seek(0) |
---|
| 317 | data = myout.read() |
---|
| 318 | finally: |
---|
| 319 | sys.stdout = back_out |
---|
| 320 | |
---|
| 321 | self.assert_('Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data, |
---|
[1593] | 322 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
---|
[1622] | 323 | self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data, |
---|
[1593] | 324 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
[1623] | 325 | |
---|
[1593] | 326 | # 2. Check for pinging only news-sitemap sitemaps |
---|
| 327 | back_out, myout = sys.stdout, StringIO() |
---|
| 328 | sys.stdout = myout |
---|
[1622] | 329 | try: |
---|
| 330 | self.workflow.doActionFor(self.my_news, 'publish') |
---|
| 331 | myout.seek(0) |
---|
| 332 | data = myout.read() |
---|
| 333 | finally: |
---|
| 334 | sys.stdout = back_out |
---|
[1593] | 335 | |
---|
[1622] | 336 | self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data, |
---|
[1593] | 337 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
[1622] | 338 | self.assert_(not 'Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data, |
---|
[1593] | 339 | "Pinged %s on news: '%s'" % (self.contentSM.id, data)) |
---|
| 340 | |
---|
| 341 | def testPingingWithSetupForm(self): |
---|
| 342 | # Ping news and content sitemaps |
---|
| 343 | formUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_settings' |
---|
| 344 | qs = 'smselected:list=%s&smselected:list=%s&form.button.Ping=1&form.submitted=1' % \ |
---|
| 345 | (self.contentSM.id, self.newsSM.id) |
---|
| 346 | |
---|
| 347 | back_out, myout = sys.stdout, StringIO() |
---|
| 348 | sys.stdout = myout |
---|
[1622] | 349 | try: |
---|
| 350 | response = self.publish("%s?%s" % (formUrl, qs), basic=self.auth) |
---|
| 351 | myout.seek(0) |
---|
| 352 | data = myout.read() |
---|
| 353 | finally: |
---|
| 354 | sys.stdout = back_out |
---|
[1593] | 355 | |
---|
[1622] | 356 | self.assert_('Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data, |
---|
[1593] | 357 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
---|
[1622] | 358 | self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data, |
---|
[1593] | 359 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | def test_suite(): |
---|
| 364 | from unittest import TestSuite, makeSuite |
---|
| 365 | suite = TestSuite() |
---|
[1619] | 366 | suite.addTest(makeSuite(TestGoogleSitemapsInstallation)) |
---|
[1593] | 367 | suite.addTest(makeSuite(TestSitemapType)) |
---|
[1619] | 368 | suite.addTest(makeSuite(TestGoogleSitemaps)) |
---|
[1593] | 369 | suite.addTest(makeSuite(TestSettings)) |
---|
| 370 | suite.addTest(makeSuite(TestPinging)) |
---|
| 371 | return suite |
---|
| 372 | |
---|
| 373 | if __name__ == '__main__': |
---|
[1619] | 374 | unittest.main(defaultTest='test_suite') |
---|
| 375 | # framework() |
---|