silverstripe-reports/tests/ReportTest.php

182 lines
4.5 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;
2016-09-09 08:11:38 +02:00
use SilverStripe\Reports\Report;
use SilverStripe\Control\Session;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
2016-06-16 07:06:51 +02:00
2013-01-17 01:22:13 +01:00
/**
* @package reports
* @subpackage tests
*/
2015-12-15 23:06:45 +01:00
class ReportTest extends SapphireTest
{
2016-07-18 14:02:20 +02:00
protected $usesDatabase = true;
2015-12-15 23:06:45 +01:00
public function testGetReports()
{
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
2016-07-29 00:44:00 +02:00
$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()
{
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
2016-07-29 00:44:00 +02:00
$reportNames = array();
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
$this->assertContains('ReportTest_FakeTest', $reportNames, 'ReportTest_FakeTest is in reports list');
//exclude one report
2016-09-09 08:11:38 +02:00
Report::add_excluded_reports('ReportTest_FakeTest');
2016-07-29 00:44:00 +02:00
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
2016-07-29 00:44:00 +02:00
$reportNames = array();
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
$this->assertNotContains('ReportTest_FakeTest', $reportNames, 'ReportTest_FakeTest is NOT in reports list');
//exclude two reports
2016-09-09 08:11:38 +02:00
Report::add_excluded_reports(array('ReportTest_FakeTest', 'ReportTest_FakeTest2'));
2016-07-29 00:44:00 +02:00
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
2016-07-29 00:44:00 +02:00
$reportNames = array();
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
$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()
{
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
2016-07-29 00:44:00 +02:00
$reportNames = array();
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
$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
*/
2016-09-09 08:11:38 +02:00
class ReportTest_FakeTest extends Report implements TestOnly
2015-12-15 23:06:45 +01:00
{
public function title()
{
2016-07-29 00:44:00 +02:00
return 'Report title';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2016-07-29 00:44:00 +02:00
return array(
"Title" => array(
"title" => "Page Title"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2016-07-29 00:44:00 +02:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2016-07-29 00:44:00 +02:00
return 100;
}
2013-01-17 01:22:13 +01:00
}
/**
* @package reports
* @subpackage tests
*/
2016-09-09 08:11:38 +02:00
class ReportTest_FakeTest2 extends Report implements TestOnly
2015-12-15 23:06:45 +01:00
{
public function title()
{
2016-07-29 00:44:00 +02:00
return 'Report title 2';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2016-07-29 00:44:00 +02:00
return array(
"Title" => array(
"title" => "Page Title 2"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2016-07-29 00:44:00 +02:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2016-07-29 00:44:00 +02:00
return 98;
}
2013-01-17 01:22:13 +01:00
}
/**
* @package reports
* @subpackage tests
*/
2016-09-09 08:11:38 +02:00
abstract class ReportTest_FakeTest_Abstract extends Report implements TestOnly
2015-12-15 23:06:45 +01:00
{
2016-06-16 07:06:51 +02:00
2015-12-15 23:06:45 +01:00
public function title()
{
2016-07-29 00:44:00 +02:00
return 'Report title Abstract';
}
2015-12-15 23:06:45 +01:00
public function columns()
{
2016-07-29 00:44:00 +02:00
return array(
"Title" => array(
"title" => "Page Title Abstract"
)
);
}
2015-12-15 23:06:45 +01:00
public function sourceRecords($params, $sort, $limit)
{
2016-07-29 00:44:00 +02:00
return new ArrayList();
}
2015-12-15 23:06:45 +01:00
public function sort()
{
2016-07-29 00:44:00 +02:00
return 5;
}
2013-01-17 01:22:13 +01:00
}