Skip to content

Commit

Permalink
NEW: Adding tests and code tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
kmayo-ss committed Jul 28, 2014
1 parent bb30288 commit d25adca
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,4 +25,9 @@ function canEdit($member = false) {
return false;
}

function canView($member = false) {
$member = $member ? $member : Member::currentUser();
$codes = array('content-authors', 'administrators');
return Permission::checkMember($member, $codes);
}
}
2 changes: 1 addition & 1 deletion code/reports/BrokenExternalLinksReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getColumns() {

public function sourceRecords() {
$returnSet = new ArrayList();
$links = BrokenExternalLinks::get();
$links = BrokenExternalLink::get();
foreach ($links as $link) {
$link->PageLink = $link->Page()->Title;
$returnSet->push($link);
Expand Down
6 changes: 4 additions & 2 deletions code/tasks/CheckExternalLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ function run($request) {
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;
Expand Down Expand Up @@ -94,7 +96,7 @@ function run($request) {
}

// 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')
Expand Down
34 changes: 34 additions & 0 deletions tests/ExternalLinksTest.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
7 changes: 7 additions & 0 deletions tests/ExternalLinksTest.yml
Original file line number Diff line number Diff line change
@@ -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>'

0 comments on commit d25adca

Please sign in to comment.