Back to index

plone3  3.1.7
test_DCWorkflow.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for DCWorkflow module.
00014 
00015 $Id: test_DCWorkflow.py 78745 2007-08-10 20:19:46Z mj $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from zope.component import adapter
00022 from zope.component import provideHandler
00023 
00024 from Products.CMFCore.testing import TraversingEventZCMLLayer
00025 from Products.CMFCore.tests.base.dummy import DummyContent
00026 from Products.CMFCore.tests.base.dummy import DummySite
00027 from Products.CMFCore.tests.base.dummy import DummyTool
00028 from Products.CMFCore.WorkflowTool import WorkflowTool
00029 from Products.DCWorkflow.interfaces import IAfterTransitionEvent
00030 from Products.DCWorkflow.interfaces import IBeforeTransitionEvent
00031 
00032 
00033 class DCWorkflowDefinitionTests(unittest.TestCase):
00034 
00035     layer = TraversingEventZCMLLayer
00036 
00037     def setUp(self):
00038         self.site = DummySite('site')
00039         self.site._setObject( 'portal_types', DummyTool() )
00040         self.site._setObject( 'portal_workflow', WorkflowTool() )
00041         self._constructDummyWorkflow()
00042 
00043     def test_z2interfaces(self):
00044         from Interface.Verify import verifyClass
00045         from Products.CMFCore.interfaces.portal_workflow \
00046              import WorkflowDefinition as IWorkflowDefinition
00047         from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
00048 
00049         verifyClass(IWorkflowDefinition, DCWorkflowDefinition)
00050 
00051     def test_z3interfaces(self):
00052         from zope.interface.verify import verifyClass
00053         from Products.CMFCore.interfaces import IWorkflowDefinition
00054         from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
00055 
00056         verifyClass(IWorkflowDefinition, DCWorkflowDefinition)
00057 
00058     def _constructDummyWorkflow(self):
00059         from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition
00060 
00061         wftool = self.site.portal_workflow
00062         wftool._setObject('wf', DCWorkflowDefinition('wf'))
00063         wftool.setDefaultChain('wf')
00064         wf = wftool.wf
00065 
00066         wf.states.addState('private')
00067         sdef = wf.states['private']
00068         sdef.setProperties( transitions=('publish',) )
00069 
00070         wf.states.addState('published')
00071         wf.states.setInitialState('private')
00072 
00073         wf.transitions.addTransition('publish')
00074         tdef = wf.transitions['publish']
00075         tdef.setProperties(title='', new_state_id='published', actbox_name='')
00076 
00077         wf.variables.addVariable('comments')
00078         vdef = wf.variables['comments']
00079         vdef.setProperties(description='',
00080                  default_expr="python:state_change.kwargs.get('comment', '')",
00081                  for_status=1, update_always=1)
00082 
00083     def _getDummyWorkflow(self):
00084         wftool = self.site.portal_workflow
00085         return wftool.wf
00086 
00087     def test_doActionFor(self):
00088 
00089         wftool = self.site.portal_workflow
00090         wf = self._getDummyWorkflow()
00091 
00092         dummy = self.site._setObject( 'dummy', DummyContent() )
00093         wftool.notifyCreated(dummy)
00094         self.assertEqual( wf._getStatusOf(dummy),
00095                           {'state': 'private', 'comments': ''} )
00096         wf.doActionFor(dummy, 'publish', comment='foo' )
00097         self.assertEqual( wf._getStatusOf(dummy),
00098                           {'state': 'published', 'comments': 'foo'} )
00099 
00100         # XXX more
00101 
00102     def test_events(self):
00103 
00104         events = []
00105 
00106         @adapter(IBeforeTransitionEvent)
00107         def _handleBefore(event):
00108             events.append(event)
00109         provideHandler(_handleBefore)
00110 
00111         @adapter(IAfterTransitionEvent)
00112         def _handleAfter(event):
00113             events.append(event)
00114         provideHandler(_handleAfter)
00115 
00116         wftool = self.site.portal_workflow
00117         wf = self._getDummyWorkflow()
00118 
00119         dummy = self.site._setObject( 'dummy', DummyContent() )
00120         wftool.notifyCreated(dummy)
00121         wf.doActionFor(dummy, 'publish', comment='foo', test='bar')
00122 
00123         self.assertEquals(4, len(events))
00124 
00125         evt = events[0]
00126         self.failUnless(IBeforeTransitionEvent.providedBy(evt))
00127         self.assertEquals(dummy, evt.object)
00128         self.assertEquals('private', evt.old_state.id)
00129         self.assertEquals('private', evt.new_state.id)
00130         self.assertEquals(None, evt.transition)
00131         self.assertEquals({}, evt.status)
00132         self.assertEquals(None, evt.kwargs)
00133 
00134         evt = events[1]
00135         self.failUnless(IAfterTransitionEvent.providedBy(evt))
00136         self.assertEquals(dummy, evt.object)
00137         self.assertEquals('private', evt.old_state.id)
00138         self.assertEquals('private', evt.new_state.id)
00139         self.assertEquals(None, evt.transition)
00140         self.assertEquals({'state': 'private', 'comments': ''}, evt.status)
00141         self.assertEquals(None, evt.kwargs)
00142 
00143         evt = events[2]
00144         self.failUnless(IBeforeTransitionEvent.providedBy(evt))
00145         self.assertEquals(dummy, evt.object)
00146         self.assertEquals('private', evt.old_state.id)
00147         self.assertEquals('published', evt.new_state.id)
00148         self.assertEquals('publish', evt.transition.id)
00149         self.assertEquals({'state': 'private', 'comments': ''}, evt.status)
00150         self.assertEquals({'test' : 'bar', 'comment' : 'foo'}, evt.kwargs)
00151 
00152         evt = events[3]
00153         self.failUnless(IAfterTransitionEvent.providedBy(evt))
00154         self.assertEquals(dummy, evt.object)
00155         self.assertEquals('private', evt.old_state.id)
00156         self.assertEquals('published', evt.new_state.id)
00157         self.assertEquals('publish', evt.transition.id)
00158         self.assertEquals({'state': 'published', 'comments': 'foo'}, evt.status)
00159         self.assertEquals({'test' : 'bar', 'comment' : 'foo'}, evt.kwargs)
00160 
00161     def test_checkTransitionGuard(self):
00162 
00163         wftool = self.site.portal_workflow
00164         wf = self._getDummyWorkflow()
00165         dummy = self.site._setObject( 'dummy', DummyContent() )
00166         wftool.notifyCreated(dummy)
00167         self.assertEqual( wf._getStatusOf(dummy),
00168                           {'state': 'private', 'comments': ''} )
00169 
00170         # Check
00171         self.assert_(wf._checkTransitionGuard(wf.transitions['publish'],
00172                                               dummy))
00173 
00174         # Check with kwargs propagation
00175         self.assert_(wf._checkTransitionGuard(wf.transitions['publish'],
00176                                               dummy, arg1=1, arg2=2))
00177 
00178     def test_isActionSupported(self):
00179 
00180         wf = self._getDummyWorkflow()
00181         dummy = self.site._setObject( 'dummy', DummyContent() )
00182 
00183         # check publish
00184         self.assert_(wf.isActionSupported(dummy, 'publish'))
00185 
00186         # Check with kwargs.
00187         self.assert_(wf.isActionSupported(dummy, 'publish', arg1=1, arg2=2))
00188 
00189     # XXX more tests...
00190 
00191 
00192 def test_suite():
00193     return unittest.TestSuite((
00194         unittest.makeSuite(DCWorkflowDefinitionTests),
00195         ))
00196 
00197 if __name__ == '__main__':
00198     from Products.CMFCore.testing import run
00199     run(test_suite())