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

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

fixes pep8

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