1 | # |
---|
2 | # Tests related to general Sitemap type. |
---|
3 | # |
---|
4 | from base import * |
---|
5 | from types import ListType, TupleType |
---|
6 | from zope.component import queryUtility, queryMultiAdapter |
---|
7 | |
---|
8 | from Products.CMFPlone.utils import _createObjectByType |
---|
9 | from quintagroup.plonegooglesitemaps.config import BLACKOUT_PREFIX |
---|
10 | from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilterUtility |
---|
11 | |
---|
12 | idfname = BLACKOUT_PREFIX + "id" |
---|
13 | pathfname = BLACKOUT_PREFIX + "path" |
---|
14 | |
---|
15 | class 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 | |
---|
26 | def createTestContent(self): |
---|
27 | # Add testing content to portal |
---|
28 | for cont in [self.portal, self.folder]: |
---|
29 | for i in range(1,4): |
---|
30 | doc = _createObjectByType('Document', cont, id='doc%i' % i) |
---|
31 | doc.edit(text_format='plain', text='hello world %i' % i) |
---|
32 | self.workflow.doActionFor(doc, 'publish') |
---|
33 | |
---|
34 | class TestDefaultFilters(TestCase): |
---|
35 | |
---|
36 | def afterSetUp(self): |
---|
37 | super(TestDefaultFilters, self).afterSetUp() |
---|
38 | createTestContent(self) |
---|
39 | self.catres = self.portal.portal_catalog(portal_type=["Document",]) |
---|
40 | self.logout() |
---|
41 | |
---|
42 | def testIdFilter(self): |
---|
43 | idfilter = queryUtility(IBlackoutFilterUtility, name=idfname) |
---|
44 | catpaths = [c.getPath() for c in self.catres] |
---|
45 | filtered = [f.getPath() for f in idfilter.filterOut(self.catres, fkey="doc1")] |
---|
46 | excluded = [c.getPath() for c in self.catres if c.id == "doc1"] |
---|
47 | map(lambda l:l.sort(), [catpaths, filtered, excluded]) |
---|
48 | self.assertTrue(type(filtered) in [ListType, TupleType], |
---|
49 | 'Object type, returned by filteredOut method of "%s" utility '\ |
---|
50 | 'not list nor tuple' % idfname) |
---|
51 | self.assertTrue(set(catpaths)-set(filtered) == set(excluded), |
---|
52 | 'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % ( |
---|
53 | idfname, catpaths, filtered, excluded)) |
---|
54 | |
---|
55 | def testPathFilter(self): |
---|
56 | pathfilter = queryUtility(IBlackoutFilterUtility, name=pathfname) |
---|
57 | catpaths = [c.getPath() for c in self.catres] |
---|
58 | filtered = [f.getPath() for f in pathfilter.filterOut(self.catres, fkey="/plone/doc1")] |
---|
59 | excluded = [c.getPath() for c in self.catres if c.getPath() == "/plone/doc1"] |
---|
60 | map(lambda l:l.sort(), [catpaths, filtered, excluded]) |
---|
61 | self.assertTrue(type(filtered) in [ListType, TupleType], |
---|
62 | 'Object type, returned by filteredOut method of "%s" utility '\ |
---|
63 | 'not list nor tuple' % pathfname) |
---|
64 | self.assertTrue(set(catpaths)-set(filtered) == set(excluded), |
---|
65 | 'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % ( |
---|
66 | pathfname, catpaths, filtered, excluded)) |
---|
67 | |
---|
68 | |
---|
69 | class TestFormDataProcessing(TestCase): |
---|
70 | |
---|
71 | def afterSetUp(self): |
---|
72 | super(TestFormDataProcessing, self).afterSetUp() |
---|
73 | createTestContent(self) |
---|
74 | # Add sitemap object |
---|
75 | self.sm = _createObjectByType('Sitemap', self.portal, id='google-sitemaps') |
---|
76 | self.smview = queryMultiAdapter((self.sm, self.app.REQUEST), name="sitemap.xml") |
---|
77 | self.catres = self.portal.portal_catalog(portal_type=["Document",]) |
---|
78 | |
---|
79 | def testGetNamedFilterUtility(self): |
---|
80 | self.sm.edit(blackout_list="path:/plone/doc1") |
---|
81 | filtered = [f['url'] for f in self.smview.results()] |
---|
82 | catpaths = [c.getURL() for c in self.catres] |
---|
83 | excluded = [c.getURL() for c in self.catres \ |
---|
84 | if c.getPath() == "/plone/doc1"] |
---|
85 | map(lambda l:l.sort(), [catpaths, filtered, excluded]) |
---|
86 | self.assertTrue(set(catpaths)-set(filtered) == set(excluded), |
---|
87 | 'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % ( |
---|
88 | idfname, catpaths, filtered, excluded)) |
---|
89 | |
---|
90 | def testDefaultFilterUtility(self): |
---|
91 | self.sm.edit(blackout_list="id:doc1") |
---|
92 | filtered = [f['url'] for f in self.smview.results()] |
---|
93 | catpaths = [c.getURL() for c in self.catres] |
---|
94 | excluded = [c.getURL() for c in self.catres if c.id == "doc1"] |
---|
95 | map(lambda l:l.sort(), [catpaths, filtered, excluded]) |
---|
96 | self.assertTrue(set(catpaths)-set(filtered) == set(excluded), |
---|
97 | 'Wrong filtered-out by "%s" filter:\nsrc %s\nres %s\nexcluded %s' % ( |
---|
98 | idfname, catpaths, filtered, excluded)) |
---|
99 | # Now check is output of unnamed filter same to id-named one. |
---|
100 | self.sm.edit(blackout_list="id:doc1") |
---|
101 | filtered_def = [f['url'] for f in self.smview.results()] |
---|
102 | filtered_def.sort() |
---|
103 | self.assertTrue(filtered == filtered_def, |
---|
104 | 'Output of named "id" filter is not same to unnamed one:' \ |
---|
105 | 'id-named: %s\nunnamed: %s' % (filtered, filtered_def)) |
---|
106 | |
---|
107 | |
---|
108 | def test_suite(): |
---|
109 | from unittest import TestSuite, makeSuite |
---|
110 | suite = TestSuite() |
---|
111 | suite.addTest(makeSuite(TestBOFilterUtilities)) |
---|
112 | suite.addTest(makeSuite(TestDefaultFilters)) |
---|
113 | suite.addTest(makeSuite(TestFormDataProcessing)) |
---|
114 | return suite |
---|
115 | |
---|
116 | if __name__ == '__main__': |
---|
117 | unittest.main(defaultTest='test_suite') |
---|
118 | # framework() |
---|