1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Tests\Reports; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\CMS\Model\RedirectorPage; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\CMS\Model\VirtualPage; |
9
|
|
|
use SilverStripe\CMS\Reports\BrokenFilesReport; |
10
|
|
|
use SilverStripe\CMS\Reports\BrokenLinksReport; |
11
|
|
|
use SilverStripe\CMS\Reports\BrokenRedirectorPagesReport; |
12
|
|
|
use SilverStripe\CMS\Reports\BrokenVirtualPagesReport; |
13
|
|
|
use SilverStripe\CMS\Reports\RecentlyEditedReport; |
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
15
|
|
|
use SilverStripe\ORM\DB; |
16
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
17
|
|
|
use SilverStripe\Reports\Report; |
18
|
|
|
|
19
|
|
|
class CmsReportsTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
protected static $fixture_file = 'CmsReportsTest.yml'; |
22
|
|
|
|
23
|
|
|
private static $daysAgo = 14; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
// set the dates by hand: impossible to set via yml |
30
|
|
|
$afterThreshold = strtotime('-' . (self::$daysAgo - 1) . ' days', strtotime('31-06-2009 00:00:00')); |
31
|
|
|
$beforeThreshold = strtotime('-' . (self::$daysAgo + 1) . ' days', strtotime('31-06-2009 00:00:00')); |
32
|
|
|
|
33
|
|
|
$after = $this->objFromFixture(SiteTree::class, 'after'); |
34
|
|
|
$before = $this->objFromFixture(SiteTree::class, 'before'); |
35
|
|
|
DB::query( |
36
|
|
|
"UPDATE \"SiteTree\" SET \"Created\"='2009-01-01 00:00:00', \"LastEdited\"='" . |
37
|
|
|
date('Y-m-d H:i:s', $afterThreshold) . "' WHERE \"ID\"='" . $after->ID . "'" |
38
|
|
|
); |
39
|
|
|
DB::query( |
40
|
|
|
"UPDATE \"SiteTree\" SET \"Created\"='2009-01-01 00:00:00', \"LastEdited\"='" . |
41
|
|
|
date('Y-m-d H:i:s', $beforeThreshold) . "' WHERE \"ID\"='" . $before->ID . "'" |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ASSERT whether a report is returning the correct results, based on a broken "draft" and/or "published" page. |
47
|
|
|
* |
48
|
|
|
* @param Report $report |
49
|
|
|
* @param bool $isDraftBroken |
50
|
|
|
* @param bool $isPublishedBroken |
51
|
|
|
*/ |
52
|
|
|
public function isReportBroken($report, $isDraftBroken, $isPublishedBroken) |
53
|
|
|
{ |
54
|
|
|
$class = get_class($report); |
55
|
|
|
|
56
|
|
|
// ASSERT that the "draft" report is returning the correct results. |
57
|
|
|
$parameters = ['CheckSite' => 'Draft']; |
58
|
|
|
$results = count($report->sourceRecords($parameters, null, null)) > 0; |
59
|
|
|
$isDraftBroken |
60
|
|
|
? $this->assertTrue( |
61
|
|
|
$results, |
62
|
|
|
"{$class} has NOT returned the correct DRAFT results, as NO pages were found." |
63
|
|
|
) : $this->assertFalse( |
64
|
|
|
$results, |
65
|
|
|
"{$class} has NOT returned the correct DRAFT results, as pages were found." |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// ASSERT that the "published" report is returning the correct results. |
69
|
|
|
$parameters = ['CheckSite' => 'Published', 'OnLive' => 1]; |
70
|
|
|
$results = count($report->sourceRecords($parameters, null, null)) > 0; |
71
|
|
|
$isPublishedBroken |
72
|
|
|
? $this->assertTrue( |
73
|
|
|
$results, |
74
|
|
|
"{$class} has NOT returned the correct PUBLISHED results, as NO pages were found." |
75
|
|
|
) : $this->assertFalse( |
76
|
|
|
$results, |
77
|
|
|
"{$class} has NOT returned the correct PUBLISHED results, as pages were found." |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testRecentlyEdited() |
82
|
|
|
{ |
83
|
|
|
DBDatetime::set_mock_now('2009-06-30 00:00:00'); |
84
|
|
|
|
85
|
|
|
$after = $this->objFromFixture(SiteTree::class, 'after'); |
86
|
|
|
$before = $this->objFromFixture(SiteTree::class, 'before'); |
87
|
|
|
|
88
|
|
|
$r = new RecentlyEditedReport(); |
89
|
|
|
|
90
|
|
|
// check if contains only elements not older than $daysAgo days |
91
|
|
|
$this->assertNotNull($r->records([])); |
92
|
|
|
$this->assertContains($after->ID, $r->records([])->column('ID')); |
93
|
|
|
$this->assertNotContains($before->ID, $r->records([])->column('ID')); |
94
|
|
|
|
95
|
|
|
DBDatetime::clear_mock_now(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test the broken links side report. |
100
|
|
|
*/ |
101
|
|
|
public function testBrokenLinks() |
102
|
|
|
{ |
103
|
|
|
// Create a "draft" page with a broken link. |
104
|
|
|
$page = SiteTree::create(); |
105
|
|
|
$page->Content = "<a href='[sitetree_link,id=987654321]'>This</a> is a broken link."; |
106
|
|
|
$page->writeToStage('Stage'); |
107
|
|
|
|
108
|
|
|
// Retrieve the broken links side report. |
109
|
|
|
$reports = Report::get_reports(); |
110
|
|
|
$brokenLinksReport = null; |
111
|
|
|
foreach ($reports as $report) { |
112
|
|
|
if ($report instanceof BrokenLinksReport) { |
113
|
|
|
$brokenLinksReport = $report; |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Determine that the report exists, otherwise it has been excluded. |
119
|
|
|
if (!$brokenLinksReport) { |
120
|
|
|
$this->markTestSkipped('BrokenLinksReport is not an available report'); |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
125
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link, |
126
|
|
|
// as the page has not been "published" yet. |
127
|
|
|
$this->isReportBroken($brokenLinksReport, true, false); |
128
|
|
|
|
129
|
|
|
// Make sure the page is now "published". |
130
|
|
|
$page->writeToStage('Live'); |
131
|
|
|
|
132
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
133
|
|
|
// ASSERT that the "published" report has detected the page having a broken link. |
134
|
|
|
|
135
|
|
|
$this->isReportBroken($brokenLinksReport, true, true); |
136
|
|
|
|
137
|
|
|
// Correct the "draft" broken link. |
138
|
|
|
$page->Content = str_replace('987654321', $page->ID, $page->Content); |
139
|
|
|
$page->writeToStage('Stage'); |
140
|
|
|
|
141
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
142
|
|
|
// ASSERT that the "published" report has detected the page having a broken link, |
143
|
|
|
// as the previous content remains "published". |
144
|
|
|
$this->isReportBroken($brokenLinksReport, false, true); |
145
|
|
|
|
146
|
|
|
// Make sure the change has now been "published". |
147
|
|
|
$page->writeToStage('Live'); |
148
|
|
|
|
149
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
150
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link. |
151
|
|
|
|
152
|
|
|
$this->isReportBroken($brokenLinksReport, false, false); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Test the broken files side report. |
157
|
|
|
*/ |
158
|
|
|
public function testBrokenFiles() |
159
|
|
|
{ |
160
|
|
|
// Create a "draft" page with a broken file. |
161
|
|
|
$page = SiteTree::create(); |
162
|
|
|
$page->Content = "<a href='[file_link,id=987654321]'>This</a> is a broken file."; |
163
|
|
|
$page->writeToStage('Stage'); |
164
|
|
|
|
165
|
|
|
// Retrieve the broken files side report. |
166
|
|
|
$reports = Report::get_reports(); |
167
|
|
|
$brokenFilesReport = null; |
168
|
|
|
foreach ($reports as $report) { |
169
|
|
|
if ($report instanceof BrokenFilesReport) { |
170
|
|
|
$brokenFilesReport = $report; |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Determine that the report exists, otherwise it has been excluded. |
176
|
|
|
if (!$brokenFilesReport) { |
177
|
|
|
$this->markTestSkipped('BrokenFilesReport is not an available report'); |
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// ASSERT that the "draft" report has detected the page having a broken file. |
182
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken file, |
183
|
|
|
// as the page has not been "published" yet. |
184
|
|
|
$this->isReportBroken($brokenFilesReport, true, false); |
185
|
|
|
|
186
|
|
|
// Make sure the page is now "published". |
187
|
|
|
$page->writeToStage('Live'); |
188
|
|
|
|
189
|
|
|
// ASSERT that the "draft" report has detected the page having a broken file. |
190
|
|
|
// ASSERT that the "published" report has detected the page having a broken file. |
191
|
|
|
$this->isReportBroken($brokenFilesReport, true, true); |
192
|
|
|
|
193
|
|
|
// Correct the "draft" broken file. |
194
|
|
|
$file = File::create(); |
195
|
|
|
$file->Filename = 'name.pdf'; |
196
|
|
|
$file->write(); |
197
|
|
|
$page->Content = str_replace('987654321', $file->ID, $page->Content); |
198
|
|
|
$page->writeToStage('Stage'); |
199
|
|
|
|
200
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken file. |
201
|
|
|
// ASSERT that the "published" report has detected the page having a broken file, |
202
|
|
|
// as the previous content remains "published". |
203
|
|
|
$this->isReportBroken($brokenFilesReport, false, true); |
204
|
|
|
|
205
|
|
|
// Make sure the change has now been "published". |
206
|
|
|
$page->writeToStage('Live'); |
207
|
|
|
|
208
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken file. |
209
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken file. |
210
|
|
|
$this->isReportBroken($brokenFilesReport, false, false); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Test the broken virtual pages side report. |
215
|
|
|
*/ |
216
|
|
|
|
217
|
|
|
public function testBrokenVirtualPages() |
218
|
|
|
{ |
219
|
|
|
// Create a "draft" virtual page with a broken link. |
220
|
|
|
$page = VirtualPage::create(); |
221
|
|
|
$page->CopyContentFromID = 987654321; |
222
|
|
|
$page->writeToStage('Stage'); |
223
|
|
|
|
224
|
|
|
// Retrieve the broken virtual pages side report. |
225
|
|
|
$reports = Report::get_reports(); |
226
|
|
|
$brokenVirtualPagesReport = null; |
227
|
|
|
foreach ($reports as $report) { |
228
|
|
|
if ($report instanceof BrokenVirtualPagesReport) { |
229
|
|
|
$brokenVirtualPagesReport = $report; |
230
|
|
|
break; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Determine that the report exists, otherwise it has been excluded. |
235
|
|
|
if (!$brokenVirtualPagesReport) { |
236
|
|
|
$this->markTestSkipped('BrokenFilesReport is not an available report'); |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
241
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link, |
242
|
|
|
// as the page has not been "published" yet. |
243
|
|
|
$this->isReportBroken($brokenVirtualPagesReport, true, false); |
244
|
|
|
|
245
|
|
|
// Make sure the page is now "published". |
246
|
|
|
$page->writeToStage('Live'); |
247
|
|
|
|
248
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
249
|
|
|
// ASSERT that the "published" report has detected the page having a broken link. |
250
|
|
|
$this->isReportBroken($brokenVirtualPagesReport, true, true); |
251
|
|
|
|
252
|
|
|
// Correct the "draft" broken link. |
253
|
|
|
$contentPage = SiteTree::create(); |
254
|
|
|
$contentPage->Content = 'This is some content.'; |
255
|
|
|
$contentPage->writeToStage('Stage'); |
256
|
|
|
$contentPage->writeToStage('Live'); |
257
|
|
|
$page->CopyContentFromID = $contentPage->ID; |
258
|
|
|
$page->writeToStage('Stage'); |
259
|
|
|
|
260
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
261
|
|
|
// ASSERT that the "published" report has detected the page having a broken link, |
262
|
|
|
// as the previous content remains "published". |
263
|
|
|
$this->isReportBroken($brokenVirtualPagesReport, false, true); |
264
|
|
|
|
265
|
|
|
// Make sure the change has now been "published". |
266
|
|
|
$page->writeToStage('Live'); |
267
|
|
|
|
268
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
269
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link. |
270
|
|
|
$this->isReportBroken($brokenVirtualPagesReport, false, false); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Test the broken redirector pages side report. |
275
|
|
|
*/ |
276
|
|
|
public function testBrokenRedirectorPages() |
277
|
|
|
{ |
278
|
|
|
// Create a "draft" redirector page with a broken link. |
279
|
|
|
$page = RedirectorPage::create(); |
280
|
|
|
$page->RedirectionType = 'Internal'; |
281
|
|
|
$page->LinkToID = 987654321; |
282
|
|
|
$page->writeToStage('Stage'); |
283
|
|
|
|
284
|
|
|
// Retrieve the broken redirector pages side report. |
285
|
|
|
$reports = Report::get_reports(); |
286
|
|
|
$brokenRedirectorPagesReport = null; |
287
|
|
|
foreach ($reports as $report) { |
288
|
|
|
if ($report instanceof BrokenRedirectorPagesReport) { |
289
|
|
|
$brokenRedirectorPagesReport = $report; |
290
|
|
|
break; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Determine that the report exists, otherwise it has been excluded. |
295
|
|
|
if (!$brokenRedirectorPagesReport) { |
296
|
|
|
$this->markTestSkipped('BrokenRedirectorPagesReport is not an available report'); |
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
301
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link, |
302
|
|
|
// as the page has not been "published" yet. |
303
|
|
|
$this->isReportBroken($brokenRedirectorPagesReport, true, false); |
304
|
|
|
|
305
|
|
|
// Make sure the page is now "published". |
306
|
|
|
$page->writeToStage('Live'); |
307
|
|
|
|
308
|
|
|
// ASSERT that the "draft" report has detected the page having a broken link. |
309
|
|
|
// ASSERT that the "published" report has detected the page having a broken link. |
310
|
|
|
$this->isReportBroken($brokenRedirectorPagesReport, true, true); |
311
|
|
|
|
312
|
|
|
// Correct the "draft" broken link. |
313
|
|
|
$contentPage = SiteTree::create(); |
314
|
|
|
$contentPage->Content = 'This is some content.'; |
315
|
|
|
$contentPage->writeToStage('Stage'); |
316
|
|
|
$contentPage->writeToStage('Live'); |
317
|
|
|
$page->LinkToID = $contentPage->ID; |
318
|
|
|
$page->writeToStage('Stage'); |
319
|
|
|
|
320
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
321
|
|
|
// ASSERT that the "published" report has detected the page having a broken link, |
322
|
|
|
// as the previous content remains "published". |
323
|
|
|
$this->isReportBroken($brokenRedirectorPagesReport, false, true); |
324
|
|
|
|
325
|
|
|
// Make sure the change has now been "published". |
326
|
|
|
$page->writeToStage('Live'); |
327
|
|
|
|
328
|
|
|
// ASSERT that the "draft" report has NOT detected the page having a broken link. |
329
|
|
|
// ASSERT that the "published" report has NOT detected the page having a broken link. |
330
|
|
|
$this->isReportBroken($brokenRedirectorPagesReport, false, false); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|