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

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

Merged revisions 3948,3950-3951,3954-3964,3978-3981,3984-3992,4016-4018,4028-4037,4039 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.plonegooglesitemaps/branches/blacklist

........

r3948 | mylan | 2010-10-21 16:34:22 +0300 (Thu, 21 Oct 2010) | 1 line


#228: Added IBlackoutFilterUtility interface with skeleton of id and path utilities with appropriate tests.

........

r3950 | mylan | 2010-10-22 11:58:53 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added tests for default id and path filters

........

r3951 | mylan | 2010-10-22 11:59:25 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added default id and path blackout filters

........

r3954 | mylan | 2010-10-22 14:38:20 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added TestFormDataProcessing? tests

........

r3955 | mylan | 2010-10-22 14:38:57 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added getBOFiltered method to sitemap common view

........

r3956 | mylan | 2010-10-22 15:17:40 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Simplify blacklists filter utility tests

........

r3957 | mylan | 2010-10-22 15:23:24 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Minor name updates in blackout list tests

........

r3958 | mylan | 2010-10-22 15:54:10 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added tests for relative path in path-filter

........

r3959 | mylan | 2010-10-22 15:54:50 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Added relative path processing in default path-filter

........

r3960 | mylan | 2010-10-22 15:57:18 +0300 (Fri, 22 Oct 2010) | 1 line


#228: Fixed name of the relative path filtering test

........

r3961 | mylan | 2010-10-25 13:51:32 +0300 (Mon, 25 Oct 2010) | 1 line


#228: Fix relative path filter tests and utility

........

r3962 | mylan | 2010-10-25 17:38:55 +0300 (Mon, 25 Oct 2010) | 1 line


#228: Fixed default Path filter

........

r3963 | mylan | 2010-10-25 17:49:27 +0300 (Mon, 25 Oct 2010) | 1 line


#228: Added request to extended list of args for a filter utility.

........

r3964 | mylan | 2010-10-25 17:50:57 +0300 (Mon, 25 Oct 2010) | 1 line


#228: Fixed rewrited blackout filter utility and functionality.

........

r3978 | mylan | 2010-10-28 19:26:11 +0300 (Thu, 28 Oct 2010) | 1 line


#228: Remake filter utility to multiadapter, fix tests

........

r3979 | mylan | 2010-10-28 19:57:24 +0300 (Thu, 28 Oct 2010) | 1 line


#228: Fix filter naming overhead, fix tests

........

r3980 | mylan | 2010-10-28 21:10:51 +0300 (Thu, 28 Oct 2010) | 1 line


#228: Fix overhead in parsing of filter arguments. Added test (breakage yet)

........

r3981 | mylan | 2010-10-29 12:57:34 +0300 (Fri, 29 Oct 2010) | 1 line


#228: Add blackout_list value clean-up on editing sitemap

........

r3984 | mylan | 2010-10-29 13:53:54 +0300 (Fri, 29 Oct 2010) | 1 line


#228: Update description of blackout list, remove preparations to filtering

........

r3985 | mylan | 2010-10-29 16:14:29 +0300 (Fri, 29 Oct 2010) | 1 line


#228: Added doc tests bases

........

r3986 | mylan | 2010-10-29 16:53:41 +0300 (Fri, 29 Oct 2010) | 1 line


#228: Added basic doctests of filtering

........

r3987 | mylan | 2010-10-29 16:57:40 +0300 (Fri, 29 Oct 2010) | 1 line


#228: Minor fixes of filtering doctests.

........

r3988 | mylan | 2010-11-01 13:31:04 +0200 (Mon, 01 Nov 2010) | 1 line


#228: Added doctests of id, path, default filters

........

r3989 | mylan | 2010-11-01 15:20:03 +0200 (Mon, 01 Nov 2010) | 1 line


#228: Fixed default behavior filter doctests

........

r3990 | mylan | 2010-11-01 15:20:51 +0200 (Mon, 01 Nov 2010) | 1 line


#228: Added example of new filter creation in doctests

........

r3991 | mylan | 2010-11-01 15:35:10 +0200 (Mon, 01 Nov 2010) | 1 line


#228: Clean-up, simplify filters doctests

........

r3992 | mylan | 2010-11-01 15:39:55 +0200 (Mon, 01 Nov 2010) | 1 line


#228: Force to show all doctests failures.

........

r4016 | mylan | 2010-11-05 14:19:46 +0200 (Fri, 05 Nov 2010) | 1 line


#228: Fix incorrect black_list field editing in tests

........

r4017 | mylan | 2010-11-05 14:21:35 +0200 (Fri, 05 Nov 2010) | 1 line


#228: minor docstring update for getBOFiltered method

........

r4018 | mylan | 2010-11-05 14:23:56 +0200 (Fri, 05 Nov 2010) | 1 line


#228: Remake filterOut method of into generator for default filters

........

r4028 | mylan | 2010-11-08 13:58:10 +0200 (Mon, 08 Nov 2010) | 1 line


#228: Added Plone v3.0 support

........

r4029 | mylan | 2010-11-08 16:35:22 +0200 (Mon, 08 Nov 2010) | 1 line


#228: Fixed tests issues

........

r4030 | mylan | 2010-11-08 16:49:28 +0200 (Mon, 08 Nov 2010) | 1 line


#228: Fixed ran of tearing down testing layers

........

r4031 | mylan | 2010-11-08 17:04:01 +0200 (Mon, 08 Nov 2010) | 1 line


#228: Fixed differencies in forms for plone<=3.1 and plone>3.1

........

r4032 | mylan | 2010-11-10 13:48:47 +0200 (Wed, 10 Nov 2010) | 1 line


#228: Switch to use collective.testcaselayer

........

r4033 | mylan | 2010-11-10 13:49:09 +0200 (Wed, 10 Nov 2010) | 1 line


#228: Fix doctest testcase

........

r4034 | mylan | 2010-11-10 14:34:17 +0200 (Wed, 10 Nov 2010) | 1 line


#228: Fixed list of required packages for testing

........

r4035 | mylan | 2010-11-11 12:29:06 +0200 (Thu, 11 Nov 2010) | 1 line


#228: remake filters to generators

........

r4036 | mylan | 2010-11-11 15:16:53 +0200 (Thu, 11 Nov 2010) | 1 line


#228: reviewed explanation, correct grammar for filters doctest

........

r4037 | mylan | 2010-11-11 15:19:24 +0200 (Thu, 11 Nov 2010) | 1 line


#228: updated histroy, bumped version to 1.6.0

........

r4039 | mylan | 2010-11-11 19:03:01 +0200 (Thu, 11 Nov 2010) | 1 line


#228:Fixed typo in doctests

........

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