2018-11-18 23:48:23 +01:00
|
|
|
<?php
|
2019-01-09 20:59:28 +01:00
|
|
|
|
2018-11-18 23:48:23 +01:00
|
|
|
namespace SilverStripe\Reports\Tests;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
|
|
|
use SilverStripe\Control\Controller;
|
2024-03-27 02:06:39 +01:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2018-11-18 23:48:23 +01:00
|
|
|
use SilverStripe\Reports\Report;
|
|
|
|
use SilverStripe\Reports\ReportAdmin;
|
2024-03-27 02:06:39 +01:00
|
|
|
use SilverStripe\Reports\Tests\ReportAdminTest\CannotViewReport;
|
2018-11-18 23:48:23 +01:00
|
|
|
use SilverStripe\Reports\Tests\ReportAdminTest\FakeReport;
|
|
|
|
use SilverStripe\Reports\Tests\ReportAdminTest\FakeReport2;
|
2024-09-10 06:42:23 +02:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2018-11-18 23:48:23 +01:00
|
|
|
|
2024-03-27 02:06:39 +01:00
|
|
|
class ReportAdminTest extends FunctionalTest
|
2018-11-18 23:48:23 +01:00
|
|
|
{
|
|
|
|
public function testBreadcrumbsAreGenerated()
|
|
|
|
{
|
|
|
|
$noExtraCrumbs = FakeReport::create();
|
|
|
|
|
|
|
|
$controller = $this->mockController($noExtraCrumbs);
|
|
|
|
$breadcrumbs = $controller->BreadCrumbs();
|
|
|
|
|
|
|
|
$this->assertCount(2, $breadcrumbs);
|
2021-11-05 01:23:15 +01:00
|
|
|
$map = $breadcrumbs[0]->toMap();
|
|
|
|
$this->assertSame('Reports', $map['Title']);
|
2023-01-13 01:08:13 +01:00
|
|
|
$this->assertSame('admin/reports', $map['Link']);
|
2018-11-18 23:48:23 +01:00
|
|
|
|
2021-11-05 01:23:15 +01:00
|
|
|
$map = $breadcrumbs[1]->toMap();
|
|
|
|
$this->assertSame('Fake report', $map['Title']);
|
2018-11-18 23:48:23 +01:00
|
|
|
|
|
|
|
$extraCrumbs = FakeReport2::create();
|
|
|
|
$controller = $this->mockController($extraCrumbs);
|
|
|
|
$breadcrumbs = $controller->Breadcrumbs();
|
|
|
|
|
|
|
|
$this->assertCount(3, $breadcrumbs);
|
|
|
|
|
2021-11-05 01:23:15 +01:00
|
|
|
$map = $breadcrumbs[0]->toMap();
|
|
|
|
$this->assertSame('Reports', $map['Title']);
|
2023-01-13 01:08:13 +01:00
|
|
|
$this->assertSame('admin/reports', $map['Link']);
|
2018-11-18 23:48:23 +01:00
|
|
|
|
2021-11-05 01:23:15 +01:00
|
|
|
$map = $breadcrumbs[1]->toMap();
|
|
|
|
$this->assertSame('Fake report title', $map['Title']);
|
|
|
|
$this->assertSame('admin/reports/show/SilverStripe-Reports-Tests-ReportAdminTest-FakeReport', $map['Link']);
|
2018-11-18 23:48:23 +01:00
|
|
|
|
2021-11-05 01:23:15 +01:00
|
|
|
$map = $breadcrumbs[2]->toMap();
|
|
|
|
$this->assertSame('Fake report two', $map['Title']);
|
2018-11-18 23:48:23 +01:00
|
|
|
}
|
|
|
|
|
2024-09-10 06:42:23 +02:00
|
|
|
public static function provideShowReport(): array
|
2024-03-27 02:06:39 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'cannot view' => [
|
|
|
|
'reportClass' => CannotViewReport::class,
|
|
|
|
'expected' => 403,
|
|
|
|
],
|
|
|
|
'can view' => [
|
|
|
|
'reportClass' => FakeReport::class,
|
|
|
|
'expected' => 200,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-09-10 06:42:23 +02:00
|
|
|
#[DataProvider('provideShowReport')]
|
2024-03-27 02:06:39 +01:00
|
|
|
public function testShowReport(string $reportClass, int $expected): void
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$report = new $reportClass();
|
|
|
|
$controller = $this->mockController($report);
|
|
|
|
$breadcrumbs = $controller->BreadCrumbs();
|
|
|
|
$response = $this->get($breadcrumbs[1]->Link);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2018-11-18 23:48:23 +01:00
|
|
|
/**
|
|
|
|
* @param Report $report
|
|
|
|
* @return ReportAdmin
|
|
|
|
* @throws \ReflectionException
|
|
|
|
*/
|
|
|
|
protected function mockController(Report $report)
|
|
|
|
{
|
|
|
|
$reflector = new ReflectionClass($controller = ReportAdmin::create());
|
|
|
|
|
|
|
|
$reportClass = $reflector->getProperty('reportClass');
|
|
|
|
$reportClass->setAccessible(true);
|
|
|
|
$reportClass->setValue($controller, get_class($report));
|
|
|
|
|
|
|
|
$reportObject = $reflector->getProperty('reportObject');
|
|
|
|
$reportObject->setAccessible(true);
|
|
|
|
$reportObject->setValue($controller, $report);
|
|
|
|
|
|
|
|
$controller->setRequest(Controller::curr()->getRequest());
|
|
|
|
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
}
|