source: products/quintagroup.plonegooglesitemaps/branches/test_refactoring/quintagroup/plonegooglesitemaps/tests/testSitemaps.py @ 2535

Last change on this file since 2535 was 2534, checked in by mylan, 14 years ago

#206: some cleanup, simplify tests

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1#
2# Tests related to general Sitemap type.
3#
4from base import *
5from Products.Archetypes import atapi
6from Products.CMFPlone.utils import _createObjectByType
7
8
9class TestSitemapType(FunctionalTestCase):
10
11    def afterSetUp(self):
12        super(TestSitemapType, self).afterSetUp()
13        self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
14
15    def testFields(self):
16        field_ids = map(lambda x:x.getName(), self.contentSM.Schema().fields())
17        # test old Sitemap settings fields
18        self.assert_('id' in field_ids)
19        self.assert_('portalTypes' in field_ids)
20        self.assert_('states' in field_ids)
21        self.assert_('blackout_list' in field_ids)
22        self.assert_('urls' in field_ids)
23        self.assert_('pingTransitions' in field_ids)
24        # test new sitemap type field
25        self.assert_('sitemapType' in field_ids)
26
27    def testSitemapTypes(self):
28        sitemap_types = self.contentSM.getField('sitemapType').Vocabulary().keys()
29        self.assert_('content' in sitemap_types)
30        self.assert_('mobile' in sitemap_types)
31        self.assert_('news' in sitemap_types)
32
33    def testAutoSetLayout(self):
34        response = self.publish('/%s/createObject?type_name=Sitemap' % \
35                                self.portal.absolute_url(1), basic=self.auth)
36        location = response.getHeader('location')
37        newurl = location[location.find('/'+self.portal.absolute_url(1)):]
38
39        msm_id = 'mobile_sitemap'
40        form = {'id': msm_id,
41                'sitemapType':'mobile',
42                'portalTypes':['Document',],
43                'states':['published'],
44                'form_submit':'Save',
45                'form.submitted':1,
46                }
47        post_data = StringIO(urlencode(form))
48        response = self.publish(newurl, request_method='POST', stdin=post_data, basic=self.auth)
49        msitemap = getattr(self.portal, msm_id)
50
51        self.assertEqual(msitemap.defaultView(), 'mobile-sitemap.xml')
52
53    def txestPingSetting(self):
54        pwf = self.workflow['plone_workflow']
55        self.assertEqual(self.contentSM.getPingTransitions(), ())
56
57        self.contentSM.setPingTransitions(('plone_workflow#publish',))
58        self.assertEqual(self.contentSM.getPingTransitions(), ('plone_workflow#publish',))
59        self.assert_(ping_googlesitemap in pwf.scripts.keys(),"Not add wf script")
60
61    def testWorkflowStates(self):
62        wfstates = self.contentSM.getWorkflowStates()
63        self.assertEqual(isinstance(wfstates, atapi.DisplayList), True)
64        self.assertEqual("published" in wfstates.keys(), True)
65
66    def testWorkflowStates(self):
67        wftrans = self.contentSM.getWorkflowTransitions()
68        self.assertEqual(isinstance(wftrans, atapi.DisplayList), True)
69        self.assertEqual("simple_publication_workflow#publish" in wftrans.keys(), True)
70
71
72class TestSettings(FunctionalTestCase):
73
74    def afterSetUp(self):
75        super(TestSettings, self).afterSetUp()
76        self.gsm_props = self.portal.portal_properties['googlesitemap_properties']
77        self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
78        self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps'
79        # Add testing document to portal
80        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
81        self.my_doc.edit(text_format='plain', text='hello world')
82        self.my_doc_url = self.my_doc.absolute_url()
83
84    def testMetaTypeToDig(self):
85        self.workflow.doActionFor(self.my_doc, 'publish')
86        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
87        self.assert_(hasURL(sitemap, self.my_doc_url))
88
89        self.contentSM.setPortalTypes([])
90
91        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
92        self.assert_(not hasURL(sitemap, self.my_doc_url))
93
94        self.contentSM.setPortalTypes(['Document'])
95
96        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
97        self.assert_(hasURL(sitemap, self.my_doc_url))
98
99    def testStates(self):
100        self.workflow.doActionFor(self.my_doc, 'publish')
101        self.contentSM.setStates(['visible'])
102
103        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
104        self.assert_(not hasURL(sitemap, self.my_doc_url))
105
106        self.contentSM.setStates(['published'])
107
108        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
109        self.assert_(hasURL(sitemap, self.my_doc_url))
110
111    def test_blackout_entries(self):
112        self.workflow.doActionFor(self.my_doc, 'publish')
113        self.contentSM.setBlackout_list((self.my_doc.getId(),))
114
115        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
116        self.assert_(not hasURL(sitemap, self.my_doc_url))
117
118        self.contentSM.setBlackout_list([])
119        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
120        self.assert_(hasURL(sitemap, self.my_doc_url))
121
122    def test_regexp(self):
123        self.workflow.doActionFor(self.my_doc, 'publish')
124        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
125        self.assert_(not hasURL(sitemap, self.portal.absolute_url()))
126
127        regexp = "s/\/%s//"%self.my_doc.getId()
128        self.contentSM.setReg_exp([regexp])
129
130        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
131        self.assert_(hasURL(sitemap, self.portal.absolute_url()))
132
133    def test_add_urls(self):
134        self.contentSM.setUrls(['http://w1', 'w2', '/w3'])
135        w1_url = 'http://w1'
136        w2_url = self.portal.absolute_url() + '/w2'
137        w3_url = self.portal.getPhysicalRoot().absolute_url() + '/w3'
138        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
139
140        self.assert_(hasURL(sitemap, w1_url))
141        self.assert_(hasURL(sitemap, w2_url))
142        self.assert_(hasURL(sitemap, w3_url))
143
144
145class TestPinging(FunctionalTestCase):
146
147    def afterSetUp(self):
148        super(TestPinging, self).afterSetUp()
149        self.workflow.setChainForPortalTypes(pt_names=('News Item','Document'),
150                                             chain="simple_publication_workflow")
151        self.gsm_props = self.portal.portal_properties['googlesitemap_properties']
152        # Add sitemaps
153        self.contentSM = _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
154        self.contentSM.setPingTransitions(('simple_publication_workflow#publish',))
155        self.newsSM = _createObjectByType('Sitemap', self.portal, id='news-sitemaps')
156        self.newsSM.setPortalTypes(('News Item','Document'))
157        self.newsSM.setPingTransitions(('simple_publication_workflow#publish',))
158        self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps'
159        # Add testing document to portal
160        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
161        self.my_news = _createObjectByType('News Item', self.portal, id='my_news')
162
163    def testAutomatePinging(self):
164        # 1. Check for pinging both sitemaps
165        back_out, myout = sys.stdout, StringIO()
166        sys.stdout = myout
167        try:
168            self.workflow.doActionFor(self.my_doc, 'publish')
169            myout.seek(0)
170            data = myout.read()
171        finally:
172            sys.stdout = back_out
173
174        self.assert_('Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data,
175                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
176        self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data,
177                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
178
179        # 2. Check for pinging only news-sitemap sitemaps
180        back_out, myout = sys.stdout, StringIO()
181        sys.stdout = myout
182        try:
183            self.workflow.doActionFor(self.my_news, 'publish')
184            myout.seek(0)
185            data = myout.read()
186        finally:
187            sys.stdout = back_out
188
189        self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data,
190                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
191        self.assert_(not 'Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data,
192                     "Pinged %s on news: '%s'" % (self.contentSM.id, data))
193
194    def testPingingWithSetupForm(self):
195        # Ping news and content sitemaps
196        formUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_settings'
197        qs = 'smselected:list=%s&smselected:list=%s&form.button.Ping=1&form.submitted=1' % \
198             (self.contentSM.id, self.newsSM.id)
199
200        back_out, myout = sys.stdout, StringIO()
201        sys.stdout = myout
202        try:
203            response = self.publish("%s?%s" % (formUrl, qs), basic=self.auth)
204            myout.seek(0)
205            data = myout.read()
206        finally:
207            sys.stdout = back_out
208
209        self.assert_('Pinged %s sitemap to Google' % self.contentSM.absolute_url() in data,
210                     "Not pinged %s: '%s'" % (self.contentSM.id, data))
211        self.assert_('Pinged %s sitemap to Google' % self.newsSM.absolute_url() in data,
212                     "Not pinged %s: '%s'" % (self.newsSM.id, data))
213
214
215
216def test_suite():
217    from unittest import TestSuite, makeSuite
218    suite = TestSuite()
219    suite.addTest(makeSuite(TestSitemapType))
220    suite.addTest(makeSuite(TestSettings))
221    suite.addTest(makeSuite(TestPinging))
222    return suite
223
224if __name__ == '__main__':
225    unittest.main(defaultTest='test_suite')
226#    framework()
Note: See TracBrowser for help on using the repository browser.