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