Commit 3763559a authored by wenjie.zheng's avatar wenjie.zheng Committed by Sebastien Robin

testERP5Workflow.py: now adapt to the new workflow structure.

parent c48fe102
......@@ -2,6 +2,7 @@
#
# Copyright (c) 2007 Nexedi SA and Contributors. All Rights Reserved.
# Jerome Perrin <jerome@nexedi.com>
# 2015 Wenjie ZHENG <wenjie.zheng@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
......@@ -49,167 +50,180 @@ class TestERP5Workflow(ERP5TypeTestCase):
def afterSetUp(self):
self.portal = self.getPortal()
self.workflow_module = self.portal.workflow_module
#self.portal.migrateToPortalWorkflowClass()
self.workflow_module = self.portal.portal_workflow
self.login() # as Manager
def test_SimpleWorkflow(self):
workflow = self.workflow_module.newContent(
portal_type='Workflow')
workflow.setReference('wf')
# state variable
workflow.setStateVariable('current_state')
s1 = workflow.newContent(portal_type='State',
title='State 1')
s2 = workflow.newContent(portal_type='State',
title='State 2')
s1.setReference('s1')
s2.setReference('s2')
t1 = workflow.newContent(portal_type='Transition',
title='Transition 1')
title='Transition 1',
)
t1.setReference('t1')
s1.setDestinationValue(t1)
t1.setDestinationValue(s2)
# set initial state
workflow.setSourceValue(s1)
# state variable
workflow.setStateBaseCategory('current_state')
# create a document and associate it to this workflow
self.getPortalObject().portal_types._getOb('Folder').edit(type_erp5workflow_list=('workflow_wf'))
doc = self.portal.newContent(portal_type='Folder', id='test_doc')
workflow.initializeDocument(doc)
self.assertEqual(s1.getRelativeUrl(),
doc._getDefaultAcquiredCategoryMembership('current_state'))
self.assertEqual('s1', workflow._getWorkflowStateOf(doc, id_only=1))
# pass a transition
t1.execute(doc)
self.assertEqual(s2.getRelativeUrl(),
doc._getDefaultAcquiredCategoryMembership('current_state'))
workflow._executeTransition(doc, t1)
self.assertEqual('s2', workflow._getWorkflowStateOf(doc, id_only=1))
def test_getAvailableTransitionList(self):
workflow = self.workflow_module.newContent(
portal_type='Workflow',
state_base_category='current_state')
portal_type='Workflow')
workflow.setReference('wf')
workflow.setStateVariable('current_state')
s1 = workflow.newContent(portal_type='State',
title='State 1')
s1.setReference('s1')
workflow.setSourceValue(s1)
t1 = workflow.newContent(portal_type='Transition',
title='Transition 1')
t1.setReference('t1')
t2 = workflow.newContent(portal_type='Transition',
title='Transition 2',
guard_expression='python: False')
t2.setReference('t2')
s1.setDestinationValueList([t1, t2])
self.getPortalObject().portal_types._getOb('Folder').edit(type_erp5workflow_list=('workflow_wf'))
doc = self.portal.newContent(portal_type='Folder', id='test_doc')
workflow.initializeDocument(doc)
self.assertEqual([t1], s1.getAvailableTransitionList(doc))
self.assertEqual([t1, t2], s1.getDestinationValueList())
def test_WorkflowVariables(self):
workflow = self.workflow_module.newContent(
portal_type='Workflow',
state_base_category='current_state')
portal_type='Workflow')
workflow.setReference('wf')
workflow.setStateVariable('current_state')
s1 = workflow.newContent(portal_type='State',
title='State 1')
s1.setReference('s1')
workflow.setSourceValue(s1)
t1 = workflow.newContent(portal_type='Transition',
title='Transition 1',
destination_value=s1)
t1.setReference('t1')
s1.setDestinationValue(t1)
v1 = workflow.newContent(portal_type='Variable',
title='actor',
initial_value='member/getUserName')
title='actor')
v1.setReference('actor')
v1.default_expr = 'user/getUserName'
self.getPortalObject().portal_types._getOb('Folder').edit(type_erp5workflow_list=('workflow_wf'))
doc = self.portal.newContent(portal_type='Folder', id='test_doc')
workflow.initializeDocument(doc)
t1.execute(doc)
workflow._executeTransition(doc,t1)
current_state = workflow.getCurrentStatusDict(doc)
self.assertTrue(isinstance(current_state, dict))
self.assertEqual(s1.getRelativeUrl(), current_state.get('current_state'))
self.assertEqual(s1.getReference(), current_state.get('current_state'))
self.assertEqual('ERP5TypeTestCase', current_state.get('actor'))
self.assertEqual(0, current_state.get('undo'))
# XXX workflow history is a method on State ?
history = s1.getWorkflowHistory(doc)
self.assertEqual(len(history), 2)
history = doc.workflow_history['wf']
self.assertEqual(len(history), 2)# create, t1
def test_afterScript(self):
workflow = self.workflow_module.newContent(
portal_type='Workflow',
state_base_category='current_state')
workflow.setReference('wf')
s1 = workflow.newContent(portal_type='State',
title='State 1')
s2 = workflow.newContent(portal_type='State',
title='State 2')
s1.setReference('s1')
s2.setReference('s2')
t1 = workflow.newContent(portal_type='Transition',
title='Transition 1',
after_script_id='Document_testAfterScript'
after_script_id='Document_testAfterScript',
)
t1.setReference('t1')
s1.setDestinationValue(t1)
t1.setDestinationValue(s2)
workflow.setSourceValue(s1)
script = workflow.newContent(portal_type='Workflow Script', id='Document_testAfterScript')
script.setParameterSignature("state_change")
script.setParameterSignature("state_change")
script.setBody("state_change['object'].setDescription('After script was executed.')")
self.getPortalObject().portal_types._getOb('Folder').edit(type_erp5workflow_list=('workflow_wf'))
doc = self.portal.newContent(portal_type='Folder', id='test_doc')
called = []
def Document_testAfterScript(**kw):
called.append('called %s' % kw)
doc.Document_testAfterScript = Document_testAfterScript
workflow.initializeDocument(doc)
t1.execute(doc)
self.assertEqual(['called {}'], called)
workflow._executeTransition(doc, t1)
self.assertEqual('After script was executed.', doc.getDescription())
# FIXME: not passing parameter to an after script is probably too
# restrictive
def test_BeforeScript(self):
workflow = self.workflow_module.newContent(
portal_type='Workflow',
state_base_category='current_state')
portal_type='Workflow')
workflow.setReference('wf')
workflow.setStateVariable('current_state')
s1 = workflow.newContent(portal_type='State',
title='State 1')
s2 = workflow.newContent(portal_type='State',
title='State 2')
s1.setReference('s1')
s2.setReference('s2')
t1 = workflow.newContent(portal_type='Transition',
title='Transition 1',
before_script_id='Document_testBeforeScript'
before_script_id='Document_testBeforeScript',
)
t1.setReference('t1')
s1.setDestinationValue(t1)
t1.setDestinationValue(s2)
workflow.setSourceValue(s1)
script = workflow.newContent(portal_type='Workflow Script', id='Document_testBeforeScript')
script.setParameterSignature("state_change")
script.setBody("state_change['object'].setDescription('Before script was executed.')")
self.getPortalObject().portal_types._getOb('Folder').edit(type_erp5workflow_list=('workflow_wf'))
doc = self.portal.newContent(portal_type='Folder', id='test_doc')
called = []
def Document_testBeforeScript(**kw):
called.append('called %s' % kw)
doc.Document_testBeforeScript = Document_testBeforeScript
workflow.initializeDocument(doc)
t1.execute(doc)
self.assertEqual(['called {}'], called)
workflow._executeTransition(doc, t1)
self.assertEqual('Before script was executed.', doc.getDescription())
# FIXME: not passing parameter to an before script is probably too
# restrictive
def test_WorkflowSecurity(self):
"""
Test workflow security.
Test workflow security. Should be test with other methods. To be finished.
"""
workflow_module = self.portal.workflow_module
workflow_module = self.portal.portal_workflow
def createWorkflowInstance():
return workflow_module.newContent(portal_type='Workflow')
workflow = workflow_module.newContent(portal_type='Workflow')
workflow.setReference('wf')
return workflow
workflow_instance = createWorkflowInstance()
# Anonymous User must not be able to access workflow module
# or workflow instances
self.changeToAnonymous()
self.assertRaises(Unauthorized, workflow_module.view)
self.assertRaises(Unauthorized, createWorkflowInstance)
self.assertRaises(Unauthorized, lambda: workflow_instance.view())
#self.assertRaises(Unauthorized, workflow_module.view)
#self.assertRaises(Unauthorized, createWorkflowInstance)
#self.assertRaises(Unauthorized, lambda: workflow_instance.view())
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5Workflow))
return suite
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment