summaryrefslogtreecommitdiff
path: root/_test/core/TestRequest.php
blob: dad2060e5cafb7f05e4db672774462a1030fd862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
 * Simulates a full DokuWiki HTTP Request and allows
 * runtime inspection.
 */

// output buffering
$output_buffer = '';

function ob_start_callback($buffer) {
    global $output_buffer;
    $output_buffer .= $buffer;
}


/**
 * Helper class to execute a fake request
 */
class TestRequest {

    private $valid_scripts = array('/doku.php', '/lib/exe/fetch.php', '/lib/exe/detail.php');
    private $script;

    private $server = array();
    private $session = array();
    private $get = array();
    private $post = array();

    public function getServer($key) { return $this->server[$key]; }
    public function getSession($key) { return $this->session[$key]; }
    public function getGet($key) { return $this->get[$key]; }
    public function getPost($key) { return $this->post[$key]; }
    public function getScript() { return $this->script; }

    public function setServer($key, $value) { $this->server[$key] = $value; }
    public function setSession($key, $value) { $this->session[$key] = $value; }
    public function setGet($key, $value) { $this->get[$key] = $value; }
    public function setPost($key, $value) { $this->post[$key] = $value; }

    /**
     * Executes the request
     *
     * @param string $url  end URL to simulate, needs to start with /doku.php currently
     * @return TestResponse the resulting output of the request
     */
    public function execute($uri='/doku.php') {
        global $INPUT;

        // save old environment
        $server = $_SERVER;
        $session = $_SESSION;
        $get = $_GET;
        $post = $_POST;
        $request = $_REQUEST;
        $input = $INPUT;
        
        // prepare the right URI
        $this->setUri($uri);

        // import all defined globals into the function scope
        foreach(array_keys($GLOBALS) as $glb){
            global $$glb;
        }

        // fake environment
        global $default_server_vars;
        $_SERVER = array_merge($default_server_vars, $this->server);
        $_SESSION = $this->session;
        $_GET = $this->get;
        $_POST = $this->post;
        $_REQUEST = array_merge($_GET, $_POST);

        // reset output buffer
        global $output_buffer;
        $output_buffer = '';

        // now execute dokuwiki and grep the output
        header_remove();
        ob_start('ob_start_callback');
        $INPUT = new Input();
        include(DOKU_INC.$this->script);
        ob_end_flush();

        // create the response object
        $response = new TestResponse(
            $output_buffer,
            (function_exists('xdebug_get_headers') ? xdebug_get_headers() : headers_list())   // cli sapi doesn't do headers, prefer xdebug_get_headers() which works under cli
        );

        // reset environment
        $_SERVER = $server;
        $_SESSION = $session;
        $_GET = $get;
        $_POST = $post;
        $_REQUEST = $request;
        $INPUT = $input;

        return $response;
    }

    /**
     * Set the virtual URI the request works against
     *
     * This parses the given URI and sets any contained GET variables
     * but will not overwrite any previously set ones (eg. set via setGet()).
     *
     * It initializes the $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING']
     * with all set GET variables.
     *
     * @param string $url  end URL to simulate, needs to start with /doku.php currently
     * @todo make this work with other end points
     */
    protected function setUri($uri){
        if(!preg_match('#^('.join('|',$this->valid_scripts).')#',$uri)){
            throw new Exception("$uri \n--- only ".join(', ',$this->valid_scripts)." are supported currently");
        }

        $params = array();
        list($uri, $query) = explode('?',$uri,2);
        if($query) parse_str($query, $params);

        $this->script = substr($uri,1);
        $this->get  = array_merge($params, $this->get);
        if(count($this->get)){
            $query = '?'.http_build_query($this->get, '', '&');
            $query = str_replace(
                array('%3A', '%5B', '%5D'),
                array(':', '[', ']'),
                $query
            );
            $uri = $uri.$query;
        }

        $this->setServer('QUERY_STRING', $query);
        $this->setServer('REQUEST_URI', $uri);
    }

    /**
     * Simulate a POST request with the given variables
     *
     * @param array $post  all the POST parameters to use
     * @param string $url  end URL to simulate, needs to start with /doku.php, /lib/exe/fetch.php or /lib/exe/detail.php currently
     * @param return TestResponse
     */
    public function post($post=array(), $uri='/doku.php') {
        $this->post = array_merge($this->post, $post);
        $this->setServer('REQUEST_METHOD', 'POST');
        return $this->execute($uri);
    }

    /**
     * Simulate a GET request with the given variables
     *
     * @param array $GET   all the GET parameters to use
     * @param string $url  end URL to simulate, needs to start with /doku.php, /lib/exe/fetch.php or /lib/exe/detail.php currently
     * @param return TestResponse
     */
    public function get($get=array(), $uri='/doku.php') {
        $this->get  = array_merge($this->get, $get);
        $this->setServer('REQUEST_METHOD', 'GET');
        return $this->execute($uri);
    }


}