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

Last change on this file since 2741 was 2741, checked in by mylan, 11 years ago

#226: Added tests if sitemap search content related to the place, where it added to

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