[wf] Fix Workflow.replace_state()
authorChristophe de Vienne <christophe@unlish.com>
Wed, 13 May 2015 17:12:41 +0200
changeset 10374 699e49d76334
parent 10371 88577b10b31e
child 10384 8f05aaabf355
[wf] Fix Workflow.replace_state() Closes #5359131
entities/test/unittest_wfobjs.py
entities/wfobjs.py
--- a/entities/test/unittest_wfobjs.py	Wed Feb 25 10:33:41 2015 +0100
+++ b/entities/test/unittest_wfobjs.py	Wed May 13 17:12:41 2015 +0200
@@ -416,6 +416,32 @@
                 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):
 
--- a/entities/wfobjs.py	Wed Feb 25 10:33:41 2015 +0100
+++ b/entities/wfobjs.py	Wed May 13 17:12:41 2015 +0200
@@ -174,12 +174,14 @@
             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()