Skip to content

Commit

Permalink
Merge pull request kmayo-ss#1 from tractorcow/pulls/description
Browse files Browse the repository at this point in the history
API Add description for response code to report
  • Loading branch information
halkyon committed Aug 12, 2014
2 parents 7698ba9 + 7328344 commit d9e1b99
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
27 changes: 24 additions & 3 deletions code/model/BrokenExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,44 @@ public function Page() {

public static $summary_fields = array(
'Page.Title' => 'Page',
'HTTPCode' => 'HTTP Code',
'HTTPCodeDescription' => 'HTTP Code',
'Created' => 'Created'
);

public static $searchable_fields = array(
'HTTPCode' => array('title' => 'HTTP Code')
);

function canEdit($member = false) {
public function canEdit($member = false) {
return false;
}

function canView($member = false) {
public function canView($member = false) {
$member = $member ? $member : Member::currentUser();
$codes = array('content-authors', 'administrators');
return Permission::checkMember($member, $codes);
}

/**
* Retrieve a human readable description of a response code
*
* @return string
*/
public function getHTTPCodeDescription() {
$code = $this->HTTPCode;
if(empty($code)) {
// Assume that $code = 0 means there was no response
$description = _t(__CLASS__.'.NOTAVAILABLE', 'Server Not Available');
} elseif(
($descriptions = Config::inst()->get('SS_HTTPResponse', 'status_codes'))
&& isset($descriptions[$code])
) {
$description = $descriptions[$code];
} else {
$description = _t(__CLASS__.'.UNKNOWNRESPONSE', 'Unknown Response Code');
}
return sprintf("%d (%s)", $code, $description);
}
}


2 changes: 1 addition & 1 deletion code/reports/BrokenExternalLinksReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function columns() {
);
}
),
'HTTPCode' => 'HTTP Error Code',
'HTTPCodeDescription' => 'HTTP Error Code',
"Title" => array(
"title" => 'Page link is on',
'formatting' => function($value, $item) {
Expand Down
10 changes: 10 additions & 0 deletions tests/ExternalLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public function testLinks() {
);
$actual = $links->map('Link', 'HTTPCode')->toArray();
$this->assertEquals($expected, $actual);

// Check response descriptions are correct
i18n::set_locale('en_NZ');
$expected = array(
'http://www.broken.com' => '403 (Forbidden)',
'http://www.broken.com/url/thing' => '404 (Not Found)',
'http://www.nodomain.com' => '0 (Server Not Available)'
);
$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
$this->assertEquals($expected, $actual);
}

/**
Expand Down

0 comments on commit d9e1b99

Please sign in to comment.