[2909] | 1 | # |
---|
| 2 | # Tests related to general Sitemap type. |
---|
| 3 | # |
---|
| 4 | from base import * |
---|
[2911] | 5 | from types import ListType, TupleType |
---|
[2941] | 6 | from zope import component |
---|
[2939] | 7 | from zope.component import queryMultiAdapter |
---|
[2909] | 8 | |
---|
[2911] | 9 | from Products.CMFPlone.utils import _createObjectByType |
---|
[2939] | 10 | from quintagroup.plonegooglesitemaps.interfaces import IBlackoutFilter |
---|
[2909] | 11 | |
---|
| 12 | |
---|
[2939] | 13 | class TestBOFilters(TestCase): |
---|
[2909] | 14 | |
---|
[2939] | 15 | def testDefaultId(self): |
---|
| 16 | idfilter = queryMultiAdapter((self.portal, self.app.REQUEST), |
---|
[2940] | 17 | IBlackoutFilter, name="id") |
---|
[2939] | 18 | self.assertTrue(idfilter is not None, |
---|
[2940] | 19 | "Not registered default 'id' IBlackoutFilter") |
---|
[2909] | 20 | |
---|
[2939] | 21 | def testDefaultPath(self): |
---|
| 22 | pathfilter = queryMultiAdapter((self.portal, self.app.REQUEST), |
---|
[2940] | 23 | IBlackoutFilter, name="path") |
---|
[2939] | 24 | self.assertTrue(pathfilter is not None, |
---|
[2940] | 25 | "Not registered default 'path' IBlackoutFilter") |
---|
[2909] | 26 | |
---|
| 27 | |
---|
[2917] | 28 | class TestFilterMixin(TestCase): |
---|
[2915] | 29 | |
---|
[2911] | 30 | def afterSetUp(self): |
---|
[2917] | 31 | super(TestFilterMixin, self).afterSetUp() |
---|
| 32 | self.createTestContent() |
---|
[3152] | 33 | self.sm = _createObjectByType('Sitemap', self.portal, |
---|
| 34 | id='google-sitemaps') |
---|
[2925] | 35 | self.req = self.app.REQUEST |
---|
[3152] | 36 | self.catres = self.portal.portal_catalog(portal_type=["Document", ]) |
---|
[2911] | 37 | self.logout() |
---|
| 38 | |
---|
[2917] | 39 | def createTestContent(self): |
---|
| 40 | # Add testing content to portal |
---|
| 41 | for cont in [self.portal, self.folder]: |
---|
[3152] | 42 | for i in range(1, 4): |
---|
[2917] | 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 | |
---|
| 48 | class TestDefaultFilters(TestFilterMixin): |
---|
| 49 | |
---|
[2925] | 50 | def getPreparedLists(self, fname, fargs): |
---|
[3152] | 51 | fengine = queryMultiAdapter((self.sm, self.req), IBlackoutFilter, |
---|
| 52 | name=fname) |
---|
[2939] | 53 | filtered = [f.getPath() for f in fengine.filterOut(self.catres, fargs)] |
---|
[2911] | 54 | catpaths = [c.getPath() for c in self.catres] |
---|
[2925] | 55 | return catpaths, filtered |
---|
[2917] | 56 | |
---|
| 57 | def testIdFilter(self): |
---|
[2940] | 58 | catpaths, filtered = self.getPreparedLists("id", "doc1") |
---|
[2911] | 59 | self.assertTrue(type(filtered) in [ListType, TupleType], |
---|
[2940] | 60 | 'Object type, returned by filteredOut method of "id" filter '\ |
---|
| 61 | 'not list nor tuple') |
---|
[2925] | 62 | excluded = ["/%s/doc1" % self.portal.absolute_url(1), |
---|
| 63 | "/%s/doc1" % self.folder.absolute_url(1)] |
---|
| 64 | self.assertTrue( |
---|
[3152] | 65 | set(catpaths) - set(filtered) == set(excluded), |
---|
| 66 | 'Wrong filtered-out by "id" filter:\nsrc %s\nres %s\nexcluded %s' \ |
---|
| 67 | % (catpaths, filtered, excluded)) |
---|
[2911] | 68 | |
---|
[2925] | 69 | def testAbsolutePathFilter(self): |
---|
[2940] | 70 | catpaths, filtered = self.getPreparedLists("path", "/doc1") |
---|
[2911] | 71 | self.assertTrue(type(filtered) in [ListType, TupleType], |
---|
[2940] | 72 | 'Object type, returned by filteredOut method of "path" filter '\ |
---|
| 73 | 'not list nor tuple') |
---|
[2925] | 74 | excluded = ["/%s/doc1" % self.portal.absolute_url(1)] |
---|
| 75 | self.assertTrue( |
---|
[3152] | 76 | set(catpaths) - set(filtered) == set(excluded), |
---|
| 77 | 'Wrong filtered-out by "path" filter:\nsrc %s\nres %s\nexcluded ' \ |
---|
| 78 | '%s' % (catpaths, filtered, excluded)) |
---|
[2911] | 79 | |
---|
[2921] | 80 | def testRelativePathFilter(self): |
---|
[3152] | 81 | self.sm = _createObjectByType('Sitemap', self.folder, |
---|
| 82 | id='google-sitemaps') |
---|
[2940] | 83 | catpaths, filtered = self.getPreparedLists("path", "./doc1") |
---|
[2919] | 84 | self.assertTrue(type(filtered) in [ListType, TupleType], |
---|
[2940] | 85 | 'Object type, returned by filteredOut method of "path" utility '\ |
---|
| 86 | 'not list nor tuple') |
---|
[2925] | 87 | excluded = ["/%s/doc1" % self.folder.absolute_url(1)] |
---|
| 88 | self.assertTrue( |
---|
[3152] | 89 | set(catpaths) - set(filtered) == set(excluded), |
---|
| 90 | 'Wrong filtered-out by "path" filter:\nsrc %s\nres %s\nexcluded ' \ |
---|
| 91 | '%s' % (catpaths, filtered, excluded)) |
---|
[2919] | 92 | |
---|
| 93 | |
---|
[2918] | 94 | class TestBlacklistFormProcessing(TestFilterMixin): |
---|
[2915] | 95 | |
---|
| 96 | def afterSetUp(self): |
---|
[2919] | 97 | super(TestBlacklistFormProcessing, self).afterSetUp() |
---|
[2917] | 98 | self.loginAsPortalOwner() |
---|
[3152] | 99 | self.smview = queryMultiAdapter((self.sm, self.app.REQUEST), |
---|
| 100 | name="sitemap.xml") |
---|
[2915] | 101 | |
---|
[2925] | 102 | def getPreparedLists(self, bl, fargs): |
---|
[3152] | 103 | self.sm.edit(blackout_list=[bl, ]) |
---|
[2915] | 104 | filtered = [f['url'] for f in self.smview.results()] |
---|
| 105 | catpaths = [c.getURL() for c in self.catres] |
---|
[2925] | 106 | return catpaths, filtered |
---|
[2917] | 107 | |
---|
| 108 | def testGetNamedFilterUtility(self): |
---|
[2925] | 109 | catpaths, filtered = self.getPreparedLists("path:/doc1", "/plone/doc1") |
---|
| 110 | excluded = ["%s/doc1" % self.portal.absolute_url()] |
---|
[3152] | 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)) |
---|
[2915] | 114 | |
---|
| 115 | def testDefaultFilterUtility(self): |
---|
[2925] | 116 | catpaths, filtered = self.getPreparedLists("id:doc1", "doc1") |
---|
| 117 | excluded = ["%s/doc1" % self.portal.absolute_url(), |
---|
| 118 | "%s/doc1" % self.folder.absolute_url()] |
---|
[3152] | 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)) |
---|
[2925] | 122 | # Now check is output of unnamed filter samed to named one. |
---|
[3152] | 123 | self.sm.edit(blackout_list=["doc1", ]) |
---|
[2925] | 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, |
---|
[2977] | 127 | 'Output of named "id" filter is not same to unnamed one:\n' \ |
---|
[2925] | 128 | 'id-named: %s\nunnamed: %s' % (filtered, filtered_dflt)) |
---|
[2915] | 129 | |
---|
[2941] | 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() |
---|
[3152] | 139 | # self.assertTrue("FooFilterName" in call_names, "Wrong filter " \ |
---|
| 140 | # "name parsing - no FooFilterName in %s" % call_names) |
---|
[2941] | 141 | # component._api.queryMutliAdapter = origQMA |
---|
[2915] | 142 | |
---|
[3152] | 143 | |
---|
[2909] | 144 | def test_suite(): |
---|
| 145 | from unittest import TestSuite, makeSuite |
---|
| 146 | suite = TestSuite() |
---|
[2939] | 147 | suite.addTest(makeSuite(TestBOFilters)) |
---|
[2911] | 148 | suite.addTest(makeSuite(TestDefaultFilters)) |
---|
[2918] | 149 | suite.addTest(makeSuite(TestBlacklistFormProcessing)) |
---|
[2909] | 150 | return suite |
---|
| 151 | |
---|
| 152 | if __name__ == '__main__': |
---|
| 153 | unittest.main(defaultTest='test_suite') |
---|
| 154 | # framework() |
---|