silverstripe-reports/tests/ReportTest.php

111 lines
3.7 KiB
PHP
Raw Normal View History

2013-01-17 01:22:13 +01:00
<?php
2017-06-21 06:30:14 +02:00
namespace SilverStripe\Reports\Tests;
use SilverStripe\Reports\Report;
use SilverStripe\Dev\SapphireTest;
2016-11-10 14:01:37 +01:00
use SilverStripe\Forms\GridField\GridFieldDataColumns;
2017-06-21 06:30:14 +02:00
use SilverStripe\Reports\Tests\ReportTest\FakeObject;
use SilverStripe\Reports\Tests\ReportTest\FakeTest;
use SilverStripe\Reports\Tests\ReportTest\FakeTest2;
2015-12-15 23:06:45 +01:00
class ReportTest extends SapphireTest
{
2017-06-21 06:30:14 +02:00
protected static $extra_dataobjects = [
FakeObject::class,
];
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
}
2017-06-21 06:30:14 +02:00
$this->assertContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is in reports list');
2016-07-29 00:44:00 +02:00
//exclude one report
2017-06-21 06:30:14 +02:00
Report::add_excluded_reports(FakeTest::class);
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
}
2017-06-21 06:30:14 +02:00
$this->assertNotContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is NOT in reports list');
2016-07-29 00:44:00 +02:00
//exclude two reports
2017-06-21 06:30:14 +02:00
Report::add_excluded_reports(array(FakeTest::class, FakeTest2::class));
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
}
2017-06-21 06:30:14 +02:00
$this->assertNotContains(FakeTest::class, $reportNames, 'ReportTest_FakeTest is NOT in reports list');
$this->assertNotContains(FakeTest2::class, $reportNames, 'ReportTest_FakeTest2 is NOT in reports list');
2016-07-29 00:44:00 +02:00
}
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
}
2017-06-21 06:30:14 +02:00
$this->assertNotContains(
'ReportTest_FakeTest_Abstract',
2016-07-29 00:44:00 +02:00
$reportNames,
2017-06-21 06:30:14 +02:00
'ReportTest_FakeTest_Abstract is NOT in reports list as it is abstract'
);
2016-07-29 00:44:00 +02:00
}
public function testPermissions()
{
2017-06-21 06:30:14 +02:00
$report = new ReportTest\FakeTest2();
2016-07-29 00:44:00 +02:00
// Visitor cannot view
2017-06-21 06:30:14 +02:00
$this->logOut();
2016-07-29 00:44:00 +02:00
$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());
2017-06-21 06:30:14 +02:00
// Admin can view
$this->logInWithPermission('ADMIN');
$this->assertTrue($report->canView());
2016-07-29 00:44:00 +02:00
}
2013-01-17 01:22:13 +01:00
2017-06-21 06:30:14 +02:00
public function testColumnLink()
2015-12-15 23:06:45 +01:00
{
2017-06-21 06:30:14 +02:00
$report = new ReportTest\FakeTest();
/** @var GridField $gridField */
$gridField = $report->getReportField();
/** @var GridFieldDataColumns $columns */
$columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
2015-12-15 23:06:45 +01:00
2017-06-21 06:30:14 +02:00
$page = new ReportTest\FakeObject();
$page->Title = 'My Object';
$page->ID = 959547;
2015-12-15 23:06:45 +01:00
2017-06-21 06:30:14 +02:00
$titleContent = $columns->getColumnContent($gridField, $page, 'Title');
$this->assertEquals('<a class="grid-field__link-block" href="dummy-edit-link/959547" title="My Object">My Object</a>', $titleContent);
2016-07-29 00:44:00 +02:00
}
2013-01-17 01:22:13 +01:00
}