NEW: Adding tests and code tidyup

This commit is contained in:
Kirk Mayo 2014-07-28 12:39:19 +12:00
parent bb30288252
commit d25adca175
6 changed files with 58 additions and 8 deletions

View File

@ -31,8 +31,10 @@ The external links module is a task and ModelAdmin to track and to report on bro
Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check your site for external
broken links.
If you have the queuedjobs module installed you can set the task to be run every so ofter
Add the following code to the mysite config to run the job every 24 hours (86400 seconds)
`Config::inst()->update('CheckExternalLinks', 'QueuedJob', 86400);`
Add the following yml config to config.yml in mysite/_config have the the task run once every day (86400 seconds)
`---
Name: externallinkssettings
---
CheckExternalLinks:
Delay: 86400`

View File

@ -1,6 +1,6 @@
<?php
class BrokenExternalLinks extends DataObject {
class BrokenExternalLink extends DataObject {
private static $db = array(
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
@ -25,4 +25,9 @@ class BrokenExternalLinks extends DataObject {
return false;
}
function canView($member = false) {
$member = $member ? $member : Member::currentUser();
$codes = array('content-authors', 'administrators');
return Permission::checkMember($member, $codes);
}
}

View File

@ -57,7 +57,7 @@ class BrokenExternalLinksReport extends SS_Report {
public function sourceRecords() {
$returnSet = new ArrayList();
$links = BrokenExternalLinks::get();
$links = BrokenExternalLink::get();
foreach ($links as $link) {
$link->PageLink = $link->Page()->Title;
$returnSet->push($link);

View File

@ -54,13 +54,15 @@ class CheckExternalLinks extends BuildTask {
if($href && function_exists('curl_init')) {
$handle = curl_init($href);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if (($httpCode < 200 || $httpCode > 302)
|| ($href == '' || $href[0] == '/'))
{
$brokenLink = new BrokenExternalLinks();
$brokenLink = new BrokenExternalLink();
$brokenLink->PageID = $page->ID;
$brokenLink->Link = $href;
$brokenLink->HTTPCode = $httpCode;
@ -94,7 +96,7 @@ class CheckExternalLinks extends BuildTask {
}
// run this again if queued jobs exists and is a valid int
$queuedJob = Config::inst()->get('CheckExternalLinks', 'QueuedJob');
$queuedJob = Config::inst()->get('CheckExternalLinks', 'Delay');
if (isset($queuedJob) && is_int($queuedJob) && class_exists('QueuedJobService')) {
$checkLinks = new CheckExternalLinksJob();
singleton('QueuedJobService')

View File

@ -0,0 +1,34 @@
<?php
class ExternalLinks extends FunctionalTest {
protected static $fixture_file = 'ExternalLinksTest.yml';
public function testWorkingLink() {
// uses http://127.0.0.1 to test a working link
$page = $this->objFromFixture('Page', 'working');
$task = new CheckExternalLinks();
$task->run($page);
$brokenLinks = BrokenExternalLinks::get();
$this->assertEquals(0, $brokenLinks->count());
}
public function testBrokenLink() {
// uses http://192.0.2.1 for a broken link
$page = $this->objFromFixture('Page', 'broken');
$task = new CheckExternalLinks();
$task->run($page);
$brokenLinks = BrokenExternalLinks::get();
$this->assertEquals(1, $brokenLinks->count());
}
public function testReportExists() {
$reports = SS_Report::get_reports();
$reportNames = array();
foreach($reports as $report) {
$reportNames[] = $report->class;
}
$this->assertContains('BrokenExternalLinksReport',$reportNames,
'BrokenExternalLinksReport is in reports list');
}
}

View File

@ -0,0 +1,7 @@
Page:
working:
Title: Working Link
Content: '<a href="http://127.0.0.1">Localhost</a>'
broken:
Title: Broken Link
Content: '<a href="http://192.0.2.1">Broken</a>'