silverstripe-reports/tests/ReportTest.php

134 lines
4.5 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\Core\Config\Config;
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;
2024-09-10 06:42:23 +02:00
use SilverStripe\Reports\Tests\ReportTest\FakeReport;
use SilverStripe\Reports\Tests\ReportTest\FakeReport2;
use SilverStripe\Reports\Report;
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();
$reportNames = [];
2016-07-29 00:44:00 +02:00
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
2024-09-10 06:42:23 +02:00
$this->assertContains(FakeReport::class, $reportNames, 'ReportTest_FakeReport is in reports list');
2016-07-29 00:44:00 +02:00
// Exclude one report
2024-09-10 06:42:23 +02:00
Config::modify()->merge(Report::class, 'excluded_reports', [FakeReport::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
}
2024-09-10 06:42:23 +02:00
$this->assertNotContains(FakeReport::class, $reportNames, 'ReportTest_FakeReport is NOT in reports list');
2016-07-29 00:44:00 +02:00
// Exclude two reports
Config::modify()->merge(Report::class, 'excluded_reports', [
2024-09-10 06:42:23 +02:00
FakeReport::class,
FakeReport2::class
]);
2016-07-29 00:44:00 +02:00
2016-09-09 08:11:38 +02:00
$reports = Report::get_reports();
$reportNames = [];
2016-07-29 00:44:00 +02:00
foreach ($reports as $report) {
$reportNames[] = get_class($report);
2016-07-29 00:44:00 +02:00
}
2024-09-10 06:42:23 +02:00
$this->assertNotContains(FakeReport::class, $reportNames, 'ReportTest_FakeReport is NOT in reports list');
$this->assertNotContains(FakeReport2::class, $reportNames, 'ReportTest_FakeReport2 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(
2024-09-10 06:42:23 +02:00
'ReportTest_FakeReport_Abstract',
2016-07-29 00:44:00 +02:00
$reportNames,
2024-09-10 06:42:23 +02:00
'ReportTest_FakeReport_Abstract is NOT in reports list as it is abstract'
2017-06-21 06:30:14 +02:00
);
2016-07-29 00:44:00 +02:00
}
public function testPermissions()
{
2024-09-10 06:42:23 +02:00
$report = new ReportTest\FakeReport2();
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
{
2024-09-10 06:42:23 +02:00
$report = new ReportTest\FakeReport();
2017-06-21 06:30:14 +02:00
/** @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
}
public function testCountForOverview()
{
2024-09-10 06:42:23 +02:00
$report = new ReportTest\FakeReport3();
// Count is limited to 10000 by default
$this->assertEquals('10000+', $report->getCountForOverview());
// Count is limited as per configuration
2024-09-10 06:42:23 +02:00
Config::modify()->set(ReportTest\FakeReport3::class, 'limit_count_in_overview', 15);
$this->assertEquals('15+', $report->getCountForOverview());
// A null limit displays the full count
2024-09-10 06:42:23 +02:00
Config::modify()->set(ReportTest\FakeReport3::class, 'limit_count_in_overview', null);
$this->assertEquals('15000', $report->getCountForOverview());
}
2013-01-17 01:22:13 +01:00
}