xref: /dokuwiki/_test/tests/lib/exe/fetch_imagetoken.test.php (revision 123bc813fd93ab5d8dab3cc4a66a09e613a10aa2)
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        // check we can carry out these tests
11        if (!file_exists(mediaFN($this->media))) {
12            $this->markTestSkipped('Source image required for test');
13        }
14
15        header('X-Test: check headers working');
16        $header_check = function_exists('xdebug_get_headers') ? xdebug_get_headers() : headers_list();
17        if (empty($header_check)) {
18            $this->markTestSkipped('headers not returned, perhaps your sapi does not return headers, try xdebug');
19        } else {
20            header_remove('X-Test');
21        }
22
23        parent::setUp();
24
25        global $conf;
26        $conf['xsendfile'] = 0;
27
28        global $MIME, $EXT, $CACHE, $INPUT;    // variables fetch creates in global scope -- should this be in fetch?
29    }
30
31    function getUri() {
32       $w = $this->width ? 'w='.$this->width.'&' : '';
33       $h = $this->height ? 'h='.$this->height.'&' : '';
34
35       return '/lib/exe/fetch.php?'.$w.$h.'{%token%}media='.$this->media;
36    }
37
38    function fetchResponse($token){
39        $request = new TestRequest();
40        return $request->get(array(),str_replace('{%token%}',$token,$this->getUri()));
41    }
42
43    /**
44     *  modified image request with valid token
45     *  expect: header with mime-type
46     *  expect: content
47     *  expect: no error response
48     */
49    function test_valid_token(){
50        $valid_token = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
51        $response = $this->fetchResponse($valid_token);
52        $this->assertTrue((bool)$response->getHeader('Content-Type'));
53        $this->assertTrue((bool)($response->getContent()));
54
55        $status_code = $response->getStatusCode();
56        $this->assertTrue(is_null($status_code) || (200 == $status_code));
57    }
58
59    /**
60     *  modified image request with invalid token
61     *  expect: 412 status code
62     */
63    function test_invalid_token(){
64        $invalid_token = 'tok='.media_get_token('junk',200,100).'&';
65        $this->assertEquals(412,$this->fetchResponse($invalid_token)->getStatusCode());
66    }
67
68    /**
69     *  modified image request with no token
70     *  expect: 412 status code
71     */
72    function test_missing_token(){
73        $no_token = '';
74        $this->assertEquals(412,$this->fetchResponse($no_token)->getStatusCode());
75    }
76
77    /**
78     *  native image request which doesn't require a token
79     *  try: with a token & without a token
80     *  expect: (for both) header with mime-type, content matching source image filesize & no error response
81     */
82    function test_no_token_required(){
83        $this->width = $this->height = 0;   // no width & height, means image request at native dimensions
84        $any_token = 'tok='.media_get_token('junk',200,100).'&';
85        $no_token = '';
86        $bytes = filesize(mediaFN($this->media));
87
88        foreach(array($any_token, $no_token) as $token) {
89            $response = $this->fetchResponse($token);
90            $this->assertTrue((bool)$response->getHeader('Content-Type'));
91            $this->assertEquals(strlen($response->getContent()), $bytes);
92
93            $status_code = $response->getStatusCode();
94            $this->assertTrue(is_null($status_code) || (200 == $status_code));
95        }
96    }
97
98}
99//Setup VIM: ex: et ts=4 :
100