xref: /dokuwiki/_test/core/TestRequest.php (revision 9e777cee116bd29c51b62ccd1c4353cc00014522)
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     *
39f8369d7dSTobias Sarnowski     * @return TestResponse the resulting output of the request
40f8369d7dSTobias Sarnowski     */
41f8369d7dSTobias Sarnowski    public function execute() {
42f8369d7dSTobias Sarnowski        // save old environment
43f8369d7dSTobias Sarnowski        $server = $_SERVER;
44f8369d7dSTobias Sarnowski        $session = $_SESSION;
45f8369d7dSTobias Sarnowski        $get = $_GET;
46f8369d7dSTobias Sarnowski        $post = $_POST;
47f8369d7dSTobias Sarnowski        $request = $_REQUEST;
48f8369d7dSTobias Sarnowski
490189bd86SAndreas Gohr        // import all defined globals into the function scope
500189bd86SAndreas Gohr        foreach(array_keys($GLOBALS) as $glb){
510189bd86SAndreas Gohr            global $$glb;
520189bd86SAndreas Gohr        }
530189bd86SAndreas Gohr
54f8369d7dSTobias Sarnowski        // fake environment
55f8369d7dSTobias Sarnowski        global $default_server_vars;
56f8369d7dSTobias Sarnowski        $_SERVER = array_merge($default_server_vars, $this->server);
57f8369d7dSTobias Sarnowski        $_SESSION = $this->session;
58f8369d7dSTobias Sarnowski        $_GET = $this->get;
59f8369d7dSTobias Sarnowski        $_POST = $this->post;
60f8369d7dSTobias Sarnowski        $_REQUEST = array_merge($_GET, $_POST);
61f8369d7dSTobias Sarnowski
62f8369d7dSTobias Sarnowski        // reset output buffer
63f8369d7dSTobias Sarnowski        global $output_buffer;
64f8369d7dSTobias Sarnowski        $output_buffer = '';
65f8369d7dSTobias Sarnowski
66f8369d7dSTobias Sarnowski        // now execute dokuwiki and grep the output
67f8369d7dSTobias Sarnowski        header_remove();
68f8369d7dSTobias Sarnowski        ob_start('ob_start_callback');
69f8369d7dSTobias Sarnowski        include(DOKU_INC.'doku.php');
70f8369d7dSTobias Sarnowski        ob_end_flush();
71f8369d7dSTobias Sarnowski
72f8369d7dSTobias Sarnowski        // create the response object
73f8369d7dSTobias Sarnowski        $response = new TestResponse(
74f8369d7dSTobias Sarnowski            $output_buffer,
75f8369d7dSTobias Sarnowski            headers_list()
76f8369d7dSTobias Sarnowski        );
77f8369d7dSTobias Sarnowski
78f8369d7dSTobias Sarnowski        // reset environment
79f8369d7dSTobias Sarnowski        $_SERVER = $server;
80f8369d7dSTobias Sarnowski        $_SESSION = $session;
81f8369d7dSTobias Sarnowski        $_GET = $get;
82f8369d7dSTobias Sarnowski        $_POST = $post;
83f8369d7dSTobias Sarnowski        $_REQUEST = $request;
84f8369d7dSTobias Sarnowski
85f8369d7dSTobias Sarnowski        return $response;
86f8369d7dSTobias Sarnowski    }
87*9e777ceeSAndreas Gohr
88*9e777ceeSAndreas Gohr    /**
89*9e777ceeSAndreas Gohr     * Set the virtual URI the request works against
90*9e777ceeSAndreas Gohr     *
91*9e777ceeSAndreas Gohr     * This parses the given URI and sets any contained GET variables
92*9e777ceeSAndreas Gohr     * but will not overwrite any previously set ones (eg. set via setGet()).
93*9e777ceeSAndreas Gohr     *
94*9e777ceeSAndreas Gohr     * It initializes the $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING']
95*9e777ceeSAndreas Gohr     * with all set GET variables.
96*9e777ceeSAndreas Gohr     *
97*9e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
98*9e777ceeSAndreas Gohr     */
99*9e777ceeSAndreas Gohr    public function setUri($uri){
100*9e777ceeSAndreas Gohr        if(substr($uri,0,9) != '/doku.php'){
101*9e777ceeSAndreas Gohr            throw new Exception("only '/doku.php' is supported currently");
102*9e777ceeSAndreas Gohr        }
103*9e777ceeSAndreas Gohr
104*9e777ceeSAndreas Gohr        $params = array();
105*9e777ceeSAndreas Gohr        list($uri, $query) = explode('?',$uri,2);
106*9e777ceeSAndreas Gohr        if($query) parse_str($query, $params);
107*9e777ceeSAndreas Gohr
108*9e777ceeSAndreas Gohr        $this->get  = array_merge($params, $this->get);
109*9e777ceeSAndreas Gohr        if(count($this->get)){
110*9e777ceeSAndreas Gohr            $query = '?'.http_build_query($this->get, '', '&');
111*9e777ceeSAndreas Gohr            $query = str_replace(
112*9e777ceeSAndreas Gohr                array('%3A', '%5B', '%5D'),
113*9e777ceeSAndreas Gohr                array(':', '[', ']'),
114*9e777ceeSAndreas Gohr                $query
115*9e777ceeSAndreas Gohr            );
116*9e777ceeSAndreas Gohr            $uri = $uri.$query;
117*9e777ceeSAndreas Gohr        }
118*9e777ceeSAndreas Gohr
119*9e777ceeSAndreas Gohr        $this->setServer('QUERY_STRING', $query);
120*9e777ceeSAndreas Gohr        $this->setServer('REQUEST_URI', $uri);
121*9e777ceeSAndreas Gohr    }
122*9e777ceeSAndreas Gohr
123*9e777ceeSAndreas Gohr    /**
124*9e777ceeSAndreas Gohr     * Simulate a POST request with the given variables
125*9e777ceeSAndreas Gohr     *
126*9e777ceeSAndreas Gohr     * @param array $post  all the POST parameters to use
127*9e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
128*9e777ceeSAndreas Gohr     * @param return TestResponse
129*9e777ceeSAndreas Gohr     */
130*9e777ceeSAndreas Gohr    public function post($post=array(), $uri='/doku.php') {
131*9e777ceeSAndreas Gohr        $this->setUri($uri);
132*9e777ceeSAndreas Gohr        $this->post = array_merge($this->post, $post);
133*9e777ceeSAndreas Gohr        $this->setServer('REQUEST_METHOD', 'POST');
134*9e777ceeSAndreas Gohr        return $this->execute();
135*9e777ceeSAndreas Gohr    }
136*9e777ceeSAndreas Gohr
137*9e777ceeSAndreas Gohr    /**
138*9e777ceeSAndreas Gohr     * Simulate a GET request with the given variables
139*9e777ceeSAndreas Gohr     *
140*9e777ceeSAndreas Gohr     * @param array $GET  all the POST parameters to use
141*9e777ceeSAndreas Gohr     * @param string $url  end URL to simulate, needs to start with /doku.php currently
142*9e777ceeSAndreas Gohr     * @param return TestResponse
143*9e777ceeSAndreas Gohr     */
144*9e777ceeSAndreas Gohr    public function get($get=array(), $uri='/doku.php') {
145*9e777ceeSAndreas Gohr        $this->get  = array_merge($this->get, $get);
146*9e777ceeSAndreas Gohr        $this->setUri($uri);
147*9e777ceeSAndreas Gohr        $this->setServer('REQUEST_METHOD', 'GET');
148*9e777ceeSAndreas Gohr        return $this->execute();
149*9e777ceeSAndreas Gohr    }
150*9e777ceeSAndreas Gohr
151*9e777ceeSAndreas Gohr
152f8369d7dSTobias Sarnowski}
153