1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests for workflow import/export logic. |
4
|
|
|
* |
5
|
|
|
* @author [email protected] |
6
|
|
|
* @license BSD License (http://silverstripe.org/bsd-license/) |
7
|
|
|
* @package advancedworkflow |
8
|
|
|
* @subpackage tests |
9
|
|
|
*/ |
10
|
|
|
class WorkflowImportExportTest extends SapphireTest |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public static $fixture_file = 'advancedworkflow/tests/workflowtemplateimport.yml'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Utility method, used in tests |
17
|
|
|
* @return \WorkflowDefinition |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
protected function createDefinition() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$definition = new WorkflowDefinition(); |
22
|
|
|
$definition->Title = "Dummy Workflow Definition"; |
|
|
|
|
23
|
|
|
$definition->write(); |
24
|
|
|
|
25
|
|
|
$stepOne = new WorkflowAction(); |
26
|
|
|
$stepOne->Title = "Step One"; |
|
|
|
|
27
|
|
|
$stepOne->WorkflowDefID = $definition->ID; |
|
|
|
|
28
|
|
|
$stepOne->write(); |
29
|
|
|
|
30
|
|
|
$stepTwo = new WorkflowAction(); |
31
|
|
|
$stepTwo->Title = "Step Two"; |
|
|
|
|
32
|
|
|
$stepTwo->WorkflowDefID = $definition->ID; |
|
|
|
|
33
|
|
|
$stepTwo->write(); |
34
|
|
|
|
35
|
|
|
$transitionOne = new WorkflowTransition(); |
36
|
|
|
$transitionOne->Title = 'Step One T1'; |
|
|
|
|
37
|
|
|
$transitionOne->ActionID = $stepOne->ID; |
|
|
|
|
38
|
|
|
$transitionOne->NextActionID = $stepTwo->ID; |
|
|
|
|
39
|
|
|
$transitionOne->write(); |
40
|
|
|
|
41
|
|
|
return $definition; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a WorkflowDefinition with some actions. Ensure an expected length of formatted template. |
46
|
|
|
*/ |
47
|
|
|
public function testFormatWithActions() |
48
|
|
|
{ |
49
|
|
|
$definition = $this->createDefinition(); |
50
|
|
|
$exporter = Injector::inst()->createWithArgs('WorkflowDefinitionExporter', array($definition->ID)); |
51
|
|
|
$member = new Member(); |
52
|
|
|
$member->FirstName = 'joe'; |
53
|
|
|
$member->Surname = 'bloggs'; |
54
|
|
|
$exporter->setMember($member); |
55
|
|
|
$templateData = new ArrayData(array( |
56
|
|
|
'ExportMetaData' => $exporter->ExportMetaData(), |
57
|
|
|
'ExportActions' => $exporter->getDefinition()->Actions() |
58
|
|
|
)); |
59
|
|
|
|
60
|
|
|
$formatted = $exporter->format($templateData); |
61
|
|
|
$numActions = count(preg_split("#\R#", $formatted)); |
62
|
|
|
|
63
|
|
|
$this->assertNotEmpty($formatted); |
|
|
|
|
64
|
|
|
// Seems arbitrary, but if no actions, then the resulting YAML file is exactly 18 lines long |
65
|
|
|
$this->assertGreaterThan(18, $numActions); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a WorkflowDefinition with NO actions. Ensure an expected length of formatted template. |
70
|
|
|
*/ |
71
|
|
|
public function testFormatWithoutActions() |
72
|
|
|
{ |
73
|
|
|
$definition = $this->createDefinition(); |
74
|
|
|
$exporter = Injector::inst()->createWithArgs('WorkflowDefinitionExporter', array($definition->ID)); |
75
|
|
|
$member = new Member(); |
76
|
|
|
$member->FirstName = 'joe'; |
77
|
|
|
$member->Surname = 'bloggs'; |
78
|
|
|
$exporter->setMember($member); |
79
|
|
|
$templateData = new ArrayData(array()); |
80
|
|
|
|
81
|
|
|
$formatted = $exporter->format($templateData); |
82
|
|
|
$numActions = count(preg_split("#\R#", $formatted)); |
83
|
|
|
|
84
|
|
|
// Seems arbitrary, but if no actions, then the resulting YAML file is exactly 18 lines long |
85
|
|
|
$this->assertEquals(18, $numActions); |
|
|
|
|
86
|
|
|
|
87
|
|
|
// Ensure outputted YAML has no blank lines, where SS's control structures would normally be |
88
|
|
|
$numBlanks = preg_match("#^\s*$#m", $formatted); |
89
|
|
|
$this->assertEquals(0, $numBlanks); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Tests a badly formatted YAML import for parsing (no headers) |
94
|
|
|
* Note: The available test-cases we can expect to get out of sfYamlParser is limited.. |
95
|
|
|
*/ |
96
|
|
|
public function testParseBadYAMLNoHeaderImport() |
97
|
|
|
{ |
98
|
|
|
$importer = new WorkflowDefinitionImporter(); |
99
|
|
|
$this->setExpectedException('Exception', 'Invalid YAML format.'); |
|
|
|
|
100
|
|
|
$source = <<<'EOD' |
101
|
|
|
Injector: |
102
|
|
|
ExportedWorkflow: |
103
|
|
|
class: WorkflowTemplate |
104
|
|
|
constructor: |
105
|
|
|
- 'My Workflow 4 20/02/2014 03-12-55' |
106
|
|
|
- 'Exported from localhost on 20/02/2014 03:12:55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2' |
107
|
|
|
- 0.2 |
108
|
|
|
- 0 |
109
|
|
|
- 3 |
110
|
|
|
properties: |
111
|
|
|
structure: |
112
|
|
|
'Step One': |
113
|
|
|
type: WorkflowAction |
114
|
|
|
transitions: |
115
|
|
|
- Step One T1: 'Step Two' |
116
|
|
|
'Step Two': |
117
|
|
|
type: WorkflowAction |
118
|
|
|
WorkflowService: |
119
|
|
|
properties: |
120
|
|
|
templates: |
121
|
|
|
- %$ExportedWorkflow |
122
|
|
|
EOD; |
123
|
|
|
|
124
|
|
|
$importer->parseYAMLImport($source); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Tests a badly formatted YAML import for parsing (missing YML colon) |
129
|
|
|
* Note: The available test-cases we can expect to get out of sfYamlParser is limited.. |
130
|
|
|
*/ |
131
|
|
|
public function testParseBadYAMLMalformedImport() |
132
|
|
|
{ |
133
|
|
|
$importer = new WorkflowDefinitionImporter(); |
134
|
|
|
$this->setExpectedException('ValidationException', 'Invalid YAML format. Unable to parse.'); |
|
|
|
|
135
|
|
|
$source = <<<'EOD' |
136
|
|
|
--- |
137
|
|
|
Name: exportedworkflow |
138
|
|
|
--- |
139
|
|
|
Injector: |
140
|
|
|
ExportedWorkflow: |
141
|
|
|
class: WorkflowTemplate |
142
|
|
|
constructor: |
143
|
|
|
- 'My Workflow 4 20/02/2014 03-12-55' |
144
|
|
|
- 'Exported from localhost on 20/02/2014 03-12-55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2' |
145
|
|
|
- 0.2 |
146
|
|
|
- 0 |
147
|
|
|
- 3 |
148
|
|
|
properties: |
149
|
|
|
structure: |
150
|
|
|
'Step One' |
151
|
|
|
type: WorkflowAction |
152
|
|
|
transitions: |
153
|
|
|
- Step One T1: 'Step Two' |
154
|
|
|
'Step Two': |
155
|
|
|
type: WorkflowAction |
156
|
|
|
WorkflowService: |
157
|
|
|
properties: |
158
|
|
|
templates: |
159
|
|
|
- %$ExportedWorkflow |
160
|
|
|
EOD; |
161
|
|
|
|
162
|
|
|
$importer->parseYAMLImport($source); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Tests a well-formatted YAML import for parsing |
167
|
|
|
* Note: The available test-cases we can expect to get out of sfYamlParser is limited.. |
168
|
|
|
*/ |
169
|
|
|
public function testParseGoodYAMLImport() |
170
|
|
|
{ |
171
|
|
|
$importer = new WorkflowDefinitionImporter(); |
172
|
|
|
$source = <<<'EOD' |
173
|
|
|
--- |
174
|
|
|
Name: exportedworkflow |
175
|
|
|
--- |
176
|
|
|
Injector: |
177
|
|
|
ExportedWorkflow: |
178
|
|
|
class: WorkflowTemplate |
179
|
|
|
constructor: |
180
|
|
|
- 'My Workflow 4 20/02/2014 03-12-55' |
181
|
|
|
- 'Exported from localhost on 20/02/2014 03-12-55 by joe bloggs using SilverStripe versions Framework 3.1.2, CMS 3.1.2' |
182
|
|
|
- 0.2 |
183
|
|
|
- 0 |
184
|
|
|
- 3 |
185
|
|
|
properties: |
186
|
|
|
structure: |
187
|
|
|
'Step One': |
188
|
|
|
type: WorkflowAction |
189
|
|
|
transitions: |
190
|
|
|
- Step One T1: 'Step Two' |
191
|
|
|
'Step Two': |
192
|
|
|
type: WorkflowAction |
193
|
|
|
WorkflowService: |
194
|
|
|
properties: |
195
|
|
|
templates: |
196
|
|
|
- %$ExportedWorkflow |
197
|
|
|
EOD; |
198
|
|
|
|
199
|
|
|
$this->assertNotEmpty($importer->parseYAMLImport($source)); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Given no ImportedWorkflowTemplate fixture/input data, tests an empty array is returned |
204
|
|
|
* by WorkflowDefinitionImporter#getImportedWorkflows() |
205
|
|
|
*/ |
206
|
|
|
public function testGetImportedWorkflowsNone() |
207
|
|
|
{ |
208
|
|
|
$this->clearFixtures(); |
209
|
|
|
$importer = new WorkflowDefinitionImporter(); |
210
|
|
|
$imports = $importer->getImportedWorkflows(); |
211
|
|
|
$this->assertEmpty($imports); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Given a single ImportedWorkflowTemplate fixture/input data, tests an non-empty array is returned |
216
|
|
|
* by WorkflowDefinitionImporter#getImportedWorkflows() |
217
|
|
|
*/ |
218
|
|
|
public function testGetImportedWorkflowsOne() |
219
|
|
|
{ |
220
|
|
|
$name = 'My Workflow 21/02/2014 09-01-29'; |
221
|
|
|
// Pretend a ImportedWorkflowTemplate object has been created by WorkflowBulkLoader |
222
|
|
|
$this->objFromFixture('ImportedWorkflowTemplate', 'Import01'); |
223
|
|
|
|
224
|
|
|
$importer = singleton('WorkflowDefinitionImporter'); |
225
|
|
|
$import = $importer->getImportedWorkflows($name); |
226
|
|
|
|
227
|
|
|
$this->assertNotEmpty($import); |
|
|
|
|
228
|
|
|
$this->assertInstanceOf('WorkflowTemplate', $import); |
|
|
|
|
229
|
|
|
$this->assertEquals(1, count($import)); |
|
|
|
|
230
|
|
|
$this->assertEquals($name, $import->getName()); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Given many ImportedWorkflowTemplate fixture/input data, tests an non-empty array is returned |
235
|
|
|
* by WorkflowDefinitionImporter#getImportedWorkflows() |
236
|
|
|
*/ |
237
|
|
|
public function testGetImportedWorkflowsMany() |
238
|
|
|
{ |
239
|
|
|
// Pretend some ImportedWorkflowTemplate objects have been created by WorkflowBulkLoader |
240
|
|
|
$this->objFromFixture('ImportedWorkflowTemplate', 'Import02'); |
241
|
|
|
$this->objFromFixture('ImportedWorkflowTemplate', 'Import03'); |
242
|
|
|
|
243
|
|
|
$importer = singleton('WorkflowDefinitionImporter'); |
244
|
|
|
$imports = $importer->getImportedWorkflows(); |
245
|
|
|
|
246
|
|
|
$this->assertNotEmpty($imports); |
|
|
|
|
247
|
|
|
$this->assertInternalType('array', $imports); |
|
|
|
|
248
|
|
|
$this->assertGreaterThan(1, count($imports)); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
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.