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