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