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