ENHANCEMENT Rewriting current version URL path to 'current/'

This commit is contained in:
Ingo Schommer 2010-08-01 07:03:52 +00:00
parent f623dcc887
commit 845e2e61b2
3 changed files with 52 additions and 3 deletions

View File

@ -99,7 +99,7 @@ class DocumentationViewer extends Controller {
// /2.4/en/sapphire/page and // /2.4/en/sapphire/page and
// /en/sapphire/page which is a link to the latest one // /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); array_unshift($this->Remaining, $this->ModuleName);
// not numeric so /en/sapphire/folder/page // not numeric so /en/sapphire/folder/page
@ -124,6 +124,25 @@ class DocumentationViewer extends Controller {
else { else {
$this->Lang = 'en'; $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); return parent::handleRequest($request);
} }

View File

@ -10,6 +10,8 @@
class DocumentationViewerTests extends FunctionalTest { class DocumentationViewerTests extends FunctionalTest {
static $fixture_file = 'sapphiredocs/tests/DocumentTests.yml'; static $fixture_file = 'sapphiredocs/tests/DocumentTests.yml';
protected $autoFollowRedirection = false;
function setUpOnce() { function setUpOnce() {
parent::setUpOnce(); parent::setUpOnce();
@ -17,10 +19,16 @@ class DocumentationViewerTests extends FunctionalTest {
$this->origEnabled = DocumentationService::automatic_registration_enabled(); $this->origEnabled = DocumentationService::automatic_registration_enabled();
DocumentationService::set_automatic_registration(false); DocumentationService::set_automatic_registration(false);
$this->origModules = DocumentationService::get_registered_modules(); $this->origModules = DocumentationService::get_registered_modules();
$this->origLinkBase = DocumentationViewer::get_link_base();
DocumentationViewer::set_link_base('dev/docs/');
foreach($this->origModules as $module) { foreach($this->origModules as $module) {
DocumentationService::unregister($module->getModuleFolder()); 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.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() { function tearDownOnce() {
@ -28,17 +36,39 @@ class DocumentationViewerTests extends FunctionalTest {
DocumentationService::unregister("DocumentationViewerTests"); DocumentationService::unregister("DocumentationViewerTests");
DocumentationService::set_automatic_registration($this->origEnabled); DocumentationService::set_automatic_registration($this->origEnabled);
DocumentationViewer::set_link_base($this->origLinkBase);
// $this->origModules = Documentation::get_registered_modules(); // $this->origModules = Documentation::get_registered_modules();
// foreach($this->origModules as $name => $module) { // foreach($this->origModules as $name => $module) {
// DocumentationService::register($name); // 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() { function testUrlParsing() {
// Module index // Module index
$v = new DocumentationViewer(); $v = new DocumentationViewer();
$response = $v->handleRequest(new SS_HTTPRequest('GET', '2.4/en/DocumentationViewerTests/test')); $response = $v->handleRequest(new SS_HTTPRequest('GET', '2.3/en/DocumentationViewerTests/test'));
$this->assertEquals('2.4', $v->Version); $this->assertEquals('2.3', $v->Version);
$this->assertEquals('en', $v->Lang); $this->assertEquals('en', $v->Lang);
$this->assertEquals('DocumentationViewerTests', $v->ModuleName); $this->assertEquals('DocumentationViewerTests', $v->ModuleName);
$this->assertEquals(array('test'), $v->Remaining); $this->assertEquals(array('test'), $v->Remaining);

0
tests/docs-2/en/test.md Normal file
View File