From d7e52ee0f8fb876161ac9705f4347d6431657358 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sat, 6 Mar 2010 05:40:38 +0100 Subject: [feature/request-class] Adding a request class based on ascraeus-experiment. The well known request_var function is now a wrapper that calls a method on a phpbb_request object. The class provides additional functionality. It can replace all super globals with special objects that throw errors when being accessed. They still allow isset operations to keep backward compatibility with isset($_POST['var']) checks. The phpbb_request class implements the phpbb_request_interface which is available for easy mocking of input in tests. PHPBB3-9716 --- tests/request/request.php | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/request/request.php (limited to 'tests/request/request.php') diff --git a/tests/request/request.php b/tests/request/request.php new file mode 100644 index 0000000000..1376d0665a --- /dev/null +++ b/tests/request/request.php @@ -0,0 +1,89 @@ +request = new phpbb_request(); + } + + public function test_toggle_super_globals() + { + $this->assertTrue($this->request->super_globals_disabled(), 'Superglobals were not disabled'); + + $this->request->enable_super_globals(); + + $this->assertFalse($this->request->super_globals_disabled(), 'Superglobals were not enabled'); + + $this->assertEquals(1, $_POST['test'], 'Checking $_POST after enable_super_globals'); + $this->assertEquals(2, $_GET['test'], 'Checking $_GET after enable_super_globals'); + $this->assertEquals(3, $_COOKIE['test'], 'Checking $_COOKIE after enable_super_globals'); + $this->assertEquals(3, $_REQUEST['test'], 'Checking $_REQUEST after enable_super_globals'); + + $_POST['x'] = 2; + $this->assertEquals($_POST, $GLOBALS['_POST'], 'Checking whether $_POST can still be accessed via $GLOBALS[\'_POST\']'); + } + + /** + * Checks that directly accessing $_POST will trigger + * an error. + */ + public function test_disable_post_super_global() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $_POST['test'] = 3; + } + + public function test_is_set_post() + { + $this->assertTrue($this->request->is_set_post('test')); + $this->assertFalse($this->request->is_set_post('unset')); + } + + public function test_addslashes_recursively() + { + $data = array('some"string' => array('that"' => 'really"', 'needs"' => '"escaping')); + $expected = array('some\\"string' => array('that\\"' => 'really\\"', 'needs\\"' => '\\"escaping')); + + phpbb_request::addslashes_recursively($data); + + $this->assertEquals($expected, $data); + } + + public function test_variable_names() + { + $expected = array('test', 'unset'); + $result = $this->request->variable_names(); + $this->assertEquals($expected, $result); + } + + /** + * Makes sure super globals work properly after these tests + */ + protected function tearDown() + { + $this->request->enable_super_globals(); + } +} -- cgit v1.2.1 From ea919ad8b276c78207ec33d1fc34f1f0ef15bc0d Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sat, 13 Mar 2010 11:19:28 +0100 Subject: [feature/request-class] Refactored request class and wrapper functions. The request class - now makes use of the new type cast helper (dependency injection) - has no static methods anymore. - now has a constructor argument to leave super globals turned on Brought back the set_var function in functions.php. It is now a wrapper around the type cast helper. It creates an instance on the fly. The request_var wrapper function now has an optional last argument to inject the request class instance, rather than abusing the $var_name. PHPBB3-9716 --- tests/request/request.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'tests/request/request.php') diff --git a/tests/request/request.php b/tests/request/request.php index 1376d0665a..df71d783ed 100644 --- a/tests/request/request.php +++ b/tests/request/request.php @@ -9,6 +9,8 @@ */ require_once 'test_framework/framework.php'; +require_once '../phpBB/includes/request/type_cast_helper_interface.php'; +require_once '../phpBB/includes/request/type_cast_helper.php'; require_once '../phpBB/includes/request/request_interface.php'; require_once '../phpBB/includes/request/deactivated_super_global.php'; require_once '../phpBB/includes/request/request.php'; @@ -62,16 +64,6 @@ class phpbb_request_test extends phpbb_test_case $this->assertFalse($this->request->is_set_post('unset')); } - public function test_addslashes_recursively() - { - $data = array('some"string' => array('that"' => 'really"', 'needs"' => '"escaping')); - $expected = array('some\\"string' => array('that\\"' => 'really\\"', 'needs\\"' => '\\"escaping')); - - phpbb_request::addslashes_recursively($data); - - $this->assertEquals($expected, $data); - } - public function test_variable_names() { $expected = array('test', 'unset'); -- cgit v1.2.1 From 0ae7df8a51129f2ece86f959e2e155d988feb081 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sat, 13 Mar 2010 11:28:41 +0100 Subject: [feature/request-class] Request class test now uses a type cast helper mock. Removed the dependency of the request class test on having an actual phpbb_type_cast_helper instance, by replacing it with an object mocking the phpbb_type_cast_helper_interface. PHPBB3-9716 --- tests/request/request.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests/request/request.php') diff --git a/tests/request/request.php b/tests/request/request.php index df71d783ed..ebfc3ba2b0 100644 --- a/tests/request/request.php +++ b/tests/request/request.php @@ -10,13 +10,13 @@ require_once 'test_framework/framework.php'; require_once '../phpBB/includes/request/type_cast_helper_interface.php'; -require_once '../phpBB/includes/request/type_cast_helper.php'; require_once '../phpBB/includes/request/request_interface.php'; require_once '../phpBB/includes/request/deactivated_super_global.php'; require_once '../phpBB/includes/request/request.php'; class phpbb_request_test extends phpbb_test_case { + private $type_cast_helper; private $request; protected function setUp() @@ -28,7 +28,9 @@ class phpbb_request_test extends phpbb_test_case $_REQUEST['test'] = 3; $_GET['unset'] = ''; - $this->request = new phpbb_request(); + $this->type_cast_helper = $this->getMock('phpbb_type_cast_helper_interface'); + + $this->request = new phpbb_request($this->type_cast_helper); } public function test_toggle_super_globals() -- cgit v1.2.1 From 456de639123ae3da6320bed6140ab69ac9925e74 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Tue, 31 Aug 2010 21:26:50 +0200 Subject: [feature/request-class] Refactor request classes to use autoloading All class names have been adjusted to use a phpbb_request prefix, allowing them to be autoloaded. Also introduces some improvements to autoloading in general. PHPBB3-9716 --- tests/request/request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/request/request.php') diff --git a/tests/request/request.php b/tests/request/request.php index ebfc3ba2b0..cf275e763c 100644 --- a/tests/request/request.php +++ b/tests/request/request.php @@ -10,7 +10,7 @@ require_once 'test_framework/framework.php'; require_once '../phpBB/includes/request/type_cast_helper_interface.php'; -require_once '../phpBB/includes/request/request_interface.php'; +require_once '../phpBB/includes/request/interface.php'; require_once '../phpBB/includes/request/deactivated_super_global.php'; require_once '../phpBB/includes/request/request.php'; @@ -28,7 +28,7 @@ class phpbb_request_test extends phpbb_test_case $_REQUEST['test'] = 3; $_GET['unset'] = ''; - $this->type_cast_helper = $this->getMock('phpbb_type_cast_helper_interface'); + $this->type_cast_helper = $this->getMock('phpbb_request_type_cast_helper_interface'); $this->request = new phpbb_request($this->type_cast_helper); } -- cgit v1.2.1