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