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