source: products/quintagroup.plonegooglesitemaps/branches/blacklist/quintagroup/plonegooglesitemaps/tests/testBlackoutList.py @ 2977

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

#228: Fix incorrect black_list field editing in tests

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
1#
2# Tests related to general Sitemap type.
3#
4from base import *
5from types import ListType, TupleType
6from zope import component
7from zope.component import queryMultiAdapter
8
9from Products.CMFPlone.utils import _createObjectByType
10from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter
11
12
13class TestBOFilters(TestCase):
14
15    def testDefaultId(self):
16        idfilter = queryMultiAdapter((self.portal, self.app.REQUEST),
17                       IBlackoutFilter, name="id")
18        self.assertTrue(idfilter is not None,
19            "Not registered default 'id' IBlackoutFilter")
20
21    def testDefaultPath(self):
22        pathfilter = queryMultiAdapter((self.portal, self.app.REQUEST),
23                         IBlackoutFilter, name="path")
24        self.assertTrue(pathfilter is not None,
25            "Not registered default 'path' IBlackoutFilter")
26
27
28class TestFilterMixin(TestCase):
29
30    def afterSetUp(self):
31        super(TestFilterMixin, self).afterSetUp()
32        self.createTestContent()
33        self.sm = _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
34        self.req = self.app.REQUEST
35        self.catres = self.portal.portal_catalog(portal_type=["Document",])
36        self.logout()
37
38    def createTestContent(self):
39        # Add testing content to portal
40        for cont in [self.portal, self.folder]:
41            for i in range(1,4):
42                doc = _createObjectByType('Document', cont, id='doc%i' % i)
43                doc.edit(text_format='plain', text='hello world %i' % i)
44                self.workflow.doActionFor(doc, 'publish')
45
46
47class TestDefaultFilters(TestFilterMixin):
48
49    def getPreparedLists(self, fname, fargs):
50        fengine = queryMultiAdapter((self.sm, self.req), IBlackoutFilter, name=fname)
51        filtered = [f.getPath() for f in fengine.filterOut(self.catres, fargs)]
52        catpaths = [c.getPath() for c in self.catres]
53        return catpaths, filtered
54
55    def testIdFilter(self):
56        catpaths, filtered = self.getPreparedLists("id", "doc1")
57        self.assertTrue(type(filtered) in [ListType, TupleType],
58            'Object type, returned by filteredOut method of "id" filter '\
59            'not list nor tuple')
60        excluded = ["/%s/doc1" % self.portal.absolute_url(1),
61                    "/%s/doc1" % self.folder.absolute_url(1)]
62        self.assertTrue(
63            set(catpaths)-set(filtered) == set(excluded),
64            'Wrong filtered-out by "id" filter:\nsrc %s\nres %s\nexcluded %s' % (
65             catpaths, filtered, excluded))
66
67    def testAbsolutePathFilter(self):
68        catpaths, filtered = self.getPreparedLists("path", "/doc1")
69        self.assertTrue(type(filtered) in [ListType, TupleType],
70            'Object type, returned by filteredOut method of "path" filter '\
71            'not list nor tuple')
72        excluded = ["/%s/doc1" % self.portal.absolute_url(1)]
73        self.assertTrue(
74            set(catpaths)-set(filtered) == set(excluded),
75            'Wrong filtered-out by "path" filter:\nsrc %s\nres %s\nexcluded %s' % (
76             catpaths, filtered, excluded))
77
78    def testRelativePathFilter(self):
79        self.sm = _createObjectByType('Sitemap', self.folder, id='google-sitemaps')
80        catpaths, filtered = self.getPreparedLists("path", "./doc1")
81        self.assertTrue(type(filtered) in [ListType, TupleType],
82            'Object type, returned by filteredOut method of "path" utility '\
83            'not list nor tuple')
84        excluded = ["/%s/doc1" % self.folder.absolute_url(1)]
85        self.assertTrue(
86            set(catpaths)-set(filtered) == set(excluded),
87            'Wrong filtered-out by "path" filter:\nsrc %s\nres %s\nexcluded %s' % (
88             catpaths, filtered, excluded))
89
90
91
92class TestBlacklistFormProcessing(TestFilterMixin):
93
94    def afterSetUp(self):
95        super(TestBlacklistFormProcessing, self).afterSetUp()
96        self.loginAsPortalOwner()
97        self.smview = queryMultiAdapter((self.sm, self.app.REQUEST), name="sitemap.xml")
98
99    def getPreparedLists(self, bl, fargs):
100        self.sm.edit(blackout_list=[bl,])
101        filtered = [f['url'] for f in self.smview.results()]
102        catpaths = [c.getURL() for c in self.catres]
103        return catpaths, filtered
104
105    def testGetNamedFilterUtility(self):
106        catpaths, filtered = self.getPreparedLists("path:/doc1", "/plone/doc1")
107        excluded = ["%s/doc1" % self.portal.absolute_url()]
108        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
109            'Wrong filtered-out by "id" filter:\nsrc %s\nres %s\nexcluded %s' % (
110             catpaths, filtered, excluded))
111
112    def testDefaultFilterUtility(self):
113        catpaths, filtered = self.getPreparedLists("id:doc1", "doc1")
114        excluded = ["%s/doc1" % self.portal.absolute_url(),
115                    "%s/doc1" % self.folder.absolute_url()]
116        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
117            'Wrong filtered-out by "id" filter:\nsrc %s\nres %s\nexcluded %s' % (
118             catpaths, filtered, excluded))
119        # Now check is output of unnamed filter samed to named one.
120        self.sm.edit(blackout_list=["doc1",])
121        filtered_dflt = [f['url'] for f in self.smview.results()]
122        map(lambda l: l.sort(), (filtered, filtered_dflt))
123        self.assertTrue(filtered == filtered_dflt,
124            'Output of named "id" filter is not same to unnamed one:\n' \
125            'id-named: %s\nunnamed: %s' % (filtered, filtered_dflt))
126
127    # def testGetCorrectFilterName(self):
128    #     call_names = []
129    #     origQMA = component._api.queryMultiAdapter
130    #     def patchQMA(objects, interface=Interface, name=u'', context=None):
131    #         call_names.append(name)
132    #         origQMA(objects, interface=interface, name=name, context=context)
133    #     component.queryMutliAdapter = patchQMA
134    #     self.sm.edit(blackout_list="FooFilterName:arg1:arg2:doc1")
135    #     self.smview.results()
136    #     self.assertTrue("FooFilterName" in call_names,
137    #         "Wrong filter name parsing - no FooFilterName in %s" % call_names)
138    #     component._api.queryMutliAdapter = origQMA
139       
140       
141       
142
143def test_suite():
144    from unittest import TestSuite, makeSuite
145    suite = TestSuite()
146    suite.addTest(makeSuite(TestBOFilters))
147    suite.addTest(makeSuite(TestDefaultFilters))
148    suite.addTest(makeSuite(TestBlacklistFormProcessing))
149    return suite
150
151if __name__ == '__main__':
152    unittest.main(defaultTest='test_suite')
153#    framework()
Note: See TracBrowser for help on using the repository browser.