mirror of
https://github.com/silverstripe/silverstripe-externallinks.git
synced 2024-10-22 17:05:44 +02:00
Update unit tests to be rid of Phockito
This odd dependency is causing inclusion issues to do with namespacing and the ability to work well with the framework (according to the dependency's readme). The usage of the tool in this case adds no value, as it's performing purely stub type work, rather than mock work. So instead we can use an actual stub (simply return test values depending on input) and have everything work well. After removing the Phockito dependency it was found that the tests that would skip when it isn't present were actually in a failing state. So they had to be repaired too, mostly through the use of fixing the poor code that was causing them to fail (as opposed to being bad tests). This is both configuration and proceedural work.
This commit is contained in:
parent
ed7d91becb
commit
98dac9b314
@ -2,7 +2,7 @@
|
|||||||
Name: externallinksdependencies
|
Name: externallinksdependencies
|
||||||
---
|
---
|
||||||
SilverStripe\Core\Injector\Injector:
|
SilverStripe\Core\Injector\Injector:
|
||||||
LinkChecker: SilverStripe\ExternalLinks\Tasks\CurlLinkChecker
|
SilverStripe\ExternalLinks\Tasks\LinkChecker: SilverStripe\ExternalLinks\Tasks\CurlLinkChecker
|
||||||
Psr\SimpleCache\CacheInterface.CurlLinkChecker:
|
Psr\SimpleCache\CacheInterface.CurlLinkChecker:
|
||||||
factory: SilverStripe\Core\Cache\CacheFactory
|
factory: SilverStripe\Core\Cache\CacheFactory
|
||||||
constructor:
|
constructor:
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
Name: externallink
|
Name: externallinkroutes
|
||||||
After: framework/routes
|
Before: '#adminroutes'
|
||||||
---
|
---
|
||||||
Director:
|
SilverStripe\Control\Director:
|
||||||
rules:
|
rules:
|
||||||
'admin/externallinks//$Action': 'CMSExternalLinksController'
|
'admin/externallinks//$Action': SilverStripe\ExternalLinks\Controllers\CMSExternalLinksController
|
||||||
|
@ -12,13 +12,16 @@ use Symbiote\QueuedJobs\Services\QueuedJobService;
|
|||||||
class CMSExternalLinksController extends Controller
|
class CMSExternalLinksController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
private static $allowed_actions = array('getJobStatus', 'start');
|
private static $allowed_actions = [
|
||||||
|
'getJobStatus',
|
||||||
|
'start'
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Respond to Ajax requests for info on a running job
|
* Respond to Ajax requests for info on a running job
|
||||||
*
|
*
|
||||||
* @return string JSON string detailing status of the job
|
* @return string JSON string detailing status of the job
|
||||||
*/
|
*/
|
||||||
public function getJobStatus()
|
public function getJobStatus()
|
||||||
{
|
{
|
||||||
// Set headers
|
// Set headers
|
||||||
@ -32,19 +35,19 @@ class CMSExternalLinksController extends Controller
|
|||||||
// Format status
|
// Format status
|
||||||
$track = BrokenExternalPageTrackStatus::get_latest();
|
$track = BrokenExternalPageTrackStatus::get_latest();
|
||||||
if ($track) {
|
if ($track) {
|
||||||
return json_encode(array(
|
return json_encode([
|
||||||
'TrackID' => $track->ID,
|
'TrackID' => $track->ID,
|
||||||
'Status' => $track->Status,
|
'Status' => $track->Status,
|
||||||
'Completed' => $track->getCompletedPages(),
|
'Completed' => $track->getCompletedPages(),
|
||||||
'Total' => $track->getTotalPages()
|
'Total' => $track->getTotalPages()
|
||||||
));
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a broken external link check
|
* Starts a broken external link check
|
||||||
*/
|
*/
|
||||||
public function start()
|
public function start()
|
||||||
{
|
{
|
||||||
// return if the a job is already running
|
// return if the a job is already running
|
@ -69,16 +69,17 @@ class BrokenExternalLink extends DataObject
|
|||||||
public function getHTTPCodeDescription()
|
public function getHTTPCodeDescription()
|
||||||
{
|
{
|
||||||
$code = $this->HTTPCode;
|
$code = $this->HTTPCode;
|
||||||
if (empty($code)) {
|
|
||||||
|
try {
|
||||||
|
$response = HTTPResponse::create('', $code);
|
||||||
// Assume that $code = 0 means there was no response
|
// Assume that $code = 0 means there was no response
|
||||||
$description = _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available');
|
$description = $code ?
|
||||||
} elseif (($descriptions = Config::inst()->get(HTTPResponse::class, 'status_codes'))
|
$response->getStatusDescription() :
|
||||||
&& isset($descriptions[$code])
|
_t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available');
|
||||||
) {
|
} catch (InvalidArgumentException $e) {
|
||||||
$description = $descriptions[$code];
|
|
||||||
} else {
|
|
||||||
$description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code');
|
$description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code');
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf("%d (%s)", $code, $description);
|
return sprintf("%d (%s)", $code, $description);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,10 +111,10 @@ class BrokenExternalPageTrackStatus extends DataObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and prepare a new status
|
* Create and prepare a new status
|
||||||
*
|
*
|
||||||
* @return BrokenExternalPageTrackStatus
|
* @return BrokenExternalPageTrackStatus
|
||||||
*/
|
*/
|
||||||
public static function create_status()
|
public static function create_status()
|
||||||
{
|
{
|
||||||
// If the script is to be started create a new status
|
// If the script is to be started create a new status
|
||||||
|
@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Reports;
|
namespace SilverStripe\ExternalLinks\Reports;
|
||||||
|
|
||||||
use SilverStripe\Core\Convert;
|
|
||||||
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
|
||||||
use SilverStripe\ORM\ArrayList;
|
use SilverStripe\ORM\ArrayList;
|
||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
|
use SilverStripe\Core\Convert;
|
||||||
|
use SilverStripe\View\HTML;
|
||||||
use SilverStripe\Forms\LiteralField;
|
use SilverStripe\Forms\LiteralField;
|
||||||
use SilverStripe\Reports\Report;
|
use SilverStripe\Reports\Report;
|
||||||
|
use SilverStripe\View\Requirements;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content side-report listing pages with external broken links
|
* Content side-report listing pages with external broken links
|
||||||
@ -72,7 +73,7 @@ class BrokenExternalLinksReport extends Report
|
|||||||
if ($track) {
|
if ($track) {
|
||||||
return $track->BrokenLinks();
|
return $track->BrokenLinks();
|
||||||
}
|
}
|
||||||
return new ArrayList();
|
return ArrayList::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCMSFields()
|
public function getCMSFields()
|
||||||
@ -81,17 +82,19 @@ class BrokenExternalLinksReport extends Report
|
|||||||
$fields = parent::getCMSFields();
|
$fields = parent::getCMSFields();
|
||||||
|
|
||||||
$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
|
$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
|
||||||
$reportResult = new LiteralField('ResultTitle', $reportResultSpan);
|
$reportResult = LiteralField::create('ResultTitle', $reportResultSpan);
|
||||||
$fields->push($reportResult);
|
$fields->push($reportResult);
|
||||||
|
|
||||||
$button = '<button id="externalLinksReport" type="button">%s</button>';
|
$button = HTML::createTag(
|
||||||
$runReportButton = new LiteralField(
|
'button',
|
||||||
'runReport',
|
[
|
||||||
sprintf(
|
'id' => 'externalLinksReport',
|
||||||
$button,
|
'type' => 'button',
|
||||||
_t(__CLASS__ . '.RUNREPORT', 'Create new report')
|
'class' => 'btn btn-primary'
|
||||||
)
|
],
|
||||||
|
_t(__CLASS__ . '.RUNREPORT', 'Create new report')
|
||||||
);
|
);
|
||||||
|
$runReportButton = LiteralField::create('runReport', $button);
|
||||||
$fields->push($runReportButton);
|
$fields->push($runReportButton);
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
|
@ -2,27 +2,23 @@
|
|||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Tasks;
|
namespace SilverStripe\ExternalLinks\Tasks;
|
||||||
|
|
||||||
use DOMNode;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
use SilverStripe\Dev\Debug;
|
|
||||||
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
|
|
||||||
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
|
||||||
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
|
||||||
use SilverStripe\ORM\DB;
|
|
||||||
use SilverStripe\Dev\BuildTask;
|
use SilverStripe\Dev\BuildTask;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use SilverStripe\Dev\Debug;
|
||||||
|
use DOMNode;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
||||||
|
|
||||||
class CheckExternalLinksTask extends BuildTask
|
class CheckExternalLinksTask extends BuildTask
|
||||||
{
|
{
|
||||||
|
|
||||||
private static $dependencies = array(
|
private static $dependencies = [
|
||||||
'LinkChecker' => '%$LinkChecker'
|
'LinkChecker' => '%$' . LinkChecker::class
|
||||||
);
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\ExternalLinks\Tasks;
|
namespace SilverStripe\ExternalLinks\Tasks;
|
||||||
|
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check links using curl
|
* Check links using curl
|
||||||
|
@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Tests;
|
namespace SilverStripe\ExternalLinks\Tests;
|
||||||
|
|
||||||
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
|
||||||
use SilverStripe\Core\Injector\Injector;
|
|
||||||
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
|
||||||
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
|
||||||
use SilverStripe\i18n\i18n;
|
|
||||||
use SilverStripe\Reports\Report;
|
|
||||||
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
|
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
||||||
use SilverStripe\ExternalLinks\Tests\ExternalLinksTestPage;
|
use SilverStripe\ExternalLinks\Tests\ExternalLinksTestPage;
|
||||||
use Phockito;
|
use SilverStripe\i18n\i18n;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
||||||
|
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker;
|
||||||
|
use SilverStripe\Reports\Report;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
|
||||||
class ExternalLinksTest extends SapphireTest
|
class ExternalLinksTest extends SapphireTest
|
||||||
{
|
{
|
||||||
@ -22,67 +22,12 @@ class ExternalLinksTest extends SapphireTest
|
|||||||
ExternalLinksTestPage::class
|
ExternalLinksTestPage::class
|
||||||
);
|
);
|
||||||
|
|
||||||
public function setUpOnce()
|
|
||||||
{
|
|
||||||
if (class_exists(Phockito::class)) {
|
|
||||||
Phockito::include_hamcrest(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::setUpOnce();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// Check dependencies
|
// Stub link checker
|
||||||
if (!class_exists(Phockito::class)) {
|
$checker = new PretendLinkChecker;
|
||||||
$this->skipTest = true;
|
|
||||||
return $this->markTestSkipped("These tests need the Phockito module installed to run");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mock link checker
|
|
||||||
$checker = Phockito::mock(LinkChecker::class);
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('http://www.working.com')
|
|
||||||
->return(200);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('http://www.broken.com/url/thing') // 404 on working site
|
|
||||||
->return(404);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('http://www.broken.com') // 403 on working site
|
|
||||||
->return(403);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('http://www.nodomain.com') // no ping
|
|
||||||
->return(0);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('/internal/link')
|
|
||||||
->return(null);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('[sitetree_link,id=9999]')
|
|
||||||
->return(null);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('home')
|
|
||||||
->return(null);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('broken-internal')
|
|
||||||
->return(null);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink('[sitetree_link,id=1]')
|
|
||||||
->return(null);
|
|
||||||
|
|
||||||
Phockito::when($checker)
|
|
||||||
->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
|
|
||||||
->return(404);
|
|
||||||
|
|
||||||
Injector::inst()->registerService($checker, LinkChecker::class);
|
Injector::inst()->registerService($checker, LinkChecker::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
28
tests/Stubs/PretendLinkChecker.php
Normal file
28
tests/Stubs/PretendLinkChecker.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
namespace SilverStripe\ExternalLinks\Tests\Stubs;
|
||||||
|
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
||||||
|
|
||||||
|
class PretendLinkChecker implements LinkChecker
|
||||||
|
{
|
||||||
|
public function checkLink($href)
|
||||||
|
{
|
||||||
|
switch ($href) {
|
||||||
|
case 'http://www.working.com':
|
||||||
|
return 200;
|
||||||
|
case 'http://www.broken.com':
|
||||||
|
return 403;
|
||||||
|
case 'http://www.nodomain.com':
|
||||||
|
return 0;
|
||||||
|
case '/internal/link':
|
||||||
|
case '[sitetree_link,id=9999]':
|
||||||
|
case 'home':
|
||||||
|
case 'broken-internal':
|
||||||
|
case '[sitetree_link,id=1]':
|
||||||
|
return null;
|
||||||
|
case 'http://www.broken.com/url/thing':
|
||||||
|
default:
|
||||||
|
return 404;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user