FEATURE: added ability to define a set of shortcodes or permalinks which translate to full paths

This commit is contained in:
Will Rossiter 2010-10-22 01:07:49 +00:00
parent d2eb92233e
commit 354aa95e2b
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,47 @@
<?php
/**
* A mapping store of given permalinks to the full documentation path or useful for customizing the
* shortcut URLs used in the viewer.
*
* Redirects the user from example.com/foo to example.com/en/module/foo
*
* @module sapphiredocs
*/
class DocumentationPermalinks {
/**
* @var array
*/
private static $mapping = array();
/**
* Add a mapping of nice short permalinks to a full long path
*
* DocumentationPermalinks::add(array(
* 'debugging' => 'sapphire/topics/debugging.md'
* ));
*
*
* You do not need to include the language or the version current as it will add it
* based off the language or version in the session
*/
public static function add($map = array()) {
if(ArrayLib::is_associative($map)) {
self::$mapping = array_merge(self::$mapping, $map);
}
else {
user_error("DocumentationPermalinks::add() requires an associative array", E_USER_ERROR);
}
}
/**
* Return the location for a given short value.
*
* @return String|false
*/
public static function map($url) {
return (isset(self::$mapping[$url])) ? self::$mapping[$url] : false;
}
}

View File

@ -0,0 +1,34 @@
<?php
class DocumentationPermalinksTest extends FunctionalTest {
function testSavingAndAccessingMapping() {
// basic test
DocumentationPermalinks::add(array(
'foo' => 'current/en/sapphire/subfolder/foo',
'bar' => 'current/en/cms/bar'
));
$this->assertEquals('current/en/sapphire/subfolder/foo', DocumentationPermalinks::map('foo'));
$this->assertEquals('current/en/cms/bar', DocumentationPermalinks::map('bar'));
}
/**
* Tests to make sure short codes get translated to full paths
*/
function testRedirectingMapping() {
// testing the viewer class but clearer here
DocumentationPermalinks::add(array(
'foo' => 'current/en/sapphire/subfolder/foo',
'bar' => 'current/en/cms/bar'
));
$this->autoFollowRedirection = false;
$v = new DocumentationViewer();
$response = $v->handleRequest(new SS_HTTPRequest('GET', 'foo'));
$this->assertEquals('301', $response->getStatusCode());
$this->assertContains('current/en/sapphire/subfolder/foo', $response->getHeader('Location'));
}
}