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