silverstripe-cms/tests/reports/CmsReportsTest.php

352 lines
13 KiB
PHP
Raw Normal View History

<?php
2017-01-26 05:21:00 +01:00
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Reports\RecentlyEditedReport;
use SilverStripe\CMS\Reports\BrokenLinksReport;
use SilverStripe\CMS\Reports\BrokenFilesReport;
use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\CMS\Reports\BrokenVirtualPagesReport;
use SilverStripe\CMS\Model\RedirectorPage;
use SilverStripe\CMS\Reports\BrokenRedirectorPagesReport;
2016-09-09 01:26:24 +02:00
use SilverStripe\Reports\Report;
use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest;
/**
* @package cms
* @subpackage tests
*/
2017-01-25 21:59:25 +01:00
class CmsReportsTest extends SapphireTest
{
2017-01-25 21:59:25 +01:00
protected static $fixture_file = 'CmsReportsTest.yml';
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
private static $daysAgo = 14;
2017-01-25 21:59:25 +01:00
public function setUp()
{
parent::setUp();
2017-01-25 21:59:25 +01:00
// set the dates by hand: impossible to set via yml
$afterThreshold = strtotime('-'.(self::$daysAgo-1).' days', strtotime('31-06-2009 00:00:00'));
$beforeThreshold = strtotime('-'.(self::$daysAgo+1).' days', strtotime('31-06-2009 00:00:00'));
2016-03-08 21:50:55 +01:00
2017-01-26 05:21:00 +01:00
$after = $this->objFromFixture(SiteTree::class, 'after');
$before = $this->objFromFixture(SiteTree::class, 'before');
2017-01-25 21:59:25 +01:00
DB::query("UPDATE \"SiteTree\" SET \"Created\"='2009-01-01 00:00:00', \"LastEdited\"='".date('Y-m-d H:i:s', $afterThreshold)."' WHERE \"ID\"='".$after->ID."'");
DB::query("UPDATE \"SiteTree\" SET \"Created\"='2009-01-01 00:00:00', \"LastEdited\"='".date('Y-m-d H:i:s', $beforeThreshold)."' WHERE \"ID\"='".$before->ID."'");
}
2017-01-25 21:59:25 +01:00
/**
* ASSERT whether a report is returning the correct results, based on a broken "draft" and/or "published" page.
*
* @parameter ss_report
* @parameter boolean
* @parameter boolean
*/
2017-01-25 21:59:25 +01:00
public function isReportBroken($report, $isDraftBroken, $isPublishedBroken)
{
2017-01-25 21:59:25 +01:00
$class = get_class($report);
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report is returning the correct results.
$parameters = array('CheckSite' => 'Draft');
$results = count($report->sourceRecords($parameters, null, null)) > 0;
$isDraftBroken ? $this->assertTrue($results, "{$class} has NOT returned the correct DRAFT results, as NO pages were found.") : $this->assertFalse($results, "{$class} has NOT returned the correct DRAFT results, as pages were found.");
2017-01-25 21:59:25 +01:00
// ASSERT that the "published" report is returning the correct results.
$parameters = array('CheckSite' => 'Published', 'OnLive' => 1);
$results = count($report->sourceRecords($parameters, null, null)) > 0;
$isPublishedBroken ? $this->assertTrue($results, "{$class} has NOT returned the correct PUBLISHED results, as NO pages were found.") : $this->assertFalse($results, "{$class} has NOT returned the correct PUBLISHED results, as pages were found.");
}
2017-01-25 21:59:25 +01:00
public function testRecentlyEdited()
{
2017-01-26 05:21:00 +01:00
DBDatetime::set_mock_now('2009-06-30 00:00:00');
2017-01-26 05:21:00 +01:00
$after = $this->objFromFixture(SiteTree::class, 'after');
$before = $this->objFromFixture(SiteTree::class, 'before');
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
$r = new RecentlyEditedReport();
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
// check if contains only elements not older than $daysAgo days
$this->assertNotNull($r->records(array()));
$this->assertContains($after->ID, $r->records(array())->column('ID'));
$this->assertNotContains($before->ID, $r->records(array())->column('ID'));
2017-01-25 21:59:25 +01:00
DBDatetime::clear_mock_now();
}
2017-01-25 21:59:25 +01:00
/**
* Test the broken links side report.
*/
2017-01-25 21:59:25 +01:00
public function testBrokenLinks()
{
2017-01-25 21:59:25 +01:00
// Create a "draft" page with a broken link.
2017-01-25 21:59:25 +01:00
$page = Page::create();
$page->Content = "<a href='[sitetree_link,id=987654321]'>This</a> is a broken link.";
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// Retrieve the broken links side report.
2017-01-25 21:59:25 +01:00
$reports = Report::get_reports();
$brokenLinksReport = null;
foreach ($reports as $report) {
if ($report instanceof BrokenLinksReport) {
$brokenLinksReport = $report;
break;
}
}
2017-01-25 21:59:25 +01:00
// Determine that the report exists, otherwise it has been excluded.
if (!$brokenLinksReport) {
$this->markTestSkipped('BrokenLinksReport is not an available report');
return;
}
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenLinksReport, true, false);
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenLinksReport, true, true);
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2017-01-25 21:59:25 +01:00
$page->Content = str_replace('987654321', $page->ID, $page->Content);
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenLinksReport, false, true);
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenLinksReport, false, false);
}
2017-01-25 21:59:25 +01:00
/**
* Test the broken files side report.
*/
2017-01-25 21:59:25 +01:00
public function testBrokenFiles()
{
2017-01-25 21:59:25 +01:00
// Create a "draft" page with a broken file.
2017-01-25 21:59:25 +01:00
$page = Page::create();
$page->Content = "<a href='[file_link,id=987654321]'>This</a> is a broken file.";
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// Retrieve the broken files side report.
2017-01-25 21:59:25 +01:00
$reports = Report::get_reports();
$brokenFilesReport = null;
foreach ($reports as $report) {
if ($report instanceof BrokenFilesReport) {
$brokenFilesReport = $report;
break;
}
}
2017-01-25 21:59:25 +01:00
// Determine that the report exists, otherwise it has been excluded.
if (!$brokenFilesReport) {
$this->markTestSkipped('BrokenFilesReport is not an available report');
return;
}
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken file.
// ASSERT that the "published" report has NOT detected the page having a broken file, as the page has not been "published" yet.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenFilesReport, true, false);
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken file.
// ASSERT that the "published" report has detected the page having a broken file.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenFilesReport, true, true);
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken file.
2017-01-25 21:59:25 +01:00
$file = File::create();
$file->Filename = 'name.pdf';
$file->write();
$page->Content = str_replace('987654321', $file->ID, $page->Content);
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken file.
// ASSERT that the "published" report has detected the page having a broken file, as the previous content remains "published".
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenFilesReport, false, true);
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken file.
// ASSERT that the "published" report has NOT detected the page having a broken file.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenFilesReport, false, false);
}
2017-01-25 21:59:25 +01:00
/**
* Test the broken virtual pages side report.
*/
2017-01-25 21:59:25 +01:00
public function testBrokenVirtualPages()
{
2017-01-25 21:59:25 +01:00
// Create a "draft" virtual page with a broken link.
2017-01-25 21:59:25 +01:00
$page = VirtualPage::create();
$page->CopyContentFromID = 987654321;
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// Retrieve the broken virtual pages side report.
2017-01-25 21:59:25 +01:00
$reports = Report::get_reports();
$brokenVirtualPagesReport = null;
foreach ($reports as $report) {
if ($report instanceof BrokenVirtualPagesReport) {
$brokenVirtualPagesReport = $report;
break;
}
}
2017-01-25 21:59:25 +01:00
// Determine that the report exists, otherwise it has been excluded.
if (!$brokenVirtualPagesReport) {
$this->markTestSkipped('BrokenFilesReport is not an available report');
return;
}
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenVirtualPagesReport, true, false);
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenVirtualPagesReport, true, true);
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2017-01-25 21:59:25 +01:00
$contentPage = Page::create();
$contentPage->Content = 'This is some content.';
$contentPage->writeToStage('Stage');
$contentPage->writeToStage('Live');
$page->CopyContentFromID = $contentPage->ID;
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenVirtualPagesReport, false, true);
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenVirtualPagesReport, false, false);
}
2017-01-25 21:59:25 +01:00
/**
* Test the broken redirector pages side report.
*/
2017-01-25 21:59:25 +01:00
public function testBrokenRedirectorPages()
{
2017-01-25 21:59:25 +01:00
// Create a "draft" redirector page with a broken link.
2017-01-25 21:59:25 +01:00
$page = RedirectorPage::create();
$page->RedirectionType = 'Internal';
$page->LinkToID = 987654321;
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// Retrieve the broken redirector pages side report.
2017-01-25 21:59:25 +01:00
$reports = Report::get_reports();
$brokenRedirectorPagesReport = null;
foreach ($reports as $report) {
if ($report instanceof BrokenRedirectorPagesReport) {
$brokenRedirectorPagesReport = $report;
break;
}
}
2017-01-25 21:59:25 +01:00
// Determine that the report exists, otherwise it has been excluded.
if (!$brokenRedirectorPagesReport) {
$this->markTestSkipped('BrokenRedirectorPagesReport is not an available report');
return;
}
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link, as the page has not been "published" yet.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenRedirectorPagesReport, true, false);
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenRedirectorPagesReport, true, true);
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2017-01-25 21:59:25 +01:00
$contentPage = Page::create();
$contentPage->Content = 'This is some content.';
$contentPage->writeToStage('Stage');
$contentPage->writeToStage('Live');
$page->LinkToID = $contentPage->ID;
$page->writeToStage('Stage');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has detected the page having a broken link, as the previous content remains "published".
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenRedirectorPagesReport, false, true);
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2017-01-25 21:59:25 +01:00
$page->writeToStage('Live');
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report has NOT detected the page having a broken link.
// ASSERT that the "published" report has NOT detected the page having a broken link.
2017-01-25 21:59:25 +01:00
$this->isReportBroken($brokenRedirectorPagesReport, false, false);
}
}