From 845e2e61b283a8cd6cc8cab6159f3661975fcd2d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 1 Aug 2010 07:03:52 +0000 Subject: [PATCH] ENHANCEMENT Rewriting current version URL path to 'current/' --- code/DocumentationViewer.php | 21 +++++++++++++++++- tests/DocumentationViewerTests.php | 34 ++++++++++++++++++++++++++++-- tests/docs-2/en/test.md | 0 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 tests/docs-2/en/test.md diff --git a/code/DocumentationViewer.php b/code/DocumentationViewer.php index 4191f1b..0a820c4 100755 --- a/code/DocumentationViewer.php +++ b/code/DocumentationViewer.php @@ -99,7 +99,7 @@ class DocumentationViewer extends Controller { // /2.4/en/sapphire/page and // /en/sapphire/page which is a link to the latest one - if(!is_numeric($this->Version)) { + if(!is_numeric($this->Version) && $this->Version != 'current') { array_unshift($this->Remaining, $this->ModuleName); // not numeric so /en/sapphire/folder/page @@ -124,6 +124,25 @@ class DocumentationViewer extends Controller { else { $this->Lang = 'en'; } + + // 'current' version mapping + $module = DocumentationService::is_registered_module($this->ModuleName, null, $this->Lang); + if($this->Version && $module) { + $current = $module->getCurrentVersion(); + if($this->Version == 'current') { + $this->Version = $current; + } else { + if($current == $this->Version) { + $this->Version = 'current'; + $link = $this->Link($this->Remaining); + $this->response = new SS_HTTPResponse(); + $this->redirect($link, 301); // permanent redirect + return $this->response; + } + } + + } + return parent::handleRequest($request); } diff --git a/tests/DocumentationViewerTests.php b/tests/DocumentationViewerTests.php index facd9fc..786129c 100644 --- a/tests/DocumentationViewerTests.php +++ b/tests/DocumentationViewerTests.php @@ -10,6 +10,8 @@ class DocumentationViewerTests extends FunctionalTest { static $fixture_file = 'sapphiredocs/tests/DocumentTests.yml'; + + protected $autoFollowRedirection = false; function setUpOnce() { parent::setUpOnce(); @@ -17,10 +19,16 @@ class DocumentationViewerTests extends FunctionalTest { $this->origEnabled = DocumentationService::automatic_registration_enabled(); DocumentationService::set_automatic_registration(false); $this->origModules = DocumentationService::get_registered_modules(); + $this->origLinkBase = DocumentationViewer::get_link_base(); + DocumentationViewer::set_link_base('dev/docs/'); foreach($this->origModules as $module) { DocumentationService::unregister($module->getModuleFolder()); } + + // We set 3.0 as current, and test most assertions against 2.4 - to avoid 'current' rewriting issues DocumentationService::register("DocumentationViewerTests", BASE_PATH . "/sapphiredocs/tests/docs/", '2.4'); + DocumentationService::register("DocumentationViewerTests", BASE_PATH . "/sapphiredocs/tests/docs-2/", '2.3'); + DocumentationService::register("DocumentationViewerTests", BASE_PATH . "/sapphiredocs/tests/docs-3/", '3.0'); } function tearDownOnce() { @@ -28,17 +36,39 @@ class DocumentationViewerTests extends FunctionalTest { DocumentationService::unregister("DocumentationViewerTests"); DocumentationService::set_automatic_registration($this->origEnabled); + DocumentationViewer::set_link_base($this->origLinkBase); // $this->origModules = Documentation::get_registered_modules(); // foreach($this->origModules as $name => $module) { // DocumentationService::register($name); // } } + function testCurrentRedirection() { + $response = $this->get('dev/docs/3.0/en/DocumentationViewerTests/test'); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals( + Director::absoluteBaseURL() . 'dev/docs/current/en/DocumentationViewerTests/test/', + $response->getHeader('Location'), + 'Redirection to current on page' + ); + + $response = $this->get('dev/docs/3.0/en/DocumentationViewerTests/'); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals( + Director::absoluteBaseURL() . 'dev/docs/current/en/DocumentationViewerTests/', + $response->getHeader('Location'), + 'Redirection to current on index' + ); + + $response = $this->get('dev/docs/2.3/en/DocumentationViewerTests/'); + $this->assertEquals(200, $response->getStatusCode(), 'No redirection on older versions'); + } + function testUrlParsing() { // Module index $v = new DocumentationViewer(); - $response = $v->handleRequest(new SS_HTTPRequest('GET', '2.4/en/DocumentationViewerTests/test')); - $this->assertEquals('2.4', $v->Version); + $response = $v->handleRequest(new SS_HTTPRequest('GET', '2.3/en/DocumentationViewerTests/test')); + $this->assertEquals('2.3', $v->Version); $this->assertEquals('en', $v->Lang); $this->assertEquals('DocumentationViewerTests', $v->ModuleName); $this->assertEquals(array('test'), $v->Remaining); diff --git a/tests/docs-2/en/test.md b/tests/docs-2/en/test.md new file mode 100644 index 0000000..e69de29