source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testSitemaps.py @ 3167

Last change on this file since 3167 was 3167, checked in by zidane, 13 years ago

corect pyflakes tests

  • Property svn:eol-style set to native
File size: 12.8 KB
RevLine 
[2527]1#
2# Tests related to general Sitemap type.
3#
[3163]4from quintagroup.plonegooglesitemaps.tests.base \
5    import FunctionalTestCase, TestCase, IMobileMarker
6from StringIO import StringIO
7from urllib import urlencode
8import sys
9from XMLParser import hasURL
10import unittest
11
[2741]12from DateTime import DateTime
13from zope.interface import alsoProvides
14from zope.publisher.browser import TestRequest
15
[2533]16from Products.Archetypes import atapi
[2527]17from Products.CMFPlone.utils import _createObjectByType
18
[2741]19from quintagroup.plonegooglesitemaps.browser.sitemapview import SitemapView
[3152]20from quintagroup.plonegooglesitemaps.browser.newssitemapview \
21    import NewsSitemapView
22from quintagroup.plonegooglesitemaps.browser.mobilesitemapview \
23    import MobileSitemapView
[2527]24
[2741]25
[2527]26class TestSitemapType(FunctionalTestCase):
27
28    def afterSetUp(self):
29        super(TestSitemapType, self).afterSetUp()
[3152]30        self.contentSM = _createObjectByType('Sitemap', self.portal,
31                                             id='google-sitemaps')
[2527]32
33    def testFields(self):
[3152]34        field_ids = map(lambda x: x.getName(),
35                        self.contentSM.Schema().fields())
[2527]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):
[3152]47        sm_vocabulary = self.contentSM.getField('sitemapType').Vocabulary()
48        sitemap_types = sm_vocabulary.keys()
[2527]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):
[3152]54        response = self.publish('/%s/createObject?type_name=Sitemap' \
55                                % self.portal.absolute_url(1), basic=self.auth)
[2527]56        location = response.getHeader('location')
[3152]57        newurl = location[location.find('/' + self.portal.absolute_url(1)):]
[2527]58
59        msm_id = 'mobile_sitemap'
60        form = {'id': msm_id,
[3152]61                'sitemapType': 'mobile',
62                'portalTypes': ['Document', ],
63                'states': ['published'],
64                'form_submit': 'Save',
65                'form.submitted': 1,
[2527]66                }
67        post_data = StringIO(urlencode(form))
[3152]68        response = self.publish(newurl, request_method='POST', stdin=post_data,
69                                basic=self.auth)
[2527]70        msitemap = getattr(self.portal, msm_id)
71
72        self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml')
73
[3166]74    def testPingSetting(self):
[2527]75        self.assertEqual(self.contentSM.getPingTransitions(), ())
76
77        self.contentSM.setPingTransitions(('plone_workflow#publish',))
[3152]78        self.assertEqual(self.contentSM.getPingTransitions(),
79                         ('plone_workflow#publish',))
[2527]80
[2533]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)
[2527]85
[3163]86    def testWorkflowTransitions(self):
[2533]87        wftrans = self.contentSM.getWorkflowTransitions()
88        self.assertEqual(isinstance(wftrans, atapi.DisplayList), True)
[3152]89        self.assertEqual("simple_publication_workflow#publish" in \
90                         wftrans.keys(), True)
[2533]91
[3002]92    def testSettingBlackout(self):
[3152]93        bolist = ["path:./el1  ", "   ", "", " id:index.html  ", "index_html"]
94        expect = ("path:./el1", "id:index.html", "index_html")
[3002]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))
[2533]99
[3002]100
[2527]101class TestSettings(FunctionalTestCase):
102
103    def afterSetUp(self):
104        super(TestSettings, self).afterSetUp()
[3152]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'
[2527]111        # Add testing document to portal
[2534]112        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
[2527]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
[3152]159        regexp = "s/\/%s//" % self.my_doc.getId()
[2527]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
[2530]177class TestPinging(FunctionalTestCase):
178
179    def afterSetUp(self):
180        super(TestPinging, self).afterSetUp()
[3152]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]
[2530]185        # Add sitemaps
[3152]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'
[2530]196        # Add testing document to portal
[2534]197        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
[3152]198        self.my_news = _createObjectByType('News Item', self.portal,
199                                           id='my_news')
[2530]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
[3152]212        self.assert_('Pinged %s sitemap to Google' \
213                     % self.contentSM.absolute_url() in data,
[2530]214                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
[3152]215        self.assert_('Pinged %s sitemap to Google' \
216                     % self.newsSM.absolute_url() in data,
[2530]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
[3152]229        self.assert_('Pinged %s sitemap to Google' \
230                     % self.newsSM.absolute_url() in data,
[2530]231                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
[3152]232        self.assert_(not 'Pinged %s sitemap to Google' \
233                     % self.contentSM.absolute_url() in data,
[2530]234                     "Pinged %s on news: '%s'" % (self.contentSM.id, data))
235
236    def testPingingWithSetupForm(self):
237        # Ping news and content sitemaps
[3152]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)
[2530]241
242        back_out, myout = sys.stdout, StringIO()
243        sys.stdout = myout
244        try:
[3163]245            self.publish("%s?%s" % (formUrl, qs), basic=self.auth)
[2530]246            myout.seek(0)
247            data = myout.read()
248        finally:
249            sys.stdout = back_out
250
[3152]251        self.assert_('Pinged %s sitemap to Google' \
252                     % self.contentSM.absolute_url() in data,
[2530]253                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
[3152]254        self.assert_('Pinged %s sitemap to Google' \
255                     % self.newsSM.absolute_url() in data,
[2530]256                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
257
258
[2741]259class 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
[3152]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')
[2741]276        for obj in (root_content, inner_content):
277            self.workflow.doActionFor(obj, 'publish')
278            if ifaces:
279                alsoProvides(obj, ifaces)
[3152]280            obj.edit(effectiveDate=pubdate)  # this also reindex object
[2741]281        self.inner_path = '/'.join(inner_content.getPhysicalPath())
[3152]282
[2741]283    def testGoogleSitemap(self):
284        self.prepareTestContent("content", ("Document",))
285        filtered = SitemapView(self.sm, TestRequest()).getFilteredObjects()
[3152]286        self.assertEqual(map(lambda x: x.getPath(), filtered),
287                        [self.inner_path, ])
[2530]288
[2741]289    def testNewsSitemap(self):
290        self.prepareTestContent("news", ("News Item",))
291        filtered = NewsSitemapView(self.sm, TestRequest()).getFilteredObjects()
[3152]292        self.assertEqual(map(lambda x: x.getPath(), filtered),
293                         [self.inner_path, ])
[2741]294
295    def testMobileSitemap(self):
296        self.patchMobile()
297        self.prepareTestContent("content", ("Document",), (IMobileMarker,))
[3152]298        filtered = MobileSitemapView(self.sm,
299                                     TestRequest()).getFilteredObjects()
300        self.assertEqual(map(lambda x: x.getPath(), filtered),
301                         [self.inner_path, ])
[2741]302
303
[2527]304def test_suite():
[3163]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))
[2527]310    return suite
311
312if __name__ == '__main__':
313    unittest.main(defaultTest='test_suite')
314#    framework()
Note: See TracBrowser for help on using the repository browser.