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