mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
BUGFIX Showing proper 404s
This commit is contained in:
parent
f9c8617af4
commit
24ebc64945
@ -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
|
||||
*/
|
||||
|
@ -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/'));
|
||||
|
Loading…
Reference in New Issue
Block a user