silverstripe-reports/tests/ReportTest.php

176 lines
3.8 KiB
PHP
Raw Normal View History

2013-01-17 01:22:13 +01:00
<?php
2016-06-16 07:06:51 +02:00
use SilverStripe\ORM\ArrayList;
2013-01-17 01:22:13 +01:00
/**
* @package reports
* @subpackage tests
*/
2015-12-15 23:06:45 +01:00
class ReportTest extends SapphireTest
{
protected $usesDatabase = true;
2015-12-15 23:06:45 +01:00
public function testGetReports()
{
2013-01-17 01:22:13 +01:00
$reports = SS_Report::get_reports();
$this->assertNotNull($reports, "Reports returned");
$previousSort = 0;
foreach($reports as $report) {
$this->assertGreaterThanOrEqual($previousSort, $report->sort, "Reports are in correct sort order");
$previousSort = $report->sort;
}
}
2015-12-15 23:06:45 +01:00
public function testExcludeReport()
{
2013-01-17 01:22:13 +01: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');
//exclude one report
SS_Report::add_excluded_reports('ReportTest_FakeTest');
$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');
//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');
}
2015-12-15 23:06:45 +01:00
public function testAbstractClassesAreExcluded()
{
2013-01-17 01:22:13 +01: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');
}
public function testPermissions() {
$report = new ReportTest_FakeTest2();
// Visitor cannot view
Session::clear("loggedInAs");
$this->assertFalse($report->canView());
// Logged in user that cannot view reports
$this->logInWithPermission('SITETREE_REORGANISE');
$this->assertFalse($report->canView());
// Logged in with report permissions
$this->logInWithPermission('CMS_ACCESS_ReportAdmin');
$this->assertTrue($report->canView());
// Admin can view
$this->logInWithPermission('ADMIN');
$this->assertTrue($report->canView());
}
2013-01-17 01:22:13 +01:00
}
/**
* @package reports
* @subpackage tests
*/
2015-12-15 23:06:45 +01:00
class ReportTest_FakeTest extends SS_Report implements TestOnly
{
public function title()
{
2013-01-17 01:22:13 +01:00
return 'Report title';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2013-01-17 01:22:13 +01:00
return array(
"Title" => array(
"title" => "Page Title"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2013-01-17 01:22:13 +01:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2013-01-17 01:22:13 +01:00
return 100;
}
}
/**
* @package reports
* @subpackage tests
*/
2015-12-15 23:06:45 +01:00
class ReportTest_FakeTest2 extends SS_Report implements TestOnly
{
public function title()
{
2013-01-17 01:22:13 +01:00
return 'Report title 2';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2013-01-17 01:22:13 +01:00
return array(
"Title" => array(
"title" => "Page Title 2"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2013-01-17 01:22:13 +01:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2013-01-17 01:22:13 +01:00
return 98;
}
}
/**
* @package reports
* @subpackage tests
*/
2015-12-15 23:06:45 +01:00
abstract class ReportTest_FakeTest_Abstract extends SS_Report implements TestOnly
{
2016-06-16 07:06:51 +02:00
2015-12-15 23:06:45 +01:00
public function title()
{
2013-01-17 01:22:13 +01:00
return 'Report title Abstract';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2013-01-17 01:22:13 +01:00
return array(
"Title" => array(
"title" => "Page Title Abstract"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2013-01-17 01:22:13 +01:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2013-01-17 01:22:13 +01:00
return 5;
}
}