2012-04-03 06:13:06 +02:00
< ? php
class ReportTest extends SapphireTest {
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 , both with and without the " reason " .
*
* @ parameter ss_report
* @ parameter boolean
* @ parameter boolean
* @ parameter string
*/
public function isReportBroken ( $report , $isDraftBroken , $isPublishedBroken , $reason ) {
$class = get_class ( $report );
$parameters = array ();
// ASSERT that the "draft" report is returning the correct results, both with and without the "reason".
$parameters [ 'CheckSite' ] = 'Draft' ;
$results = ( count ( $report -> sourceRecords ( $parameters , null , null )) > 0 ) && ( count ( $report -> sourceRecords ( array_merge ( $parameters , array ( 'Reason' => $reason )), 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. " );
// ASSERT that the "published" report is returning the correct results, both with and without the "reason".
$parameters [ 'CheckSite' ] = 'Published' ;
$results = ( count ( $report -> sourceRecords ( $parameters , null , null )) > 0 ) && ( count ( $report -> sourceRecords ( array_merge ( $parameters , array ( 'Reason' => $reason )), 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. " );
}
2012-09-19 12:07:46 +02:00
public function testGetReports () {
2012-04-03 06:13:06 +02:00
$reports = SS_Report :: get_reports ();
$this -> assertNotNull ( $reports , " Reports returned " );
2012-04-03 08:06:35 +02:00
$previousSort = 0 ;
foreach ( $reports as $report ) {
$this -> assertGreaterThanOrEqual ( $previousSort , $report -> sort , " Reports are in correct sort order " );
$previousSort = $report -> sort ;
}
}
2012-09-19 12:07:46 +02:00
public function testExcludeReport () {
2012-04-03 08:06:35 +02:00
$reports = SS_Report :: get_reports ();
$reportNames = array ();
foreach ( $reports as $report ) {
$reportNames [] = $report -> class ;
}
$this -> assertContains ( 'ReportTest_FakeTest' , $reportNames , 'ReportTest_FakeTest is in reports list' );
2012-04-04 00:58:48 +02:00
//exclude one report
SS_Report :: add_excluded_reports ( 'ReportTest_FakeTest' );
2012-04-03 08:06:35 +02:00
$reports = SS_Report :: get_reports ();
$reportNames = array ();
foreach ( $reports as $report ) {
$reportNames [] = $report -> class ;
}
$this -> assertNotContains ( 'ReportTest_FakeTest' , $reportNames , 'ReportTest_FakeTest is NOT in reports list' );
2012-04-04 00:58:48 +02:00
//exclude two reports
SS_Report :: add_excluded_reports ( array ( 'ReportTest_FakeTest' , 'ReportTest_FakeTest2' ));
$reports = SS_Report :: get_reports ();
$reportNames = array ();
foreach ( $reports as $report ) {
$reportNames [] = $report -> class ;
}
$this -> assertNotContains ( 'ReportTest_FakeTest' , $reportNames , 'ReportTest_FakeTest is NOT in reports list' );
$this -> assertNotContains ( 'ReportTest_FakeTest2' , $reportNames , 'ReportTest_FakeTest2 is NOT in reports list' );
2012-04-03 06:13:06 +02:00
}
2012-04-20 06:32:39 +02:00
2012-09-19 12:07:46 +02:00
public function testAbstractClassesAreExcluded () {
2012-04-20 06:32:39 +02:00
$reports = SS_Report :: get_reports ();
$reportNames = array ();
foreach ( $reports as $report ) {
$reportNames [] = $report -> class ;
}
$this -> assertNotContains ( 'ReportTest_FakeTest_Abstract' ,
$reportNames ,
'ReportTest_FakeTest_Abstract is NOT in reports list as it is abstract' );
}
2015-06-10 03:37:27 +02:00
/**
* Test the broken links report .
*/
public function testBrokenLinksReport () {
// ---
// BROKEN LINKS
// ---
// 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 report.
$reports = SS_Report :: get_reports ();
$brokenLinksReport = null ;
foreach ( $reports as $report ) {
if ( $report instanceof BrokenLinksReport ) {
$brokenLinksReport = $report ;
break ;
}
}
// Determine that the report exists, otherwise it has been excluded.
if ( $brokenLinksReport ) {
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , false , 'BROKENLINK' );
// Make sure the page is now "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , true , 'BROKENLINK' );
// Correct the "draft" broken link.
$page -> Content = str_replace ( '987654321' , $page -> ID , $page -> Content );
$page -> writeToStage ( 'Stage' );
// 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".
$this -> isReportBroken ( $brokenLinksReport , false , true , 'BROKENLINK' );
// Make sure the change has now been "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , false , false , 'BROKENLINK' );
$page -> delete ();
// ---
// BROKEN FILES
// ---
// 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' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , false , 'BROKENFILE' );
// Make sure the page is now "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , true , 'BROKENFILE' );
// Correct the "draft" broken file.
$file = File :: create ();
$file -> Filename = 'name.pdf' ;
$file -> write ();
$page -> Content = str_replace ( '987654321' , $file -> ID , $page -> Content );
$page -> writeToStage ( 'Stage' );
// 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".
$this -> isReportBroken ( $brokenLinksReport , false , true , 'BROKENFILE' );
// Make sure the change has now been "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , false , false , 'BROKENFILE' );
$page -> delete ();
// ---
// BROKEN VIRTUAL PAGES
// ---
// Create a "draft" virtual page with a broken link.
$page = VirtualPage :: create ();
$page -> CopyContentFromID = 987654321 ;
$page -> writeToStage ( 'Stage' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , false , 'VPBROKENLINK' );
// Make sure the page is now "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , true , 'VPBROKENLINK' );
// Correct the "draft" broken link.
$contentPage = Page :: create ();
$contentPage -> Content = 'This is some content.' ;
$contentPage -> writeToStage ( 'Stage' );
$contentPage -> writeToStage ( 'Live' );
$page -> CopyContentFromID = $contentPage -> ID ;
$page -> writeToStage ( 'Stage' );
// 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".
$this -> isReportBroken ( $brokenLinksReport , false , true , 'VPBROKENLINK' );
// Make sure the change has now been "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , false , false , 'VPBROKENLINK' );
$contentPage -> delete ();
$page -> delete ();
// ---
// BROKEN REDIRECTOR PAGES
// ---
// Create a "draft" redirector page with a broken link.
$page = RedirectorPage :: create ();
$page -> RedirectionType = 'Internal' ;
$page -> LinkToID = 987654321 ;
$page -> writeToStage ( 'Stage' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , false , 'RPBROKENLINK' );
// Make sure the page is now "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , true , true , 'RPBROKENLINK' );
// Correct the "draft" broken link.
$contentPage = Page :: create ();
$contentPage -> Content = 'This is some content.' ;
$contentPage -> writeToStage ( 'Stage' );
$contentPage -> writeToStage ( 'Live' );
$page -> LinkToID = $contentPage -> ID ;
$page -> writeToStage ( 'Stage' );
// 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".
$this -> isReportBroken ( $brokenLinksReport , false , true , 'RPBROKENLINK' );
// Make sure the change has now been "published".
$page -> writeToStage ( 'Live' );
// 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.
$this -> isReportBroken ( $brokenLinksReport , false , false , 'RPBROKENLINK' );
}
}
2012-04-03 06:13:06 +02:00
}
class ReportTest_FakeTest extends SS_Report implements TestOnly {
2012-09-19 12:07:46 +02:00
public function title () {
2012-04-03 06:13:06 +02:00
return 'Report title' ;
}
2012-09-19 12:07:46 +02:00
public function columns () {
2012-04-03 06:13:06 +02:00
return array (
" Title " => array (
" title " => " Page Title "
)
);
}
2012-09-19 12:07:46 +02:00
public function sourceRecords ( $params , $sort , $limit ) {
2012-04-03 06:13:06 +02:00
return new ArrayList ();
}
2012-09-19 12:07:46 +02:00
public function sort () {
2012-04-03 06:13:06 +02:00
return 100 ;
}
}
class ReportTest_FakeTest2 extends SS_Report implements TestOnly {
2012-09-19 12:07:46 +02:00
public function title () {
2012-04-03 08:06:35 +02:00
return 'Report title 2' ;
2012-04-03 06:13:06 +02:00
}
2012-09-19 12:07:46 +02:00
public function columns () {
2012-04-03 06:13:06 +02:00
return array (
" Title " => array (
2012-04-03 08:06:35 +02:00
" title " => " Page Title 2 "
2012-04-03 06:13:06 +02:00
)
);
}
2012-09-19 12:07:46 +02:00
public function sourceRecords ( $params , $sort , $limit ) {
2012-04-03 06:13:06 +02:00
return new ArrayList ();
}
2012-09-19 12:07:46 +02:00
public function sort () {
2012-04-03 06:13:06 +02:00
return 98 ;
}
2012-04-12 09:23:20 +02:00
}
2012-04-20 06:32:39 +02:00
abstract class ReportTest_FakeTest_Abstract extends SS_Report implements TestOnly {
2012-09-19 12:07:46 +02:00
public function title () {
2012-04-20 06:32:39 +02:00
return 'Report title Abstract' ;
}
2012-09-19 12:07:46 +02:00
public function columns () {
2012-04-20 06:32:39 +02:00
return array (
" Title " => array (
" title " => " Page Title Abstract "
)
);
}
2012-09-19 12:07:46 +02:00
public function sourceRecords ( $params , $sort , $limit ) {
2012-04-20 06:32:39 +02:00
return new ArrayList ();
}
2012-09-19 12:07:46 +02:00
public function sort () {
2012-04-20 06:32:39 +02:00
return 5 ;
}
}