|
1 # copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 import copy |
|
20 |
|
21 from cubicweb.devtools import testlib |
|
22 from cubicweb.wfutils import setup_workflow |
|
23 |
|
24 |
|
25 class TestWFUtils(testlib.CubicWebTC): |
|
26 |
|
27 defs = { |
|
28 'group': { |
|
29 'etypes': 'CWGroup', |
|
30 'default': True, |
|
31 'initial_state': u'draft', |
|
32 'states': [u'draft', u'published'], |
|
33 'transitions': { |
|
34 u'publish': { |
|
35 'fromstates': u'draft', |
|
36 'tostate': u'published', |
|
37 'requiredgroups': u'managers' |
|
38 } |
|
39 } |
|
40 } |
|
41 } |
|
42 |
|
43 def test_create_workflow(self): |
|
44 with self.admin_access.cnx() as cnx: |
|
45 wf = setup_workflow(cnx, 'group', self.defs['group']) |
|
46 self.assertEqual(wf.name, 'group') |
|
47 self.assertEqual(wf.initial.name, u'draft') |
|
48 |
|
49 draft = wf.state_by_name(u'draft') |
|
50 self.assertIsNotNone(draft) |
|
51 |
|
52 published = wf.state_by_name(u'published') |
|
53 self.assertIsNotNone(published) |
|
54 |
|
55 publish = wf.transition_by_name(u'publish') |
|
56 self.assertIsNotNone(publish) |
|
57 |
|
58 self.assertEqual(publish.destination_state, (published, )) |
|
59 self.assertEqual(draft.allowed_transition, (publish, )) |
|
60 |
|
61 self.assertEqual( |
|
62 {g.name for g in publish.require_group}, |
|
63 {'managers'}) |
|
64 |
|
65 def test_update(self): |
|
66 with self.admin_access.cnx() as cnx: |
|
67 wf = setup_workflow(cnx, 'group', self.defs['group']) |
|
68 eid = wf.eid |
|
69 |
|
70 with self.admin_access.cnx() as cnx: |
|
71 wfdef = copy.deepcopy(self.defs['group']) |
|
72 wfdef['states'].append('new') |
|
73 wfdef['initial_state'] = 'new' |
|
74 wfdef['transitions'][u'publish']['fromstates'] = ('draft', 'new') |
|
75 wfdef['transitions'][u'publish']['requiredgroups'] = ( |
|
76 u'managers', u'users') |
|
77 wfdef['transitions'][u'todraft'] = { |
|
78 'fromstates': ('new', 'published'), |
|
79 'tostate': 'draft', |
|
80 } |
|
81 |
|
82 wf = setup_workflow(cnx, 'group', wfdef) |
|
83 self.assertEqual(wf.eid, eid) |
|
84 self.assertEqual(wf.name, 'group') |
|
85 self.assertEqual(wf.initial.name, u'new') |
|
86 |
|
87 new = wf.state_by_name(u'new') |
|
88 self.assertIsNotNone(new) |
|
89 |
|
90 draft = wf.state_by_name(u'draft') |
|
91 self.assertIsNotNone(draft) |
|
92 |
|
93 published = wf.state_by_name(u'published') |
|
94 self.assertIsNotNone(published) |
|
95 |
|
96 publish = wf.transition_by_name(u'publish') |
|
97 self.assertIsNotNone(publish) |
|
98 |
|
99 todraft = wf.transition_by_name(u'todraft') |
|
100 self.assertIsNotNone(todraft) |
|
101 |
|
102 self.assertEqual( |
|
103 {g.name for g in publish.require_group}, |
|
104 {'managers', 'users'}) |
|
105 |
|
106 self.assertEqual(publish.destination_state, (published, )) |
|
107 self.assertEqual(draft.allowed_transition, (publish, )) |
|
108 self.assertEqual(todraft.destination_state, (draft, )) |
|
109 self.assertEqual(published.allowed_transition, (todraft, )) |
|
110 self.assertCountEqual(new.allowed_transition, (publish, todraft)) |
|
111 |
|
112 |
|
113 if __name__ == '__main__': |
|
114 import unittest |
|
115 unittest.main() |