xref: /dokuwiki/_test/core/TestRequest.php (revision 4d053d04448a73826574eeb5e6be7e4d53c38ae6)
1f8369d7dSTobias Sarnowski<?php
2f8369d7dSTobias Sarnowski/**
3f8369d7dSTobias Sarnowski * Simulates a full DokuWiki HTTP Request and allows
4f8369d7dSTobias Sarnowski * runtime inspection.
5f8369d7dSTobias Sarnowski */
6f8369d7dSTobias Sarnowski
7f8369d7dSTobias Sarnowski// output buffering
8f8369d7dSTobias Sarnowski$output_buffer = '';
9f8369d7dSTobias Sarnowski
10f8369d7dSTobias Sarnowskifunction ob_start_callback($buffer) {
11f8369d7dSTobias Sarnowski    global $output_buffer;
12f8369d7dSTobias Sarnowski    $output_buffer .= $buffer;
13f8369d7dSTobias Sarnowski}
14f8369d7dSTobias Sarnowski
15f8369d7dSTobias Sarnowski
16f8369d7dSTobias Sarnowski/**
17f8369d7dSTobias Sarnowski * Helper class to execute a fake request
18f8369d7dSTobias Sarnowski */
19f8369d7dSTobias Sarnowskiclass TestRequest {
20f8369d7dSTobias Sarnowski
21f8369d7dSTobias Sarnowski    private $server = array();
22f8369d7dSTobias Sarnowski    private $session = array();
23f8369d7dSTobias Sarnowski    private $get = array();
24f8369d7dSTobias Sarnowski    private $post = array();
25f8369d7dSTobias Sarnowski
26f8369d7dSTobias Sarnowski    public function getServer($key) { return $this->server[$key]; }
27f8369d7dSTobias Sarnowski    public function getSession($key) { return $this->session[$key]; }
28f8369d7dSTobias Sarnowski    public function getGet($key) { return $this->get[$key]; }
29f8369d7dSTobias Sarnowski    public function getPost($key) { return $this->post[$key]; }
30f8369d7dSTobias Sarnowski
31f8369d7dSTobias Sarnowski    public function setServer($key, $value) { $this->server[$key] = $value; }
32f8369d7dSTobias Sarnowski    public function setSession($key, $value) { $this->session[$key] = $value; }
33f8369d7dSTobias Sarnowski    public function setGet($key, $value) { $this->get[$key] = $value; }
34f8369d7dSTobias Sarnowski    public function setPost($key, $value) { $this->post[$key] = $value; }
35f8369d7dSTobias Sarnowski
36f8369d7dSTobias Sarnowski    /**
37f8369d7dSTobias Sarnowski     * Executes the request
38f8369d7dSTobias Sarnowski     *
39*4d053d04SAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
40f8369d7dSTobias Sarnowski     * @return TestResponse the resulting output of the request
41f8369d7dSTobias Sarnowski     */
42*4d053d04SAndreas Gohr    public function execute($uri='/doku.php') {
43f8369d7dSTobias Sarnowski        // save old environment
44f8369d7dSTobias Sarnowski        $server = $_SERVER;
45f8369d7dSTobias Sarnowski        $session = $_SESSION;
46f8369d7dSTobias Sarnowski        $get = $_GET;
47f8369d7dSTobias Sarnowski        $post = $_POST;
48f8369d7dSTobias Sarnowski        $request = $_REQUEST;
49f8369d7dSTobias Sarnowski
50*4d053d04SAndreas Gohr        // prepare the right URI
51*4d053d04SAndreas Gohr        $this->setUri($uri);
52*4d053d04SAndreas Gohr
530189bd86SAndreas Gohr        // import all defined globals into the function scope
540189bd86SAndreas Gohr        foreach(array_keys($GLOBALS) as $glb){
550189bd86SAndreas Gohr            global $$glb;
560189bd86SAndreas Gohr        }
570189bd86SAndreas Gohr
58f8369d7dSTobias Sarnowski        // fake environment
59f8369d7dSTobias Sarnowski        global $default_server_vars;
60f8369d7dSTobias Sarnowski        $_SERVER = array_merge($default_server_vars, $this->server);
61f8369d7dSTobias Sarnowski        $_SESSION = $this->session;
62f8369d7dSTobias Sarnowski        $_GET = $this->get;
63f8369d7dSTobias Sarnowski        $_POST = $this->post;
64f8369d7dSTobias Sarnowski        $_REQUEST = array_merge($_GET, $_POST);
65f8369d7dSTobias Sarnowski
66f8369d7dSTobias Sarnowski        // reset output buffer
67f8369d7dSTobias Sarnowski        global $output_buffer;
68f8369d7dSTobias Sarnowski        $output_buffer = '';
69f8369d7dSTobias Sarnowski
70f8369d7dSTobias Sarnowski        // now execute dokuwiki and grep the output
71f8369d7dSTobias Sarnowski        header_remove();
72f8369d7dSTobias Sarnowski        ob_start('ob_start_callback');
73f8369d7dSTobias Sarnowski        include(DOKU_INC.'doku.php');
74f8369d7dSTobias Sarnowski        ob_end_flush();
75f8369d7dSTobias Sarnowski
76f8369d7dSTobias Sarnowski        // create the response object
77f8369d7dSTobias Sarnowski        $response = new TestResponse(
78f8369d7dSTobias Sarnowski            $output_buffer,
79f8369d7dSTobias Sarnowski            headers_list()
80f8369d7dSTobias Sarnowski        );
81f8369d7dSTobias Sarnowski
82f8369d7dSTobias Sarnowski        // reset environment
83f8369d7dSTobias Sarnowski        $_SERVER = $server;
84f8369d7dSTobias Sarnowski        $_SESSION = $session;
85f8369d7dSTobias Sarnowski        $_GET = $get;
86f8369d7dSTobias Sarnowski        $_POST = $post;
87f8369d7dSTobias Sarnowski        $_REQUEST = $request;
88f8369d7dSTobias Sarnowski
89f8369d7dSTobias Sarnowski        return $response;
90f8369d7dSTobias Sarnowski    }
919e777ceeSAndreas Gohr
929e777ceeSAndreas Gohr    /**
939e777ceeSAndreas Gohr     * Set the virtual URI the request works against
949e777ceeSAndreas Gohr     *
959e777ceeSAndreas Gohr     * This parses the given URI and sets any contained GET variables
969e777ceeSAndreas Gohr     * but will not overwrite any previously set ones (eg. set via setGet()).
979e777ceeSAndreas Gohr     *
989e777ceeSAndreas Gohr     * It initializes the $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING']
999e777ceeSAndreas Gohr     * with all set GET variables.
1009e777ceeSAndreas Gohr     *
1019e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
102*4d053d04SAndreas Gohr     * @todo make this work with other end points
1039e777ceeSAndreas Gohr     */
104*4d053d04SAndreas Gohr    protected function setUri($uri){
1059e777ceeSAndreas Gohr        if(substr($uri,0,9) != '/doku.php'){
1069e777ceeSAndreas Gohr            throw new Exception("only '/doku.php' is supported currently");
1079e777ceeSAndreas Gohr        }
1089e777ceeSAndreas Gohr
1099e777ceeSAndreas Gohr        $params = array();
1109e777ceeSAndreas Gohr        list($uri, $query) = explode('?',$uri,2);
1119e777ceeSAndreas Gohr        if($query) parse_str($query, $params);
1129e777ceeSAndreas Gohr
1139e777ceeSAndreas Gohr        $this->get  = array_merge($params, $this->get);
1149e777ceeSAndreas Gohr        if(count($this->get)){
1159e777ceeSAndreas Gohr            $query = '?'.http_build_query($this->get, '', '&');
1169e777ceeSAndreas Gohr            $query = str_replace(
1179e777ceeSAndreas Gohr                array('%3A', '%5B', '%5D'),
1189e777ceeSAndreas Gohr                array(':', '[', ']'),
1199e777ceeSAndreas Gohr                $query
1209e777ceeSAndreas Gohr            );
1219e777ceeSAndreas Gohr            $uri = $uri.$query;
1229e777ceeSAndreas Gohr        }
1239e777ceeSAndreas Gohr
1249e777ceeSAndreas Gohr        $this->setServer('QUERY_STRING', $query);
1259e777ceeSAndreas Gohr        $this->setServer('REQUEST_URI', $uri);
1269e777ceeSAndreas Gohr    }
1279e777ceeSAndreas Gohr
1289e777ceeSAndreas Gohr    /**
1299e777ceeSAndreas Gohr     * Simulate a POST request with the given variables
1309e777ceeSAndreas Gohr     *
1319e777ceeSAndreas Gohr     * @param array $post  all the POST parameters to use
1329e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
1339e777ceeSAndreas Gohr     * @param return TestResponse
1349e777ceeSAndreas Gohr     */
1359e777ceeSAndreas Gohr    public function post($post=array(), $uri='/doku.php') {
1369e777ceeSAndreas Gohr        $this->post = array_merge($this->post, $post);
1379e777ceeSAndreas Gohr        $this->setServer('REQUEST_METHOD', 'POST');
138*4d053d04SAndreas Gohr        return $this->execute($uri);
1399e777ceeSAndreas Gohr    }
1409e777ceeSAndreas Gohr
1419e777ceeSAndreas Gohr    /**
1429e777ceeSAndreas Gohr     * Simulate a GET request with the given variables
1439e777ceeSAndreas Gohr     *
1449e777ceeSAndreas Gohr     * @param array $GET  all the POST parameters to use
1459e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
1469e777ceeSAndreas Gohr     * @param return TestResponse
1479e777ceeSAndreas Gohr     */
1489e777ceeSAndreas Gohr    public function get($get=array(), $uri='/doku.php') {
1499e777ceeSAndreas Gohr        $this->get  = array_merge($this->get, $get);
1509e777ceeSAndreas Gohr        $this->setServer('REQUEST_METHOD', 'GET');
151*4d053d04SAndreas Gohr        return $this->execute($uri);
1529e777ceeSAndreas Gohr    }
1539e777ceeSAndreas Gohr
1549e777ceeSAndreas Gohr
155f8369d7dSTobias Sarnowski}
156