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

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

fixes pyflakes and pylint

  • Property svn:eol-style set to native
File size: 13.0 KB
Line 
1#
2# Tests related to general Sitemap type.
3#
4from quintagroup.plonegooglesitemaps.tests.base \
5    import FunctionalTestCase, TestCase, IMobileMarker
6from quintagroup.plonegooglesitemaps.config import ping_googlesitemap
7from StringIO import StringIO
8from urllib import urlencode
9import sys
10from XMLParser import hasURL
11import unittest
12
13from DateTime import DateTime
14from zope.interface import alsoProvides
15from zope.publisher.browser import TestRequest
16
17from Products.Archetypes import atapi
18from Products.CMFPlone.utils import _createObjectByType
19
20from quintagroup.plonegooglesitemaps.browser.sitemapview import SitemapView
21from quintagroup.plonegooglesitemaps.browser.newssitemapview \
22    import NewsSitemapView
23from quintagroup.plonegooglesitemaps.browser.mobilesitemapview \
24    import MobileSitemapView
25
26
27class TestSitemapType(FunctionalTestCase):
28
29    def afterSetUp(self):
30        super(TestSitemapType, self).afterSetUp()
31        self.contentSM = _createObjectByType('Sitemap', self.portal,
32                                             id='google-sitemaps')
33
34    def testFields(self):
35        field_ids = map(lambda x: x.getName(),
36                        self.contentSM.Schema().fields())
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):
48        sm_vocabulary = self.contentSM.getField('sitemapType').Vocabulary()
49        sitemap_types = sm_vocabulary.keys()
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):
55        response = self.publish('/%s/createObject?type_name=Sitemap' \
56                                % self.portal.absolute_url(1), basic=self.auth)
57        location = response.getHeader('location')
58        newurl = location[location.find('/' + self.portal.absolute_url(1)):]
59
60        msm_id = 'mobile_sitemap'
61        form = {'id': msm_id,
62                'sitemapType': 'mobile',
63                'portalTypes': ['Document', ],
64                'states': ['published'],
65                'form_submit': 'Save',
66                'form.submitted': 1,
67                }
68        post_data = StringIO(urlencode(form))
69        response = self.publish(newurl, request_method='POST', stdin=post_data,
70                                basic=self.auth)
71        msitemap = getattr(self.portal, msm_id)
72
73        self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml')
74
75    def txestPingSetting(self):
76        pwf = self.workflow['plone_workflow']
77        self.assertEqual(self.contentSM.getPingTransitions(), ())
78
79        self.contentSM.setPingTransitions(('plone_workflow#publish',))
80        self.assertEqual(self.contentSM.getPingTransitions(),
81                         ('plone_workflow#publish',))
82        self.assert_(ping_googlesitemap in pwf.scripts.keys(),
83                     "Not add wf script")
84
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)
89
90    def testWorkflowTransitions(self):
91        wftrans = self.contentSM.getWorkflowTransitions()
92        self.assertEqual(isinstance(wftrans, atapi.DisplayList), True)
93        self.assertEqual("simple_publication_workflow#publish" in \
94                         wftrans.keys(), True)
95
96    def testSettingBlackout(self):
97        bolist = ["path:./el1  ", "   ", "", " id:index.html  ", "index_html"]
98        expect = ("path:./el1", "id:index.html", "index_html")
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))
103
104
105class TestSettings(FunctionalTestCase):
106
107    def afterSetUp(self):
108        super(TestSettings, self).afterSetUp()
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'
115        # Add testing document to portal
116        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
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
163        regexp = "s/\/%s//" % self.my_doc.getId()
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
181class TestPinging(FunctionalTestCase):
182
183    def afterSetUp(self):
184        super(TestPinging, self).afterSetUp()
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]
189        # Add sitemaps
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'
200        # Add testing document to portal
201        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
202        self.my_news = _createObjectByType('News Item', self.portal,
203                                           id='my_news')
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
216        self.assert_('Pinged %s sitemap to Google' \
217                     % self.contentSM.absolute_url() in data,
218                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
219        self.assert_('Pinged %s sitemap to Google' \
220                     % self.newsSM.absolute_url() in data,
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
233        self.assert_('Pinged %s sitemap to Google' \
234                     % self.newsSM.absolute_url() in data,
235                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
236        self.assert_(not 'Pinged %s sitemap to Google' \
237                     % self.contentSM.absolute_url() in data,
238                     "Pinged %s on news: '%s'" % (self.contentSM.id, data))
239
240    def testPingingWithSetupForm(self):
241        # Ping news and content sitemaps
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)
245
246        back_out, myout = sys.stdout, StringIO()
247        sys.stdout = myout
248        try:
249            self.publish("%s?%s" % (formUrl, qs), basic=self.auth)
250            myout.seek(0)
251            data = myout.read()
252        finally:
253            sys.stdout = back_out
254
255        self.assert_('Pinged %s sitemap to Google' \
256                     % self.contentSM.absolute_url() in data,
257                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
258        self.assert_('Pinged %s sitemap to Google' \
259                     % self.newsSM.absolute_url() in data,
260                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
261
262
263class 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
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')
280        for obj in (root_content, inner_content):
281            self.workflow.doActionFor(obj, 'publish')
282            if ifaces:
283                alsoProvides(obj, ifaces)
284            obj.edit(effectiveDate=pubdate)  # this also reindex object
285        self.inner_path = '/'.join(inner_content.getPhysicalPath())
286
287    def testGoogleSitemap(self):
288        self.prepareTestContent("content", ("Document",))
289        filtered = SitemapView(self.sm, TestRequest()).getFilteredObjects()
290        self.assertEqual(map(lambda x: x.getPath(), filtered),
291                        [self.inner_path, ])
292
293    def testNewsSitemap(self):
294        self.prepareTestContent("news", ("News Item",))
295        filtered = NewsSitemapView(self.sm, TestRequest()).getFilteredObjects()
296        self.assertEqual(map(lambda x: x.getPath(), filtered),
297                         [self.inner_path, ])
298
299    def testMobileSitemap(self):
300        self.patchMobile()
301        self.prepareTestContent("content", ("Document",), (IMobileMarker,))
302        filtered = MobileSitemapView(self.sm,
303                                     TestRequest()).getFilteredObjects()
304        self.assertEqual(map(lambda x: x.getPath(), filtered),
305                         [self.inner_path, ])
306
307
308def test_suite():
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))
314    return suite
315
316if __name__ == '__main__':
317    unittest.main(defaultTest='test_suite')
318#    framework()
Note: See TracBrowser for help on using the repository browser.