Commit de9de61e authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

[wf] Fix Workflow.replace_state()

Closes #5359131
parent 88577b10b31e
......@@ -416,6 +416,32 @@ class WorkflowTC(CubicWebTC):
group.cw_clear_all_caches()
self.assertEqual(iworkflowable.state, nextstate)
def test_replace_state(self):
with self.admin_access.shell() as shell:
wf = add_wf(shell, 'CWGroup', name='groupwf', default=True)
s_new = wf.add_state('new', initial=True)
s_state1 = wf.add_state('state1')
wf.add_transition('tr', (s_new,), s_state1)
shell.commit()
with self.admin_access.repo_cnx() as cnx:
group = cnx.create_entity('CWGroup', name=u'grp1')
cnx.commit()
iwf = group.cw_adapt_to('IWorkflowable')
iwf.fire_transition('tr')
cnx.commit()
group.cw_clear_all_caches()
wf = cnx.entity_from_eid(wf.eid)
wf.add_state('state2')
with cnx.security_enabled(write=False):
wf.replace_state('state1', 'state2')
cnx.commit()
self.assertEqual(iwf.state, 'state2')
self.assertEqual(iwf.latest_trinfo().to_state[0].name, 'state2')
class CustomWorkflowTC(CubicWebTC):
......
......@@ -174,12 +174,14 @@ class Workflow(AnyEntity):
todelstate = self.state_by_name(todelstate)
if not hasattr(replacement, 'eid'):
replacement = self.state_by_name(replacement)
args = {'os': todelstate.eid, 'ns': replacement.eid}
execute = self._cw.execute
execute('SET X in_state S WHERE S eid %(s)s', {'s': todelstate.eid})
execute('SET X from_state NS WHERE X to_state OS, OS eid %(os)s, NS eid %(ns)s',
{'os': todelstate.eid, 'ns': replacement.eid})
execute('SET X to_state NS WHERE X to_state OS, OS eid %(os)s, NS eid %(ns)s',
{'os': todelstate.eid, 'ns': replacement.eid})
execute('SET X in_state NS WHERE X in_state OS, '
'NS eid %(ns)s, OS eid %(os)s', args)
execute('SET X from_state NS WHERE X from_state OS, '
'OS eid %(os)s, NS eid %(ns)s', args)
execute('SET X to_state NS WHERE X to_state OS, '
'OS eid %(os)s, NS eid %(ns)s', args)
todelstate.cw_delete()
......
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