source: products/quintagroup.plonetabs/branches/tests/quintagroup/plonetabs/tests/test_controlpanel.py @ 3403

Last change on this file since 3403 was 3403, checked in by potar, 8 years ago

Fixed code according with pylint, pyflakes

File size: 20.0 KB
Line 
1import unittest
2
3from zope.interface import Interface
4from zope.interface.verify import verifyClass
5from zope.component import getMultiAdapter, provideAdapter
6
7from Products.CMFCore.utils import getToolByName
8from Products.CMFCore.ActionInformation import Action
9
10from quintagroup.plonetabs import messageFactory as _
11from quintagroup.plonetabs.browser.interfaces import IPloneTabsControlPanel
12from quintagroup.plonetabs.browser.plonetabs \
13                                      import PloneTabsControlPanel as ptp
14from quintagroup.plonetabs.tests.base import PloneTabsTestCase
15from quintagroup.plonetabs.tests.data import PORTAL_ACTIONS
16
17
18class TestControlPanelHelperMethods(PloneTabsTestCase):
19    """Test here configlet helper methods"""
20    def afterSetUp(self):
21        super(TestControlPanelHelperMethods, self).afterSetUp()
22        self.loginAsPortalOwner()
23        panel = getMultiAdapter((self.portal, self.portal.REQUEST),
24            name='plonetabs-controlpanel')
25        # we need this to apply zope2 security (got from zope2 traverse method)
26        self.panel = panel.__of__(self.portal)
27        self.tool = getToolByName(self.portal, 'portal_actions')
28
29    def test_redirect(self):
30        response = self.portal.REQUEST.RESPONSE
31        method = self.panel.redirect
32        portal_url = getMultiAdapter((self.portal, self.portal.REQUEST),
33                                      name=u"plone_portal_state").portal_url()
34        url = '%s/@@plonetabs-controlpanel' % portal_url
35        method()
36        self.assertEquals(response.headers.get('location', ''), url,
37            'Redirect method is not working properly.')
38
39        # check query string and anchor hash
40        method('http://quintagroup.com', 'q=test', 'hash_code')
41        self.assertEquals(response.headers.get('location', ''),
42            'http://quintagroup.com?q=test#hash_code',
43            'Redirect method is not working properly.')
44
45    def test_fixExpression(self):
46        method = self.panel.fixExpression
47        self.assertEquals(method('/slash'), 'string:${portal_url}/slash')
48        self.assertEquals(method('https://test.com'),
49                                 'string:https://test.com')
50        self.assertEquals(method('python:True'), 'python:True')
51        self.assertEquals(method('hello'), 'string:${object_url}/hello')
52
53    def test_copyAction(self):
54        data = PORTAL_ACTIONS[0][1]['children'][0][1]
55        action = Action('act1', **data)
56        info = self.panel.copyAction(action)
57        self.assertEquals(len(info.keys()), 6)
58        self.assertEquals(info['description'], 'The most important place')
59
60    def test_validateActionFields(self):
61        method = self.panel.validateActionFields
62        good_data = PORTAL_ACTIONS[0][1]['children'][0][1].copy()
63        good_data['id'] = 'new_one'
64        errors = method('new_category', good_data)
65        self.assertEquals(errors, {},
66            'There should be no errors for valid data.')
67
68        bad_data = {'id': '',
69                    'title': ' ',
70                    'available_expr': 'bad_type:test',
71                    'url_expr': 'bad_type:test'}
72
73        # Revert PloneTestCase's optimization
74        # because this breaks our test
75        #### pyflakes.scripts.pyflakes, modified:
76        ## - return array of warnings instead of printing them
77        ## - honour pyflakes:ignore comments
78        def __init__(self, text):
79            self.text = text
80            if text.strip():
81                self._v_compiled = getEngine().compile(text)
82        from Products.CMFCore.Expression import Expression
83
84        optimized__init__ = Expression.__init__
85        Expression.__init__ = __init__
86        errors = method('new_category', bad_data)
87        # rollback our patch
88        Expression.__init__ = optimized__init__
89
90        self.assertEquals(len(errors.keys()), 4,
91            'validateActionFields method is not working properly.')
92        #### pyflakes.scripts.pyflakes ends.
93
94    def test_processErrors(self):
95        method = self.panel.processErrors
96        errors = {'error': 'error message'}
97        self.assertEquals(method(errors), errors,
98            'processErrors method is not working properly.')
99        self.assertEquals(method(errors, 'pre_', '_post'),
100            {'pre_error_post': 'error message'},
101            'processErrors method is not working properly.')
102
103    def test_parseEditForm(self):
104        method = self.panel.parseEditForm
105        form = {'orig_id': 'id1',
106                'category': 'cat1',
107                'visible_id1': True,
108                'id_id1': 'id_new',
109                'title_id1': 'title1',
110                'url_expr_id1': 'expr1',
111                'available_expr_id1': 'expr2'}
112        self.assertEquals(method(form),
113            ('id1', 'cat1', {'id': 'id_new',
114                             'title': 'title1',
115                             'url_expr': 'expr1',
116                             'available_expr': 'expr2',
117                             'visible': True}),
118            'parseEditForm method is not working properly.')
119
120        del form['orig_id']
121        self.failUnlessRaises(KeyError, method, form)
122
123    def test_parseAddForm(self):
124        method = self.panel.parseAddForm
125        form = {'id': 'id1',
126                'category': 'cat1',
127                'visible': True,
128                'title': 'title1',
129                'url_expr': 'string:expr1',
130                'available_expr': 'expr2'}
131        self.assertEquals(method(form),
132            ('id1', 'cat1', {'id': 'id1',
133                             'visible': True,
134                             'title': 'title1',
135                             'url_expr': 'string:expr1',
136                             'available_expr': 'expr2'}),
137            'parseAddForm method is not working properly.')
138
139        del form['id']
140        self.failUnlessRaises(KeyError, method, form)
141
142    def test_getActionCategory(self):
143        method = self.panel.getActionCategory
144        self.purgeActions()
145        self.failUnlessRaises(KeyError, method, 'portal_tabs')
146
147        self.setupActions(self.tool)
148        self.assertEquals(method('portal_tabs').id, 'portal_tabs',
149            'getActionCategory is not working properly.')
150
151    def test_getOrCreateCategory(self):
152        method = self.panel.getOrCreateCategory
153        self.purgeActions()
154        self.assertEquals(method('portal_tabs').id, 'portal_tabs',
155            'getOrCreateCategory is not working properly.')
156
157    def test_setSiteProperties(self):
158        self.panel.setSiteProperties(title='Test Title')
159        sp = getToolByName(self.portal, 'portal_properties').site_properties
160        self.assertEquals(sp.getProperty('title'), 'Test Title',
161            'setSiteProperties method is not working properly.')
162
163    def test_renderViewlet(self):
164        # register test viewlet and it's manager
165        from zope.viewlet.interfaces import IViewlet, IViewletManager
166        from zope.viewlet.viewlet import ViewletBase
167        from zope.viewlet.manager import ViewletManagerBase
168        from zope.publisher.interfaces.browser import IDefaultBrowserLayer
169        from zope.publisher.interfaces.browser import IBrowserView
170
171        class TestViewlet(ViewletBase):
172            def __of__(self, obj):
173                return self
174
175            def render(self):
176                return 'test viewlet'
177
178        provideAdapter(
179            TestViewlet,
180            (Interface, IDefaultBrowserLayer, IBrowserView, IViewletManager),
181            IViewlet,
182            name=u'test_viewlet')
183        provideAdapter(
184            ViewletManagerBase,
185            (Interface, IDefaultBrowserLayer, IBrowserView),
186            IViewletManager,
187            name=u'test_manager')
188
189        self.assertEquals(
190            self.panel.renderViewlet('test_manager', 'test_viewlet'),
191            'test viewlet',
192            'renderViewlet method is not workig properly')
193
194    def test_addAction(self):
195        self.purgeActions()
196        self.panel.addAction('new_category', {'id': 'id1', 'title': 'Test'})
197        self.failUnless('id1' in self.tool.new_category.objectIds(),
198            'addAction method is not workig properly')
199
200    def test_updateAction(self):
201        method = self.panel.updateAction
202
203        self.purgeActions()
204        self.failUnlessRaises(KeyError, method, 'id1', 'cat1', {'id': 'new'})
205
206        self.setupActions(self.tool)
207        # we need to commit transaction because
208        # we are going to rename just added action now
209        import transaction
210        transaction.savepoint()
211        method('home', 'portal_tabs', {'id': 'new_home'})
212        self.failUnless('new_home' in self.tool.portal_tabs.objectIds(),
213            'updateAction method is not workig properly')
214
215    def test_deleteAction(self):
216        self.purgeActions()
217        self.setupActions(self.tool)
218        self.panel.deleteAction('home', 'portal_tabs')
219        self.failIf('home' in self.tool.portal_tabs.objectIds(),
220             'deleteAction method is not workig properly')
221
222    def test_moveAction(self):
223        self.purgeActions()
224        self.setupActions(self.tool)
225        pos = self.tool.portal_tabs.getObjectPosition
226        self.assertEquals(pos('home'), 0,
227            'moveAction method is not workig properly')
228        self.panel.moveAction('home', 'portal_tabs', -1)
229        self.assertEquals(pos('home'), 1,
230             'moveAction method is not workig properly')
231
232
233class TestControlPanelAPIMethods(PloneTabsTestCase):
234    """Test here interface methods of control panel class"""
235
236    def afterSetUp(self):
237        super(TestControlPanelAPIMethods, self).afterSetUp()
238        self.loginAsPortalOwner()
239        panel = getMultiAdapter((self.portal, self.portal.REQUEST),
240            name='plonetabs-controlpanel')
241        # we need this to apply zope2 security (got from zope2 traverse method)
242        self.panel = panel.__of__(self.portal)
243        self.tool = getToolByName(self.portal, 'portal_actions')
244
245    def test_interface(self):
246        self.failUnless(IPloneTabsControlPanel.implementedBy(ptp),
247            'PloneTabs control panel does not implement required interface.')
248        self.failUnless(verifyClass(IPloneTabsControlPanel, ptp),
249            'PloneTabs control panel does not implement required interface.')
250
251    def test_getPageTitle(self):
252        self.assertEquals(self.panel.getPageTitle(),
253            _(u"Portal Tabs Configuration"),
254            'getPageTitle method is broken')
255        self.assertEquals(self.panel.getPageTitle(category='notexists'),
256            _(u"Plone '${cat_name}' Configuration",
257              mapping={'cat_name': 'notexists'}),
258            'getPageTitle method is broken')
259
260    def test_hasActions(self):
261        method = self.panel.hasActions
262        # purge any default portal actions
263        self.purgeActions()
264        self.failIf(method(),
265            'There should be no portal_tab actions in portal')
266
267        # setup our own actions
268        self.setupActions(self.tool)
269        self.failUnless(method(),
270            'There should be portal_tab actions in portal')
271
272    def test_getPortalActions(self):
273        method = self.panel.getPortalActions
274        # purge any default portal actions
275        self.purgeActions()
276        self.assertEquals(len(method()), 0,
277            'There should be no actions in portal_tabs category.')
278
279        # setup our own actions
280        self.setupActions(self.tool)
281        self.assertEquals(len(method()), 2,
282            'There should be 2 actions in portal_tabs category.')
283
284        # marginal arguments
285        self.assertEquals(len(method('notexistent_category')), 0,
286            'There should be no actions for not existed category.')
287
288    def test_isGeneratedTabs(self):
289        method = self.panel.isGeneratedTabs
290        # prepare value
291        sp = getToolByName(self.portal, 'portal_properties').site_properties
292        sp.manage_changeProperties(disable_folder_sections=True)
293        self.failIf(method(), 'But folder sections are disabled...')
294
295    def test_isNotFoldersGenerated(self):
296        method = self.panel.isNotFoldersGenerated
297        # prepare value
298        sp = getToolByName(self.portal, 'portal_properties').site_properties
299        sp.manage_changeProperties(disable_nonfolderish_sections=True)
300        self.failIf(method(), 'But non folderish sections are disabled...')
301
302    def test_getActionsList(self):
303        method = self.panel.getActionsList
304        # purge any default portal actions
305        self.purgeActions()
306        self.failIf('class="editform"' in method(),
307            'There should no be actions in actions list template.')
308        self.setupActions(self.tool)
309        self.failUnless('class="editform"' in method(),
310            'There are no actions in actions list template.')
311
312    def test_getAutoGenereatedSection(self):
313        method = self.panel.getAutoGenereatedSection
314        self.failIf('<form' in method('user'),
315            'There should be no form in autogenerated tabs template '
316            'for category other than portal_tabs.')
317        self.failUnless('<form' in method('portal_tabs'),
318            'There should be form in autogenerated tabs template '
319            'for portal_tabs category.')
320
321    def test_getGeneratedTabs(self):
322        self.panel.getGeneratedTabs()
323        # check expiration header set by generated tabs template
324        self.assertEquals(
325            self.portal.REQUEST.RESPONSE.headers.get('expires', ''),
326            'Mon, 26 Jul 1996 05:00:00 GMT',
327            'Expiration header is not set properly.')
328
329    def test_getRootTabs(self):
330        method = self.panel.getRootTabs
331        # make sure we don't depend on external settings
332        self.purgeContent()
333        self.assertEquals(len(method()), 0,
334            'There should be no root elements for navigation.')
335
336        # now add some testing content
337        self.setupContent(self.portal)
338        self.assertEquals(len(method()), 2,
339            'There should be 2 elements in portal root for navigation.')
340
341        # now switch off autogeneration
342        sp = getToolByName(self.portal, 'portal_properties').site_properties
343        sp.manage_changeProperties(disable_folder_sections=True)
344        self.assertEquals(len(method()), 0,
345            'There should be no root elements for navigation when '
346            'tabs autogeneration is switched off.')
347
348    def test_getCategories(self):
349        method = self.panel.getCategories
350        # purge any default portal actions
351        self.purgeActions()
352        self.assertEquals(len(method()), 0,
353            'There should be no categories in portal_actions tool.')
354
355        # now setup actions
356        self.setupActions(self.tool)
357        self.assertEquals(method(), ['portal_tabs', 'new_category'],
358            'There should be exactly 2 categories in portal_actions tool.')
359
360    def test_portal_tabs(self):
361        method = self.panel.portal_tabs
362        self.purgeContent()
363        self.purgeActions()
364        self.assertEquals(len(method()), 0,
365            'There should be no portal tabs.')
366
367        # cleanup memoize cache
368        # cause actions method of portal context state is caching it's
369        # results in request and we have the same request for every call
370        self.purgeCache(self.portal.REQUEST)
371
372        # add actions
373        self.setupActions(self.tool)
374        self.assertEquals(len(method()), 2,
375            'There should be 2 portal tabs.')
376
377        # add content
378        self.setupContent(self.portal)
379        self.assertEquals(len(method()), 4,
380            'There should be 4 portal tabs.')
381
382    def test_selected_portal_tab(self):
383        self.assertEquals(self.panel.selected_portal_tab(), 'index_html',
384            'index_html is not selected tab while being on configlet.')
385
386    def test_test(self):
387        self.assertEquals(self.panel.test(True, 'true', 'false'), 'true',
388            'Test function does not work properly.')
389
390
391class TestControlPanelManageMethods(PloneTabsTestCase):
392    """Test here management methods of control panel class"""
393
394    def afterSetUp(self):
395        super(TestControlPanelManageMethods, self).afterSetUp()
396        self.loginAsPortalOwner()
397        panel = getMultiAdapter((self.portal, self.portal.REQUEST),
398            name='plonetabs-controlpanel')
399        # we need this to apply zope2 security (got from zope2 traverse method)
400        self.panel = panel.__of__(self.portal)
401        self.tool = getToolByName(self.portal, 'portal_actions')
402
403        # purge standard set of actions and set our own testing ones
404        self.purgeActions()
405        self.setupActions(self.tool)
406
407    def test_manage_setAutogeneration(self):
408        self.setupContent(self.portal)
409        form = {'generated_tabs': '1',
410                'nonfolderish_tabs': '0',
411                'folder1': '0'}
412        self.panel.manage_setAutogeneration(form, {})
413        self.failUnless(self.portal.folder1.exclude_from_nav())
414        sp = getToolByName(self.portal, 'portal_properties').site_properties
415        self.failIf(sp.disable_folder_sections)
416        self.failUnless(sp.disable_nonfolderish_sections)
417
418    def test_manage_addAction(self):
419        self.purgeActions()
420        form = {'id': 'id1',
421                'category': 'cat1',
422                'visible': True,
423                'title': 'title1',
424                'url_expr': 'string:expr1',
425                'available_expr': 'expr2'}
426        postback = self.panel.manage_addAction(form, {})
427        self.failUnless('id1' in self.tool.cat1.objectIds())
428        self.failIf(postback,
429            'There should be redirect after successfull adding.')
430
431    def test_manage_editAction(self):
432        method = self.panel.manage_editAction
433        self.purgeActions()
434        self.setupActions(self.tool)
435        form = {'orig_id': 'home',
436                'category': 'portal_tabs',
437                'visible_home': True,
438                'id_home': 'id_new',
439                'title_home': 'title1',
440                'url_expr_home': 'expr1',
441                'available_expr_home': 'expr2'}
442        import transaction
443        transaction.savepoint()
444
445        postback = method(form, {})
446        self.failUnless('id_new' in self.tool.portal_tabs.objectIds())
447        self.failIf(postback,
448            'There should be redirect after successfull edition.')
449
450        form['category'] = 'non_existent'
451        self.failUnlessRaises(KeyError, method, form, {})
452
453    def test_manage_deleteAction(self):
454        self.purgeActions()
455        self.setupActions(self.tool)
456        form = {'orig_id': 'home',
457                'category': 'portal_tabs',
458                'visible_home': True,
459                'id_home': 'id_new',
460                'title_home': 'title1',
461                'url_expr_home': 'expr1',
462                'available_expr_home': 'expr2'}
463        self.panel.manage_deleteAction(form, {})
464        self.failIf('home' in self.tool.portal_tabs.objectIds())
465
466    def test_manage_moveUpAction(self):
467        self.purgeActions()
468        self.setupActions(self.tool)
469        form = {'orig_id': 'quintagroup',
470                'category': 'portal_tabs',
471                'visible_quintagroup': True,
472                'id_quintagroup': 'quintagroup',
473                'title_quintagroup': 'title1',
474                'url_expr_quintagroup': 'expr1',
475                'available_expr_quintagroup': 'expr2'}
476        self.panel.manage_moveUpAction(form, {})
477        self.assertEquals(
478            self.tool.portal_tabs.getObjectPosition('quintagroup'), 0)
479
480    def test_manage_moveDownAction(self):
481        self.purgeActions()
482        self.setupActions(self.tool)
483        form = {'orig_id': 'home',
484                'category': 'portal_tabs',
485                'visible_home': True,
486                'id_home': 'home',
487                'title_home': 'title1',
488                'url_expr_home': 'expr1',
489                'available_expr_home': 'expr2'}
490        self.panel.manage_moveDownAction(form, {})
491        self.assertEquals(self.tool.portal_tabs.getObjectPosition('home'), 1)
492
493
494def test_suite():
495    suite = unittest.TestSuite()
496    suite.addTest(unittest.makeSuite(TestControlPanelHelperMethods))
497    suite.addTest(unittest.makeSuite(TestControlPanelAPIMethods))
498    suite.addTest(unittest.makeSuite(TestControlPanelManageMethods))
499
500    # these tests are implemented as Selenium KSS Tests
501    # using kss.demo package, and KSS plugins are tested by means of
502    # ecmaunit.js
503    #suite.addTest(unittest.makeSuite(TestControlPanelKSSMethods))
504    return suite
Note: See TracBrowser for help on using the repository browser.