|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author [email protected] |
|
5
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
class WorkflowEmbargoExpiryTest extends SapphireTest { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
public function setUp() { |
|
10
|
|
|
parent::setUp(); |
|
11
|
|
|
|
|
12
|
|
|
SS_Datetime::set_mock_now('2014-01-05 12:00:00'); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function tearDown() { |
|
16
|
|
|
SS_Datetime::clear_mock_now(); |
|
17
|
|
|
parent::tearDown(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $requiredExtensions = array( |
|
24
|
|
|
'SiteTree' => array( |
|
25
|
|
|
'WorkflowEmbargoExpiryExtension', |
|
26
|
|
|
'Versioned', |
|
27
|
|
|
) |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $illegalExtensions = array( |
|
34
|
|
|
'SiteTree' => array( |
|
35
|
|
|
"Translatable", |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
public function __construct() { |
|
40
|
|
|
if(!class_exists('AbstractQueuedJob')) { |
|
41
|
|
|
$this->skipTest = true; |
|
42
|
|
|
} |
|
43
|
|
|
parent::__construct(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testFutureDatesJobs() { |
|
47
|
|
|
$page = new Page(); |
|
48
|
|
|
|
|
49
|
|
|
$page->PublishOnDate = '2020-01-01 00:00:00'; |
|
50
|
|
|
$page->UnPublishOnDate = '2020-01-01 01:00:00'; |
|
51
|
|
|
|
|
52
|
|
|
// Two writes are necessary for this to work on new objects |
|
53
|
|
|
$page->write(); |
|
54
|
|
|
$page->write(); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue($page->PublishJobID > 0); |
|
|
|
|
|
|
57
|
|
|
$this->assertTrue($page->UnPublishJobID > 0); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
// Check date ranges |
|
60
|
|
|
$now = strtotime(SS_Datetime::now()->getValue()); |
|
61
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
62
|
|
|
$unPublish = strtotime($page->UnPublishJob()->StartAfter); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertGreaterThan($now, $publish); |
|
|
|
|
|
|
65
|
|
|
$this->assertGreaterThan($now, $unPublish); |
|
|
|
|
|
|
66
|
|
|
$this->assertGreaterThan($publish, $unPublish); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testDesiredRemovesJobs() { |
|
70
|
|
|
$page = new Page(); |
|
71
|
|
|
|
|
72
|
|
|
$page->PublishOnDate = '2020-01-01 00:00:00'; |
|
73
|
|
|
$page->UnPublishOnDate = '2020-01-01 01:00:00'; |
|
74
|
|
|
|
|
75
|
|
|
// Two writes are necessary for this to work on new objects |
|
76
|
|
|
$page->write(); |
|
77
|
|
|
$page->write(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertTrue($page->PublishJobID > 0); |
|
|
|
|
|
|
80
|
|
|
$this->assertTrue($page->UnPublishJobID > 0); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// Check date ranges |
|
83
|
|
|
$now = strtotime(SS_Datetime::now()->getValue()); |
|
84
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
85
|
|
|
$unPublish = strtotime($page->UnPublishJob()->StartAfter); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertGreaterThan($now, $publish); |
|
|
|
|
|
|
88
|
|
|
$this->assertGreaterThan($now, $unPublish); |
|
|
|
|
|
|
89
|
|
|
$this->assertGreaterThan($publish, $unPublish); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$page->DesiredPublishDate = '2020-02-01 00:00:00'; |
|
92
|
|
|
$page->DesiredUnPublishDate = '2020-02-01 02:00:00'; |
|
93
|
|
|
|
|
94
|
|
|
$page->write(); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertTrue($page->PublishJobID == 0); |
|
|
|
|
|
|
97
|
|
|
$this->assertTrue($page->UnPublishJobID == 0); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testPublishActionWithFutureDates() { |
|
101
|
|
|
$action = new PublishItemWorkflowAction(); |
|
102
|
|
|
$instance = new WorkflowInstance(); |
|
103
|
|
|
|
|
104
|
|
|
$page = new Page(); |
|
105
|
|
|
$page->Title = 'stuff'; |
|
106
|
|
|
$page->DesiredPublishDate = '2020-02-01 00:00:00'; |
|
107
|
|
|
$page->DesiredUnPublishDate = '2020-02-01 02:00:00'; |
|
108
|
|
|
|
|
109
|
|
|
$page->write(); |
|
110
|
|
|
|
|
111
|
|
|
$instance->TargetClass = $page->ClassName; |
|
|
|
|
|
|
112
|
|
|
$instance->TargetID = $page->ID; |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$action->execute($instance); |
|
115
|
|
|
|
|
116
|
|
|
$page = DataObject::get_by_id('Page', $page->ID); |
|
117
|
|
|
$this->assertTrue($page->PublishJobID > 0); |
|
|
|
|
|
|
118
|
|
|
$this->assertTrue($page->UnPublishJobID > 0); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// Check date ranges |
|
121
|
|
|
$now = strtotime(SS_Datetime::now()->getValue()); |
|
122
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
123
|
|
|
$unPublish = strtotime($page->UnPublishJob()->StartAfter); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertGreaterThan($now, $publish); |
|
|
|
|
|
|
126
|
|
|
$this->assertGreaterThan($now, $unPublish); |
|
|
|
|
|
|
127
|
|
|
$this->assertGreaterThan($publish, $unPublish); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Test that a page with a past publish date creates the correct jobs |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
public function testPastPublishThenUnpublish() { |
|
|
|
|
|
|
134
|
|
|
$page = new Page(); |
|
135
|
|
|
$page->Title = 'My Page'; |
|
136
|
|
|
$page->write(); |
|
137
|
|
|
|
|
138
|
|
|
// No publish |
|
139
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
140
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
// Set a past publish date |
|
143
|
|
|
$page->PublishOnDate = '2010-01-01 00:00:00'; |
|
144
|
|
|
$page->write(); |
|
145
|
|
|
|
|
146
|
|
|
// We should still have a job to publish this page, but not unpublish |
|
147
|
|
|
$this->assertNotEmpty($page->PublishJobID); |
|
|
|
|
|
|
148
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
// Check that this job is set for immediate run |
|
151
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
152
|
|
|
$this->assertEmpty($publish); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
// Now add an expiry date in the past, but after the current publish job, |
|
155
|
|
|
// and ensure that this correctly overrides the open publish request |
|
156
|
|
|
$page->UnPublishOnDate = '2010-01-02 00:00:00'; |
|
157
|
|
|
$page->write(); |
|
158
|
|
|
|
|
159
|
|
|
// Now we should have an unpublish job, but the publish job is noticably absent |
|
160
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
161
|
|
|
$this->assertNotEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
// Check that this unpublish job is set for immediate run |
|
164
|
|
|
$unpublish = strtotime($page->UnPublishJob()->StartAfter); |
|
165
|
|
|
$this->assertEmpty($unpublish); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
// Now add an expiry date in the future, and ensure that we get the correct combination of |
|
168
|
|
|
// publish and unpublish jobs |
|
169
|
|
|
$page->UnPublishOnDate = '2015-01-01 12:00:00'; |
|
170
|
|
|
$page->write(); |
|
171
|
|
|
|
|
172
|
|
|
// Both jobs exist |
|
173
|
|
|
$this->assertNotEmpty($page->PublishJobID); |
|
|
|
|
|
|
174
|
|
|
$this->assertNotEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
// Check that this unpublish job is set for immediate run and the unpublish for future |
|
177
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
178
|
|
|
$unpublish = strtotime($page->UnPublishJob()->StartAfter); |
|
179
|
|
|
$this->assertEmpty($publish); // for immediate run |
|
|
|
|
|
|
180
|
|
|
$this->assertGreaterThan(strtotime(SS_Datetime::now()->getValue()), $unpublish); // for later run |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Test that a page with a past unpublish date creates the correct jobs |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
public function testPastUnPublishThenPublish() { |
|
|
|
|
|
|
187
|
|
|
$page = new Page(); |
|
188
|
|
|
$page->Title = 'My Page'; |
|
189
|
|
|
$page->write(); |
|
190
|
|
|
|
|
191
|
|
|
// No publish |
|
192
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
193
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
// Set a past unpublish date |
|
196
|
|
|
$page->UnPublishOnDate = '2010-01-01 00:00:00'; |
|
197
|
|
|
$page->write(); |
|
198
|
|
|
|
|
199
|
|
|
// We should still have a job to unpublish this page, but not publish |
|
200
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
201
|
|
|
$this->assertNotEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
// Check that this job is set for immediate run |
|
204
|
|
|
$unpublish = strtotime($page->UnPublishJob()->StartAfter); |
|
205
|
|
|
$this->assertEmpty($unpublish); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
// Now add an publish date in the past, but after the unpublish job, |
|
208
|
|
|
// and ensure that this correctly overrides the open unpublish request |
|
209
|
|
|
$page->PublishOnDate = '2010-01-02 00:00:00'; |
|
210
|
|
|
$page->write(); |
|
211
|
|
|
|
|
212
|
|
|
// Now we should have an publish job, but the unpublish job is noticably absent |
|
213
|
|
|
$this->assertNotEmpty($page->PublishJobID); |
|
|
|
|
|
|
214
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
// Check that this publish job is set for immediate run |
|
217
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
218
|
|
|
$this->assertEmpty($publish); |
|
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
// Now add a publish date in the future, and ensure that we get the correct combination of |
|
221
|
|
|
// publish and unpublish jobs |
|
222
|
|
|
$page->PublishOnDate = '2015-01-01 12:00:00'; |
|
223
|
|
|
$page->write(); |
|
224
|
|
|
|
|
225
|
|
|
// Both jobs exist |
|
226
|
|
|
$this->assertNotEmpty($page->PublishJobID); |
|
|
|
|
|
|
227
|
|
|
$this->assertNotEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
// Check that this unpublish job is set for immediate run and the unpublish for future |
|
230
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
231
|
|
|
$unpublish = strtotime($page->UnPublishJob()->StartAfter); |
|
232
|
|
|
$this->assertEmpty($unpublish); // for immediate run |
|
|
|
|
|
|
233
|
|
|
$this->assertGreaterThan(strtotime(SS_Datetime::now()->getValue()), $publish); // for later run |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function testPastPublishWithWorkflowInEffect() { |
|
237
|
|
|
$definition = $this->createDefinition(); |
|
238
|
|
|
|
|
239
|
|
|
$page = new Page(); |
|
240
|
|
|
$page->Title = 'My page'; |
|
241
|
|
|
$page->WorkflowDefinitionID = $definition->ID; |
|
242
|
|
|
$page->write(); |
|
243
|
|
|
|
|
244
|
|
|
// No publish |
|
245
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
246
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
// Set a past publish date |
|
249
|
|
|
$page->DesiredPublishDate = '2010-01-01 00:00:00'; |
|
250
|
|
|
$page->write(); |
|
251
|
|
|
|
|
252
|
|
|
// Workflow is in effect. No jobs have been created yet as it's not approved. |
|
253
|
|
|
$this->assertEmpty($page->PublishJobID); |
|
|
|
|
|
|
254
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
// Advance the workflow so we can see what happens |
|
257
|
|
|
$instance = new WorkflowInstance(); |
|
258
|
|
|
$instance->beginWorkflow($definition, $page); |
|
259
|
|
|
$instance->execute(); |
|
260
|
|
|
|
|
261
|
|
|
// execute the "publish" workflow action |
|
262
|
|
|
$action = new PublishItemWorkflowAction(); |
|
263
|
|
|
$action->execute($instance); |
|
264
|
|
|
|
|
265
|
|
|
// re-fetch the Page again. |
|
266
|
|
|
$page = Page::get()->byId($page->ID); |
|
267
|
|
|
|
|
268
|
|
|
// We now have a PublishOnDate field set |
|
269
|
|
|
$this->assertEquals('2010-01-01 00:00:00', $page->PublishOnDate); |
|
|
|
|
|
|
270
|
|
|
$this->assertEmpty($page->DesiredPublishDate); |
|
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
// Publish job has been setup |
|
273
|
|
|
$this->assertNotEmpty($page->PublishJobID); |
|
|
|
|
|
|
274
|
|
|
$this->assertEmpty($page->UnPublishJobID); |
|
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
// Check that this publish job is set for immediate run |
|
277
|
|
|
$publish = strtotime($page->PublishJob()->StartAfter); |
|
278
|
|
|
$this->assertEmpty($publish); |
|
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
View Code Duplication |
protected function createDefinition() { |
|
|
|
|
|
|
282
|
|
|
$definition = new WorkflowDefinition(); |
|
283
|
|
|
$definition->Title = 'Dummy Workflow Definition'; |
|
|
|
|
|
|
284
|
|
|
$definition->write(); |
|
285
|
|
|
|
|
286
|
|
|
$stepOne = new WorkflowAction(); |
|
287
|
|
|
$stepOne->Title = 'Step One'; |
|
|
|
|
|
|
288
|
|
|
$stepOne->WorkflowDefID = $definition->ID; |
|
|
|
|
|
|
289
|
|
|
$stepOne->write(); |
|
290
|
|
|
|
|
291
|
|
|
$stepTwo = new WorkflowAction(); |
|
292
|
|
|
$stepTwo->Title = 'Step Two'; |
|
|
|
|
|
|
293
|
|
|
$stepTwo->WorkflowDefID = $definition->ID; |
|
|
|
|
|
|
294
|
|
|
$stepTwo->write(); |
|
295
|
|
|
|
|
296
|
|
|
$transitionOne = new WorkflowTransition(); |
|
297
|
|
|
$transitionOne->Title = 'Step One T1'; |
|
|
|
|
|
|
298
|
|
|
$transitionOne->ActionID = $stepOne->ID; |
|
|
|
|
|
|
299
|
|
|
$transitionOne->NextActionID = $stepTwo->ID; |
|
|
|
|
|
|
300
|
|
|
$transitionOne->write(); |
|
301
|
|
|
|
|
302
|
|
|
return $definition; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|
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.