/** * @covers ::fromError */ function test_fromError() { $error = new WP_Error(); $exception = \Cassava\Exception\GeneralException::fromError($error); $this->assertEquals($error->get_error_message(), $exception->getMessage(), "Exception generated from WP_Error has the same default message."); $this->assertEquals($error->get_error_code(), $exception->getCASCode(), "Exception generated from WP_Error has the same default code."); $message = 'Test message.'; $code = 'TEST_CODE'; $error = new WP_Error($code, $message); $exception = \Cassava\Exception\GeneralException::fromError($error); $this->assertEquals($error->get_error_message(), $exception->getMessage(), "Exception generated from WP_Error has the same '{$message}' message."); $this->assertEquals($error->get_error_code(), $exception->getCASCode(), "Exception generated from WP_Error has the same '{$code}' code."); }
/** * Dispatch the request for processing by the relevant callback as determined by the routes * list returned by `Server::routes()`. * * @param string $path Requested URI path. * @return mixed Service response string or WordPress error. * * @throws \Cassava\Exception\GeneralException * @throws \Cassava\Exception\RequestException * * @global $_GET * * @uses \apply_filters() * @uses \is_ssl() * @uses \is_wp_error() */ protected function dispatch($path) { if (!\is_ssl()) { throw new GeneralException(__('The CAS server requires SSL.', 'wp-cas-server')); } /** * Allows developers to disable CAS. * * @param boolean $cas_enabled Whether the server should respond to single sign-on requests. */ $enabled = apply_filters('cas_enabled', true); if (!$enabled) { throw new GeneralException(__('The CAS server is disabled.', 'wp-cas-server')); } $routes = $this->routes(); foreach ($routes as $route => $controller) { $match = preg_match('@^' . preg_quote($route) . '/?$@', $path); if (!$match) { continue; } if (!class_exists($controller)) { throw new GeneralException(__('The controller for the route is invalid.', 'wp-cas-server')); } $args = $_GET; /** * Filters the controller arguments to be dispatched for the request. * * Plugin developers may return a WP_Error object via the * `cas_server_dispatch_args` filter to abort the request. Avoid throwing * a `\Cassava\Exception\GeneralException` exception here because that * would interrupt the filter controller chain. * * @param array $args Arguments to pass the controller. * @param mixed $controller Controller class. * @param string $path Requested URI path. * * @return mixed Arguments to pass the controller, or `WP_Error`. */ $args = \apply_filters('cas_server_dispatch_args', $args, $controller, $path); if (\is_wp_error($args)) { throw GeneralException::fromError($args); } $controllerInstance = new $controller($this); return $controllerInstance->handleRequest($args); } throw new RequestException(__('The server does not support the method requested.', 'wp-cas-server')); }