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

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

fixes pep8

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