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