From b245e3475f66c94afb94f8b2287bf0185a343732 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 28 May 2009 06:07:27 +0800 Subject: Restructure things so that the application is now just another module. Kohana makes this type of transition fairly straightforward in that all controllers/helpers/etc are still located in the cascading filesystem without any extra effort, except that I've temporarily added a hack to force modules/gallery into the module path. Rename what's left of "core" to be "application" so that it conforms more closely to the Kohana standard (basically, just application/config/config.php which is the minimal thing that you need in the application directory) There's still considerable work left to be done here. Signed-off-by: Gallery Role Account --- modules/gallery/tests/REST_Controller_Test.php | 197 +++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 modules/gallery/tests/REST_Controller_Test.php (limited to 'modules/gallery/tests/REST_Controller_Test.php') diff --git a/modules/gallery/tests/REST_Controller_Test.php b/modules/gallery/tests/REST_Controller_Test.php new file mode 100644 index 00000000..8fb04d86 --- /dev/null +++ b/modules/gallery/tests/REST_Controller_Test.php @@ -0,0 +1,197 @@ +_post = $_POST; + $this->mock_controller = new Mock_RESTful_Controller("mock"); + $this->mock_not_loaded_controller = new Mock_RESTful_Controller("mock_not_loaded"); + $_POST = array(); + } + + public function teardown() { + $_POST = $this->_post; + } + + public function dispatch_index_test() { + $_SERVER["REQUEST_METHOD"] = "GET"; + $_POST["_method"] = ""; + $this->mock_controller->__call("index", ""); + $this->assert_equal("index", $this->mock_controller->method_called); + } + + public function dispatch_show_test() { + $_SERVER["REQUEST_METHOD"] = "GET"; + $_POST["_method"] = ""; + $this->mock_controller->__call("3", ""); + $this->assert_equal("show", $this->mock_controller->method_called); + $this->assert_equal("Mock_Model", get_class($this->mock_controller->resource)); + } + + public function dispatch_update_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = "PUT"; + $_POST["csrf"] = access::csrf_token(); + $this->mock_controller->__call("3", ""); + $this->assert_equal("update", $this->mock_controller->method_called); + $this->assert_equal("Mock_Model", get_class($this->mock_controller->resource)); + } + + public function dispatch_update_fails_without_csrf_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = "PUT"; + try { + $this->mock_controller->__call("3", ""); + $this->assert_false(true, "this should fail with a forbidden exception"); + } catch (Exception $e) { + // pass + } + } + + public function dispatch_delete_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = "DELETE"; + $_POST["csrf"] = access::csrf_token(); + $this->mock_controller->__call("3", ""); + $this->assert_equal("delete", $this->mock_controller->method_called); + $this->assert_equal("Mock_Model", get_class($this->mock_controller->resource)); + } + + public function dispatch_delete_fails_without_csrf_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = "DELETE"; + try { + $this->mock_controller->__call("3", ""); + $this->assert_false(true, "this should fail with a forbidden exception"); + } catch (Exception $e) { + // pass + } + } + + public function dispatch_404_test() { + /* The dispatcher should throw a 404 if the resource isn't loaded and the method isn't POST. */ + $methods = array( + array("GET", ""), + array("POST", "PUT"), + array("POST", "DELETE")); + + foreach ($methods as $method) { + $_SERVER["REQUEST_METHOD"] = $method[0]; + $_POST["_method"] = $method[1]; + $exception_caught = false; + try { + $this->mock_not_loaded_controller->__call(rand(), ""); + } catch (Kohana_404_Exception $e) { + $exception_caught = true; + } + $this->assert_true($exception_caught, "$method[0], $method[1]"); + } + } + + public function dispatch_create_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = ""; + $_POST["csrf"] = access::csrf_token(); + $this->mock_not_loaded_controller->__call("", ""); + $this->assert_equal("create", $this->mock_not_loaded_controller->method_called); + $this->assert_equal( + "Mock_Not_Loaded_Model", get_class($this->mock_not_loaded_controller->resource)); + } + + public function dispatch_create_fails_without_csrf_test() { + $_SERVER["REQUEST_METHOD"] = "POST"; + $_POST["_method"] = ""; + try { + $this->mock_not_loaded_controller->__call("", ""); + $this->assert_false(true, "this should fail with a forbidden exception"); + } catch (Exception $e) { + // pass + } + } + + public function dispatch_form_test_add() { + $this->mock_controller->form_add("args"); + $this->assert_equal("form_add", $this->mock_controller->method_called); + $this->assert_equal("args", $this->mock_controller->resource); + } + + public function dispatch_form_test_edit() { + $this->mock_controller->form_edit("1"); + $this->assert_equal("form_edit", $this->mock_controller->method_called); + $this->assert_equal("Mock_Model", get_class($this->mock_controller->resource)); + } + + public function routes_test() { + $this->assert_equal("mock/form_add/args", router::routed_uri("form/add/mock/args")); + $this->assert_equal("mock/form_edit/args", router::routed_uri("form/edit/mock/args")); + $this->assert_equal(null, router::routed_uri("rest/args")); + } +} + +class Mock_RESTful_Controller extends REST_Controller { + public $method_called; + public $resource; + + public function __construct($type) { + $this->resource_type = $type; + parent::__construct(); + } + + public function _index() { + $this->method_called = "index"; + } + + public function _create($resource) { + $this->method_called = "create"; + $this->resource = $resource; + } + + public function _show($resource) { + $this->method_called = "show"; + $this->resource = $resource; + } + + public function _update($resource) { + $this->method_called = "update"; + $this->resource = $resource; + } + + public function _delete($resource) { + $this->method_called = "delete"; + $this->resource = $resource; + } + + public function _form_add($args) { + $this->method_called = "form_add"; + $this->resource = $args; + } + + public function _form_edit($resource) { + $this->method_called = "form_edit"; + $this->resource = $resource; + } +} + +class Mock_Model { + public $loaded = true; +} + +class Mock_Not_Loaded_Model { + public $loaded = false; +} -- cgit v1.2.3