diff --git a/tests/php/test_api_role_userpref_methods.php b/tests/php/test_api_role_userpref_methods.php new file mode 100644 index 0000000000..af88382e50 --- /dev/null +++ b/tests/php/test_api_role_userpref_methods.php @@ -0,0 +1,85 @@ +method = $m; } + function is($type) { return in_array($this->method, (array)$type); } + function allowMethod() { + if (!in_array($this->method, func_get_args())) throw new MethodNotAllowedException(); + throw new Reached(); + } + function __get($n) { throw new Reached(); } + function __call($n, $a) { throw new Reached(); } +} + +function extract_method($source, $name) { + $start = strpos($source, 'public function '.$name.'('); + if ($start === false) return null; + $open = strpos($source, '{', $start); + $depth = 0; + for ($i = $open; $i < strlen($source); $i++) { + if ($source[$i] == '{') $depth++; + else if ($source[$i] == '}' && --$depth == 0) break; + } + return substr($source, $start, $i - $start + 1); +} + +foreach (array('Roles', 'UserPreference') as $n => $controller) { + $src = file_get_contents("$root/web/api/app/Controller/{$controller}Controller.php"); + $methods = extract_method($src, 'add')."\n".extract_method($src, 'edit'); + $class = "Fake{$controller}"; + eval("class $class { public \$request; + function __get(\$n) { throw new Reached(); } + function __call(\$n, \$a) { throw new Reached(); } + $methods }"); + $actions = array('edit' => array('get', 'delete')); + if ($controller == 'UserPreference') $actions = array('add' => array('get', 'put', 'delete')) + $actions; + foreach ($actions as $action => $refused) { + foreach ($refused as $m) { + $c = new $class(); $c->request = new FakeRequest($m); + try { $c->$action(1); $got = 'answered'; } + catch (MethodNotAllowedException $e) { $got = 'refused'; } + catch (Reached $e) { $got = "ran"; } + catch (Exception $e) { $got = "answered"; } + check("$controller $action refuses ".strtoupper($m), $got, 'refused'); + } + $ok = ($action == 'add') ? 'post' : 'put'; + $c = new $class(); $c->request = new FakeRequest($ok); + try { $c->$action(1); $got = 'answered'; } + catch (MethodNotAllowedException $e) { $got = 'refused'; } + catch (Reached $e) { $got = 'ran'; } + check("$controller $action accepts ".strtoupper($ok), $got, 'ran'); + } +} + +echo $failures ? "\n$failures check(s) FAILED\n" : "\nall checks passed\n"; +exit($failures ? 1 : 0); diff --git a/tests/php/test_api_roles_add_edit_status.php b/tests/php/test_api_roles_add_edit_status.php index e5fe7a3987..edb9073e60 100644 --- a/tests/php/test_api_roles_add_edit_status.php +++ b/tests/php/test_api_roles_add_edit_status.php @@ -40,6 +40,10 @@ class FakeRequest { public $data = array(); public $method; function __construct($m) { $this->method = $m; } function is($t) { return strtolower($t) == strtolower($this->method); } + function allowMethod() { + foreach (func_get_args() as $m) if ($this->is($m)) return true; + throw new Exception('Method Not Allowed'); + } } eval('class Harness { public $Role, $request, $response, $set = array(); diff --git a/web/api/app/Controller/RolesController.php b/web/api/app/Controller/RolesController.php index 86e797d89a..327e722b30 100644 --- a/web/api/app/Controller/RolesController.php +++ b/web/api/app/Controller/RolesController.php @@ -121,6 +121,7 @@ class RolesController extends AppController { * @return void */ public function edit($id = null) { + $this->request->allowMethod('post', 'put'); $this->Role->id = $id; global $user; diff --git a/web/api/app/Controller/UserPreferenceController.php b/web/api/app/Controller/UserPreferenceController.php index 53d7e27682..70950b5505 100644 --- a/web/api/app/Controller/UserPreferenceController.php +++ b/web/api/app/Controller/UserPreferenceController.php @@ -102,6 +102,7 @@ class UserPreferenceController extends AppController { * @return void */ public function add() { + $this->request->allowMethod('post'); $data = $this->request->data; if ($this->RequestHandler->requestedWith('json')) { $data = $this->request->input('json_decode', true) ; @@ -146,6 +147,7 @@ class UserPreferenceController extends AppController { * @return void */ public function edit($id = null) { + $this->request->allowMethod('post', 'put'); if (!$this->UserPreference->exists($id)) { throw new NotFoundException(__('Invalid user_preference')); }