From d25adca17506b326c0aef69ba993201bee092ba2 Mon Sep 17 00:00:00 2001 From: Kirk Mayo Date: Mon, 28 Jul 2014 12:39:19 +1200 Subject: [PATCH] NEW: Adding tests and code tidyup --- README.md | 10 +++--- ...ternalLinks.php => BrokenExternalLink.php} | 7 +++- code/reports/BrokenExternalLinksReport.php | 2 +- code/tasks/CheckExternalLinks.php | 6 ++-- tests/ExternalLinksTest.php | 34 +++++++++++++++++++ tests/ExternalLinksTest.yml | 7 ++++ 6 files changed, 58 insertions(+), 8 deletions(-) rename code/model/{BrokenExternalLinks.php => BrokenExternalLink.php} (66%) create mode 100644 tests/ExternalLinksTest.php create mode 100644 tests/ExternalLinksTest.yml diff --git a/README.md b/README.md index 408a914..ffb35a3 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/code/model/BrokenExternalLinks.php b/code/model/BrokenExternalLink.php similarity index 66% rename from code/model/BrokenExternalLinks.php rename to code/model/BrokenExternalLink.php index e002405..1de3554 100644 --- a/code/model/BrokenExternalLinks.php +++ b/code/model/BrokenExternalLink.php @@ -1,6 +1,6 @@ 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer. @@ -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); + } } diff --git a/code/reports/BrokenExternalLinksReport.php b/code/reports/BrokenExternalLinksReport.php index 5a7e79d..4bfe568 100644 --- a/code/reports/BrokenExternalLinksReport.php +++ b/code/reports/BrokenExternalLinksReport.php @@ -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); diff --git a/code/tasks/CheckExternalLinks.php b/code/tasks/CheckExternalLinks.php index ba2f148..a6ef0b0 100644 --- a/code/tasks/CheckExternalLinks.php +++ b/code/tasks/CheckExternalLinks.php @@ -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; @@ -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') diff --git a/tests/ExternalLinksTest.php b/tests/ExternalLinksTest.php new file mode 100644 index 0000000..ae9d898 --- /dev/null +++ b/tests/ExternalLinksTest.php @@ -0,0 +1,34 @@ +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'); + } +} diff --git a/tests/ExternalLinksTest.yml b/tests/ExternalLinksTest.yml new file mode 100644 index 0000000..8a25106 --- /dev/null +++ b/tests/ExternalLinksTest.yml @@ -0,0 +1,7 @@ +Page: + working: + Title: Working Link + Content: 'Localhost' + broken: + Title: Broken Link + Content: 'Broken' \ No newline at end of file