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