BUGFIX Showing proper 404s

This commit is contained in:
Ingo Schommer 2011-01-17 06:00:16 +00:00
parent f9c8617af4
commit 24ebc64945
2 changed files with 50 additions and 8 deletions

View File

@ -182,6 +182,13 @@ class DocumentationViewer extends Controller {
}
}
// Check if page exists, otherwise return 404
if(!$this->locationExists()) {
$body = $this->renderWith(get_class($this));
$this->response = new SS_HTTPResponse($body, 404);
return $this->response;
}
return parent::handleRequest($request);
}
@ -364,6 +371,24 @@ class DocumentationViewer extends Controller {
return false;
}
/**
* Simple way to check for existence of page of folder
* without constructing too much object state.
* Useful for generating 404 pages.
*
* @return boolean
*/
function locationExists() {
$module = $this->getModule();
return (
$module
&& (
DocumentationService::find_page($module, $this->Remaining)
|| is_dir($module->getPath() . implode('/', $this->Remaining))
)
);
}
/**
* @return DocumentationPage
*/

View File

@ -39,6 +39,23 @@ class DocumentationViewerTest extends FunctionalTest {
DocumentationViewer::set_link_base($this->origLinkBase);
}
function testLocationExists() {
$response = $this->get('DocumentationViewerTests/en/2.4/');
$this->assertEquals($response->getStatusCode(), 200, 'Existing base folder');
$response = $this->get('DocumentationViewerTests/en/2.4/subfolder');
$this->assertEquals($response->getStatusCode(), 200, 'Existing subfolder');
$response = $this->get('DocumentationViewerTests/en/2.4/nonexistant-subfolder');
$this->assertEquals($response->getStatusCode(), 404, 'Nonexistant subfolder');
$response = $this->get('DocumentationViewerTests/en/2.4/nonexistant-file.txt');
$this->assertEquals($response->getStatusCode(), 404, 'Nonexistant file');
$response = $this->get('DocumentationViewerTests/en/2.4/test');
$this->assertEquals($response->getStatusCode(), 200, 'Existing file');
}
function testGetModulePagesShort() {
$v = new DocumentationViewer();
$response = $v->handleRequest(new SS_HTTPRequest('GET', 'DocumentationViewerTests/en/2.4/subfolder/'));