xref: /dokuwiki/_test/tests/lib/exe/fetch_imagetoken.test.php (revision f56bb251b5bdb3bb4f2793e921cf479d2402a47c)
1<?php
2
3class fetch_imagetoken_test extends DokuWikiTest {
4
5    private $media = 'wiki:dokuwiki-128.png';
6    private $width = 200;
7    private $height = 0;
8
9    function setUp() {
10        parent::setUp();
11
12        global $conf;
13        $conf['sendfile'] = 0;
14
15        global $MIME, $EXT, $CACHE, $INPUT;    // variables fetch creates in global scope -- should this be in fetch?
16    }
17
18    function getUri() {
19       $w = $this->width ? 'w='.$this->width.'&' : '';
20       $h = $this->height ? 'h='.$this->height.'&' : '';
21
22       return '/lib/exe/fetch.php?'.$w.$h.'{%token%}media='.$this->media;
23    }
24
25    function fetchResponse($token){
26        $request = new TestRequest();
27        return $request->get(array(),str_replace('{%token%}',$token,$this->getUri()));
28    }
29
30    /**
31     *  modified image request with valid token
32     *  expect: header with mime-type
33     *  expect: content
34     *  expect: no error response
35     */
36    function test_valid_token(){
37        $valid_token = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
38        $response = $this->fetchResponse($valid_token);
39        $this->assertTrue((bool)$response->getHeader('Content-Type'));
40        $this->assertTrue((bool)($response->getContent()));
41
42        $status_code = $response->getStatusCode();
43        $this->assertTrue(is_null($status_code) || (200 == $status_code));
44    }
45
46    /**
47     *  modified image request with invalid token
48     *  expect: 412 status code
49     */
50    function test_invalid_token(){
51        $invalid_token = 'tok='.media_get_token('junk',200,100).'&';
52        $this->assertEquals(412,$this->fetchResponse($invalid_token)->getStatusCode());
53    }
54
55    /**
56     *  modified image request with no token
57     *  expect: 412 status code
58     */
59    function test_missing_token(){
60        $no_token = '';
61        $this->assertEquals(412,$this->fetchResponse($notoken)->getStatusCode());
62    }
63
64    /**
65     *  native image request which doesn't require a token
66     */
67    function test_no_token_required(){
68        $this->width = $this->height = 0;   // no width & height, means image request at native dimensions
69        $any_token = 'tok='.media_get_token('junk',200,100).'&';
70        $no_token = '';
71
72        foreach(array($any_token, $no_token) as $token) {
73            $response = $this->fetchResponse($token);
74            $this->assertTrue((bool)$response->getHeader('Content-Type'));
75            $this->assertTrue((bool)($response->getContent()));
76
77            $status_code = $response->getStatusCode();
78            $this->assertTrue(is_null($status_code) || (200 == $status_code));
79        }
80    }
81
82}
83//Setup VIM: ex: et ts=4 :
84