Back to index

plone3  3.1.7
test_portlet_collection.py
Go to the documentation of this file.
00001 from zope.component import getUtility, getMultiAdapter
00002 
00003 from plone.portlets.interfaces import IPortletType
00004 from plone.portlets.interfaces import IPortletManager
00005 from plone.portlets.interfaces import IPortletAssignment
00006 from plone.portlets.interfaces import IPortletDataProvider
00007 from plone.portlets.interfaces import IPortletRenderer
00008 
00009 from plone.app.portlets.storage import PortletAssignmentMapping
00010 
00011 from plone.portlet.collection import collection
00012 
00013 from plone.portlet.collection.tests.base import TestCase
00014 
00015 
00016 from Products.CMFCore.utils import getToolByName
00017 
00018 
00019 
00020 class TestPortlet(TestCase):
00021 
00022     def afterSetUp(self):
00023         self.setRoles(('Manager',))
00024 
00025     def testPortletTypeRegistered(self):
00026         portlet = getUtility(IPortletType, name='plone.portlet.collection.Collection')
00027         self.assertEquals(portlet.addview, 'plone.portlet.collection.Collection')
00028 
00029     def testInterfaces(self):
00030         portlet = collection.Assignment(header=u"title")
00031         self.failUnless(IPortletAssignment.providedBy(portlet))
00032         self.failUnless(IPortletDataProvider.providedBy(portlet.data))
00033 
00034     def testInvokeAddview(self):
00035         portlet = getUtility(IPortletType, name='plone.portlet.collection.Collection')
00036         mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
00037         for m in mapping.keys():
00038             del mapping[m]
00039         addview = mapping.restrictedTraverse('+/' + portlet.addview)
00040 
00041         addview.createAndAdd(data={'header' : u"test title"})
00042 
00043         self.assertEquals(len(mapping), 1)
00044         self.failUnless(isinstance(mapping.values()[0], collection.Assignment))
00045 
00046     def testInvokeEditView(self):
00047         mapping = PortletAssignmentMapping()
00048         request = self.folder.REQUEST
00049 
00050         mapping['foo'] = collection.Assignment(header=u"title")
00051         editview = getMultiAdapter((mapping['foo'], request), name='edit')
00052         self.failUnless(isinstance(editview, collection.EditForm))
00053 
00054     def testRenderer(self):
00055         context = self.folder
00056         request = self.folder.REQUEST
00057         view = self.folder.restrictedTraverse('@@plone')
00058         manager = getUtility(IPortletManager, name='plone.rightcolumn', context=self.portal)
00059         assignment = collection.Assignment(header=u"title")
00060 
00061         renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00062         self.failUnless(isinstance(renderer, collection.Renderer))
00063 
00064 class TestRenderer(TestCase):
00065 
00066     def afterSetUp(self):
00067         self.setRoles(('Manager',))
00068 
00069     def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
00070         context = context or self.folder
00071         request = request or self.folder.REQUEST
00072         view = view or self.folder.restrictedTraverse('@@plone')
00073         manager = manager or getUtility(IPortletManager, name='plone.rightcolumn', context=self.portal)
00074         assignment = assignment or collection.Assignment(header=u"title")
00075 
00076         return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00077 
00078     def test_render(self):
00079         r = self.renderer(context=self.portal, assignment=collection.Assignment(header=u"title"))
00080         r = r.__of__(self.folder)
00081         r.update()
00082         output = r.render()
00083 
00084         # this test failed due to changed behavior. We'll not output anything if the portlet does not point to a collection
00085 
00086         #self.failUnless('title' in output)
00087         #self.failUnless('<b>text</b>' in output)
00088 
00089 class TestCollectionQuery(TestCase):
00090 
00091     def afterSetUp(self):
00092         self.setRoles(('Manager',))
00093         #make a collection
00094         self.collection = self._createType(self.folder, 'Topic', 'collection')
00095 
00096     def _createType(self, context, portal_type, id, **kwargs):
00097         """Helper method to create a new type
00098         """
00099         ttool = getToolByName(context, 'portal_types')
00100         cat = self.portal.portal_catalog
00101 
00102         fti = ttool.getTypeInfo(portal_type)
00103         fti.constructInstance(context, id, **kwargs)
00104         obj = getattr(context.aq_inner.aq_explicit, id)
00105         cat.indexObject(obj)
00106         return obj
00107 
00108     def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
00109         context = context or self.folder
00110         request = request or self.folder.REQUEST
00111         view = view or self.folder.restrictedTraverse('@@plone')
00112         manager = manager or getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
00113         assignment = assignment
00114         return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
00115 
00116     def testSimpleQuery(self):
00117         # set up our collection to search for Folders
00118         crit = self.folder.collection.addCriterion('portal_type', 'ATSimpleStringCriterion')
00119         crit.setValue('Folder')
00120 
00121         # add a few folders
00122         for i in range(6):
00123             self.folder.invokeFactory('Folder', 'folder_%s'%i)
00124             getattr(self.folder, 'folder_%s'%i).reindexObject()
00125 
00126         # the folders are returned by the topic
00127         collection_num_items = len(self.folder.collection.queryCatalog())
00128         # We better have some folders
00129         self.failUnless(collection_num_items >= 6)
00130 
00131         mapping = PortletAssignmentMapping()
00132         request = self.folder.REQUEST
00133         mapping['foo'] = collection.Assignment(header=u"title", target_collection='/Members/test_user_1_/collection')
00134         collectionrenderer = self.renderer(context=None, request=None, view=None, manager=None, assignment=mapping['foo'])
00135 
00136         # we want the portlet to return us the same results as the collection
00137         self.assertEquals(collection_num_items, len(collectionrenderer.results()))
00138         
00139     def testRandomQuery(self):
00140         # we're being perhaps a bit too clever in random mode with the internals of the
00141         # LazyMap returned by the collection query, so let's try a bunch of scenarios
00142         # to make sure they work
00143         
00144         # set up our portlet renderer
00145         mapping = PortletAssignmentMapping()
00146         request = self.folder.REQUEST
00147         mapping['foo'] = collection.Assignment(header=u"title",
00148                                                random=True,
00149                                                target_collection='/Members/test_user_1_/collection')
00150         collectionrenderer = self.renderer(context=None, request=None, view=None, manager=None, assignment=mapping['foo'])
00151 
00152         # add some folders
00153         for i in range(6):
00154             self.folder.invokeFactory('Folder', 'folder_%s'%i)
00155             getattr(self.folder, 'folder_%s'%i).reindexObject()
00156 
00157         # collection with no criteria -- should return empty list, without error
00158         self.assertEqual(len(collectionrenderer.results()), 0)
00159 
00160         # let's make sure the results aren't being memoized
00161         old_func = self.folder.collection.queryCatalog
00162         global collection_was_called
00163         collection_was_called = False
00164         def mark_collection_called(**kw):
00165             global collection_was_called
00166             collection_was_called = True
00167         self.folder.collection.queryCatalog = mark_collection_called
00168         collectionrenderer.results()
00169         self.folder.collection.queryCatalog = old_func
00170         self.failUnless(collection_was_called)
00171         
00172         # collection with simple criterion -- should return 1 (random) folder
00173         crit = self.folder.collection.addCriterion('portal_type', 'ATSimpleStringCriterion')
00174         crit.setValue('Folder')
00175         self.assertEqual(len(collectionrenderer.results()), 1)
00176         
00177         # collection with multiple criteria -- should behave similarly
00178         crit = self.folder.collection.addCriterion('Creator', 'ATSimpleStringCriterion')
00179         crit.setValue('test_user_1_')
00180         collectionrenderer.results()
00181         
00182         # collection with sorting -- should behave similarly (sort is ignored internally)
00183         self.folder.collection.setSortCriterion('modified', False)
00184         self.assertEqual(len(collectionrenderer.results()), 1)
00185         
00186         # same criteria, now with limit set to 2 -- should return 2 (random) folders
00187         collectionrenderer.data.limit = 2
00188         self.assertEqual(len(collectionrenderer.results()), 2)
00189         
00190         # make sure there's no error if the limit is greater than the # of results found
00191         collectionrenderer.data.limit = 10
00192         self.failUnless(len(collectionrenderer.results()) >= 6)
00193         
00194         
00195 def test_suite():
00196     from unittest import TestSuite, makeSuite
00197     suite = TestSuite()
00198     suite.addTest(makeSuite(TestPortlet))
00199     suite.addTest(makeSuite(TestRenderer))
00200     suite.addTest(makeSuite(TestCollectionQuery))
00201     return suite