1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-08 17:56:52 +02:00

Merge remote-tracking branch 'imkingdavid/feature/controller-new' into develop

* imkingdavid/feature/controller-new: (67 commits)
  [feature/controller] Fix misnamed route for functional test
  [feature/controller] Fix comments, check against more general HttpException
  [feature/controller] Check for proper status codes from controllers
  [feature/controller] Correctly create Symfony object from globals
  [feature/controller] Add documentation about input being HTML-escaped
  [feature/controller] Create Symfony Request in new function
  [feature/controller] Remove unused language strings
  [feature/controller] Don't use $user->lang() before container compilation
  [feature/controller] Update routing documentation for using query string
  [feature/controller] Remove now-unused code
  [feature/controller] Remove url rewriting until we use pathinfo in controllers
  [feature/controller] Fix functional tests to use query string for controllers
  [feature/controller] Allow injecting Symfony Request into controllers
  [feature/controller] Use query string, not path info, for controller access
  [feature/controller] Fix line endings and permissions, and check responses
  [feature/controller] Remove URL rewriting by default
  [feature/controller] Add controller functional test with template
  [feature/controller] Use warning instead of echo for copy() and unlink()
  [feature/controller] Flip method parameters, require $message
  [feature/controller] Rename $root_path class property to $phpbb_root_path
  ...
This commit is contained in:
Igor Wiedler
2012-11-19 22:28:12 +01:00
45 changed files with 1267 additions and 219 deletions

View File

@@ -0,0 +1,3 @@
core_controller:
pattern: /core_foo
defaults: { _controller: core_foo.controller:bar }

View File

@@ -0,0 +1,3 @@
services:
core_foo.controller:
class: phpbb_controller_foo

View File

@@ -0,0 +1,76 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class phpbb_controller_test extends phpbb_test_case
{
public function setUp()
{
$this->extension_manager = new phpbb_mock_extension_manager(
dirname(__FILE__) . '/',
array(
'foo' => array(
'ext_name' => 'foo',
'ext_active' => '1',
'ext_path' => 'ext/foo/',
),
));
}
public function test_provider()
{
$provider = new phpbb_controller_provider;
$routes = $provider
->import_paths_from_finder($this->extension_manager->get_finder())
->find('./tests/controller/');
// This will need to be updated if any new routes are defined
$this->assertEquals(2, sizeof($routes));
}
public function test_controller_resolver()
{
$container = new ContainerBuilder();
// YamlFileLoader only uses one path at a time, so we need to loop
// through all of the ones we are using.
foreach (array(__DIR__.'/config', __DIR__.'/ext/foo/config') as $path)
{
$loader = new YamlFileLoader($container, new FileLocator($path));
$loader->load('services.yml');
}
// Autoloading classes within the tests folder does not work
// so I'll include them manually.
if (!class_exists('phpbb_ext_foo_controller'))
{
include(__DIR__.'/ext/foo/controller.php');
}
if (!class_exists('phpbb_controller_foo'))
{
include(__DIR__.'/includes/controller/foo.php');
}
$resolver = new phpbb_controller_resolver(new phpbb_user, $container);
$symfony_request = new Request();
$symfony_request->attributes->set('_controller', 'foo.controller:handle');
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_ext_foo_controller, 'handle'));
$symfony_request = new Request();
$symfony_request->attributes->set('_controller', 'core_foo.controller:bar');
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb_controller_foo, 'bar'));
}
}

View File

@@ -0,0 +1,3 @@
controller1:
pattern: /foo
defaults: { _controller: foo.controller:handle }

View File

@@ -0,0 +1,3 @@
services:
foo.controller:
class: phpbb_ext_foo_controller

View File

@@ -0,0 +1,16 @@
<?php
use Symfony\Component\HttpFoundation\Response;
class phpbb_ext_foo_controller
{
/**
* Handle method
*
* @return null
*/
public function handle()
{
return new Response('Test', 200);
}
}

View File

@@ -0,0 +1,16 @@
<?php
use Symfony\Component\HttpFoundation\Response;
class phpbb_controller_foo
{
/**
* Bar method
*
* @return null
*/
public function bar()
{
return new Response('bar()', 200);
}
}

View File

@@ -13,6 +13,14 @@
class phpbb_functional_extension_controller_test extends phpbb_functional_test_case
{
protected $phpbb_extension_manager;
static protected $fixtures = array(
'foo/bar/config/routing.yml',
'foo/bar/config/services.yml',
'foo/bar/controller/controller.php',
'foo/bar/styles/prosilver/template/foo_bar_body.html',
);
/**
* This should only be called once before the tests are run.
* This is used to copy the fixtures to the phpBB install
@@ -22,15 +30,11 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
global $phpbb_root_path;
parent::setUpBeforeClass();
// these directories need to be created before the files can be copied
$directories = array(
$phpbb_root_path . 'ext/error/class/',
$phpbb_root_path . 'ext/error/classtype/',
$phpbb_root_path . 'ext/error/disabled/',
$phpbb_root_path . 'ext/foo/bar/',
$phpbb_root_path . 'ext/foo/bar/styles/prosilver/template/',
$phpbb_root_path . 'ext/foobar/',
$phpbb_root_path . 'ext/foobar/styles/prosilver/template/',
$phpbb_root_path . 'ext/foo/bar/config/',
$phpbb_root_path . 'ext/foo/bar/controller/',
$phpbb_root_path . 'ext/foo/bar/styles/prosilver/template',
);
foreach ($directories as $dir)
@@ -41,30 +45,36 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
}
}
$fixtures = array(
'error/class/controller.php',
'error/class/ext.php',
'error/classtype/controller.php',
'error/classtype/ext.php',
'error/disabled/controller.php',
'error/disabled/ext.php',
'foo/bar/controller.php',
'foo/bar/ext.php',
'foo/bar/styles/prosilver/template/foobar_body.html',
'foobar/controller.php',
'foobar/ext.php',
'foobar/styles/prosilver/template/foobar_body.html',
);
foreach ($fixtures as $fixture)
foreach (self::$fixtures as $fixture)
{
if (!copy("tests/functional/fixtures/ext/$fixture", "{$phpbb_root_path}ext/$fixture"))
{
echo 'Could not copy file ' . $fixture;
}
copy(
"tests/functional/fixtures/ext/$fixture",
"{$phpbb_root_path}ext/$fixture");
}
}
/**
* This should only be called once after the tests are run.
* This is used to remove the fixtures from the phpBB install
*/
static public function tearDownAfterClass()
{
global $phpbb_root_path;
foreach (self::$fixtures as $fixture)
{
unlink("{$phpbb_root_path}ext/$fixture");
}
rmdir("{$phpbb_root_path}ext/foo/bar/config");
rmdir("{$phpbb_root_path}ext/foo/bar/controller");
rmdir("{$phpbb_root_path}ext/foo/bar/styles/prosilver/template");
rmdir("{$phpbb_root_path}ext/foo/bar/styles/prosilver");
rmdir("{$phpbb_root_path}ext/foo/bar/styles");
rmdir("{$phpbb_root_path}ext/foo/bar");
rmdir("{$phpbb_root_path}ext/foo");
}
public function setUp()
{
parent::setUp();
@@ -75,72 +85,67 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c
}
/**
* Check an extension at ./ext/foobar/ which should have the class
* phpbb_ext_foobar_controller
*/
public function test_foobar()
{
$this->phpbb_extension_manager->enable('foobar');
$crawler = $this->request('GET', 'index.php?ext=foobar');
$this->assert_response_success();
$this->assertContains("This is for testing purposes.", $crawler->filter('#page-body')->text());
$this->phpbb_extension_manager->purge('foobar');
}
/**
* Check an extension at ./ext/foo/bar/ which should have the class
* phpbb_ext_foo_bar_controller
* Check a controller for extension foo/bar.
*/
public function test_foo_bar()
{
$this->phpbb_extension_manager->enable('foo/bar');
$crawler = $this->request('GET', 'index.php?ext=foo/bar');
$crawler = $this->request('GET', 'app.php?controller=foo/bar');
$this->assert_response_success();
$this->assertContains("This is for testing purposes.", $crawler->filter('#page-body')->text());
$this->assertContains("foo/bar controller handle() method", $crawler->filter('body')->text());
$this->phpbb_extension_manager->purge('foo/bar');
}
/**
* Check the error produced by extension at ./ext/error/class which has class
* phpbb_ext_foobar_controller
* Check the output of a controller using the template system
*/
public function test_error_class_name()
public function test_controller_with_template()
{
$this->phpbb_extension_manager->enable('error/class');
$crawler = $this->request('GET', 'index.php?ext=error/class');
$this->assertContains("The extension error/class is missing a controller class and cannot be accessed through the front-end.", $crawler->filter('#message')->text());
$this->phpbb_extension_manager->purge('error/class');
$this->phpbb_extension_manager->enable('foo/bar');
$crawler = $this->request('GET', 'app.php?controller=foo/template');
$this->assert_response_success();
$this->assertContains("I am a variable", $crawler->filter('#content')->text());
$this->phpbb_extension_manager->purge('foo/bar');
}
/**
* Check the error produced by extension at ./ext/error/classtype which has class
* phpbb_ext_error_classtype_controller but does not implement phpbb_extension_controller_interface
* Check the error produced by calling a controller without a required
* argument.
*/
public function test_error_class_type()
public function test_missing_argument()
{
$this->phpbb_extension_manager->enable('error/classtype');
$crawler = $this->request('GET', 'index.php?ext=error/classtype');
$this->assertContains("The extension controller class phpbb_ext_error_classtype_controller is not an instance of the phpbb_extension_controller_interface.", $crawler->filter('#message')->text());
$this->phpbb_extension_manager->purge('error/classtype');
$this->phpbb_extension_manager->enable('foo/bar');
$crawler = $this->request('GET', 'app.php?controller=foo/baz');
$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->phpbb_extension_manager->purge('foo/bar');
}
/**
* Check the error produced by extension at ./ext/error/disabled that is (obviously)
* a disabled extension
* Check the status code resulting from an exception thrown by a controller
*/
public function test_error_ext_disabled()
public function test_exception_should_result_in_500_status_code()
{
$crawler = $this->request('GET', 'index.php?ext=error/disabled');
$this->assertContains("The extension error/disabled is not enabled", $crawler->filter('#message')->text());
$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/error/404 that is (obviously)
* not existant
* Check the error produced by extension at ./ext/does/not/exist.
*
* If an extension is disabled, its routes are not loaded. Because we
* are not looking for a controller based on a specified extension,
* we don't know the difference between a route in a disabled
* extension and a route that is not defined anyway; it is the same
* error message.
*/
public function test_error_ext_missing()
public function test_error_ext_disabled_or_404()
{
$crawler = $this->request('GET', 'index.php?ext=error/404');
$this->assertContains("The extension error/404 does not exist.", $crawler->filter('#message')->text());
$crawler = $this->request('GET', 'app.php?controller=does/not/exist');
$this->assertEquals(404, $this->client->getResponse()->getStatus());
$this->assertContains('No route found for "GET /does/not/exist"', $crawler->filter('body')->text());
}
}

View File

@@ -1,14 +0,0 @@
<?php
class phpbb_ext_foobar_controller extends phpbb_extension_controller
{
public function handle()
{
$this->template->set_filenames(array(
'body' => 'index_body.html'
));
page_header('Test extension');
page_footer();
}
}

View File

@@ -1,6 +0,0 @@
<?php
class phpbb_ext_error_class_ext extends phpbb_extension_base
{
}

View File

@@ -1,15 +0,0 @@
<?php
class phpbb_ext_error_classtype_controller
{
public function handle()
{
global $template;
$template->set_filenames(array(
'body' => 'index_body.html'
));
page_header('Test extension');
page_footer();
}
}

View File

@@ -1,6 +0,0 @@
<?php
class phpbb_ext_error_classtype_ext extends phpbb_extension_base
{
}

View File

@@ -1,14 +0,0 @@
<?php
class phpbb_ext_error_disabled_controller extends phpbb_extension_controller
{
public function handle()
{
$this->template->set_filenames(array(
'body' => 'index_body.html'
));
page_header('Test extension');
page_footer();
}
}

View File

@@ -1,6 +0,0 @@
<?php
class phpbb_ext_error_disabled_ext extends phpbb_extension_base
{
}

View File

@@ -0,0 +1,15 @@
foo_bar_controller:
pattern: /foo/bar
defaults: { _controller: foo_bar.controller:handle }
foo_baz_controller:
pattern: /foo/baz
defaults: { _controller: foo_bar.controller:baz }
foo_template_controller:
pattern: /foo/template
defaults: { _controller: foo_bar.controller:template }
foo_exception_controller:
pattern: /foo/exception
defaults: { _controller: foo_bar.controller:exception }

View File

@@ -0,0 +1,6 @@
services:
foo_bar.controller:
class: phpbb_ext_foo_bar_controller
arguments:
- @controller.helper
- @template

View File

@@ -1,14 +0,0 @@
<?php
class phpbb_ext_foo_bar_controller extends phpbb_extension_controller
{
public function handle()
{
$this->template->set_filenames(array(
'body' => 'foobar_body.html'
));
page_header('Test extension');
page_footer();
}
}

View File

@@ -0,0 +1,35 @@
<?php
use Symfony\Component\HttpFoundation\Response;
class phpbb_ext_foo_bar_controller
{
protected $template;
public function __construct(phpbb_controller_helper $helper, phpbb_template $template)
{
$this->template = $template;
$this->helper = $helper;
}
public function handle()
{
return new Response('foo/bar controller handle() method', 200);
}
public function baz($test)
{
return new Response('Value of "test" URL argument is: ' . $test);
}
public function template()
{
$this->template->assign_var('A_VARIABLE', 'I am a variable');
return $this->helper->render('foo_bar_body.html');
}
public function exception()
{
throw new phpbb_controller_exception('Exception thrown from foo/exception route');
}
}

View File

@@ -2,5 +2,5 @@
class phpbb_ext_foo_bar_ext extends phpbb_extension_base
{
}

View File

@@ -1,5 +1,3 @@
<!-- INCLUDE overall_header.html -->
<div id="welcome">This is for testing purposes.</div>
<div id="content">{A_VARIABLE}</div>
<!-- INCLUDE overall_footer.html -->

View File

@@ -1,14 +0,0 @@
<?php
class phpbb_ext_foobar_controller extends phpbb_extension_controller
{
public function handle()
{
$this->template->set_filenames(array(
'body' => 'foobar_body.html'
));
page_header('Test extension');
page_footer();
}
}

View File

@@ -1,6 +0,0 @@
<?php
class phpbb_ext_foobar_ext extends phpbb_extension_base
{
}

View File

@@ -1,5 +0,0 @@
<!-- INCLUDE overall_header.html -->
<div id="welcome">This is for testing purposes.</div>
<!-- INCLUDE overall_footer.html -->