2014-07-28 02:39:19 +02:00
|
|
|
<?php
|
|
|
|
|
2017-11-23 01:19:00 +01:00
|
|
|
namespace SilverStripe\ExternalLinks\Tests;
|
|
|
|
|
2017-11-27 21:35:31 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-11-23 00:56:44 +01:00
|
|
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
2017-11-27 21:35:31 +01:00
|
|
|
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
|
2017-11-27 03:14:16 +01:00
|
|
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
|
|
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
2017-11-27 21:35:31 +01:00
|
|
|
use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage;
|
2017-11-27 03:14:16 +01:00
|
|
|
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker;
|
2017-11-27 21:35:31 +01:00
|
|
|
use SilverStripe\i18n\i18n;
|
2017-11-23 00:56:44 +01:00
|
|
|
use SilverStripe\Reports\Report;
|
|
|
|
|
|
|
|
class ExternalLinksTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'ExternalLinksTest.yml';
|
|
|
|
|
2017-11-23 02:54:27 +01:00
|
|
|
protected static $extra_dataobjects = array(
|
|
|
|
ExternalLinksTestPage::class
|
2017-05-12 01:38:49 +02:00
|
|
|
);
|
|
|
|
|
2017-11-26 23:19:58 +01:00
|
|
|
protected function setUp()
|
2017-11-23 00:56:44 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-11-27 03:14:16 +01:00
|
|
|
// Stub link checker
|
|
|
|
$checker = new PretendLinkChecker;
|
2017-11-23 00:56:44 +01:00
|
|
|
Injector::inst()->registerService($checker, LinkChecker::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinks()
|
|
|
|
{
|
|
|
|
// Run link checker
|
|
|
|
$task = CheckExternalLinksTask::create();
|
|
|
|
$task->setSilent(true); // Be quiet during the test!
|
|
|
|
$task->runLinksCheck();
|
|
|
|
|
|
|
|
// Get all links checked
|
|
|
|
$status = BrokenExternalPageTrackStatus::get_latest();
|
|
|
|
$this->assertEquals('Completed', $status->Status);
|
|
|
|
$this->assertEquals(5, $status->TotalPages);
|
|
|
|
$this->assertEquals(5, $status->CompletedPages);
|
|
|
|
|
|
|
|
// Check all pages have had the correct HTML adjusted
|
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
2017-11-26 23:19:58 +01:00
|
|
|
$page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i);
|
2017-11-23 00:56:44 +01:00
|
|
|
$this->assertNotEmpty($page->Content);
|
|
|
|
$this->assertEquals(
|
|
|
|
$page->ExpectedContent,
|
|
|
|
$page->Content,
|
|
|
|
"Assert that the content of page{$i} has been updated"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the correct report of broken links is generated
|
|
|
|
$links = $status
|
|
|
|
->BrokenLinks()
|
|
|
|
->sort('Link');
|
|
|
|
|
|
|
|
$this->assertEquals(4, $links->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'http://www.broken.com',
|
|
|
|
'http://www.broken.com/url/thing',
|
|
|
|
'http://www.broken.com/url/thing',
|
|
|
|
'http://www.nodomain.com'
|
|
|
|
),
|
|
|
|
array_values($links->map('ID', 'Link')->toArray())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check response codes are correct
|
|
|
|
$expected = array(
|
|
|
|
'http://www.broken.com' => 403,
|
|
|
|
'http://www.broken.com/url/thing' => 404,
|
|
|
|
'http://www.nodomain.com' => 0
|
|
|
|
);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that broken links appears in the reports list
|
|
|
|
*/
|
|
|
|
public function testReportExists()
|
|
|
|
{
|
|
|
|
$reports = Report::get_reports();
|
|
|
|
$reportNames = array();
|
|
|
|
foreach ($reports as $report) {
|
2017-11-26 23:19:58 +01:00
|
|
|
$reportNames[] = get_class($report);
|
2017-11-23 00:56:44 +01:00
|
|
|
}
|
|
|
|
$this->assertContains(
|
|
|
|
BrokenExternalLinksReport::class,
|
|
|
|
$reportNames,
|
|
|
|
'BrokenExternalLinksReport is in reports list'
|
|
|
|
);
|
|
|
|
}
|
2014-07-28 02:39:19 +02:00
|
|
|
}
|