NEW Adding tests for new report breadcrumbs feature

This commit is contained in:
Guy Marriott 2018-11-19 11:48:23 +13:00
parent edecbabe61
commit d6b1c071b6
No known key found for this signature in database
GPG Key ID: A80F9ACCB86D3DA7
3 changed files with 112 additions and 0 deletions

75
tests/ReportAdminTest.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace SilverStripe\Reports\Tests;
use ReflectionClass;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Reports\Report;
use SilverStripe\Reports\ReportAdmin;
use SilverStripe\Reports\Tests\ReportAdminTest\FakeReport;
use SilverStripe\Reports\Tests\ReportAdminTest\FakeReport2;
class ReportAdminTest extends SapphireTest
{
public function testBreadcrumbsAreGenerated()
{
$noExtraCrumbs = FakeReport::create();
$controller = $this->mockController($noExtraCrumbs);
$breadcrumbs = $controller->BreadCrumbs();
$this->assertCount(2, $breadcrumbs);
$this->assertArraySubset([
'Title' => 'Reports',
'Link' => 'admin/reports/',
], $breadcrumbs[0]->toMap(), true, 'Link to top level reports is within breadcrumbs');
$this->assertArraySubset([
'Title' => 'Fake report'
], $breadcrumbs[1]->toMap(), true, 'Current report is within breadcrumbs');
$extraCrumbs = FakeReport2::create();
$controller = $this->mockController($extraCrumbs);
$breadcrumbs = $controller->Breadcrumbs();
$this->assertCount(3, $breadcrumbs);
$this->assertArraySubset([
'Title' => 'Reports',
'Link' => 'admin/reports/',
], $breadcrumbs[0]->toMap(), true, 'Link to top level reports is within breadcrumbs (again)');
$this->assertArraySubset([
'Title' => 'Fake report title',
'Link' => 'admin/reports/show/SilverStripe-Reports-Tests-ReportAdminTest-FakeReport',
], $breadcrumbs[1]->toMap(), true, 'Custom breadcrumb appears');
$this->assertArraySubset([
'Title' => 'Fake report two'
], $breadcrumbs[2]->toMap(), true, 'Current report is still within breadcrumbs');
}
/**
* @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;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace SilverStripe\Reports\Tests\ReportAdminTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Reports\Report;
class FakeReport extends Report implements TestOnly
{
public function title()
{
return 'Fake report';
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace SilverStripe\Reports\Tests\ReportAdminTest;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Reports\Report;
use SilverStripe\Reports\ReportAdmin;
use SilverStripe\View\ArrayData;
class FakeReport2 extends Report implements TestOnly
{
public function title()
{
return 'Fake report two';
}
public function getBreadcrumbs()
{
return [ArrayData::create([
'Title' => 'Fake report title',
'Link' => FakeReport::singleton()->getLink()
])];
}
}