summaryrefslogtreecommitdiff
path: root/_test/tests/inc/common_pageinfo.test.php
blob: 2b230d9ce037fb7279b75f5ad425db9af12f4978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
 * Unit Test for inc/common.php - pageinfo()
 *
 * @author Christopher Smith <chris@jalakai.co.uk>
 */
class common_pageinfo_test extends DokuWikiTest {

    function setup(){
        parent::setup();

        global $USERINFO;
        $USERINFO = array(
           'pass' => '179ad45c6ce2cb97cf1029e212046e81',
           'name' => 'Arthur Dent',
           'mail' => 'arthur@example.com',
           'grps' => array ('admin','user'),
        );
        $_SERVER['REMOTE_USER'] = 'testuser';
        $_SERVER['REMOTE_ADDR'] = '1.2.3.4';
    }

    function _get_expected_pageinfo() {
        global $USERINFO;
        $info = array (
          'isadmin' => true,
          'ismanager' => true,
          'userinfo' => $USERINFO,
          'perm' => 255,
          'namespace' => false,
          'ismobile' => false,
          'client' => 'testuser',
        );
        $info['rev'] = null;
        $info['subscribed'] = false;
        $info['locked'] = false;
        $info['exists'] = false;
        $info['writable'] = true;
        $info['editable'] = true;
        $info['lastmod'] = false;
        $info['currentrev'] = false;
        $info['meta'] = array();
        $info['ip'] = null;
        $info['user'] = null;
        $info['sum'] = null;
        $info['editor'] = null;

        return $info;
    }

    /**
     *  check info keys and values for a non-existent page & admin user
     */
    function test_basic_nonexistentpage(){
        global $ID,$conf;
        $ID = 'wiki:start';

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:start';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $conf['datadir'].'/wiki/start.txt';

        $this->assertEquals($info, pageinfo());
    }

    /**
     *  check info keys and values for a existing page & admin user
     */
    function test_basic_existingpage(){
        global $ID,$conf;
        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $filename;
        $info['exists'] = true;
        $info['lastmod'] = $rev;
        $info['currentrev'] = $rev;
        $info['meta'] = p_get_metadata($ID);

        $this->assertEquals($info, pageinfo());
    }

    /**
     *  check info keys and values for anonymous user
     */
    function test_anonymoususer(){
        global $ID,$conf,$REV;

        unset($_SERVER['REMOTE_USER']);
        global $USERINFO; $USERINFO = array();

        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $filename;
        $info['exists'] = true;
        $info['lastmod'] = $rev;
        $info['currentrev'] = $rev;
        $info['meta'] = p_get_metadata($ID);
        $info['rev'] = '';

        $info = array_merge($info, array(
          'isadmin' => false,
          'ismanager' => false,
          'perm' => 8,
          'client' => '1.2.3.4',
        ));
        unset($info['userinfo']);
        $this->assertEquals($info, pageinfo());
    }

    /**
     *  check info keys and values with $REV
     *  (also see $RANGE tests)
     */
    function test_rev(){
        global $ID,$conf,$REV;

        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);
        $REV = $rev - 100;

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['meta'] = p_get_metadata($ID);
        $info['rev'] = $REV;
        $info['currentrev'] = $rev;
        $info['filepath'] = str_replace('pages','attic',substr($filename,0,-3).$REV.'.txt.gz');

        $this->assertEquals($info, pageinfo());
        $this->assertEquals($rev-100, $REV);
    }

    /**
     *  check info keys and values with $RANGE
     */
    function test_range(){
        global $ID,$conf,$REV,$RANGE;

        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);
        $range = '1000-2000';

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['exists'] = true;
        $info['lastmod'] = $rev;
        $info['currentrev'] = $rev;
        $info['meta'] = p_get_metadata($ID);
        $info['filepath'] = $filename;

        // check $RANGE without $REV
        // expected result $RANGE unchanged
        $RANGE = $range;

        $this->assertEquals($info, pageinfo());
        $this->assertFalse(isset($REV));
        $this->assertEquals($range,$RANGE);

        // check $RANGE with $REV = current
        // expected result: $RANGE unchanged, $REV cleared
        $REV = $rev;
        $info['rev'] = '';

        $this->assertEquals($info, pageinfo());
        $this->assertEquals('',$REV);
        $this->assertEquals($range,$RANGE);

        // check with a real $REV
        // expected result: $REV and $RANGE are cleared
        $REV = $rev - 100;

        $this->assertEquals($info, pageinfo());
        $this->assertEquals('',$REV);
        $this->assertEquals('',$RANGE);
    }

    /**
     *  test editor entry and external edit
     */
    function test_editor_and_externaledits(){
        global $ID,$conf;
        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $filename;
        $info['exists'] = true;
        $info['lastmod'] = $rev;
        $info['currentrev'] = $rev;
        $info['meta'] = p_get_metadata($ID);  // need $INFO set correctly for addLogEntry()

        global $INFO;
        $INFO = $info;

        // add an editor for the current version of $ID
        addLogEntry($rev, $ID);

        $info['meta'] = p_get_metadata($ID);
        $info['editor'] = $_SERVER['REMOTE_USER'];
        $info['user'] = $_SERVER['REMOTE_USER'];
        $info['ip'] = $_SERVER['REMOTE_ADDR'];
        $info['sum'] = '';

        // with an editor ...
        $this->assertEquals($info, pageinfo());

        // clear the meta['last_change'] value, pageinfo should restore it
        p_set_metadata($ID,array('last_change' => false));

        $this->assertEquals($info, pageinfo());
        $this->assertEquals($info['meta']['last_change'], p_get_metadata($ID,'last_change'));

        // fake an external edit, pageinfo should clear the last change from meta data
        // and not return any editor data
        $now = time()+10;
        touch($filename,$now);

        $info['lastmod'] = $now;
        $info['currentrev'] = $now;
        $info['meta']['last_change'] = false;
        $info['ip'] = null;
        $info['user'] = null;
        $info['sum'] = null;
        $info['editor'] = null;

        $this->assertEquals($info, pageinfo());
        $this->assertEquals($info['meta'], p_get_metadata($ID));   // check metadata has been updated correctly
    }

    /**
     *  check draft
     */
    function test_draft(){
        global $ID,$conf;
        $ID = 'wiki:syntax';
        $filename = $conf['datadir'].'/wiki/syntax.txt';
        $rev = filemtime($filename);

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:syntax';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $filename;
        $info['exists'] = true;
        $info['lastmod'] = $rev;
        $info['currentrev'] = $rev;
        $info['meta'] = p_get_metadata($ID);

        // setup a draft, make it more recent than the current page
        // - pageinfo should recognise it and keep it
        $draft = getCacheName($info['client'].$ID,'.draft');
        touch($draft,$rev + 10);

        $info['draft'] = $draft;

        $this->assertEquals($info, pageinfo());
        $this->assertFileExists($draft);

        // make the draft older than the current page
        // - pageinfo should remove it and not return the 'draft' key
        touch($draft,$rev - 10);
        unset($info['draft']);

        $this->assertEquals($info, pageinfo());
        $this->assertFalse(file_exists($draft));
    }

    /**
     *  check ismobile
     */
    function test_ismobile(){
        global $ID,$conf;
        $ID = 'wiki:start';

        $info = $this->_get_expected_pageinfo();
        $info['id'] = 'wiki:start';
        $info['namespace'] = 'wiki';
        $info['filepath'] = $conf['datadir'].'/wiki/start.txt';

        // overkill, ripped from clientismobile() as we aren't testing detection - but forcing it
        $_SERVER['HTTP_X_WAP_PROFILE'] = 'a fake url';
        $_SERVER['HTTP_ACCEPT'] .= ';wap';
        $_SERVER['HTTP_USER_AGENT'] = 'blackberry,symbian,hand,mobi,phone';

        $info['ismobile'] = clientismobile();

        $this->assertTrue(clientismobile());     // ensure THIS test fails if clientismobile() returns false
        $this->assertEquals($info, pageinfo());  // it would be a test failure not a pageinfo failure.
    }
}

//Setup VIM: ex: et ts=4 :