1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ExternalLinksTest extends SapphireTest { |
|
|
|
|
4
|
|
|
|
5
|
|
|
protected static $fixture_file = 'ExternalLinksTest.yml'; |
6
|
|
|
|
7
|
|
|
protected $extraDataObjects = array( |
8
|
|
|
'ExternalLinksTestPage' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
protected $illegalExtensions = array( |
12
|
|
|
'SiteTree' => array('Translatable') |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
public function setUpOnce() { |
16
|
|
|
if (class_exists('Phockito')) { |
17
|
|
|
Phockito::include_hamcrest(false); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
parent::setUpOnce(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setUp() { |
|
|
|
|
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
// Check dependencies |
27
|
|
|
if (!class_exists('Phockito')) { |
28
|
|
|
$this->skipTest = true; |
29
|
|
|
return $this->markTestSkipped("These tests need the Phockito module installed to run"); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Mock link checker |
33
|
|
|
$checker = Phockito::mock('LinkChecker'); |
34
|
|
|
Phockito::when($checker) |
35
|
|
|
->checkLink('http://www.working.com') |
36
|
|
|
->return(200); |
37
|
|
|
|
38
|
|
|
Phockito::when($checker) |
39
|
|
|
->checkLink('http://www.broken.com/url/thing') // 404 on working site |
40
|
|
|
->return(404); |
41
|
|
|
|
42
|
|
|
Phockito::when($checker) |
43
|
|
|
->checkLink('http://www.broken.com') // 403 on working site |
44
|
|
|
->return(403); |
45
|
|
|
|
46
|
|
|
Phockito::when($checker) |
47
|
|
|
->checkLink('http://www.nodomain.com') // no ping |
48
|
|
|
->return(0); |
49
|
|
|
|
50
|
|
|
Phockito::when($checker) |
51
|
|
|
->checkLink('/internal/link') |
52
|
|
|
->return(null); |
53
|
|
|
|
54
|
|
|
Phockito::when($checker) |
55
|
|
|
->checkLink('[sitetree_link,id=9999]') |
56
|
|
|
->return(null); |
57
|
|
|
|
58
|
|
|
Phockito::when($checker) |
59
|
|
|
->checkLink('home') |
60
|
|
|
->return(null); |
61
|
|
|
|
62
|
|
|
Phockito::when($checker) |
63
|
|
|
->checkLink('broken-internal') |
64
|
|
|
->return(null); |
65
|
|
|
|
66
|
|
|
Phockito::when($checker) |
67
|
|
|
->checkLink('[sitetree_link,id=1]') |
68
|
|
|
->return(null); |
69
|
|
|
|
70
|
|
|
Phockito::when($checker) |
71
|
|
|
->checkLink(Hamcrest_Matchers::anything()) // anything else is 404 |
72
|
|
|
->return(404); |
73
|
|
|
|
74
|
|
|
Injector::inst()->registerService($checker, 'LinkChecker'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testLinks() { |
78
|
|
|
// Run link checker |
79
|
|
|
$task = CheckExternalLinksTask::create(); |
80
|
|
|
$task->setSilent(true); // Be quiet during the test! |
81
|
|
|
$task->runLinksCheck(); |
82
|
|
|
|
83
|
|
|
// Get all links checked |
84
|
|
|
$status = BrokenExternalPageTrackStatus::get_latest(); |
85
|
|
|
$this->assertEquals('Completed', $status->Status); |
|
|
|
|
86
|
|
|
$this->assertEquals(5, $status->TotalPages); |
|
|
|
|
87
|
|
|
$this->assertEquals(5, $status->CompletedPages); |
|
|
|
|
88
|
|
|
|
89
|
|
|
// Check all pages have had the correct HTML adjusted |
90
|
|
|
for($i = 1; $i <= 5; $i++) { |
91
|
|
|
$page = $this->objFromFixture('ExternalLinksTestPage', 'page'.$i); |
92
|
|
|
$this->assertNotEmpty($page->Content); |
|
|
|
|
93
|
|
|
$this->assertEquals( |
|
|
|
|
94
|
|
|
$page->ExpectedContent, |
95
|
|
|
$page->Content, |
96
|
|
|
"Assert that the content of page{$i} has been updated" |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Check that the correct report of broken links is generated |
101
|
|
|
$links = $status |
102
|
|
|
->BrokenLinks() |
103
|
|
|
->sort('Link'); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(4, $links->count()); |
|
|
|
|
106
|
|
|
$this->assertEquals( |
|
|
|
|
107
|
|
|
array( |
108
|
|
|
'http://www.broken.com', |
109
|
|
|
'http://www.broken.com/url/thing', |
110
|
|
|
'http://www.broken.com/url/thing', |
111
|
|
|
'http://www.nodomain.com' |
112
|
|
|
), |
113
|
|
|
array_values($links->map('ID', 'Link')->toArray()) |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
// Check response codes are correct |
117
|
|
|
$expected = array( |
118
|
|
|
'http://www.broken.com' => 403, |
119
|
|
|
'http://www.broken.com/url/thing' => 404, |
120
|
|
|
'http://www.nodomain.com' => 0 |
121
|
|
|
); |
122
|
|
|
$actual = $links->map('Link', 'HTTPCode')->toArray(); |
123
|
|
|
$this->assertEquals($expected, $actual); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Check response descriptions are correct |
126
|
|
|
i18n::set_locale('en_NZ'); |
127
|
|
|
$expected = array( |
128
|
|
|
'http://www.broken.com' => '403 (Forbidden)', |
129
|
|
|
'http://www.broken.com/url/thing' => '404 (Not Found)', |
130
|
|
|
'http://www.nodomain.com' => '0 (Server Not Available)' |
131
|
|
|
); |
132
|
|
|
$actual = $links->map('Link', 'HTTPCodeDescription')->toArray(); |
133
|
|
|
$this->assertEquals($expected, $actual); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test that broken links appears in the reports list |
138
|
|
|
*/ |
139
|
|
|
public function testReportExists() { |
140
|
|
|
$reports = SS_Report::get_reports(); |
141
|
|
|
$reportNames = array(); |
142
|
|
|
foreach($reports as $report) { |
143
|
|
|
$reportNames[] = $report->class; |
144
|
|
|
} |
145
|
|
|
$this->assertContains('BrokenExternalLinksReport',$reportNames, |
146
|
|
|
'BrokenExternalLinksReport is in reports list'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.