1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowActionInstance; |
8
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; |
9
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowTransition; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests for the workflow engine. |
13
|
|
|
* |
14
|
|
|
* @author [email protected] |
15
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
16
|
|
|
* @package advancedworkflow |
17
|
|
|
* @subpackage tests |
18
|
|
|
*/ |
19
|
|
|
class WorkflowInstanceTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected static $fixture_file = 'useractioninstancehistory.yml'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Member |
28
|
|
|
*/ |
29
|
|
|
protected $currentMember; |
30
|
|
|
|
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
$this->currentMember = $this->objFromFixture(Member::class, 'ApproverMember01'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Tests WorkflowInstance#getMostRecentActionForUser() |
39
|
|
|
*/ |
40
|
|
|
public function testGetMostRecentActionForUser() |
41
|
|
|
{ |
42
|
|
|
// Single, AssignUsersToWorkflowAction in "History" |
43
|
|
|
$history01 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance01'); |
44
|
|
|
$mostRecentActionForUser01 = $history01->getMostRecentActionForUser($this->currentMember); |
45
|
|
|
$this->assertInstanceOf( |
46
|
|
|
WorkflowActionInstance::class, |
47
|
|
|
$mostRecentActionForUser01, |
48
|
|
|
'Asserts the correct ClassName is retured #1' |
49
|
|
|
); |
50
|
|
|
$this->assertEquals( |
51
|
|
|
'Assign', |
52
|
|
|
$mostRecentActionForUser01->BaseAction()->Title, |
53
|
|
|
'Asserts the correct BaseAction is retured #1' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
// No AssignUsersToWorkflowAction found with Member's related Group in "History" |
57
|
|
|
$history02 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance02'); |
58
|
|
|
$mostRecentActionForUser02 = $history02->getMostRecentActionForUser($this->currentMember); |
59
|
|
|
$this->assertFalse( |
60
|
|
|
$mostRecentActionForUser02, |
61
|
|
|
'Asserts false is returned because no WorkflowActionInstance was found' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// Multiple AssignUsersToWorkflowAction in "History", only one with Group relations |
65
|
|
|
$history03 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance03'); |
66
|
|
|
$mostRecentActionForUser03 = $history03->getMostRecentActionForUser($this->currentMember); |
67
|
|
|
$this->assertInstanceOf( |
68
|
|
|
WorkflowActionInstance::class, |
69
|
|
|
$mostRecentActionForUser03, |
70
|
|
|
'Asserts the correct ClassName is retured #2' |
71
|
|
|
); |
72
|
|
|
$this->assertEquals( |
73
|
|
|
'Assign', |
74
|
|
|
$mostRecentActionForUser03->BaseAction()->Title, |
75
|
|
|
'Asserts the correct BaseAction is retured #2' |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
// Multiple AssignUsersToWorkflowAction in "History", both with Group relations |
79
|
|
|
$history04 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance04'); |
80
|
|
|
$mostRecentActionForUser04 = $history04->getMostRecentActionForUser($this->currentMember); |
81
|
|
|
$this->assertInstanceOf( |
82
|
|
|
WorkflowActionInstance::class, |
83
|
|
|
$mostRecentActionForUser04, |
84
|
|
|
'Asserts the correct ClassName is retured #3' |
85
|
|
|
); |
86
|
|
|
$this->assertEquals( |
87
|
|
|
'Assigned Again', |
88
|
|
|
$mostRecentActionForUser04->BaseAction()->Title, |
89
|
|
|
'Asserts the correct BaseAction is retured #3' |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
// Multiple AssignUsersToWorkflowAction in "History", one with Group relations |
93
|
|
|
$history05 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance05'); |
94
|
|
|
$mostRecentActionForUser05 = $history05->getMostRecentActionForUser($this->currentMember); |
95
|
|
|
$this->assertInstanceOf( |
96
|
|
|
WorkflowActionInstance::class, |
97
|
|
|
$mostRecentActionForUser05, |
98
|
|
|
'Asserts the correct ClassName is retured #4' |
99
|
|
|
); |
100
|
|
|
$this->assertEquals( |
101
|
|
|
'Assigned Me', |
102
|
|
|
$mostRecentActionForUser05->BaseAction()->Title, |
103
|
|
|
'Asserts the correct BaseAction is retured #4' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests WorkflowInstance#canView() |
109
|
|
|
*/ |
110
|
|
|
public function testCanView() |
111
|
|
|
{ |
112
|
|
|
// Single, AssignUsersToWorkflowAction in "History" |
113
|
|
|
$history01 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance01'); |
114
|
|
|
$this->assertTrue($history01->canView($this->currentMember)); |
115
|
|
|
|
116
|
|
|
// No AssignUsersToWorkflowAction found with Member's related Group in "History" |
117
|
|
|
$history02 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance02'); |
118
|
|
|
$this->assertFalse($history02->canView($this->currentMember)); |
119
|
|
|
|
120
|
|
|
// Multiple AssignUsersToWorkflowAction in "History", only one with Group relations |
121
|
|
|
$history03 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance03'); |
122
|
|
|
$this->assertTrue($history03->canView($this->currentMember)); |
123
|
|
|
|
124
|
|
|
// Multiple AssignUsersToWorkflowAction in "History", both with Group relations |
125
|
|
|
$history04 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance04'); |
126
|
|
|
$this->assertTrue($history04->canView($this->currentMember)); |
127
|
|
|
|
128
|
|
|
// Multiple AssignUsersToWorkflowAction in "History" |
129
|
|
|
$history05 = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance05'); |
130
|
|
|
$this->assertTrue($history05->canView($this->currentMember)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testValidTransitions() |
134
|
|
|
{ |
135
|
|
|
$instance = $this->objFromFixture(WorkflowInstance::class, 'WorkflowInstance06'); |
136
|
|
|
$transition1 = $this->objFromFixture(WorkflowTransition::class, 'Transition05'); |
137
|
|
|
$transition2 = $this->objFromFixture(WorkflowTransition::class, 'Transition06'); |
138
|
|
|
$member1 = $this->objFromFixture(Member::class, 'Transition05Member'); |
139
|
|
|
$member2 = $this->objFromFixture(Member::class, 'Transition06Member'); |
140
|
|
|
|
141
|
|
|
// Given logged in as admin, check that there are two actions |
142
|
|
|
$this->logInWithPermission('ADMIN'); |
143
|
|
|
$transitions = $instance->validTransitions()->column('ID'); |
144
|
|
|
$this->assertContains($transition1->ID, $transitions); |
145
|
|
|
$this->assertContains($transition2->ID, $transitions); |
146
|
|
|
|
147
|
|
|
// Logged in as a member with permission on one transition, check that only this one is present |
148
|
|
|
$member1->logIn(); |
149
|
|
|
$transitions = $instance->validTransitions()->column('ID'); |
150
|
|
|
$this->assertContains($transition1->ID, $transitions); |
151
|
|
|
$this->assertNotContains($transition2->ID, $transitions); |
152
|
|
|
|
153
|
|
|
// Logged in as a member with permissions via group |
154
|
|
|
$member2->logIn(); |
155
|
|
|
$transitions = $instance->validTransitions()->column('ID'); |
156
|
|
|
$this->assertNotContains($transition1->ID, $transitions); |
157
|
|
|
$this->assertContains($transition2->ID, $transitions); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.