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

Last change on this file since 2922 was 2922, checked in by mylan, 14 years ago

#228: Fix relative path filter tests and utility

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1#
2# Tests related to general Sitemap type.
3#
4from base import *
5from types import ListType, TupleType
6from zope.component import queryUtility, queryMultiAdapter
7
8from Products.CMFPlone.utils import _createObjectByType
9from quintagroup.plonegooglesitemaps.config import BLACKOUT_PREFIX
10from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilterUtility
11
12idfname = BLACKOUT_PREFIX + "id"
13pathfname = BLACKOUT_PREFIX + "path"
14
15class TestBOFilterUtilities(TestCase):
16
17    def testDefaultIdUtility(self):
18        self.assertTrue(queryUtility(IBlackoutFilterUtility, name=idfname) is not None,
19            "Not registered default '%s' IBlackoutFilterUtility" % idfname)
20
21    def testDefaultPathUtility(self):
22        self.assertTrue(queryUtility(IBlackoutFilterUtility, name=pathfname) is not None,
23            "Not registered default '%s' IBlackoutFilterUtility" % pathfname)
24
25
26class TestFilterMixin(TestCase):
27
28    def afterSetUp(self):
29        super(TestFilterMixin, self).afterSetUp()
30        self.createTestContent()
31        self.sm = _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
32        self.catres = self.portal.portal_catalog(portal_type=["Document",])
33        self.logout()
34
35    def createTestContent(self):
36        # Add testing content to portal
37        for cont in [self.portal, self.folder]:
38            for i in range(1,4):
39                doc = _createObjectByType('Document', cont, id='doc%i' % i)
40                doc.edit(text_format='plain', text='hello world %i' % i)
41                self.workflow.doActionFor(doc, 'publish')
42
43
44class TestDefaultFilters(TestFilterMixin):
45
46    def getPreparedLists(self, fname, fargs, checkindex):
47        futil = queryUtility(IBlackoutFilterUtility, name=fname)
48        filtered = [f.getPath() for f in futil.filterOut(self.catres, fkey=fargs)]
49        catpaths = [c.getPath() for c in self.catres]
50        excluded = [c.getPath() for c in self.catres if checkindex(c) == fargs]
51        map(lambda l:l.sort(), [catpaths, filtered, excluded])
52        return catpaths, filtered, excluded
53
54    def testIdFilter(self):
55        catpaths, filtered, excluded = self.getPreparedLists(idfname, "doc1",
56            lambda b: b.id)
57        self.assertTrue(type(filtered) in [ListType, TupleType],
58            'Object type, returned by filteredOut method of "%s" utility '\
59            'not list nor tuple' % idfname)
60        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
61            'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % (
62             idfname, catpaths, filtered, excluded))
63
64    def testPathFilter(self):
65        catpaths, filtered, excluded = self.getPreparedLists(pathfname,
66            "/plone/doc1", lambda b: b.getPath())
67        self.assertTrue(type(filtered) in [ListType, TupleType],
68            'Object type, returned by filteredOut method of "%s" utility '\
69            'not list nor tuple' % pathfname)
70        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
71            'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % (
72             pathfname, catpaths, filtered, excluded))
73
74    def testRelativePathFilter(self):
75        #
76        fpath = '/'.join(self.folder.getPhysicalPath())
77        futil = queryUtility(IBlackoutFilterUtility, name=pathfname)
78        sm = _createObjectByType('Sitemap', self.folder, id='google-sitemaps')
79       
80        fres = futil.filterOut(self.catres, fkey="./doc1", sitemap=sm)
81        filtered = [f.getPath() for f in fres]
82        catpaths = [c.getPath() for c in self.catres]
83        excluded = [c.getPath() for c in self.catres if c.getPath()==fpath+"/doc1"]
84        map(lambda l:l.sort(), [catpaths, filtered, excluded])
85
86        self.assertTrue(type(filtered) in [ListType, TupleType],
87            'Object type, returned by filteredOut method of "%s" utility '\
88            'not list nor tuple' % pathfname)
89        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
90            'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % (
91             pathfname, 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), name="sitemap.xml")
100
101    def getPreparedLists(self, bl, fargs, checkindex):
102        self.sm.edit(blackout_list=bl)
103        filtered = [f['url'] for f in self.smview.results()]
104        catpaths = [c.getURL() for c in self.catres]
105        excluded = [c.getURL() for c in self.catres if checkindex(c) == fargs]
106        map(lambda l:l.sort(), [catpaths, filtered, excluded])
107        return catpaths, filtered, excluded
108
109    def testGetNamedFilterUtility(self):
110        catpaths, filtered, excluded = self.getPreparedLists("path:/plone/doc1",
111            "/plone/doc1", lambda b: b.getPath())
112        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
113            'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % (
114             idfname, catpaths, filtered, excluded))
115
116    def testDefaultFilterUtility(self):
117        catpaths, filtered, excluded = self.getPreparedLists("id:doc1",
118            "doc1", lambda b: b.id)
119        self.assertTrue(set(catpaths)-set(filtered) == set(excluded),
120            'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % (
121             idfname, catpaths, filtered, excluded))
122        # Now check is output of unnamed filter same to id-named one.
123        self.sm.edit(blackout_list="doc1")
124        filtered_def = [f['url'] for f in self.smview.results()]
125        filtered_def.sort()
126        self.assertTrue(filtered == filtered_def,
127            'Output of named "id" filter is not same to unnamed one:' \
128            'id-named: %s\nunnamed: %s' % (filtered, filtered_def))
129
130
131def test_suite():
132    from unittest import TestSuite, makeSuite
133    suite = TestSuite()
134    suite.addTest(makeSuite(TestBOFilterUtilities))
135    suite.addTest(makeSuite(TestDefaultFilters))
136    suite.addTest(makeSuite(TestBlacklistFormProcessing))
137    return suite
138
139if __name__ == '__main__':
140    unittest.main(defaultTest='test_suite')
141#    framework()
Note: See TracBrowser for help on using the repository browser.