1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests for the workflow engine. |
4
|
|
|
* |
5
|
|
|
* @author [email protected] |
6
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
7
|
|
|
* @package advancedworkflow |
8
|
|
|
* @subpackage tests |
9
|
|
|
*/ |
10
|
|
|
class WorkflowEngineTest extends SapphireTest { |
|
|
|
|
11
|
|
|
|
12
|
|
|
public static $fixture_file = 'advancedworkflow/tests/workflowinstancetargets.yml'; |
13
|
|
|
|
14
|
|
|
public function testCreateWorkflowInstance() { |
15
|
|
|
|
16
|
|
|
$definition = new WorkflowDefinition(); |
17
|
|
|
$definition->Title = "Create Workflow Instance"; |
|
|
|
|
18
|
|
|
$definition->write(); |
19
|
|
|
|
20
|
|
|
$stepOne = new WorkflowAction(); |
21
|
|
|
$stepOne->Title = "Step One"; |
|
|
|
|
22
|
|
|
$stepOne->WorkflowDefID = $definition->ID; |
|
|
|
|
23
|
|
|
$stepOne->write(); |
24
|
|
|
|
25
|
|
|
$stepTwo = new WorkflowAction(); |
26
|
|
|
$stepTwo->Title = "Step Two"; |
|
|
|
|
27
|
|
|
$stepTwo->WorkflowDefID = $definition->ID; |
|
|
|
|
28
|
|
|
$stepTwo->write(); |
29
|
|
|
|
30
|
|
|
$transitionOne = new WorkflowTransition(); |
31
|
|
|
$transitionOne->Title = 'Step One T1'; |
|
|
|
|
32
|
|
|
$transitionOne->ActionID = $stepOne->ID; |
|
|
|
|
33
|
|
|
$transitionOne->NextActionID = $stepTwo->ID; |
|
|
|
|
34
|
|
|
$transitionOne->write(); |
35
|
|
|
|
36
|
|
|
$instance = new WorkflowInstance(); |
37
|
|
|
$instance->write(); |
38
|
|
|
|
39
|
|
|
$instance->beginWorkflow($definition); |
40
|
|
|
|
41
|
|
|
$actions = $definition->Actions(); |
|
|
|
|
42
|
|
|
$this->assertEquals(2, $actions->Count()); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$transitions = $actions->find('Title', 'Step One')->Transitions(); |
45
|
|
|
$this->assertEquals(1, $transitions->Count()); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testExecuteImmediateWorkflow() { |
49
|
|
|
$def = $this->createDefinition(); |
50
|
|
|
|
51
|
|
|
$actions = $def->Actions(); |
|
|
|
|
52
|
|
|
$firstAction = $def->getInitialAction(); |
53
|
|
|
$this->assertEquals('Step One', $firstAction->Title); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$instance = new WorkflowInstance(); |
56
|
|
|
$instance->beginWorkflow($def); |
57
|
|
|
$this->assertTrue($instance->CurrentActionID > 0); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$instance->execute(); |
60
|
|
|
|
61
|
|
|
// the instance should be complete, and have two finished workflow action |
62
|
|
|
// instances. |
63
|
|
|
$actions = $instance->Actions(); |
|
|
|
|
64
|
|
|
$this->assertEquals(2, $actions->Count()); |
|
|
|
|
65
|
|
|
|
66
|
|
|
foreach($actions as $action) { |
67
|
|
|
$this->assertTrue((bool) $action->Finished); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Ensure WorkflowInstance returns expected values for a Published target object. |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function testInstanceGetTargetPublished() { |
|
|
|
|
75
|
|
|
$def = $this->createDefinition(); |
76
|
|
|
$target = $this->objFromFixture('SiteTree', 'published-object'); |
77
|
|
|
$target->doPublish(); |
78
|
|
|
|
79
|
|
|
$instance = $this->objFromFixture('WorkflowInstance', 'target-is-published'); |
80
|
|
|
$instance->beginWorkflow($def); |
81
|
|
|
$instance->execute(); |
82
|
|
|
|
83
|
|
|
$this->assertTrue($target->isPublished()); |
|
|
|
|
84
|
|
|
$this->assertEquals($target->ID, $instance->getTarget()->ID); |
|
|
|
|
85
|
|
|
$this->assertEquals($target->Title, $instance->getTarget()->Title); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Ensure WorkflowInstance returns expected values for a Draft target object. |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function testInstanceGetTargetDraft() { |
|
|
|
|
92
|
|
|
$def = $this->createDefinition(); |
93
|
|
|
$target = $this->objFromFixture('SiteTree', 'draft-object'); |
94
|
|
|
|
95
|
|
|
$instance = $this->objFromFixture('WorkflowInstance', 'target-is-draft'); |
96
|
|
|
$instance->beginWorkflow($def); |
97
|
|
|
$instance->execute(); |
98
|
|
|
|
99
|
|
|
$this->assertFalse($target->isPublished()); |
|
|
|
|
100
|
|
|
$this->assertEquals($target->ID, $instance->getTarget()->ID); |
|
|
|
|
101
|
|
|
$this->assertEquals($target->Title, $instance->getTarget()->Title); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testPublishAction() { |
105
|
|
|
$this->logInWithPermission(); |
106
|
|
|
|
107
|
|
|
$action = new PublishItemWorkflowAction; |
108
|
|
|
$instance = new WorkflowInstance(); |
109
|
|
|
|
110
|
|
|
$page = new Page(); |
111
|
|
|
$page->Title = 'stuff'; |
112
|
|
|
$page->write(); |
113
|
|
|
|
114
|
|
|
$instance->TargetClass = 'Page'; |
|
|
|
|
115
|
|
|
$instance->TargetID = $page->ID; |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->assertFalse($page->isPublished()); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$action->execute($instance); |
120
|
|
|
|
121
|
|
|
$page = DataObject::get_by_id('Page', $page->ID); |
122
|
|
|
$this->assertTrue($page->isPublished()); |
|
|
|
|
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testCreateDefinitionWithEmptyTitle() { |
127
|
|
|
$definition = new WorkflowDefinition(); |
128
|
|
|
$definition->Title = ""; |
|
|
|
|
129
|
|
|
$definition->write(); |
130
|
|
|
$this->assertContains('My Workflow',$definition->Title,'Workflow created without title is assigned a default title.'); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
protected function createDefinition() { |
|
|
|
|
134
|
|
|
$definition = new WorkflowDefinition(); |
135
|
|
|
$definition->Title = "Dummy Workflow Definition"; |
|
|
|
|
136
|
|
|
$definition->write(); |
137
|
|
|
|
138
|
|
|
$stepOne = new WorkflowAction(); |
139
|
|
|
$stepOne->Title = "Step One"; |
|
|
|
|
140
|
|
|
$stepOne->WorkflowDefID = $definition->ID; |
|
|
|
|
141
|
|
|
$stepOne->write(); |
142
|
|
|
|
143
|
|
|
$stepTwo = new WorkflowAction(); |
144
|
|
|
$stepTwo->Title = "Step Two"; |
|
|
|
|
145
|
|
|
$stepTwo->WorkflowDefID = $definition->ID; |
|
|
|
|
146
|
|
|
$stepTwo->write(); |
147
|
|
|
|
148
|
|
|
$transitionOne = new WorkflowTransition(); |
149
|
|
|
$transitionOne->Title = 'Step One T1'; |
|
|
|
|
150
|
|
|
$transitionOne->ActionID = $stepOne->ID; |
|
|
|
|
151
|
|
|
$transitionOne->NextActionID = $stepTwo->ID; |
|
|
|
|
152
|
|
|
$transitionOne->write(); |
153
|
|
|
|
154
|
|
|
return $definition; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
public function testCreateFromTemplate() { |
159
|
|
|
$structure = array( |
160
|
|
|
'First step' => array( |
161
|
|
|
'type' => 'AssignUsersToWorkflowAction', |
162
|
|
|
'transitions' => array( |
163
|
|
|
'second' => 'Second step' |
164
|
|
|
) |
165
|
|
|
), |
166
|
|
|
'Second step' => array( |
167
|
|
|
'type' => 'NotifyUsersWorkflowAction', |
168
|
|
|
'transitions' => array( |
169
|
|
|
'Approve' => 'Third step' |
170
|
|
|
) |
171
|
|
|
), |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$template = new WorkflowTemplate('Test'); |
175
|
|
|
|
176
|
|
|
$template->setStructure($structure); |
177
|
|
|
|
178
|
|
|
$actions = $template->createRelations(); |
179
|
|
|
|
180
|
|
|
$this->assertEquals(2, count($actions)); |
|
|
|
|
181
|
|
|
$this->assertTrue(isset($actions['First step'])); |
|
|
|
|
182
|
|
|
$this->assertTrue(isset($actions['Second step'])); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$this->assertTrue($actions['First step']->exists()); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$transitions = $actions['First step']->Transitions(); |
187
|
|
|
|
188
|
|
|
$this->assertTrue($transitions->count() == 1); |
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Tests whether if user(s) are able to delete a workflow, dependent on permissions. |
195
|
|
|
*/ |
196
|
|
|
public function testCanDeleteWorkflow() { |
197
|
|
|
// Create a definition |
198
|
|
|
$def = $this->createDefinition(); |
199
|
|
|
|
200
|
|
|
// Test a user with lame permissions |
201
|
|
|
$memberID = $this->logInWithPermission('SITETREE_VIEW_ALL'); |
202
|
|
|
$member = DataObject::get_by_id('Member', $memberID); |
203
|
|
|
$this->assertFalse($def->canCreate($member)); |
|
|
|
|
204
|
|
|
|
205
|
|
|
// Test a user with good permissions |
206
|
|
|
$memberID = $this->logInWithPermission('CREATE_WORKFLOW'); |
207
|
|
|
$member = DataObject::get_by_id('Member', $memberID); |
208
|
|
|
$this->assertTrue($def->canCreate($member)); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* For a context around this test, see: https://github.com/silverstripe-australia/advancedworkflow/issues/141 |
213
|
|
|
* |
214
|
|
|
* 1). Create a workflow definition |
215
|
|
|
* 2). Step the content into that workflow |
216
|
|
|
* 3). Delete the workflow |
217
|
|
|
* 4). Check that the content: |
218
|
|
|
* i). Has no remaining related actions |
219
|
|
|
* ii). Can be re-assigned a new Workflow |
220
|
|
|
* 5). Check that the object under workflow, maintains its status (Draft, Published etc) |
221
|
|
|
*/ |
222
|
|
|
public function testDeleteWorkflowTargetStillWorks() { |
223
|
|
|
// 1). Create a workflow definition |
224
|
|
|
$def = $this->createDefinition(); |
225
|
|
|
$page = Page::create(); |
226
|
|
|
$page->Title = 'dummy test'; |
227
|
|
|
$page->WorkflowDefinitionID = $def->ID; // Normally done via CMS |
228
|
|
|
Versioned::reading_stage('Stage'); |
229
|
|
|
$page->write(); |
230
|
|
|
|
231
|
|
|
// Check $page is in draft, pre-deletion |
232
|
|
|
$status = ($page->getIsAddedToStage() && !$page->getExistsOnLive()); |
233
|
|
|
$this->assertTrue($status); |
|
|
|
|
234
|
|
|
|
235
|
|
|
// 2). Step the content into that workflow |
236
|
|
|
$instance = new WorkflowInstance(); |
237
|
|
|
$instance->beginWorkflow($def, $page); |
238
|
|
|
$instance->execute(); |
239
|
|
|
|
240
|
|
|
// Check the content is assigned |
241
|
|
|
$testPage = DataObject::get_by_id('Page', $page->ID); |
242
|
|
|
$this->assertEquals($instance->TargetID, $testPage->ID); |
|
|
|
|
243
|
|
|
|
244
|
|
|
// 3). Delete the workflow |
245
|
|
|
$def->delete(); |
246
|
|
|
|
247
|
|
|
// Check $testPage is _still_ in draft, post-deletion |
248
|
|
|
$status = ($testPage->getIsAddedToStage() && !$testPage->getExistsOnLive()); |
|
|
|
|
249
|
|
|
$this->assertTrue($status); |
|
|
|
|
250
|
|
|
|
251
|
|
|
/* |
252
|
|
|
* 4). i). Check that the content: Has no remaining related actions |
253
|
|
|
* Note: WorkflowApplicable::WorkflowDefinitionID does _not_ get updated until assigned a new workflow |
254
|
|
|
* so we can use it to check that all related actions are gone |
255
|
|
|
*/ |
256
|
|
|
$defID = $testPage->WorkflowDefinitionID; |
257
|
|
|
$this->assertEquals(0, DataObject::get('WorkflowAction')->filter('WorkflowDefID', $defID)->count()); |
|
|
|
|
258
|
|
|
|
259
|
|
|
/* |
260
|
|
|
* 4). ii). Check that the content: Can be re-assigned a new Workflow Definition |
261
|
|
|
*/ |
262
|
|
|
$newDef = $this->createDefinition(); |
263
|
|
|
$testPage->WorkflowDefinitionID = $newDef->ID; // Normally done via CMS |
264
|
|
|
$instance = new WorkflowInstance(); |
265
|
|
|
$instance->beginWorkflow($newDef, $testPage); |
266
|
|
|
$instance->execute(); |
267
|
|
|
|
268
|
|
|
// Check the content is assigned to the new Workflow Definition correctly |
269
|
|
|
$this->assertEquals($newDef->ID, $testPage->WorkflowDefinitionID); |
|
|
|
|
270
|
|
|
$this->assertEquals($newDef->Actions()->count(), DataObject::get('WorkflowAction')->filter('WorkflowDefID', $newDef->ID)->count()); |
|
|
|
|
271
|
|
|
|
272
|
|
|
// 5). Check that the object under workflow, maintains its status |
273
|
|
|
$newDef2 = $this->createDefinition(); |
274
|
|
|
|
275
|
|
|
// Login so SiteTree::canPublish() returns true |
276
|
|
|
$testPage->WorkflowDefinitionID = $newDef2->ID; // Normally done via CMS |
277
|
|
|
$this->logInWithPermission(); |
278
|
|
|
$testPage->doPublish(); |
279
|
|
|
|
280
|
|
|
// Check $testPage is published, pre-deletion (getStatusFlags() returns empty array) |
281
|
|
|
$this->assertTrue($testPage->getExistsOnLive()); |
|
|
|
|
282
|
|
|
|
283
|
|
|
$instance = new WorkflowInstance(); |
284
|
|
|
$instance->beginWorkflow($newDef2, $testPage); |
285
|
|
|
$instance->execute(); |
286
|
|
|
|
287
|
|
|
// Now delete the related WorkflowDefinition and ensure status is the same (i.e. so it's not 'modified' for example) |
288
|
|
|
$newDef2->delete(); |
289
|
|
|
|
290
|
|
|
// Check $testPage is _still_ published, post-deletion (getStatusFlags() returns empty array) |
291
|
|
|
$this->assertTrue($testPage->getExistsOnLive()); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.