[feature/controller] Check for proper status codes from controllers

PHPBB3-10864
This commit is contained in:
David King 2012-11-19 12:37:20 -05:00
parent 3004350281
commit f8614bfc84
5 changed files with 29 additions and 9 deletions

View file

@ -24,12 +24,7 @@ $user->session_begin();
$auth->acl($user->data); $auth->acl($user->data);
$user->setup('app'); $user->setup('app');
// Until we fix the issue with relative paths, we have to fake path info to $symfony_request = phpbb_create_symfony_request($request);
// allow urls like app.php?controller=foo/bar
$controller = $request->variable('controller', '');
$uri = '/' . $controller;
$symfony_request = phpbb_create_symfony_request($uri, $request);
$http_kernel = $phpbb_container->get('http_kernel'); $http_kernel = $phpbb_container->get('http_kernel');
$response = $http_kernel->handle($symfony_request); $response = $http_kernel->handle($symfony_request);
$response->send(); $response->send();

View file

@ -18,6 +18,7 @@ if (!defined('IN_PHPBB'))
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class phpbb_event_kernel_exception_subscriber implements EventSubscriberInterface class phpbb_event_kernel_exception_subscriber implements EventSubscriberInterface
@ -56,9 +57,11 @@ class phpbb_event_kernel_exception_subscriber implements EventSubscriberInterfac
{ {
page_header($this->user->lang('INFORMATION')); page_header($this->user->lang('INFORMATION'));
$exception = $event->getException();
$this->template->assign_vars(array( $this->template->assign_vars(array(
'MESSAGE_TITLE' => $this->user->lang('INFORMATION'), 'MESSAGE_TITLE' => $this->user->lang('INFORMATION'),
'MESSAGE_TEXT' => $event->getException()->getMessage(), 'MESSAGE_TEXT' => $exception->getMessage(),
)); ));
$this->template->set_filenames(array( $this->template->set_filenames(array(
@ -67,7 +70,9 @@ class phpbb_event_kernel_exception_subscriber implements EventSubscriberInterfac
page_footer(true, false, false); page_footer(true, false, false);
$response = new Response($this->template->assign_display('body'), 404);
$status_code = $exception instanceof NotFoundHttpException ? $exception->getStatusCode() : 500;
$response = new Response($this->template->assign_display('body'), $status_code);
$event->setResponse($response); $event->setResponse($response);
} }

View file

@ -116,11 +116,20 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
{ {
$this->phpbb_extension_manager->enable('foo/bar'); $this->phpbb_extension_manager->enable('foo/bar');
$crawler = $this->request('GET', 'app.php?controller=foo/baz'); $crawler = $this->request('GET', 'app.php?controller=foo/baz');
$this->assertEquals(404, $this->client->getResponse()->getStatus()); $this->assertEquals(500, $this->client->getResponse()->getStatus());
$this->assertContains('Missing value for argument #1: test in class phpbb_ext_foo_bar_controller:baz', $crawler->filter('body')->text()); $this->assertContains('Missing value for argument #1: test in class phpbb_ext_foo_bar_controller:baz', $crawler->filter('body')->text());
$this->phpbb_extension_manager->purge('foo/bar'); $this->phpbb_extension_manager->purge('foo/bar');
} }
public function test_exception_thrown_status_code()
{
$this->phpbb_extension_manager->enable('foo/bar');
$crawler = $this->request('GET', 'app.php?controller=foo/exception');
$this->assertEquals(500, $this->client->getResponse()->getStatus());
$this->assertContains('Exception thrown from foo/exception route', $crawler->filter('body')->text());
$this->phpbb_extension_manager->purge('foo/bar');
}
/** /**
* Check the error produced by extension at ./ext/does/not/exist. * Check the error produced by extension at ./ext/does/not/exist.
* *
@ -133,6 +142,8 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
public function test_error_ext_disabled_or_404() public function test_error_ext_disabled_or_404()
{ {
$crawler = $this->request('GET', 'app.php?controller=does/not/exist'); $crawler = $this->request('GET', 'app.php?controller=does/not/exist');
// This is 500 response because the exception is thrown from within Symfony
// and does not provide a exception code, so we assign it 500 by default
$this->assertEquals(404, $this->client->getResponse()->getStatus()); $this->assertEquals(404, $this->client->getResponse()->getStatus());
$this->assertContains('No route found for "GET /does/not/exist"', $crawler->filter('body')->text()); $this->assertContains('No route found for "GET /does/not/exist"', $crawler->filter('body')->text());
} }

View file

@ -9,3 +9,7 @@ foo_baz_controller:
foo_template_controller: foo_template_controller:
pattern: /foo/template pattern: /foo/template
defaults: { _controller: foo_bar.controller:template } defaults: { _controller: foo_bar.controller:template }
foo_exception_controller:
pattern: /foo/foo_exception
defaults: { _controller: foo_bar.controller:exception }

View file

@ -27,4 +27,9 @@ class phpbb_ext_foo_bar_controller
return $this->helper->render('foo_bar_body.html'); return $this->helper->render('foo_bar_body.html');
} }
public function exception()
{
throw new phpbb_controller_exception('Exception thrown from foo/exception route');
}
} }