2009-08-06 23:41:49 +02:00
< ? php
2016-06-16 06:57:19 +02:00
2017-08-09 04:53:38 +02:00
namespace SilverStripe\CMS\Tests\Reports ;
2017-08-09 03:25:12 +02:00
2017-01-26 05:21:00 +01:00
use SilverStripe\CMS\Model\SiteTree ;
2016-06-16 06:57:19 +02:00
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 ;
2016-08-23 04:36:06 +02:00
use SilverStripe\Assets\File ;
use SilverStripe\Dev\SapphireTest ;
2017-08-09 03:25:12 +02:00
use Page ;
2017-01-25 21:59:25 +01:00
class CmsReportsTest extends SapphireTest
{
2011-03-30 09:04:31 +02:00
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 ;
2009-08-06 23:41:49 +02:00
2021-10-27 23:40:52 +02:00
protected function setUp () : void
2017-01-25 21:59:25 +01:00
{
parent :: setUp ();
2009-08-06 23:41:49 +02:00
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' );
2009-08-06 23:41:49 +02:00
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 . " ' " );
}
2015-06-10 03:37:27 +02:00
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 .
*
2017-08-09 04:53:38 +02:00
* @ param Report $report
* @ param bool $isDraftBroken
* @ param bool $isPublishedBroken
2017-01-25 21:59:25 +01:00
*/
public function isReportBroken ( $report , $isDraftBroken , $isPublishedBroken )
{
$class = get_class ( $report );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// ASSERT that the "draft" report is returning the correct results.
2020-04-19 06:18:01 +02:00
$parameters = [ 'CheckSite' => 'Draft' ];
2022-04-13 07:07:59 +02:00
$results = count ( $report -> sourceRecords ( $parameters , null , null ) ? ? []) > 0 ;
2017-01-25 21:59:25 +01:00
$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. " );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// ASSERT that the "published" report is returning the correct results.
2020-04-19 06:18:01 +02:00
$parameters = [ 'CheckSite' => 'Published' , 'OnLive' => 1 ];
2022-04-13 07:07:59 +02:00
$results = count ( $report -> sourceRecords ( $parameters , null , null ) ? ? []) > 0 ;
2017-01-25 21:59:25 +01:00
$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. " );
}
2009-08-06 23:41:49 +02:00
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' );
2009-08-06 23:41:49 +02: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
2020-04-19 06:18:01 +02:00
$this -> assertNotNull ( $r -> records ([]));
$this -> assertContains ( $after -> ID , $r -> records ([]) -> column ( 'ID' ));
$this -> assertNotContains ( $before -> ID , $r -> records ([]) -> column ( 'ID' ));
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
DBDatetime :: clear_mock_now ();
}
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
/**
* Test the broken links side report .
*/
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
public function testBrokenLinks ()
{
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Create a "draft" page with a broken link.
2015-06-10 03:37:27 +02:00
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' );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Retrieve the broken links side report.
2015-06-10 03:37:27 +02:00
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 ;
}
}
2015-06-10 03:37:27 +02:00
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 ;
}
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenLinksReport , true , false );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenLinksReport , true , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
2022-04-13 07:07:59 +02:00
$page -> Content = str_replace ( '987654321' , $page -> ID ? ? '' , $page -> Content ? ? '' );
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
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".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenLinksReport , false , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenLinksReport , false , false );
}
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
/**
* Test the broken files side report .
*/
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
public function testBrokenFiles ()
{
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Create a "draft" page with a broken file.
2015-06-10 03:37:27 +02:00
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' );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Retrieve the broken files side report.
2015-06-10 03:37:27 +02:00
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 ;
}
}
2015-06-10 03:37:27 +02:00
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 ;
}
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenFilesReport , true , false );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenFilesReport , true , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken file.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$file = File :: create ();
$file -> Filename = 'name.pdf' ;
$file -> write ();
2022-04-13 07:07:59 +02:00
$page -> Content = str_replace ( '987654321' , $file -> ID ? ? '' , $page -> Content ? ? '' );
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
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".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenFilesReport , false , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenFilesReport , false , false );
}
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
/**
* Test the broken virtual pages side report .
*/
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
public function testBrokenVirtualPages ()
{
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Create a "draft" virtual page with a broken link.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page = VirtualPage :: create ();
$page -> CopyContentFromID = 987654321 ;
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Retrieve the broken virtual pages side report.
2015-06-10 03:37:27 +02:00
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 ;
}
}
2015-06-10 03:37:27 +02:00
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 ;
}
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , true , false );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , true , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
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' );
2015-06-10 03:37:27 +02:00
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".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , false , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , false , false );
}
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
/**
* Test the broken redirector pages side report .
*/
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
public function testBrokenRedirectorPages ()
{
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Create a "draft" redirector page with a broken link.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page = RedirectorPage :: create ();
$page -> RedirectionType = 'Internal' ;
$page -> LinkToID = 987654321 ;
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Retrieve the broken redirector pages side report.
2015-06-10 03:37:27 +02:00
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 ;
}
}
2015-06-10 03:37:27 +02:00
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 ;
}
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , true , false );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , true , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
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' );
2015-06-10 03:37:27 +02:00
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".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , false , true );
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
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.
2015-06-10 03:37:27 +02:00
2017-01-25 21:59:25 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , false , false );
}
2012-04-12 09:23:20 +02:00
}