source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testBlackoutList.py @ 3163

Last change on this file since 3163 was 3163, checked in by zidane, 9 years ago

fixes pyflakes and pylint

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