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