Skip to content

Commit

Permalink
FIX Use get_one_by_stage when fetching SiteTree for a specific stage
Browse files Browse the repository at this point in the history
  • Loading branch information
ScopeyNZ committed Aug 22, 2018
1 parent 5e8bf7b commit 6be253c
Showing 4 changed files with 78 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Controllers/HistoryControllerFactory.php
Original file line number Diff line number Diff line change
@@ -29,10 +29,11 @@ public function create($service, array $params = array())

if ($id) {
// Ensure we read from the draft stage at this position
$originalStage = Versioned::get_stage();
Versioned::set_stage(Versioned::DRAFT);
$page = SiteTree::get()->byID($id);
Versioned::set_stage($originalStage);
$page = Versioned::get_one_by_stage(
SiteTree::class,
Versioned::DRAFT,
sprintf('"SiteTree"."ID" = \'%d\'', $id)
);

if ($page && $this->isEnabled($page)) {
return Injector::inst()->create(CMSPageHistoryViewerController::class);
50 changes: 50 additions & 0 deletions tests/Controllers/HistoryControllerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace SilverStripe\VersionedAdmin\Tests\Controllers;

use SilverStripe\VersionedAdmin\Tests\Controller\HistoryControllerFactory\HistoryControllerFactoryExtension;
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\VersionedAdmin\Controllers\CMSPageHistoryViewerController;
use SilverStripe\VersionedAdmin\Controllers\HistoryControllerFactory;

class HistoryControllerFactoryTest extends SapphireTest
{
protected static $fixture_file = 'HistoryControllerFactoryTest.yml';

protected static $illegal_extensions = [
HistoryControllerFactory::class => '*'
];

protected static $required_extensions = [
HistoryControllerFactory::class => [HistoryControllerFactoryExtension::class]
];

public function testCreateController()
{
$factory = new HistoryControllerFactory;

$controller = $factory->create(null);
$this->assertInstanceOf(CMSPageHistoryController::class, $controller);

$id = $this->idFromFixture(SiteTree::class, 'page_one');

$mockRequest = new HTTPRequest('GET', '');
$mockRequest->setRouteParams(['ID' => $id]);
Injector::inst()->registerService($mockRequest);

$controller = $factory->create(null);
$this->assertInstanceOf(CMSPageHistoryController::class, $controller);

$id = $this->idFromFixture(SiteTree::class, 'page_two');

$mockRequest = new HTTPRequest('GET', '');
$mockRequest->setRouteParams(['ID' => $id]);
Injector::inst()->registerService($mockRequest);

$controller = $factory->create(null);
$this->assertInstanceOf(CMSPageHistoryViewerController::class, $controller);
}
}
9 changes: 9 additions & 0 deletions tests/Controllers/HistoryControllerFactoryTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SilverStripe\CMS\Model\SiteTree:
page_one:
Title: 1
URLSegment: page-one
Content: <p>Hello world!</p>
page_two:
Title: 2
URLSegment: page-two
Content: <p>Hello world!</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace SilverStripe\VersionedAdmin\Tests\Controller\HistoryControllerFactory;

use SilverStripe\Core\Extension;
use SilverStripe\Dev\TestOnly;

class HistoryControllerFactoryExtension extends Extension implements TestOnly
{
public function updateIsEnabled($record)
{
// Only "enable" for the second fixture (from HistoryControllerFactoryTest.yml)
return $record->Title === '2';
}
}

0 comments on commit 6be253c

Please sign in to comment.