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