From d7a68dbec36d2b90a0ff42b42c73df5aec8889f1 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 12 Nov 2013 17:41:54 +0100 Subject: [PATCH] Remove http_request --- lib/Alchemy/Phrasea/Helper/User/Edit.php | 23 +- lib/classes/http/request.php | 261 ------------------ .../Phrasea/Controller/Admin/UsersTest.php | 16 +- tests/classes/http/http_requestTest.php | 176 ------------ 4 files changed, 27 insertions(+), 449 deletions(-) delete mode 100644 lib/classes/http/request.php delete mode 100644 tests/classes/http/http_requestTest.php diff --git a/lib/Alchemy/Phrasea/Helper/User/Edit.php b/lib/Alchemy/Phrasea/Helper/User/Edit.php index f2a7a4301e..916bd00e70 100644 --- a/lib/Alchemy/Phrasea/Helper/User/Edit.php +++ b/lib/Alchemy/Phrasea/Helper/User/Edit.php @@ -443,7 +443,6 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper public function apply_rights() { - $request = \http_request::getInstance(); $ACL = $this->app['acl']->get($this->app['authentication']->getUser()); $base_ids = array_keys($ACL->get_granted_base(array('canadmin'))); @@ -477,7 +476,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper } $rights[$k] = $right . '_' . $base_id; } - $parm = $request->get_parms_from_serialized_datas($rights, 'values'); + $parm = $this->unserializedRequestData($this->app['request'], $rights, 'values'); foreach ($parm as $p => $v) { if (trim($v) == '') @@ -518,7 +517,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper $rights[$k] = $right . '_' . $databox->get_sbas_id(); } - $parm = $request->get_parms_from_serialized_datas($rights, 'values'); + $parm = $this->unserializedRequestData($this->app['request'], $rights, 'values'); foreach ($parm as $p => $v) { if (trim($v) == '') @@ -577,8 +576,6 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper return $this; } - $request = \http_request::getInstance(); - $infos = array( 'gender' , 'first_name' @@ -594,7 +591,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper , 'fax' ); - $parm = $request->get_parms_from_serialized_datas($infos, 'user_infos'); + $parm = $this->unserializedRequestData($this->app['request'], $infos, 'user_infos'); if ($parm['email'] && !\Swift_Validate::email($parm['email'])) { throw new \Exception_InvalidArgument(_('Email addess is not valid')); @@ -758,4 +755,18 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper $ACL->revoke_unused_sbas_rights(); } } + + private function unserializedRequestData(Request $request, array $indexes, $requestIndex) + { + $parameters = $data = array(); + parse_str($request->get($requestIndex), $data); + + if (count($data) > 0) { + foreach ($indexes as $index) { + $parameters[$index] = isset($data[$index]) ? $data[$index] : null; + } + } + + return $parameters; + } } diff --git a/lib/classes/http/request.php b/lib/classes/http/request.php deleted file mode 100644 index c022dacdca..0000000000 --- a/lib/classes/http/request.php +++ /dev/null @@ -1,261 +0,0 @@ - - */ - private static $_instance; - - /** - * - * @var boolean - */ - protected static $_cli_usage; - - /** - * - * @var - */ - protected $code; - - /** - * - * @return http_request - */ - public static function getInstance() - { - if ( ! (self::$_instance instanceof self)) { - self::$_instance = new self(); - } - - return self::$_instance; - } - - /** - * - * @return http_request - */ - public function __construct() - { - return $this; - } - - /** - * - * @return boolean - */ - public function is_ajax() - { - if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && - strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { - return true; - } - - return false; - } - - public function is_secure() - { - return ( - isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == 1) - ); - } - - public function comes_from_flash() - { - return (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/\bflash\b/i', $_SERVER['HTTP_USER_AGENT']) > 0); - } - - /** - * - * @return int - */ - public function get_code() - { - if (is_null($this->code) && isset($_SERVER['REDIRECT_STATUS'])) { - $this->code = $_SERVER['REDIRECT_STATUS']; - } - - return $this->code; - } - - /** - * - * @param int $code - * @return http_request - */ - public function set_code($code) - { - $this->code = (int) $code; - - return $this; - } - - /** - * - * @return Array - */ - public function get_parms() - { - $parm = array(); - $nargs = func_num_args(); - - if ($nargs == 1 && is_array(func_get_arg(0))) { - foreach (func_get_arg(0) as $key => $nom) { - if (is_string($key)) { - $value = isset($_GET[$key]) ? - $_GET[$key] : (isset($_POST[$key]) ? $_POST[$key] : NULL); - switch ($nom) { - case self::SANITIZE_NUMBER_INT: - $value = (int) $value; - break; - case self::SANITIZE_STRING: - $value = trim((string) $value); - break; - default: - break; - } - $parm[$key] = $value; - } else { - $parm[$nom] = isset($_GET[$nom]) ? - $_GET[$nom] : (isset($_POST[$nom]) ? $_POST[$nom] : NULL); - } - } - } else { - for ($i = 0; $i < $nargs; $i ++) { - $nom = func_get_arg($i); - $parm[$nom] = isset($_GET[$nom]) ? - $_GET[$nom] : (isset($_POST[$nom]) ? $_POST[$nom] : NULL); - } - } - - return($parm); - } - - /** - * - * @param array $indexes - * @param string $serializeds_datas_index - * @return array - */ - public function get_parms_from_serialized_datas(Array $indexes, $serializeds_datas_index) - { - $parm = array(); - $tmp_parms = array(); - - if (isset($_GET[$serializeds_datas_index])) - parse_str($_GET[$serializeds_datas_index], $tmp_parms); - elseif (isset($_POST[$serializeds_datas_index])) - parse_str($_POST[$serializeds_datas_index], $tmp_parms); - - if (count($tmp_parms) > 0) { - foreach ($indexes as $nom) { - $parm[$nom] = isset($tmp_parms[$nom]) ? $tmp_parms[$nom] : NULL; - } - } - - return $parm; - } - - /** - * - * @return boolean - */ - public function has_post_datas() - { - return ! empty($_POST); - } - - /** - * - * @return Array - */ - public function get_post_datas() - { - return $_POST; - } - - /** - * - * @return boolean - */ - public function has_get_datas() - { - return ! empty($_GET); - } - - /** - * - * @return boolean - */ - public function has_datas() - { - return ($this->has_post_datas() || $this->has_get_datas()); - } - - /** - * - * @param mixed content $data - * @param const $filter - * @return mixed content - */ - public function filter($data, $filter) - { - return filter_var($data, $filter); - } - - /** - * Tells wheter or not it's command line script - * - * @return boolean - */ - public static function is_command_line() - { - if (self::$_cli_usage === null) { - $sapi_name = strtolower(substr(php_sapi_name(), 0, 3)); - self::$_cli_usage = ($sapi_name == 'cli'); - } - - return self::$_cli_usage; - } -} diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Admin/UsersTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Admin/UsersTest.php index 1b598eac3f..1e76be3770 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Admin/UsersTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Admin/UsersTest.php @@ -63,10 +63,12 @@ class ControllerUsersTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $user = \User_Adapter::create(self::$DI['app'], $username, "test", $username . "@email.com", false); $base_id = self::$DI['collection']->get_base_id(); - $_GET['values'] = 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1'; - $_GET['user_infos'] = "user_infos[email]=" . $user->get_email(); - self::$DI['client']->request('POST', '/admin/users/rights/apply/', array('users' => $user->get_id())); + self::$DI['client']->request('POST', '/admin/users/rights/apply/', array( + 'users' => $user->get_id(), + 'values' => 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1', + 'user_infos' => "user_infos[email]=" . $user->get_email(), + )); $response = self::$DI['client']->getResponse(); $this->assertTrue($response->isOK()); $this->assertEquals("application/json", $response->headers->get("content-type")); @@ -87,9 +89,11 @@ class ControllerUsersTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $username = uniqid('user_'); $user = \User_Adapter::create(self::$DI['app'], $username, "test", $username . "@email.com", false); $base_id = self::$DI['collection']->get_base_id(); - $_GET['values'] = 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1'; - $_GET['user_infos'] = "user_infos[email]=" . $user->get_email(); - self::$DI['client']->request('POST', '/admin/users/rights/apply/', array('users' => $user->get_id())); + self::$DI['client']->request('POST', '/admin/users/rights/apply/', array( + 'users' => $user->get_id(), + 'values' => 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1', + 'user_infos' => "user_infos[email]=" . $user->get_email(), + )); $response = self::$DI['client']->getResponse(); $this->assertTrue($response->isOK()); $this->assertEquals("application/json", $response->headers->get("content-type")); diff --git a/tests/classes/http/http_requestTest.php b/tests/classes/http/http_requestTest.php deleted file mode 100644 index 8dd55f678b..0000000000 --- a/tests/classes/http/http_requestTest.php +++ /dev/null @@ -1,176 +0,0 @@ -object = new http_request(); - } - - public function testGetInstance() - { - $this->assertInstanceOf('http_request', http_request::getInstance()); - } - - public function testIs_ajax() - { - $this->assertFalse($this->object->is_ajax()); - $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; - $this->assertTrue($this->object->is_ajax()); - } - - public function testComes_from_flash() - { - $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $this->assertFalse($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash Player'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Flash'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Flash Player'; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Flash '; - $this->assertTrue($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = 'Flashs '; - $this->assertFalse($this->object->comes_from_flash()); - $_SERVER['HTTP_USER_AGENT'] = $user_agent; - $this->assertFalse($this->object->comes_from_flash()); - } - - public function testGet_code() - { - $this->assertNull($this->object->get_code()); - $_SERVER['REDIRECT_STATUS'] = 301; - $this->assertEquals(301, $this->object->get_code()); - $this->object->set_code(580); - $this->assertEquals(580, $this->object->get_code()); - $this->object->set_code('a'); - $this->assertEquals(0, $this->object->get_code()); - $this->object->set_code('a'); - $this->assertEquals(0, $this->object->get_code()); - } - - public function testSet_code() - { - $this->object->set_code(302); - $this->assertEquals(302, $this->object->get_code()); - } - - public function testGet_parms() - { - $_GET = array('lili' => '25', 'popo' => array('tip', 'top')); - $_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop')); - - $parm = $this->object->get_parms('lili', 'Plili', 'popo', 'Gpopo', 'notexists'); - - $this->assertEquals($_GET['lili'], $parm['lili']); - $this->assertEquals($_POST['Plili'], $parm['Plili']); - $this->assertEquals($_GET['popo'], $parm['popo']); - $this->assertEquals($_POST['Gpopo'], $parm['Gpopo']); - $this->assertNull($parm['notexists']); - - $parm = $this->object->get_parms( - array( - 'lili' => http_request::SANITIZE_NUMBER_INT - , 'Plili' - , 'popo' - , 'notexists' => http_request::SANITIZE_STRING - ) - ); - - $this->assertEquals((int) $_GET['lili'], $parm['lili']); - $this->assertTrue(is_int($parm['lili'])); - $this->assertEquals($_POST['Plili'], $parm['Plili']); - $this->assertEquals($_GET['popo'], $parm['popo']); - $this->assertEquals('', $parm['notexists']); - - $_GET = $_POST = array(); - } - - public function testGet_parmsArraToString() - { - $_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop')); - - try { - $this->object->get_parms(array('Gpopo' => http_request::SANITIZE_STRING)); - - if (version_compare(PHP_VERSION, '5.4', '>=')) { - $this->fail('Should raise an error notice'); - } - } catch (\PHPUnit_Framework_Error_Notice $e) { - - } - $_GET = $_POST = array(); - } - - public function testGet_parms_from_serialized_datas() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - public function testHas_post_datas() - { - $this->assertFalse($this->object->has_post_datas()); - $_POST = array('TOPPy' => null); - $this->assertTrue($this->object->has_post_datas()); - } - - public function testGet_post_datas() - { - $post = $_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop')); - $this->assertEquals($post, $this->object->get_post_datas()); - } - - public function testHas_get_datas() - { - $this->assertFalse($this->object->has_get_datas()); - $_GET = array('TOPPy' => null); - $this->assertTrue($this->object->has_get_datas()); - } - - public function testHas_datas() - { - $_POST = $_GET = array(); - $this->assertFalse($this->object->has_datas()); - $_POST = array('malal' => true); - $this->assertTrue($this->object->has_datas()); - $_GET = array('malal' => true); - $_POST = array(); - $this->assertTrue($this->object->has_datas()); - $_GET = array('malal' => true); - $_POST = array('malal' => true); - $this->assertTrue($this->object->has_datas()); - $_POST = $_GET = array(); - $this->assertFalse($this->object->has_datas()); - } - - /** - * @todo Implement testFilter(). - */ - public function testFilter() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - public function testIs_command_line() - { - $this->assertTrue($this->object->is_command_line()); - } -}