source: products/quintagroup.portlet.collection/trunk/quintagroup/portlet/collection/tests/test_portlet.py @ 2755

Last change on this file since 2755 was 2755, checked in by fenix, 10 years ago
  • added tests;
  • fixed style bug (None class was added when no portlet style is selected);
  • removed trailing characters;
  • removed unnecessary pice of code;
  • some javascript optimizations;
  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1from zope.component import getUtility, getMultiAdapter
2
3from plone.portlets.interfaces import IPortletType
4from plone.portlets.interfaces import IPortletManager
5from plone.portlets.interfaces import IPortletAssignment
6from plone.portlets.interfaces import IPortletDataProvider
7from plone.portlets.interfaces import IPortletRenderer
8
9from plone.app.portlets.storage import PortletAssignmentMapping
10
11from quintagroup.portlet.collection import collection
12
13from quintagroup.portlet.collection.tests.base import TestCase
14
15
16from Products.CMFCore.utils import getToolByName
17
18class TestQPortletCollection(TestCase):
19    def afterSetUp(self):
20        self.setRoles(('Manager',))
21
22    def testPortletTypeRegistered(self):
23        portlet = getUtility(IPortletType, name='quintagroup.portlet.collection.Collection')
24        self.assertEquals(portlet.addview, 'quintagroup.portlet.collection.Collection')
25
26    def testInterfaces(self):
27        portlet = collection.Assignment(header=u"title")
28        self.failUnless(IPortletAssignment.providedBy(portlet))
29        self.failUnless(IPortletDataProvider.providedBy(portlet.data))
30
31    def testInvokeAddview(self):
32        portlet = getUtility(IPortletType, name='quintagroup.portlet.collection.Collection')
33        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
34        for m in mapping.keys():
35            del mapping[m]
36        addview = mapping.restrictedTraverse('+/' + portlet.addview)
37
38        addview.createAndAdd(data={'header' : u"test title"})
39
40        self.assertEquals(len(mapping), 1)
41        self.failUnless(isinstance(mapping.values()[0], collection.Assignment))
42
43    def testInvokeEditView(self):
44        mapping = PortletAssignmentMapping()
45        request = self.folder.REQUEST
46
47        mapping['foo'] = collection.Assignment(header=u"title")
48        editview = getMultiAdapter((mapping['foo'], request), name='edit')
49        self.failUnless(isinstance(editview, collection.EditForm))
50
51    def testRenderer(self):
52        context = self.folder
53        request = self.folder.REQUEST
54        view = self.folder.restrictedTraverse('@@plone')
55        manager = getUtility(IPortletManager, name='plone.rightcolumn', context=self.portal)
56        assignment = collection.Assignment(header=u"title")
57
58        renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
59        self.failUnless(isinstance(renderer, collection.Renderer))
60
61class TestQPortletCollectionRenderer(TestCase):
62
63    def afterSetUp(self):
64        self.setRoles(('Manager',))
65        self.pages = self._createType(self.folder, 'Topic', 'pages')
66        crit = self.folder.pages.addCriterion('portal_type', 'ATSimpleStringCriterion')
67        crit.setValue('Document')
68
69    def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
70        context = context or self.folder
71        request = request or self.folder.REQUEST
72        view = view or self.folder.restrictedTraverse('@@plone')
73        manager = manager or getUtility(IPortletManager, name='plone.rightcolumn', context=self.portal)
74        assignment = assignment or collection.Assignment(header=u"title")
75
76        return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
77
78    def _createType(self, context, portal_type, id, **kwargs):
79        """Helper method to create a new type
80        """
81        ttool = getToolByName(context, 'portal_types')
82        cat = self.portal.portal_catalog
83
84        fti = ttool.getTypeInfo(portal_type)
85        fti.constructInstance(context, id, **kwargs)
86        obj = getattr(context.aq_inner.aq_explicit, id)
87        cat.indexObject(obj)
88        return obj
89
90    def test_render(self):
91        r = self.renderer(context=self.portal, assignment=collection.Assignment(header=u"title"))
92        r = r.__of__(self.folder)
93        r.update()
94        output = r.render()
95
96        # this test failed due to changed behavior. We'll not output anything if the portlet does not point to a collection
97
98        #self.failUnless('title' in output)
99        #self.failUnless('<b>text</b>' in output)
100
101
102    def test_collection_path_unicode(self):
103        """
104        Cover problem in #9184
105        """
106        r = self.renderer(context=self.portal,
107                          assignment=collection.Assignment(header=u"title",
108                                                           target_collection=u"/events"))
109        r  = r.__of__(self.folder)
110        self.assertEqual(r.collection().id, 'events')
111
112    def test_portletStyle(self):
113        renderer = self.renderer(context=self.portal,
114                                 assignment=collection.Assignment(header=u"title",
115                                                                  styling="TestClass"))
116        renderer = renderer.__of__(self.folder)
117        renderer.update()
118
119        self.failUnless('TestClass' in renderer.render())
120
121    def test_attributes(self):
122        page = self._createType(self.folder, 'Document', 'page')
123        page.update(title="Test Page", description="Test description")
124        renderer = self.renderer(context=self.portal,
125                                 assignment=collection.Assignment(header=u"title",
126                                                                  item_attributes=[u'Title'],
127                                                                  target_collection='/Members/test_user_1_/pages'))
128        renderer = renderer.__of__(self.folder)
129        renderer.update()
130        self.failUnless('Test Page' in renderer.render())
131        self.failUnless('Test description' not in renderer.render())
132
133class TestQPortletCollectionQuery(TestCase):
134
135    def afterSetUp(self):
136        self.setRoles(('Manager',))
137        #make a collection
138        self.collection = self._createType(self.folder, 'Topic', 'collection')
139
140    def _createType(self, context, portal_type, id, **kwargs):
141        """Helper method to create a new type
142        """
143        ttool = getToolByName(context, 'portal_types')
144        cat = self.portal.portal_catalog
145
146        fti = ttool.getTypeInfo(portal_type)
147        fti.constructInstance(context, id, **kwargs)
148        obj = getattr(context.aq_inner.aq_explicit, id)
149        cat.indexObject(obj)
150        return obj
151
152
153    def renderer(self, context=None, request=None, view=None, manager=None, assignment=None):
154        context = context or self.folder
155        request = request or self.folder.REQUEST
156        view = view or self.folder.restrictedTraverse('@@plone')
157        manager = manager or getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
158        assignment = assignment
159        return getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
160
161    def testSimpleQuery(self):
162        # set up our collection to search for Folders
163        crit = self.folder.collection.addCriterion('portal_type', 'ATSimpleStringCriterion')
164        crit.setValue('Folder')
165
166        # add a few folders
167        for i in range(6):
168            self.folder.invokeFactory('Folder', 'folder_%s'%i)
169            getattr(self.folder, 'folder_%s'%i).reindexObject()
170
171        # the folders are returned by the topic
172        collection_num_items = len(self.folder.collection.queryCatalog())
173        # We better have some folders
174        self.failUnless(collection_num_items >= 6)
175
176        mapping = PortletAssignmentMapping()
177        request = self.folder.REQUEST
178        mapping['foo'] = collection.Assignment(header=u"title", target_collection='/Members/test_user_1_/collection')
179        collectionrenderer = self.renderer(context=None, request=None, view=None, manager=None, assignment=mapping['foo'])
180
181        # we want the portlet to return us the same results as the collection
182        self.assertEquals(collection_num_items, len(collectionrenderer.results()))
183
184    def testRandomQuery(self):
185        # we're being perhaps a bit too clever in random mode with the internals of the
186        # LazyMap returned by the collection query, so let's try a bunch of scenarios
187        # to make sure they work
188
189        # set up our portlet renderer
190        mapping = PortletAssignmentMapping()
191        request = self.folder.REQUEST
192        mapping['foo'] = collection.Assignment(header=u"title",
193                                               random=True,
194                                               target_collection='/Members/test_user_1_/collection')
195        collectionrenderer = self.renderer(context=None, request=None, view=None, manager=None, assignment=mapping['foo'])
196
197        # add some folders
198        for i in range(6):
199            self.folder.invokeFactory('Folder', 'folder_%s'%i)
200            getattr(self.folder, 'folder_%s'%i).reindexObject()
201
202        # collection with no criteria -- should return empty list, without error
203        self.assertEqual(len(collectionrenderer.results()), 0)
204
205        # let's make sure the results aren't being memoized
206        old_func = self.folder.collection.queryCatalog
207        global collection_was_called
208        collection_was_called = False
209        def mark_collection_called(**kw):
210            global collection_was_called
211            collection_was_called = True
212        self.folder.collection.queryCatalog = mark_collection_called
213        collectionrenderer.results()
214        self.folder.collection.queryCatalog = old_func
215        self.failUnless(collection_was_called)
216
217        # collection with simple criterion -- should return 1 (random) folder
218        crit = self.folder.collection.addCriterion('portal_type', 'ATSimpleStringCriterion')
219        crit.setValue('Folder')
220        self.assertEqual(len(collectionrenderer.results()), 1)
221
222        # collection with multiple criteria -- should behave similarly
223        crit = self.folder.collection.addCriterion('Creator', 'ATSimpleStringCriterion')
224        crit.setValue('test_user_1_')
225        collectionrenderer.results()
226
227        # collection with sorting -- should behave similarly (sort is ignored internally)
228        self.folder.collection.setSortCriterion('modified', False)
229        self.assertEqual(len(collectionrenderer.results()), 1)
230
231        # same criteria, now with limit set to 2 -- should return 2 (random) folders
232        collectionrenderer.data.limit = 2
233        self.assertEqual(len(collectionrenderer.results()), 2)
234
235        # make sure there's no error if the limit is greater than the # of results found
236        collectionrenderer.data.limit = 10
237        self.failUnless(len(collectionrenderer.results()) >= 6)
238
239
240def test_suite():
241    from unittest import TestSuite, makeSuite
242    suite = TestSuite()
243    suite.addTest(makeSuite(TestQPortletCollection))
244    suite.addTest(makeSuite(TestQPortletCollectionRenderer))
245    suite.addTest(makeSuite(TestQPortletCollectionQuery))
246    return suite
Note: See TracBrowser for help on using the repository browser.