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

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

corect ping setting tests

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