2009-08-06 23:41:49 +02:00
< ? php
/**
* @ package cms
* @ subpackage tests
*/
2013-01-17 01:27:14 +01:00
class CmsReportsTest extends SapphireTest {
2011-03-30 09:04:31 +02:00
2013-03-26 01:21:36 +01:00
protected static $fixture_file = 'CmsReportsTest.yml' ;
2011-03-30 09:04:31 +02:00
2013-03-18 11:47:15 +01:00
private static $daysAgo = 14 ;
2009-08-06 23:41:49 +02:00
2012-09-19 12:07:46 +02:00
public function setUp () {
2009-08-06 23:41:49 +02:00
parent :: setUp ();
// set the dates by hand: impossible to set via yml
2013-01-17 01:27:14 +01:00
$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' ));
2009-08-06 23:41:49 +02:00
$after = $this -> objFromFixture ( 'SiteTree' , 'after' );
$before = $this -> objFromFixture ( 'SiteTree' , 'before' );
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
/**
* 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
*/
public function isReportBroken ( $report , $isDraftBroken , $isPublishedBroken ) {
$class = get_class ( $report );
// ASSERT that the "draft" report is returning the correct results.
2015-11-10 03:23:58 +01:00
$parameters = array ( 'CheckSite' => 'Draft' );
$results = count ( $report -> sourceRecords ( $parameters , null , null )) > 0 ;
2015-06-10 03:37:27 +02: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. " );
// ASSERT that the "published" report is returning the correct results.
2015-11-10 03:23:58 +01:00
$parameters = array ( 'CheckSite' => 'Published' , 'OnLive' => 1 );
$results = count ( $report -> sourceRecords ( $parameters , null , null )) > 0 ;
2015-06-10 03:37:27 +02: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. " );
}
2012-09-19 12:07:46 +02:00
public function testRecentlyEdited () {
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Sam Minnee <sam@silverstripe.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@90076 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:42 +01:00
SS_Datetime :: set_mock_now ( '31-06-2009 00:00:00' );
2009-08-06 23:41:49 +02:00
$after = $this -> objFromFixture ( 'SiteTree' , 'after' );
$before = $this -> objFromFixture ( 'SiteTree' , 'before' );
2013-01-17 01:27:14 +01:00
$r = new RecentlyEditedReport ();
2009-08-06 23:41:49 +02:00
// check if contains only elements not older than $daysAgo days
2010-05-28 04:30:46 +02:00
$this -> assertNotNull ( $r -> records ( array ()));
$this -> assertContains ( $after -> ID , $r -> records ( array ()) -> column ( 'ID' ));
$this -> assertNotContains ( $before -> ID , $r -> records ( array ()) -> column ( 'ID' ));
2009-08-06 23:41:49 +02:00
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Sam Minnee <sam@silverstripe.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@90076 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:42 +01:00
SS_DateTime :: clear_mock_now ();
2009-08-06 23:41:49 +02:00
}
2015-06-10 03:37:27 +02:00
/**
* Test the broken links side report .
*/
public function testBrokenLinks () {
// Create a "draft" page with a broken link.
$page = Page :: create ();
$page -> Content = " <a href='[sitetree_link,id=987654321]'>This</a> is a broken link. " ;
$page -> writeToStage ( 'Stage' );
// Retrieve the broken links side report.
$reports = SS_Report :: get_reports ();
$brokenLinksReport = null ;
foreach ( $reports as $report ) {
2015-11-10 03:23:58 +01:00
if ( $report instanceof BrokenLinksReport ) {
2015-06-10 03:37:27 +02:00
$brokenLinksReport = $report ;
break ;
}
}
// Determine that the report exists, otherwise it has been excluded.
2015-11-10 03:23:58 +01:00
if ( ! $brokenLinksReport ){
$this -> markTestSkipped ( 'BrokenLinksReport is not an available report' );
return ;
}
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenLinksReport , true , false );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenLinksReport , true , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> Content = str_replace ( '987654321' , $page -> ID , $page -> Content );
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenLinksReport , false , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenLinksReport , false , false );
2015-06-10 03:37:27 +02:00
}
/**
* Test the broken files side report .
*/
public function testBrokenFiles () {
// Create a "draft" page with a broken file.
$page = Page :: create ();
$page -> Content = " <a href='[file_link,id=987654321]'>This</a> is a broken file. " ;
$page -> writeToStage ( 'Stage' );
// Retrieve the broken files side report.
$reports = SS_Report :: get_reports ();
$brokenFilesReport = null ;
foreach ( $reports as $report ) {
2015-11-10 03:23:58 +01:00
if ( $report instanceof BrokenFilesReport ) {
2015-06-10 03:37:27 +02:00
$brokenFilesReport = $report ;
break ;
}
}
// Determine that the report exists, otherwise it has been excluded.
2015-11-10 03:23:58 +01:00
if ( ! $brokenFilesReport ){
$this -> markTestSkipped ( 'BrokenFilesReport is not an available report' );
return ;
}
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenFilesReport , true , false );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenFilesReport , true , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Correct the "draft" broken file.
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$file = File :: create ();
$file -> Filename = 'name.pdf' ;
$file -> write ();
$page -> Content = str_replace ( '987654321' , $file -> ID , $page -> Content );
$page -> writeToStage ( 'Stage' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenFilesReport , false , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenFilesReport , false , false );
2015-06-10 03:37:27 +02:00
}
/**
* Test the broken virtual pages side report .
*/
public function testBrokenVirtualPages () {
// Create a "draft" virtual page with a broken link.
$page = VirtualPage :: create ();
$page -> CopyContentFromID = 987654321 ;
$page -> writeToStage ( 'Stage' );
// Retrieve the broken virtual pages side report.
$reports = SS_Report :: get_reports ();
$brokenVirtualPagesReport = null ;
foreach ( $reports as $report ) {
2015-11-10 03:23:58 +01:00
if ( $report instanceof BrokenVirtualPagesReport ) {
2015-06-10 03:37:27 +02:00
$brokenVirtualPagesReport = $report ;
break ;
}
}
// Determine that the report exists, otherwise it has been excluded.
2015-11-10 03:23:58 +01:00
if ( ! $brokenVirtualPagesReport ){
$this -> markTestSkipped ( 'BrokenFilesReport is not an available report' );
return ;
}
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , true , false );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , true , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , false , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenVirtualPagesReport , false , false );
2015-06-10 03:37:27 +02:00
}
/**
* Test the broken redirector pages side report .
*/
public function testBrokenRedirectorPages () {
// Create a "draft" redirector page with a broken link.
$page = RedirectorPage :: create ();
$page -> RedirectionType = 'Internal' ;
$page -> LinkToID = 987654321 ;
$page -> writeToStage ( 'Stage' );
// Retrieve the broken redirector pages side report.
$reports = SS_Report :: get_reports ();
$brokenRedirectorPagesReport = null ;
foreach ( $reports as $report ) {
2015-11-10 03:23:58 +01:00
if ( $report instanceof BrokenRedirectorPagesReport ) {
2015-06-10 03:37:27 +02:00
$brokenRedirectorPagesReport = $report ;
break ;
}
}
// Determine that the report exists, otherwise it has been excluded.
2015-11-10 03:23:58 +01:00
if ( ! $brokenRedirectorPagesReport ){
$this -> markTestSkipped ( 'BrokenRedirectorPagesReport is not an available report' );
return ;
}
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , true , false );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the page is now "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , true , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Correct the "draft" broken link.
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , false , true );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
// Make sure the change has now been "published".
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +01:00
$page -> writeToStage ( 'Live' );
2015-06-10 03:37:27 +02:00
2015-11-10 03:23:58 +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
2015-11-10 03:23:58 +01:00
$this -> isReportBroken ( $brokenRedirectorPagesReport , false , false );
2015-06-10 03:37:27 +02:00
}
2012-04-12 09:23:20 +02:00
}