xref: /dokuwiki/_test/tests/inc/common_pageinfo.test.php (revision f449b8c9764a91a15cbda1130ef0cd053c48f8e8)
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(){
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        $info = $this->_get_expected_pageinfo();
76        $info['id'] = 'wiki:syntax';
77        $info['namespace'] = 'wiki';
78        $info['filepath'] = $filename;
79        $info['exists'] = true;
80        $info['lastmod'] = $rev;
81        $info['currentrev'] = $rev;
82        $info['meta'] = p_get_metadata($ID);
83
84        $this->assertEquals($info, pageinfo());
85    }
86
87    /**
88     *  check info keys and values for anonymous user
89     */
90    function test_anonymoususer(){
91        global $ID,$conf,$REV;
92
93        unset($_SERVER['REMOTE_USER']);
94        global $USERINFO; $USERINFO = array();
95
96        $ID = 'wiki:syntax';
97        $filename = $conf['datadir'].'/wiki/syntax.txt';
98        $rev = filemtime($filename);
99
100        $info = $this->_get_expected_pageinfo();
101        $info['id'] = 'wiki:syntax';
102        $info['namespace'] = 'wiki';
103        $info['filepath'] = $filename;
104        $info['exists'] = true;
105        $info['lastmod'] = $rev;
106        $info['currentrev'] = $rev;
107        $info['meta'] = p_get_metadata($ID);
108        $info['rev'] = '';
109
110        $info = array_merge($info, array(
111          'isadmin' => false,
112          'ismanager' => false,
113          'perm' => 8,
114          'client' => '1.2.3.4',
115        ));
116        unset($info['userinfo']);
117        $this->assertEquals($info, pageinfo());
118    }
119
120    /**
121     *  check info keys and values with $REV
122     *  (also see $RANGE tests)
123     */
124    function test_rev(){
125        global $ID,$conf,$REV;
126
127        $ID = 'wiki:syntax';
128        $filename = $conf['datadir'].'/wiki/syntax.txt';
129        $rev = filemtime($filename);
130        $REV = $rev - 100;
131        $ext = '.txt';
132        if($conf['compression']) {  //compression in $info['filepath'] determined by wikiFN depends also on if the page exist
133            $ext .= "." . $conf['compression']; //.gz or .bz2
134        }
135
136        $info = $this->_get_expected_pageinfo();
137        $info['id'] = 'wiki:syntax';
138        $info['namespace'] = 'wiki';
139        $info['meta'] = p_get_metadata($ID);
140        $info['rev'] = $REV;
141        $info['currentrev'] = $rev;
142        $info['filepath'] = str_replace('pages','attic',substr($filename,0,-3).$REV.$ext);
143
144        $this->assertEquals($info, pageinfo());
145        $this->assertEquals($rev-100, $REV);
146    }
147
148    /**
149     *  check info keys and values with $RANGE
150     */
151    function test_range(){
152        global $ID,$conf,$REV,$RANGE;
153
154        $ID = 'wiki:syntax';
155        $filename = $conf['datadir'].'/wiki/syntax.txt';
156        $rev = filemtime($filename);
157        $range = '1000-2000';
158
159        $info = $this->_get_expected_pageinfo();
160        $info['id'] = 'wiki:syntax';
161        $info['namespace'] = 'wiki';
162        $info['exists'] = true;
163        $info['lastmod'] = $rev;
164        $info['currentrev'] = $rev;
165        $info['meta'] = p_get_metadata($ID);
166        $info['filepath'] = $filename;
167
168        // check $RANGE without $REV
169        // expected result $RANGE unchanged
170        $RANGE = $range;
171
172        $this->assertEquals($info, pageinfo());
173        $this->assertFalse(isset($REV));
174        $this->assertEquals($range,$RANGE);
175
176        // check $RANGE with $REV = current
177        // expected result: $RANGE unchanged, $REV cleared
178        $REV = $rev;
179        $info['rev'] = '';
180
181        $this->assertEquals($info, pageinfo());
182        $this->assertEquals('',$REV);
183        $this->assertEquals($range,$RANGE);
184
185        // check with a real $REV
186        // expected result: $REV and $RANGE are cleared
187        $REV = $rev - 100;
188
189        $this->assertEquals($info, pageinfo());
190        $this->assertEquals('',$REV);
191        $this->assertEquals('',$RANGE);
192    }
193
194    /**
195     *  test editor entry and external edit
196     */
197    function test_editor_and_externaledits(){
198        global $ID,$conf;
199        $ID = 'wiki:syntax';
200        $filename = $conf['datadir'].'/wiki/syntax.txt';
201        $rev = filemtime($filename);
202
203        $info = $this->_get_expected_pageinfo();
204        $info['id'] = 'wiki:syntax';
205        $info['namespace'] = 'wiki';
206        $info['filepath'] = $filename;
207        $info['exists'] = true;
208        $info['lastmod'] = $rev;
209        $info['currentrev'] = $rev;
210        $info['meta'] = p_get_metadata($ID);  // need $INFO set correctly for addLogEntry()
211
212        global $INFO;
213        $INFO = $info;
214
215        // add an editor for the current version of $ID
216        addLogEntry($rev, $ID);
217
218        $info['meta'] = p_get_metadata($ID);
219        $info['editor'] = $_SERVER['REMOTE_USER'];
220        $info['user'] = $_SERVER['REMOTE_USER'];
221        $info['ip'] = $_SERVER['REMOTE_ADDR'];
222        $info['sum'] = '';
223
224        // with an editor ...
225        $this->assertEquals($info, pageinfo());
226
227        // clear the meta['last_change'] value, pageinfo should restore it
228        p_set_metadata($ID,array('last_change' => false));
229
230        $this->assertEquals($info, pageinfo());
231        $this->assertEquals($info['meta']['last_change'], p_get_metadata($ID,'last_change'));
232
233        // fake an external edit, pageinfo should clear the last change from meta data
234        // and not return any editor data
235        $now = time()+10;
236        touch($filename,$now);
237
238        $info['lastmod'] = $now;
239        $info['currentrev'] = $now;
240        $info['meta']['last_change'] = false;
241        $info['ip'] = null;
242        $info['user'] = null;
243        $info['sum'] = null;
244        $info['editor'] = null;
245
246        $this->assertEquals($info, pageinfo());
247        $this->assertEquals($info['meta'], p_get_metadata($ID));   // check metadata has been updated correctly
248    }
249
250    /**
251     *  check draft
252     */
253    function test_draft(){
254        global $ID,$conf;
255        $ID = 'wiki:syntax';
256        $filename = $conf['datadir'].'/wiki/syntax.txt';
257        $rev = filemtime($filename);
258
259        $info = $this->_get_expected_pageinfo();
260        $info['id'] = 'wiki:syntax';
261        $info['namespace'] = 'wiki';
262        $info['filepath'] = $filename;
263        $info['exists'] = true;
264        $info['lastmod'] = $rev;
265        $info['currentrev'] = $rev;
266        $info['meta'] = p_get_metadata($ID);
267
268        // setup a draft, make it more recent than the current page
269        // - pageinfo should recognise it and keep it
270        $draft = getCacheName($info['client'].$ID,'.draft');
271        touch($draft,$rev + 10);
272
273        $info['draft'] = $draft;
274
275        $this->assertEquals($info, pageinfo());
276        $this->assertFileExists($draft);
277
278        // make the draft older than the current page
279        // - pageinfo should remove it and not return the 'draft' key
280        touch($draft,$rev - 10);
281        unset($info['draft']);
282
283        $this->assertEquals($info, pageinfo());
284        $this->assertFalse(file_exists($draft));
285    }
286
287    /**
288     *  check ismobile
289     */
290    function test_ismobile(){
291        global $ID,$conf;
292        $ID = 'wiki:start';
293
294        $info = $this->_get_expected_pageinfo();
295        $info['id'] = 'wiki:start';
296        $info['namespace'] = 'wiki';
297        $info['filepath'] = $conf['datadir'].'/wiki/start.txt';
298
299        // overkill, ripped from clientismobile() as we aren't testing detection - but forcing it
300        $_SERVER['HTTP_X_WAP_PROFILE'] = 'a fake url';
301        $_SERVER['HTTP_ACCEPT'] .= ';wap';
302        $_SERVER['HTTP_USER_AGENT'] = 'blackberry,symbian,hand,mobi,phone';
303
304        $info['ismobile'] = clientismobile();
305
306        $this->assertTrue(clientismobile());     // ensure THIS test fails if clientismobile() returns false
307        $this->assertEquals($info, pageinfo());  // it would be a test failure not a pageinfo failure.
308    }
309}
310
311//Setup VIM: ex: et ts=4 :
312