1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ExternalLinks\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus; |
8
|
|
|
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport; |
9
|
|
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask; |
10
|
|
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker; |
11
|
|
|
use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage; |
12
|
|
|
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker; |
13
|
|
|
use SilverStripe\i18n\i18n; |
14
|
|
|
use SilverStripe\Reports\Report; |
15
|
|
|
|
16
|
|
|
class ExternalLinksTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected static $fixture_file = 'ExternalLinksTest.yml'; |
20
|
|
|
|
21
|
|
|
protected static $extra_dataobjects = array( |
22
|
|
|
ExternalLinksTestPage::class |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
// Stub link checker |
30
|
|
|
$checker = new PretendLinkChecker; |
31
|
|
|
Injector::inst()->registerService($checker, LinkChecker::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testLinks() |
35
|
|
|
{ |
36
|
|
|
// Run link checker |
37
|
|
|
$task = CheckExternalLinksTask::create(); |
38
|
|
|
$task->setSilent(true); // Be quiet during the test! |
39
|
|
|
$task->runLinksCheck(); |
40
|
|
|
|
41
|
|
|
// Get all links checked |
42
|
|
|
$status = BrokenExternalPageTrackStatus::get_latest(); |
43
|
|
|
$this->assertEquals('Completed', $status->Status); |
44
|
|
|
$this->assertEquals(5, $status->TotalPages); |
45
|
|
|
$this->assertEquals(5, $status->CompletedPages); |
46
|
|
|
|
47
|
|
|
// Check all pages have had the correct HTML adjusted |
48
|
|
|
for ($i = 1; $i <= 5; $i++) { |
49
|
|
|
$page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i); |
50
|
|
|
$this->assertNotEmpty($page->Content); |
51
|
|
|
$this->assertEquals( |
52
|
|
|
$page->ExpectedContent, |
53
|
|
|
$page->Content, |
54
|
|
|
"Assert that the content of page{$i} has been updated" |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Check that the correct report of broken links is generated |
59
|
|
|
$links = $status |
60
|
|
|
->BrokenLinks() |
61
|
|
|
->sort('Link'); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(4, $links->count()); |
64
|
|
|
$this->assertEquals( |
65
|
|
|
array( |
66
|
|
|
'http://www.broken.com', |
67
|
|
|
'http://www.broken.com/url/thing', |
68
|
|
|
'http://www.broken.com/url/thing', |
69
|
|
|
'http://www.nodomain.com' |
70
|
|
|
), |
71
|
|
|
array_values($links->map('ID', 'Link')->toArray()) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// Check response codes are correct |
75
|
|
|
$expected = array( |
76
|
|
|
'http://www.broken.com' => 403, |
77
|
|
|
'http://www.broken.com/url/thing' => 404, |
78
|
|
|
'http://www.nodomain.com' => 0 |
79
|
|
|
); |
80
|
|
|
$actual = $links->map('Link', 'HTTPCode')->toArray(); |
81
|
|
|
$this->assertEquals($expected, $actual); |
82
|
|
|
|
83
|
|
|
// Check response descriptions are correct |
84
|
|
|
i18n::set_locale('en_NZ'); |
85
|
|
|
$expected = array( |
86
|
|
|
'http://www.broken.com' => '403 (Forbidden)', |
87
|
|
|
'http://www.broken.com/url/thing' => '404 (Not Found)', |
88
|
|
|
'http://www.nodomain.com' => '0 (Server Not Available)' |
89
|
|
|
); |
90
|
|
|
$actual = $links->map('Link', 'HTTPCodeDescription')->toArray(); |
91
|
|
|
$this->assertEquals($expected, $actual); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Test that broken links appears in the reports list |
96
|
|
|
*/ |
97
|
|
|
public function testReportExists() |
98
|
|
|
{ |
99
|
|
|
$reports = Report::get_reports(); |
100
|
|
|
$reportNames = array(); |
101
|
|
|
foreach ($reports as $report) { |
102
|
|
|
$reportNames[] = get_class($report); |
103
|
|
|
} |
104
|
|
|
$this->assertContains( |
105
|
|
|
BrokenExternalLinksReport::class, |
106
|
|
|
$reportNames, |
107
|
|
|
'BrokenExternalLinksReport is in reports list' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|