1<?php
2
3class common_ml_test extends DokuWikiTest {
4
5    private $script = 'lib/exe/fetch.php';
6
7    function test_ml_empty() {
8        global $conf;
9        $conf['useslash'] = 0;
10        $conf['userewrite'] = 0;
11        $conf['start'] = 'start';
12
13        $this->assertEquals(DOKU_BASE . $this->script . '?media=' , ml());
14    }
15
16    function test_ml_args_array() {
17        global $conf;
18        $conf['useslash'] = 0;
19        $conf['userewrite'] = 0;
20
21        $args = array('a' => 'b', 'c' => 'd', 'q' => '&ä');
22
23        $expect = DOKU_BASE . $this->script . '?a=b&amp;c=d&amp;q=%26%C3%A4&amp;media=some:img.jpg';
24        $this->assertEquals($expect, ml('some:img.jpg', $args));
25    }
26
27    function test_ml_args_string() {
28        global $conf;
29        $conf['useslash'] = 0;
30        $conf['userewrite'] = 0;
31
32        $args = 'a=b&c=d';
33
34        $expect = DOKU_BASE . $this->script . '?a=b&c=d&amp;media=some:img.png';
35        $this->assertEquals($expect, ml('some:img.png', $args));
36    }
37
38    function test_ml_args_comma_string() {
39        global $conf;
40        $conf['useslash'] = 0;
41        $conf['userewrite'] = 0;
42
43        $args = 'a=b,c=d';
44
45        $expect = DOKU_BASE . $this->script . '?a=b&amp;c=d&amp;media=some:img.gif';
46        $this->assertEquals($expect, ml('some:img.gif', $args));
47    }
48
49
50    function test_ml_imgresize_array() {
51        global $conf;
52        $conf['useslash'] = 0;
53        $conf['userewrite'] = 0;
54
55        $id = 'some:img.png';
56        $w = 80;
57        $args = array('w' => $w);
58        $tok = media_get_token($id,$w,0);
59
60        $expect = DOKU_BASE . $this->script . '?w='.$w.'&amp;tok='.$tok.'&amp;media='.$id;
61        $this->assertEquals($expect, ml($id, $args));
62    }
63
64    function test_ml_imgresize_string() {
65        global $conf;
66        $conf['useslash'] = 0;
67        $conf['userewrite'] = 0;
68
69        $id = 'some:img.png';
70        $w = 80;
71        $args = 'w='.$w;
72        $tok = media_get_token($id,$w,0);
73
74        $expect = DOKU_BASE . $this->script . '?w='.$w.'&amp;tok='.$tok.'&amp;media='.$id;
75        $this->assertEquals($expect, ml($id, $args));
76    }
77
78    function test_ml_imgresize_array_rootid() {
79        global $conf;
80        $conf['useslash']   = 0;
81        $conf['userewrite'] = 0;
82
83        $id      = ':wiki:dokuwiki-128.png';
84        $cleanid = 'wiki:dokuwiki-128.png';
85        $w       = 80;
86        $args    = array('w' => $w);
87        $tok     = media_get_token($cleanid, $w, 0);
88
89        $expect = DOKU_BASE.$this->script.'?w='.$w.'&amp;tok='.$tok.'&amp;media='.$cleanid;
90        $this->assertEquals($expect, ml($id, $args));
91    }
92
93    function test_ml_img_external() {
94        global $conf;
95        $conf['useslash']   = 0;
96        $conf['userewrite'] = 0;
97
98        $ids  = array(
99            'https://example.com/lib/tpl/dokuwiki/images/logo.png',
100            'http://example.com/lib/tpl/dokuwiki/images/logo.png',
101            'ftp://example.com/lib/tpl/dokuwiki/images/logo.png'
102        );
103
104        foreach($ids as $id) {
105            $tok = media_get_token($id, 0, 0);
106
107            $expect = DOKU_BASE.$this->script.'?tok='.$tok.'&amp;media='.rawurlencode($id);
108            $this->assertEquals($expect, ml($id));
109        }
110    }
111
112    function test_ml_imgresize_array_external() {
113        global $conf;
114        $conf['useslash']   = 0;
115        $conf['userewrite'] = 0;
116
117        $ids  = array(
118            'https://example.com/lib/tpl/dokuwiki/images/logo.png',
119            'http://example.com/lib/tpl/dokuwiki/images/logo.png',
120            'ftp://example.com/lib/tpl/dokuwiki/images/logo.png'
121        );
122        $w    = 80;
123        $args = array('w' => $w);
124
125        foreach($ids as $id) {
126            $tok = media_get_token($id, $w, 0);
127            $hash = substr(\dokuwiki\PassHash::hmac('md5', $id, auth_cookiesalt()), 0, 6);
128
129            $expect = DOKU_BASE.$this->script.'?w='.$w.'&amp;tok='.$tok.'&amp;media='.rawurlencode($id);
130            $this->assertEquals($expect, ml($id, $args));
131        }
132
133        $h    = 50;
134        $args = array('h' => $h);
135        $tok = media_get_token($id, $h, 0);
136
137        $expect = DOKU_BASE.$this->script.'?h='.$h.'&amp;tok='.$tok.'&amp;media='.rawurlencode($id);
138        $this->assertEquals($expect, ml($id, $args));
139
140        $w    = 80;
141        $h    = 50;
142        $args = array('w' => $w, 'h' => $h);
143        $tok = media_get_token($id, $w, $h);
144
145        $expect = DOKU_BASE.$this->script.'?w='.$w.'&amp;h='.$h.'&amp;tok='.$tok.'&amp;media='.rawurlencode($id);
146        $this->assertEquals($expect, ml($id, $args));
147
148    }
149
150    function test_ml_empty_rev() {
151        global $conf;
152        $conf['useslash'] = 0;
153        $conf['userewrite'] = 0;
154
155        $args = array('a' => 'b', 'c' => 'd', 'rev' => '');
156
157        $expect = DOKU_BASE . $this->script . '?a=b&amp;c=d&amp;media=some:img.jpg';
158        $this->assertEquals($expect, ml('some:img.jpg', $args));
159    }
160}
161