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

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

Fixing pep8

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