diff --git a/code/DocumentationPermalinks.php b/code/DocumentationPermalinks.php new file mode 100644 index 0000000..f99d540 --- /dev/null +++ b/code/DocumentationPermalinks.php @@ -0,0 +1,47 @@ + '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; + } +} \ No newline at end of file diff --git a/tests/DocumentationPermalinksTest.php b/tests/DocumentationPermalinksTest.php new file mode 100644 index 0000000..2d7a84c --- /dev/null +++ b/tests/DocumentationPermalinksTest.php @@ -0,0 +1,34 @@ + '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')); + } +}