| 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" % str(value)) |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | class TestSettings(FunctionalTestCase): |
|---|
| 102 | |
|---|
| 103 | def afterSetUp(self): |
|---|
| 104 | super(TestSettings, self).afterSetUp() |
|---|
| 105 | gsm_properties = 'googlesitemap_properties' |
|---|
| 106 | self.gsm_props = self.portal.portal_properties[gsm_properties] |
|---|
| 107 | self.contentSM = _createObjectByType('Sitemap', self.portal, |
|---|
| 108 | id='google-sitemaps') |
|---|
| 109 | self.sitemapUrl = '/' + self.portal.absolute_url(1) + \ |
|---|
| 110 | '/google-sitemaps' |
|---|
| 111 | # Add testing document to portal |
|---|
| 112 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
|---|
| 113 | self.my_doc.edit(text_format='plain', text='hello world') |
|---|
| 114 | self.my_doc_url = self.my_doc.absolute_url() |
|---|
| 115 | |
|---|
| 116 | def testMetaTypeToDig(self): |
|---|
| 117 | self.workflow.doActionFor(self.my_doc, 'publish') |
|---|
| 118 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 119 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
|---|
| 120 | |
|---|
| 121 | self.contentSM.setPortalTypes([]) |
|---|
| 122 | |
|---|
| 123 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 124 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
|---|
| 125 | |
|---|
| 126 | self.contentSM.setPortalTypes(['Document']) |
|---|
| 127 | |
|---|
| 128 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 129 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
|---|
| 130 | |
|---|
| 131 | def testStates(self): |
|---|
| 132 | self.workflow.doActionFor(self.my_doc, 'publish') |
|---|
| 133 | self.contentSM.setStates(['visible']) |
|---|
| 134 | |
|---|
| 135 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 136 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
|---|
| 137 | |
|---|
| 138 | self.contentSM.setStates(['published']) |
|---|
| 139 | |
|---|
| 140 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 141 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
|---|
| 142 | |
|---|
| 143 | def test_blackout_entries(self): |
|---|
| 144 | self.workflow.doActionFor(self.my_doc, 'publish') |
|---|
| 145 | self.contentSM.setBlackout_list((self.my_doc.getId(),)) |
|---|
| 146 | |
|---|
| 147 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 148 | self.assert_(not hasURL(sitemap, self.my_doc_url)) |
|---|
| 149 | |
|---|
| 150 | self.contentSM.setBlackout_list([]) |
|---|
| 151 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 152 | self.assert_(hasURL(sitemap, self.my_doc_url)) |
|---|
| 153 | |
|---|
| 154 | def test_regexp(self): |
|---|
| 155 | self.workflow.doActionFor(self.my_doc, 'publish') |
|---|
| 156 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 157 | self.assert_(not hasURL(sitemap, self.portal.absolute_url())) |
|---|
| 158 | |
|---|
| 159 | regexp = "s/\/%s//" % self.my_doc.getId() |
|---|
| 160 | self.contentSM.setReg_exp([regexp]) |
|---|
| 161 | |
|---|
| 162 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 163 | self.assert_(hasURL(sitemap, self.portal.absolute_url())) |
|---|
| 164 | |
|---|
| 165 | def test_add_urls(self): |
|---|
| 166 | self.contentSM.setUrls(['http://w1', 'w2', '/w3']) |
|---|
| 167 | w1_url = 'http://w1' |
|---|
| 168 | w2_url = self.portal.absolute_url() + '/w2' |
|---|
| 169 | w3_url = self.portal.getPhysicalRoot().absolute_url() + '/w3' |
|---|
| 170 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
|---|
| 171 | |
|---|
| 172 | self.assert_(hasURL(sitemap, w1_url)) |
|---|
| 173 | self.assert_(hasURL(sitemap, w2_url)) |
|---|
| 174 | self.assert_(hasURL(sitemap, w3_url)) |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | class TestPinging(FunctionalTestCase): |
|---|
| 178 | |
|---|
| 179 | def afterSetUp(self): |
|---|
| 180 | super(TestPinging, self).afterSetUp() |
|---|
| 181 | self.workflow.setChainForPortalTypes(pt_names=('News Item', |
|---|
| 182 | 'Document'), chain="simple_publication_workflow") |
|---|
| 183 | gsm_properties = 'googlesitemap_properties' |
|---|
| 184 | self.gsm_props = self.portal.portal_properties[gsm_properties] |
|---|
| 185 | # Add sitemaps |
|---|
| 186 | self.contentSM = _createObjectByType('Sitemap', self.portal, |
|---|
| 187 | id='google-sitemaps') |
|---|
| 188 | spw_publish = 'simple_publication_workflow#publish' |
|---|
| 189 | self.contentSM.setPingTransitions((spw_publish,)) |
|---|
| 190 | self.newsSM = _createObjectByType('Sitemap', self.portal, |
|---|
| 191 | id='news-sitemaps') |
|---|
| 192 | self.newsSM.setPortalTypes(('News Item', 'Document')) |
|---|
| 193 | self.newsSM.setPingTransitions((spw_publish,)) |
|---|
| 194 | self.sitemapUrl = '/' + self.portal.absolute_url(1) + \ |
|---|
| 195 | '/google-sitemaps' |
|---|
| 196 | # Add testing document to portal |
|---|
| 197 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
|---|
| 198 | self.my_news = _createObjectByType('News Item', self.portal, |
|---|
| 199 | id='my_news') |
|---|
| 200 | |
|---|
| 201 | def testAutomatePinging(self): |
|---|
| 202 | # 1. Check for pinging both sitemaps |
|---|
| 203 | back_out, myout = sys.stdout, StringIO() |
|---|
| 204 | sys.stdout = myout |
|---|
| 205 | try: |
|---|
| 206 | self.workflow.doActionFor(self.my_doc, 'publish') |
|---|
| 207 | myout.seek(0) |
|---|
| 208 | data = myout.read() |
|---|
| 209 | finally: |
|---|
| 210 | sys.stdout = back_out |
|---|
| 211 | |
|---|
| 212 | self.assert_('Pinged %s sitemap to Google' \ |
|---|
| 213 | % self.contentSM.absolute_url() in data, |
|---|
| 214 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
|---|
| 215 | self.assert_('Pinged %s sitemap to Google' \ |
|---|
| 216 | % self.newsSM.absolute_url() in data, |
|---|
| 217 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
|---|
| 218 | |
|---|
| 219 | # 2. Check for pinging only news-sitemap sitemaps |
|---|
| 220 | back_out, myout = sys.stdout, StringIO() |
|---|
| 221 | sys.stdout = myout |
|---|
| 222 | try: |
|---|
| 223 | self.workflow.doActionFor(self.my_news, 'publish') |
|---|
| 224 | myout.seek(0) |
|---|
| 225 | data = myout.read() |
|---|
| 226 | finally: |
|---|
| 227 | sys.stdout = back_out |
|---|
| 228 | |
|---|
| 229 | self.assert_('Pinged %s sitemap to Google' \ |
|---|
| 230 | % self.newsSM.absolute_url() in data, |
|---|
| 231 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
|---|
| 232 | self.assert_(not 'Pinged %s sitemap to Google' \ |
|---|
| 233 | % self.contentSM.absolute_url() in data, |
|---|
| 234 | "Pinged %s on news: '%s'" % (self.contentSM.id, data)) |
|---|
| 235 | |
|---|
| 236 | def testPingingWithSetupForm(self): |
|---|
| 237 | # Ping news and content sitemaps |
|---|
| 238 | formUrl = '/' + self.portal.absolute_url(1) + '/prefs_gsm_settings' |
|---|
| 239 | qs = 'smselected:list=%s&smselected:list=%s&form.button.Ping=1' \ |
|---|
| 240 | '&form.submitted=1' % (self.contentSM.id, self.newsSM.id) |
|---|
| 241 | |
|---|
| 242 | back_out, myout = sys.stdout, StringIO() |
|---|
| 243 | sys.stdout = myout |
|---|
| 244 | try: |
|---|
| 245 | self.publish("%s?%s" % (formUrl, qs), basic=self.auth) |
|---|
| 246 | myout.seek(0) |
|---|
| 247 | data = myout.read() |
|---|
| 248 | finally: |
|---|
| 249 | sys.stdout = back_out |
|---|
| 250 | |
|---|
| 251 | self.assert_('Pinged %s sitemap to Google' \ |
|---|
| 252 | % self.contentSM.absolute_url() in data, |
|---|
| 253 | "Not pinged %s: '%s'" % (self.contentSM.id, data)) |
|---|
| 254 | self.assert_('Pinged %s sitemap to Google' \ |
|---|
| 255 | % self.newsSM.absolute_url() in data, |
|---|
| 256 | "Not pinged %s: '%s'" % (self.newsSM.id, data)) |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | class TestContextSearch(TestCase): |
|---|
| 260 | """ Test if sitemaps collect objects from the container, |
|---|
| 261 | where it added to, but not from the portal root. |
|---|
| 262 | """ |
|---|
| 263 | def prepareTestContent(self, smtype, ptypes, ifaces=()): |
|---|
| 264 | # Create test folder |
|---|
| 265 | tfolder = _createObjectByType("Folder", self.portal, id="test-folder") |
|---|
| 266 | # Add SiteMap in the test folder |
|---|
| 267 | self.sm = _createObjectByType("Sitemap", tfolder, id='sitemap', |
|---|
| 268 | sitemapType=smtype, portalTypes=ptypes) |
|---|
| 269 | self.sm.at_post_create_script() |
|---|
| 270 | # Add content in root and in the test folder |
|---|
| 271 | pubdate = (DateTime() + 1).strftime("%Y-%m-%d") |
|---|
| 272 | root_content = _createObjectByType(ptypes[0], self.portal, |
|---|
| 273 | id='root-content') |
|---|
| 274 | inner_content = _createObjectByType(ptypes[0], tfolder, |
|---|
| 275 | id='inner-content') |
|---|
| 276 | for obj in (root_content, inner_content): |
|---|
| 277 | self.workflow.doActionFor(obj, 'publish') |
|---|
| 278 | if ifaces: |
|---|
| 279 | alsoProvides(obj, ifaces) |
|---|
| 280 | obj.edit(effectiveDate=pubdate) # this also reindex object |
|---|
| 281 | self.inner_path = '/'.join(inner_content.getPhysicalPath()) |
|---|
| 282 | |
|---|
| 283 | def testGoogleSitemap(self): |
|---|
| 284 | self.prepareTestContent("content", ("Document",)) |
|---|
| 285 | filtered = SitemapView(self.sm, TestRequest()).getFilteredObjects() |
|---|
| 286 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
|---|
| 287 | [self.inner_path, ]) |
|---|
| 288 | |
|---|
| 289 | def testNewsSitemap(self): |
|---|
| 290 | self.prepareTestContent("news", ("News Item",)) |
|---|
| 291 | filtered = NewsSitemapView(self.sm, TestRequest()).getFilteredObjects() |
|---|
| 292 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
|---|
| 293 | [self.inner_path, ]) |
|---|
| 294 | |
|---|
| 295 | def testMobileSitemap(self): |
|---|
| 296 | self.patchMobile() |
|---|
| 297 | self.prepareTestContent("content", ("Document",), (IMobileMarker,)) |
|---|
| 298 | filtered = MobileSitemapView(self.sm, |
|---|
| 299 | TestRequest()).getFilteredObjects() |
|---|
| 300 | self.assertEqual(map(lambda x: x.getPath(), filtered), |
|---|
| 301 | [self.inner_path, ]) |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | def test_suite(): |
|---|
| 305 | suite = unittest.TestSuite() |
|---|
| 306 | suite.addTest(unittest.makeSuite(TestSitemapType)) |
|---|
| 307 | suite.addTest(unittest.makeSuite(TestSettings)) |
|---|
| 308 | suite.addTest(unittest.makeSuite(TestPinging)) |
|---|
| 309 | suite.addTest(unittest.makeSuite(TestContextSearch)) |
|---|
| 310 | return suite |
|---|
| 311 | |
|---|
| 312 | if __name__ == '__main__': |
|---|
| 313 | unittest.main(defaultTest='test_suite') |
|---|
| 314 | # framework() |
|---|