diff --git a/src/Controllers/HistoryControllerFactory.php b/src/Controllers/HistoryControllerFactory.php index 5ebaada8..92ae7d5d 100644 --- a/src/Controllers/HistoryControllerFactory.php +++ b/src/Controllers/HistoryControllerFactory.php @@ -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); diff --git a/tests/Controllers/HistoryControllerFactoryTest.php b/tests/Controllers/HistoryControllerFactoryTest.php new file mode 100644 index 00000000..da241086 --- /dev/null +++ b/tests/Controllers/HistoryControllerFactoryTest.php @@ -0,0 +1,50 @@ + '*' + ]; + + 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); + } +} diff --git a/tests/Controllers/HistoryControllerFactoryTest.yml b/tests/Controllers/HistoryControllerFactoryTest.yml new file mode 100644 index 00000000..8861d224 --- /dev/null +++ b/tests/Controllers/HistoryControllerFactoryTest.yml @@ -0,0 +1,9 @@ +SilverStripe\CMS\Model\SiteTree: + page_one: + Title: 1 + URLSegment: page-one + Content:
Hello world!
+ page_two: + Title: 2 + URLSegment: page-two + Content:Hello world!
diff --git a/tests/Controllers/HistoryControllerFactoryTest/HistoryControllerFactoryExtension.php b/tests/Controllers/HistoryControllerFactoryTest/HistoryControllerFactoryExtension.php new file mode 100644 index 00000000..fb39c075 --- /dev/null +++ b/tests/Controllers/HistoryControllerFactoryTest/HistoryControllerFactoryExtension.php @@ -0,0 +1,14 @@ +Title === '2'; + } +}