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