1<?php
2/**
3 * Unit Test for inc/common.php - pageinfo()
4 *
5 * @author Christopher Smith <chris@jalakai.co.uk>
6 */
7class common_pageinfo_test extends DokuWikiTest {
8
9    function setup() : void {
10        parent::setup();
11
12        global $USERINFO;
13        $USERINFO = array(
14           'pass' => '179ad45c6ce2cb97cf1029e212046e81',
15           'name' => 'Arthur Dent',
16           'mail' => 'arthur@example.com',
17           'grps' => array ('admin','user'),
18        );
19        $_SERVER['REMOTE_USER'] = 'testuser';
20        $_SERVER['REMOTE_ADDR'] = '1.2.3.4';
21    }
22
23    function _get_expected_pageinfo() {
24        global $USERINFO;
25        $info = array (
26          'isadmin' => true,
27          'ismanager' => true,
28          'userinfo' => $USERINFO,
29          'perm' => 255,
30          'namespace' => false,
31          'ismobile' => false,
32          'client' => 'testuser',
33        );
34        $info['rev'] = null;
35        $info['subscribed'] = false;
36        $info['locked'] = false;
37        $info['exists'] = false;
38        $info['writable'] = true;
39        $info['editable'] = true;
40        $info['lastmod'] = false;
41        $info['currentrev'] = false;
42        $info['meta'] = array();
43        $info['ip'] = null;
44        $info['user'] = null;
45        $info['sum'] = null;
46        $info['editor'] = null;
47
48        return $info;
49    }
50
51    /**
52     *  check info keys and values for a non-existent page & admin user
53     */
54    function test_basic_nonexistentpage() {
55        global $ID,$conf;
56        $ID = 'wiki:start';
57
58        $info = $this->_get_expected_pageinfo();
59        $info['id'] = 'wiki:start';
60        $info['namespace'] = 'wiki';
61        $info['filepath'] = $conf['datadir'].'/wiki/start.txt';
62
63        $this->assertEquals($info, pageinfo());
64    }
65
66    /**
67     *  check info keys and values for a existing page & admin user
68     */
69    function test_basic_existingpage() {
70        global $ID,$conf;
71        $ID = 'wiki:syntax';
72        $filename = $conf['datadir'].'/wiki/syntax.txt';
73        $rev = filemtime($filename);
74
75        // run once to prepare meta/wiki/syntax.change file for existing page
76        // because pageinfo() set $info['meta']['last_change'] entry
77        pageinfo();
78
79        $info = $this->_get_expected_pageinfo();
80        $info['id'] = 'wiki:syntax';
81        $info['namespace'] = 'wiki';
82        $info['filepath'] = $filename;
83        $info['exists'] = true;
84        $info['lastmod'] = $rev;
85        $info['currentrev'] = $rev;
86        $info['meta'] = p_get_metadata($ID);
87        // set from revinfo, $pagelog->getRevisionInfo($info['lastmod'])
88        $info = array_merge($info, array(
89            'ip' => '127.0.0.1',
90            'user' => '',
91            'sum' => 'created - external edit',
92        ));
93        $info['editor'] = '127.0.0.1';
94
95        $this->assertEquals($info, pageinfo());
96    }
97
98    /**
99     *  check info keys and values for anonymous user
100     */
101    function test_anonymoususer() {
102        global $ID,$conf,$REV;
103
104        unset($_SERVER['REMOTE_USER']);
105        global $USERINFO; $USERINFO = array();
106
107        $ID = 'wiki:syntax';
108        $filename = $conf['datadir'].'/wiki/syntax.txt';
109        $rev = filemtime($filename);
110
111        $info = $this->_get_expected_pageinfo();
112        $info['id'] = 'wiki:syntax';
113        $info['namespace'] = 'wiki';
114        $info['filepath'] = $filename;
115        $info['exists'] = true;
116        $info['lastmod'] = $rev;
117        $info['currentrev'] = $rev;
118        $info['meta'] = p_get_metadata($ID);
119        $info['rev'] = '';
120        // set from revinfo, $pagelog->getRevisionInfo($info['lastmod'])
121        $info = array_merge($info, array(
122            'ip' => '127.0.0.1',
123            'user' => '',
124            'sum' => 'created - external edit',
125        ));
126        $info['editor'] = '127.0.0.1';
127
128        // anonymous user
129        $info = array_merge($info, array(
130          'isadmin' => false,
131          'ismanager' => false,
132          'perm' => 8,
133          'client' => '1.2.3.4',
134        ));
135        unset($info['userinfo']);
136
137        $this->assertEquals($info, pageinfo());
138    }
139
140    /**
141     *  check info keys and values with $REV
142     *  (also see $RANGE tests)
143     */
144    function test_rev() {
145        global $ID,$conf,$REV;
146
147        $ID = 'wiki:syntax';
148        $filename = $conf['datadir'].'/wiki/syntax.txt';
149        $rev = filemtime($filename);
150        $REV = $rev - 100;
151        $ext = '.txt';
152        if ($conf['compression']) {
153            //compression in $info['filepath'] determined by wikiFN depends also on if the page exist
154            $ext .= "." . $conf['compression']; //.gz or .bz2
155        }
156
157        $info = $this->_get_expected_pageinfo();
158        $info['id'] = 'wiki:syntax';
159        $info['namespace'] = 'wiki';
160        $info['meta'] = p_get_metadata($ID);
161        $info['rev'] = $REV;
162        $info['currentrev'] = $rev;
163        $info['filepath'] = str_replace('pages','attic',substr($filename,0,-3).$REV.$ext);
164
165        $this->assertEquals($info, pageinfo());
166        $this->assertEquals($rev-100, $REV);
167    }
168
169    /**
170     *  check info keys and values with $RANGE
171     */
172    function test_range() {
173        global $ID,$conf,$REV,$RANGE;
174
175        $ID = 'wiki:syntax';
176        $filename = $conf['datadir'].'/wiki/syntax.txt';
177        $rev = filemtime($filename);
178        $range = '1000-2000';
179
180        $info = $this->_get_expected_pageinfo();
181        $info['id'] = 'wiki:syntax';
182        $info['namespace'] = 'wiki';
183        $info['exists'] = true;
184        $info['lastmod'] = $rev;
185        $info['currentrev'] = $rev;
186        $info['meta'] = p_get_metadata($ID);
187        $info['filepath'] = $filename;
188        // set from revinfo, $pagelog->getRevisionInfo($info['lastmod'])
189        $info = array_merge($info, array(
190            'ip' => '127.0.0.1',
191            'user' => '',
192            'sum' => 'created - external edit',
193        ));
194        $info['editor'] = '127.0.0.1';
195
196        // check $RANGE without $REV
197        // expected result $RANGE unchanged
198        $RANGE = $range;
199
200        $this->assertEquals($info, pageinfo());
201        $this->assertFalse(isset($REV));
202        $this->assertEquals($range, $RANGE);
203
204        // check $RANGE with $REV = current
205        // expected result: $RANGE unchanged, $REV cleared
206        $REV = $rev;
207        $info['rev'] = '';
208
209        $this->assertEquals($info, pageinfo());
210        $this->assertEquals('',$REV);
211        $this->assertEquals($range, $RANGE);
212
213        // check with a real $REV
214        // expected result: $REV and $RANGE are cleared
215        $REV = $rev - 100;
216
217        $this->assertEquals($info, pageinfo());
218        $this->assertEquals('', $REV);
219        $this->assertEquals('', $RANGE);
220    }
221
222    /**
223     *  test editor entry and external edit
224     */
225    function test_editor_and_externaledits() {
226        global $ID,$conf;
227        $ID = 'wiki:syntax';
228        $filename = $conf['datadir'].'/wiki/syntax.txt';
229        $rev = filemtime($filename);
230
231        $info = $this->_get_expected_pageinfo();
232        $info['id'] = 'wiki:syntax';
233        $info['namespace'] = 'wiki';
234        $info['filepath'] = $filename;
235        $info['exists'] = true;
236        $info['lastmod'] = $rev;
237        $info['currentrev'] = $rev;
238        $info['meta'] = p_get_metadata($ID);  // need $INFO set correctly for addLogEntry()
239
240        global $INFO;
241        $INFO = $info;
242
243        // add an editor for the current version of $ID
244        addLogEntry($rev, $ID);
245
246        $info['meta'] = p_get_metadata($ID);
247        $info['ip'] = $_SERVER['REMOTE_ADDR'];
248        $info['user'] = $_SERVER['REMOTE_USER'];
249        $info['sum'] = '';
250        $info['editor'] = $info['user'];
251
252        // with an editor ...
253        $this->assertEquals($info, pageinfo());
254
255        // clear the meta['last_change'] value, pageinfo should restore it
256        p_set_metadata($ID, array('last_change' => false));
257
258        $this->assertEquals($info, pageinfo());
259        $this->assertEquals($info['meta']['last_change'], p_get_metadata($ID,'last_change'));
260
261        // fake an external edit, pageinfo should clear the last change from meta data
262        // and not return any editor data
263        $now = time() + 10;
264        touch($filename, $now);
265
266        $info['lastmod'] = $now;
267        $info['currentrev'] = $now;
268        $info['meta']['last_change'] = false;
269        $info['ip'] = null;
270        $info['user'] = null;
271        $info['sum'] = null;
272        $info['editor'] = null;
273
274        $this->assertEquals($info, pageinfo());
275        $this->assertEquals($info['meta'], p_get_metadata($ID));   // check metadata has been updated correctly
276    }
277
278    /**
279     *  check draft
280     */
281    function test_draft() {
282        global $ID,$conf;
283        $ID = 'wiki:syntax';
284        $filename = $conf['datadir'].'/wiki/syntax.txt';
285        $rev = filemtime($filename);
286
287        // run once to prepare meta/wiki/syntax.change file for existing page
288        // because pageinfo() set $info['meta']['last_change'] entry
289        pageinfo();
290
291        $info = $this->_get_expected_pageinfo();
292        $info['id'] = 'wiki:syntax';
293        $info['namespace'] = 'wiki';
294        $info['filepath'] = $filename;
295        $info['exists'] = true;
296        $info['lastmod'] = $rev;
297        $info['currentrev'] = $rev;
298        $info['meta'] = p_get_metadata($ID);
299//        $info['ip'] = $_SERVER['REMOTE_ADDR'];
300//        $info['user'] = $_SERVER['REMOTE_USER'];
301//        $info['sum'] = '';
302//        $info['editor'] = $info['user'];
303        // set from revinfo, $pagelog->getRevisionInfo($info['lastmod'])
304        $info = array_merge($info, array(
305            'ip' => '127.0.0.1',
306            'user' => '',
307            'sum' => 'external edit',
308        ));
309        $info['editor'] = '127.0.0.1';
310
311        // setup a draft, make it more recent than the current page
312        // - pageinfo should recognise it and keep it
313
314        $draft = getCacheName($info['client']."\n".$ID,'.draft');
315        touch($draft, $rev + 10);
316
317        $info['draft'] = $draft;
318
319        $this->assertEquals($info, pageinfo());
320        $this->assertFileExists($draft);
321
322        // make the draft older than the current page
323        // - pageinfo should remove it and not return the 'draft' key
324        touch($draft,$rev - 10);
325        unset($info['draft']);
326
327        $this->assertEquals($info, pageinfo());
328        $this->assertFalse(file_exists($draft));
329    }
330
331    /**
332     *  check ismobile
333     */
334    function test_ismobile() {
335        global $ID,$conf;
336        $ID = 'wiki:start';
337
338        $info = $this->_get_expected_pageinfo();
339        $info['id'] = 'wiki:start';
340        $info['namespace'] = 'wiki';
341        $info['filepath'] = $conf['datadir'].'/wiki/start.txt';
342
343        // overkill, ripped from clientismobile() as we aren't testing detection - but forcing it
344        $_SERVER['HTTP_X_WAP_PROFILE'] = 'a fake url';
345        $_SERVER['HTTP_ACCEPT'] .= ';wap';
346        $_SERVER['HTTP_USER_AGENT'] = 'blackberry,symbian,hand,mobi,phone';
347
348        $info['ismobile'] = clientismobile();
349
350        $this->assertTrue(clientismobile());     // ensure THIS test fails if clientismobile() returns false
351        $this->assertEquals($info, pageinfo());  // it would be a test failure not a pageinfo failure.
352    }
353}
354
355//Setup VIM: ex: et ts=4 :
356