Source of file CmsReportsTest.php
Size: 13,218 Bytes - Last Modified: 2021-12-23T10:28:18+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Reports/CmsReportsTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 | <?php namespace SilverStripe\CMS\Tests\Reports; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBDatetime; 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; use SilverStripe\Reports\Report; use SilverStripe\Assets\File; use SilverStripe\Dev\SapphireTest; use Page; class CmsReportsTest extends SapphireTest { protected static $fixture_file = 'CmsReportsTest.yml'; private static $daysAgo = 14; protected function setUp(): void { parent::setUp(); // 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')); $after = $this->objFromFixture(SiteTree::class, 'after'); $before = $this->objFromFixture(SiteTree::class, '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."'"); } /** * ASSERT whether a report is returning the correct results, based on a broken "draft" and/or "published" page. * * @param Report $report * @param bool $isDraftBroken * @param bool $isPublishedBroken */ public function isReportBroken($report, $isDraftBroken, $isPublishedBroken) { $class = get_class($report); // ASSERT that the "draft" report is returning the correct results. $parameters = ['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."); // ASSERT that the "published" report is returning the correct results. $parameters = ['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."); } public function testRecentlyEdited() { DBDatetime::set_mock_now('2009-06-30 00:00:00'); $after = $this->objFromFixture(SiteTree::class, 'after'); $before = $this->objFromFixture(SiteTree::class, 'before'); $r = new RecentlyEditedReport(); // check if contains only elements not older than $daysAgo days $this->assertNotNull($r->records([])); $this->assertContains($after->ID, $r->records([])->column('ID')); $this->assertNotContains($before->ID, $r->records([])->column('ID')); DBDatetime::clear_mock_now(); } /** * 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 = 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) { $this->markTestSkipped('BrokenLinksReport is not an available report'); return; } // 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); // 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); // 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); // 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); } /** * 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 = Report::get_reports(); $brokenFilesReport = null; foreach ($reports as $report) { if ($report instanceof BrokenFilesReport) { $brokenFilesReport = $report; break; } } // Determine that the report exists, otherwise it has been excluded. if (!$brokenFilesReport) { $this->markTestSkipped('BrokenFilesReport is not an available report'); return; } // 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($brokenFilesReport, true, false); // 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($brokenFilesReport, true, true); // 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($brokenFilesReport, false, true); // 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($brokenFilesReport, false, false); } /** * 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 = Report::get_reports(); $brokenVirtualPagesReport = null; foreach ($reports as $report) { if ($report instanceof BrokenVirtualPagesReport) { $brokenVirtualPagesReport = $report; break; } } // Determine that the report exists, otherwise it has been excluded. if (!$brokenVirtualPagesReport) { $this->markTestSkipped('BrokenFilesReport is not an available report'); return; } // 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($brokenVirtualPagesReport, true, false); // 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($brokenVirtualPagesReport, true, true); // 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($brokenVirtualPagesReport, false, true); // 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($brokenVirtualPagesReport, false, false); } /** * 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 = Report::get_reports(); $brokenRedirectorPagesReport = null; foreach ($reports as $report) { if ($report instanceof BrokenRedirectorPagesReport) { $brokenRedirectorPagesReport = $report; break; } } // Determine that the report exists, otherwise it has been excluded. if (!$brokenRedirectorPagesReport) { $this->markTestSkipped('BrokenRedirectorPagesReport is not an available report'); return; } // 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($brokenRedirectorPagesReport, true, false); // 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($brokenRedirectorPagesReport, true, true); // 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($brokenRedirectorPagesReport, false, true); // 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($brokenRedirectorPagesReport, false, false); } } |