xref: /dokuwiki/_test/tests/Remote/ApiCoreTest.php (revision 535851891f27d4edf214975656242cb81be5b6ac)
1*53585189SAndreas Gohr<?php
2*53585189SAndreas Gohr
3*53585189SAndreas Gohrnamespace dokuwiki\test\Remote;
4*53585189SAndreas Gohr
5*53585189SAndreas Gohruse dokuwiki\Extension\Event;
6*53585189SAndreas Gohruse dokuwiki\Remote\Api;
7*53585189SAndreas Gohruse dokuwiki\Remote\ApiCore;
8*53585189SAndreas Gohruse dokuwiki\test\mock\AuthDeletePlugin;
9*53585189SAndreas Gohruse dokuwiki\test\mock\AuthPlugin;
10*53585189SAndreas Gohr
11*53585189SAndreas Gohr/**
12*53585189SAndreas Gohr * Class remoteapicore_test
13*53585189SAndreas Gohr */
14*53585189SAndreas Gohrclass ApiCoreTest extends \DokuWikiTest
15*53585189SAndreas Gohr{
16*53585189SAndreas Gohr
17*53585189SAndreas Gohr    protected $userinfo;
18*53585189SAndreas Gohr    protected $oldAuthAcl;
19*53585189SAndreas Gohr    /** @var  Api */
20*53585189SAndreas Gohr    protected $remote;
21*53585189SAndreas Gohr
22*53585189SAndreas Gohr    public function setUp(): void
23*53585189SAndreas Gohr    {
24*53585189SAndreas Gohr        // we need a clean setup before each single test:
25*53585189SAndreas Gohr        \DokuWikiTest::setUpBeforeClass();
26*53585189SAndreas Gohr
27*53585189SAndreas Gohr        parent::setUp();
28*53585189SAndreas Gohr        global $conf;
29*53585189SAndreas Gohr        global $USERINFO;
30*53585189SAndreas Gohr        global $AUTH_ACL;
31*53585189SAndreas Gohr        global $auth;
32*53585189SAndreas Gohr        $this->oldAuthAcl = $AUTH_ACL;
33*53585189SAndreas Gohr        $this->userinfo = $USERINFO;
34*53585189SAndreas Gohr        $auth = new AuthPlugin();
35*53585189SAndreas Gohr
36*53585189SAndreas Gohr        $conf['remote'] = 1;
37*53585189SAndreas Gohr        $conf['remoteuser'] = '@user';
38*53585189SAndreas Gohr        $conf['useacl'] = 0;
39*53585189SAndreas Gohr
40*53585189SAndreas Gohr        $this->remote = new Api();
41*53585189SAndreas Gohr    }
42*53585189SAndreas Gohr
43*53585189SAndreas Gohr    public function tearDown(): void
44*53585189SAndreas Gohr    {
45*53585189SAndreas Gohr        parent::tearDown();
46*53585189SAndreas Gohr
47*53585189SAndreas Gohr        global $USERINFO;
48*53585189SAndreas Gohr        global $AUTH_ACL;
49*53585189SAndreas Gohr
50*53585189SAndreas Gohr        $USERINFO = $this->userinfo;
51*53585189SAndreas Gohr        $AUTH_ACL = $this->oldAuthAcl;
52*53585189SAndreas Gohr    }
53*53585189SAndreas Gohr
54*53585189SAndreas Gohr    /** Delay writes of old revisions by a second. */
55*53585189SAndreas Gohr    public function handle_write(Event $event, $param)
56*53585189SAndreas Gohr    {
57*53585189SAndreas Gohr        if ($event->data[3] !== false) {
58*53585189SAndreas Gohr            $this->waitForTick();
59*53585189SAndreas Gohr        }
60*53585189SAndreas Gohr    }
61*53585189SAndreas Gohr
62*53585189SAndreas Gohr    public function testGetVersion()
63*53585189SAndreas Gohr    {
64*53585189SAndreas Gohr        $this->assertEquals(getVersion(), $this->remote->call('dokuwiki.getVersion'));
65*53585189SAndreas Gohr    }
66*53585189SAndreas Gohr
67*53585189SAndreas Gohr    public function testGetPageList()
68*53585189SAndreas Gohr    {
69*53585189SAndreas Gohr        $file1 = wikiFN('wiki:dokuwiki');
70*53585189SAndreas Gohr        $file2 = wikiFN('wiki:syntax');
71*53585189SAndreas Gohr        $expected = [
72*53585189SAndreas Gohr            [
73*53585189SAndreas Gohr                'id' => 'wiki:dokuwiki',
74*53585189SAndreas Gohr                'rev' => filemtime($file1),
75*53585189SAndreas Gohr                'mtime' => filemtime($file1),
76*53585189SAndreas Gohr                'size' => filesize($file1),
77*53585189SAndreas Gohr                'hash' => md5(trim(rawWiki('wiki:dokuwiki')))
78*53585189SAndreas Gohr            ],
79*53585189SAndreas Gohr            [
80*53585189SAndreas Gohr                'id' => 'wiki:syntax',
81*53585189SAndreas Gohr                'rev' => filemtime($file2),
82*53585189SAndreas Gohr                'mtime' => filemtime($file2),
83*53585189SAndreas Gohr                'size' => filesize($file2),
84*53585189SAndreas Gohr                'hash' => md5(trim(rawWiki('wiki:syntax')))
85*53585189SAndreas Gohr            ]
86*53585189SAndreas Gohr        ];
87*53585189SAndreas Gohr        $params = [
88*53585189SAndreas Gohr            'wiki:',
89*53585189SAndreas Gohr            [
90*53585189SAndreas Gohr                'depth' => 0, // 0 for all
91*53585189SAndreas Gohr                'hash' => 1,
92*53585189SAndreas Gohr                'skipacl' => 1 // is ignored
93*53585189SAndreas Gohr            ]
94*53585189SAndreas Gohr        ];
95*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('dokuwiki.getPagelist', $params));
96*53585189SAndreas Gohr    }
97*53585189SAndreas Gohr
98*53585189SAndreas Gohr    public function testSearch()
99*53585189SAndreas Gohr    {
100*53585189SAndreas Gohr        $id = 'wiki:syntax';
101*53585189SAndreas Gohr        $file = wikiFN($id);
102*53585189SAndreas Gohr
103*53585189SAndreas Gohr        idx_addPage($id); //full text search depends on index
104*53585189SAndreas Gohr        $expected = [
105*53585189SAndreas Gohr            [
106*53585189SAndreas Gohr                'id' => $id,
107*53585189SAndreas Gohr                'score' => 1,
108*53585189SAndreas Gohr                'rev' => filemtime($file),
109*53585189SAndreas Gohr                'mtime' => filemtime($file),
110*53585189SAndreas Gohr                'size' => filesize($file),
111*53585189SAndreas Gohr                'snippet' => ' a footnote)) by using double parentheses.
112*53585189SAndreas Gohr
113*53585189SAndreas Gohr===== <strong class="search_hit">Sectioning</strong> =====
114*53585189SAndreas Gohr
115*53585189SAndreas GohrYou can use up to five different levels of',
116*53585189SAndreas Gohr                'title' => 'wiki:syntax'
117*53585189SAndreas Gohr            ]
118*53585189SAndreas Gohr        ];
119*53585189SAndreas Gohr        $params = ['Sectioning'];
120*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('dokuwiki.search', $params));
121*53585189SAndreas Gohr    }
122*53585189SAndreas Gohr
123*53585189SAndreas Gohr    public function testGetTime()
124*53585189SAndreas Gohr    {
125*53585189SAndreas Gohr        $timeexpect = time();
126*53585189SAndreas Gohr        $timeactual = $this->remote->call('dokuwiki.getTime');
127*53585189SAndreas Gohr        $this->assertTrue(($timeexpect <= $timeactual) && ($timeactual <= $timeexpect + 1));
128*53585189SAndreas Gohr    }
129*53585189SAndreas Gohr
130*53585189SAndreas Gohr    public function testSetLocks()
131*53585189SAndreas Gohr    {
132*53585189SAndreas Gohr        $expected = [
133*53585189SAndreas Gohr            'locked' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
134*53585189SAndreas Gohr            'lockfail' => [],
135*53585189SAndreas Gohr            'unlocked' => [],
136*53585189SAndreas Gohr            'unlockfail' => [],
137*53585189SAndreas Gohr        ];
138*53585189SAndreas Gohr        $params = [
139*53585189SAndreas Gohr            [
140*53585189SAndreas Gohr                'lock' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
141*53585189SAndreas Gohr                'unlock' => []
142*53585189SAndreas Gohr            ]
143*53585189SAndreas Gohr        ];
144*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
145*53585189SAndreas Gohr
146*53585189SAndreas Gohr        $expected = [
147*53585189SAndreas Gohr            'locked' => [],
148*53585189SAndreas Gohr            'lockfail' => [],
149*53585189SAndreas Gohr            'unlocked' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
150*53585189SAndreas Gohr            'unlockfail' => ['nonexisting2'],
151*53585189SAndreas Gohr        ];
152*53585189SAndreas Gohr        $params = [
153*53585189SAndreas Gohr            [
154*53585189SAndreas Gohr                'lock' => [],
155*53585189SAndreas Gohr                'unlock' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting', 'nonexisting2']
156*53585189SAndreas Gohr            ]
157*53585189SAndreas Gohr        ];
158*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
159*53585189SAndreas Gohr    }
160*53585189SAndreas Gohr
161*53585189SAndreas Gohr    public function testGetTitle()
162*53585189SAndreas Gohr    {
163*53585189SAndreas Gohr        global $conf;
164*53585189SAndreas Gohr        $this->assertEquals($conf['title'], $this->remote->call('dokuwiki.getTitle'));
165*53585189SAndreas Gohr    }
166*53585189SAndreas Gohr
167*53585189SAndreas Gohr    public function testPutPage()
168*53585189SAndreas Gohr    {
169*53585189SAndreas Gohr        $id = 'putpage';
170*53585189SAndreas Gohr
171*53585189SAndreas Gohr        $content = "====Title====\nText";
172*53585189SAndreas Gohr        $params = [
173*53585189SAndreas Gohr            $id,
174*53585189SAndreas Gohr            $content,
175*53585189SAndreas Gohr            [
176*53585189SAndreas Gohr                'minor' => false,
177*53585189SAndreas Gohr                'sum' => 'Summary of nice text'
178*53585189SAndreas Gohr            ]
179*53585189SAndreas Gohr        ];
180*53585189SAndreas Gohr        $this->assertTrue($this->remote->call('wiki.putPage', $params));
181*53585189SAndreas Gohr        $this->assertEquals($content, rawWiki($id));
182*53585189SAndreas Gohr
183*53585189SAndreas Gohr        //remove page
184*53585189SAndreas Gohr        $params = [
185*53585189SAndreas Gohr            $id,
186*53585189SAndreas Gohr            '',
187*53585189SAndreas Gohr            [
188*53585189SAndreas Gohr                'minor' => false,
189*53585189SAndreas Gohr            ]
190*53585189SAndreas Gohr        ];
191*53585189SAndreas Gohr        $this->assertTrue($this->remote->call('wiki.putPage', $params));
192*53585189SAndreas Gohr        $this->assertFileNotExists(wikiFN($id));
193*53585189SAndreas Gohr    }
194*53585189SAndreas Gohr
195*53585189SAndreas Gohr    public function testGetPage()
196*53585189SAndreas Gohr    {
197*53585189SAndreas Gohr        $id = 'getpage';
198*53585189SAndreas Gohr        $content = 'a test';
199*53585189SAndreas Gohr        saveWikiText($id, $content, 'test for getpage');
200*53585189SAndreas Gohr
201*53585189SAndreas Gohr        $params = [$id];
202*53585189SAndreas Gohr        $this->assertEquals($content, $this->remote->call('wiki.getPage', $params));
203*53585189SAndreas Gohr    }
204*53585189SAndreas Gohr
205*53585189SAndreas Gohr    public function testAppendPage()
206*53585189SAndreas Gohr    {
207*53585189SAndreas Gohr        $id = 'appendpage';
208*53585189SAndreas Gohr        $content = 'a test';
209*53585189SAndreas Gohr        $morecontent = "\nOther text";
210*53585189SAndreas Gohr        saveWikiText($id, $content, 'local');
211*53585189SAndreas Gohr
212*53585189SAndreas Gohr        $params = [
213*53585189SAndreas Gohr            $id,
214*53585189SAndreas Gohr            $morecontent,
215*53585189SAndreas Gohr            []
216*53585189SAndreas Gohr        ];
217*53585189SAndreas Gohr        $this->assertEquals(true, $this->remote->call('dokuwiki.appendPage', $params));
218*53585189SAndreas Gohr        $this->assertEquals($content . $morecontent, rawWiki($id));
219*53585189SAndreas Gohr    }
220*53585189SAndreas Gohr
221*53585189SAndreas Gohr    public function testGetPageVersion()
222*53585189SAndreas Gohr    {
223*53585189SAndreas Gohr        $id = 'pageversion';
224*53585189SAndreas Gohr        $file = wikiFN($id);
225*53585189SAndreas Gohr
226*53585189SAndreas Gohr        saveWikiText($id, 'first version', 'first');
227*53585189SAndreas Gohr        $rev1 = filemtime($file);
228*53585189SAndreas Gohr        clearstatcache(false, $file);
229*53585189SAndreas Gohr        $this->waitForTick(true);
230*53585189SAndreas Gohr        saveWikiText($id, 'second version', 'second');
231*53585189SAndreas Gohr        $rev2 = filemtime($file);
232*53585189SAndreas Gohr
233*53585189SAndreas Gohr        $params = [$id, ''];
234*53585189SAndreas Gohr        $this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), 'no revision given');
235*53585189SAndreas Gohr
236*53585189SAndreas Gohr        $params = [$id, $rev1];
237*53585189SAndreas Gohr        $this->assertEquals('first version', $this->remote->call('wiki.getPageVersion', $params), '1st revision given');
238*53585189SAndreas Gohr
239*53585189SAndreas Gohr        $params = [$id, $rev2];
240*53585189SAndreas Gohr        $this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), '2nd revision given');
241*53585189SAndreas Gohr
242*53585189SAndreas Gohr        $params = [$id, 1234];
243*53585189SAndreas Gohr        $this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing revision given');
244*53585189SAndreas Gohr
245*53585189SAndreas Gohr        $params = ['foobar', 1234];
246*53585189SAndreas Gohr        $this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing page given');
247*53585189SAndreas Gohr    }
248*53585189SAndreas Gohr
249*53585189SAndreas Gohr    public function testGetPageHTML()
250*53585189SAndreas Gohr    {
251*53585189SAndreas Gohr        $id = 'htmltest';
252*53585189SAndreas Gohr        $content = "====Title====\nText";
253*53585189SAndreas Gohr        $html = "\n<h3 class=\"sectionedit1\" id=\"title\">Title</h3>\n<div class=\"level3\">\n\n<p>\nText\n</p>\n\n</div>\n";
254*53585189SAndreas Gohr
255*53585189SAndreas Gohr        saveWikiText($id, $content, 'htmltest');
256*53585189SAndreas Gohr
257*53585189SAndreas Gohr        $params = [$id];
258*53585189SAndreas Gohr        $this->assertEquals($html, $this->remote->call('wiki.getPageHTML', $params));
259*53585189SAndreas Gohr    }
260*53585189SAndreas Gohr
261*53585189SAndreas Gohr    public function testGetPageHTMLVersion()
262*53585189SAndreas Gohr    {
263*53585189SAndreas Gohr        $id = 'htmltest';
264*53585189SAndreas Gohr        $file = wikiFN($id);
265*53585189SAndreas Gohr
266*53585189SAndreas Gohr        $content1 = "====Title====\nText";
267*53585189SAndreas Gohr        $html1 = "\n<h3 class=\"sectionedit1\" id=\"title\">Title</h3>\n<div class=\"level3\">\n\n<p>\nText\n</p>\n\n</div>\n";
268*53585189SAndreas Gohr        $content2 = "====Foobar====\nText Bamm";
269*53585189SAndreas Gohr        $html2 = "\n<h3 class=\"sectionedit1\" id=\"foobar\">Foobar</h3>\n<div class=\"level3\">\n\n<p>\nText Bamm\n</p>\n\n</div>\n";
270*53585189SAndreas Gohr
271*53585189SAndreas Gohr        saveWikiText($id, $content1, 'first');
272*53585189SAndreas Gohr        $rev1 = filemtime($file);
273*53585189SAndreas Gohr        clearstatcache(false, $file);
274*53585189SAndreas Gohr        $this->waitForTick(true);
275*53585189SAndreas Gohr        saveWikiText($id, $content2, 'second');
276*53585189SAndreas Gohr        $rev2 = filemtime($file);
277*53585189SAndreas Gohr
278*53585189SAndreas Gohr        $params = [$id, ''];
279*53585189SAndreas Gohr        $this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), 'no revision given');
280*53585189SAndreas Gohr
281*53585189SAndreas Gohr        $params = [$id, $rev1];
282*53585189SAndreas Gohr        $this->assertEquals($html1, $this->remote->call('wiki.getPageHTMLVersion', $params), '1st revision given');
283*53585189SAndreas Gohr
284*53585189SAndreas Gohr        $params = [$id, $rev2];
285*53585189SAndreas Gohr        $this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), '2nd revision given');
286*53585189SAndreas Gohr
287*53585189SAndreas Gohr        $params = [$id, 1234];
288*53585189SAndreas Gohr        $this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing revision given');
289*53585189SAndreas Gohr
290*53585189SAndreas Gohr        $params = ['foobar', 1234];
291*53585189SAndreas Gohr        $this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing page given');
292*53585189SAndreas Gohr    }
293*53585189SAndreas Gohr
294*53585189SAndreas Gohr    public function testGetAllPages()
295*53585189SAndreas Gohr    {
296*53585189SAndreas Gohr        // all pages depends on index
297*53585189SAndreas Gohr        idx_addPage('wiki:syntax');
298*53585189SAndreas Gohr        idx_addPage('wiki:dokuwiki');
299*53585189SAndreas Gohr
300*53585189SAndreas Gohr        $file1 = wikiFN('wiki:syntax');
301*53585189SAndreas Gohr        $file2 = wikiFN('wiki:dokuwiki');
302*53585189SAndreas Gohr
303*53585189SAndreas Gohr        $expected = [
304*53585189SAndreas Gohr            [
305*53585189SAndreas Gohr                'id' => 'wiki:syntax',
306*53585189SAndreas Gohr                'perms' => 8,
307*53585189SAndreas Gohr                'size' => filesize($file1),
308*53585189SAndreas Gohr                'lastModified' => filemtime($file1)
309*53585189SAndreas Gohr            ],
310*53585189SAndreas Gohr            [
311*53585189SAndreas Gohr                'id' => 'wiki:dokuwiki',
312*53585189SAndreas Gohr                'perms' => 8,
313*53585189SAndreas Gohr                'size' => filesize($file2),
314*53585189SAndreas Gohr                'lastModified' => filemtime($file2)
315*53585189SAndreas Gohr            ]
316*53585189SAndreas Gohr        ];
317*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getAllPages'));
318*53585189SAndreas Gohr    }
319*53585189SAndreas Gohr
320*53585189SAndreas Gohr    public function testGetBacklinks()
321*53585189SAndreas Gohr    {
322*53585189SAndreas Gohr        saveWikiText('linky', '[[wiki:syntax]]', 'test');
323*53585189SAndreas Gohr        // backlinks need index
324*53585189SAndreas Gohr        idx_addPage('wiki:syntax');
325*53585189SAndreas Gohr        idx_addPage('linky');
326*53585189SAndreas Gohr
327*53585189SAndreas Gohr        $params = ['wiki:syntax'];
328*53585189SAndreas Gohr        $result = $this->remote->call('wiki.getBackLinks', $params);
329*53585189SAndreas Gohr        $this->assertTrue(count($result) > 0);
330*53585189SAndreas Gohr        $this->assertEquals(ft_backlinks('wiki:syntax'), $result);
331*53585189SAndreas Gohr    }
332*53585189SAndreas Gohr
333*53585189SAndreas Gohr    public function testGetPageInfo()
334*53585189SAndreas Gohr    {
335*53585189SAndreas Gohr        $id = 'pageinfo';
336*53585189SAndreas Gohr        $file = wikiFN($id);
337*53585189SAndreas Gohr
338*53585189SAndreas Gohr        saveWikiText($id, 'test', 'test');
339*53585189SAndreas Gohr
340*53585189SAndreas Gohr        $expected = [
341*53585189SAndreas Gohr            'name' => $id,
342*53585189SAndreas Gohr            'lastModified' => filemtime($file),
343*53585189SAndreas Gohr            'author' => clientIP(),
344*53585189SAndreas Gohr            'version' => filemtime($file)
345*53585189SAndreas Gohr        ];
346*53585189SAndreas Gohr        $params = [$id];
347*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getPageInfo', $params));
348*53585189SAndreas Gohr    }
349*53585189SAndreas Gohr
350*53585189SAndreas Gohr    public function testGetPageInfoVersion()
351*53585189SAndreas Gohr    {
352*53585189SAndreas Gohr        $id = 'pageinfo';
353*53585189SAndreas Gohr        $file = wikiFN($id);
354*53585189SAndreas Gohr
355*53585189SAndreas Gohr        saveWikiText($id, 'first version', 'first');
356*53585189SAndreas Gohr        $rev1 = filemtime($file);
357*53585189SAndreas Gohr        clearstatcache(false, $file);
358*53585189SAndreas Gohr        $this->waitForTick(true);
359*53585189SAndreas Gohr        saveWikiText($id, 'second version', 'second');
360*53585189SAndreas Gohr        $rev2 = filemtime($file);
361*53585189SAndreas Gohr
362*53585189SAndreas Gohr        $expected = [
363*53585189SAndreas Gohr            'name' => $id,
364*53585189SAndreas Gohr            'lastModified' => $rev2,
365*53585189SAndreas Gohr            'author' => clientIP(),
366*53585189SAndreas Gohr            'version' => $rev2
367*53585189SAndreas Gohr        ];
368*53585189SAndreas Gohr        $params = [$id, ''];
369*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), 'no revision given');
370*53585189SAndreas Gohr
371*53585189SAndreas Gohr        $expected = [
372*53585189SAndreas Gohr            'name' => $id,
373*53585189SAndreas Gohr            'lastModified' => $rev1,
374*53585189SAndreas Gohr            'author' => clientIP(),
375*53585189SAndreas Gohr            'version' => $rev1
376*53585189SAndreas Gohr        ];
377*53585189SAndreas Gohr        $params = [$id, $rev1];
378*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '1st revision given');
379*53585189SAndreas Gohr
380*53585189SAndreas Gohr        $expected = [
381*53585189SAndreas Gohr            'name' => $id,
382*53585189SAndreas Gohr            'lastModified' => $rev2,
383*53585189SAndreas Gohr            'author' => clientIP(),
384*53585189SAndreas Gohr            'version' => $rev2
385*53585189SAndreas Gohr        ];
386*53585189SAndreas Gohr        $params = [$id, $rev2];
387*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '2nd revision given');
388*53585189SAndreas Gohr    }
389*53585189SAndreas Gohr
390*53585189SAndreas Gohr    public function testGetRecentChanges()
391*53585189SAndreas Gohr    {
392*53585189SAndreas Gohr
393*53585189SAndreas Gohr        saveWikiText('pageone', 'test', 'test');
394*53585189SAndreas Gohr        $rev1 = filemtime(wikiFN('pageone'));
395*53585189SAndreas Gohr        saveWikiText('pagetwo', 'test', 'test');
396*53585189SAndreas Gohr        $rev2 = filemtime(wikiFN('pagetwo'));
397*53585189SAndreas Gohr
398*53585189SAndreas Gohr        $expected = [
399*53585189SAndreas Gohr            [
400*53585189SAndreas Gohr                'name' => 'pageone',
401*53585189SAndreas Gohr                'lastModified' => $rev1,
402*53585189SAndreas Gohr                'author' => '',
403*53585189SAndreas Gohr                'version' => $rev1,
404*53585189SAndreas Gohr                'perms' => 8,
405*53585189SAndreas Gohr                'size' => 4
406*53585189SAndreas Gohr            ],
407*53585189SAndreas Gohr            [
408*53585189SAndreas Gohr                'name' => 'pagetwo',
409*53585189SAndreas Gohr                'lastModified' => $rev2,
410*53585189SAndreas Gohr                'author' => '',
411*53585189SAndreas Gohr                'version' => $rev2,
412*53585189SAndreas Gohr                'perms' => 8,
413*53585189SAndreas Gohr                'size' => 4
414*53585189SAndreas Gohr            ]
415*53585189SAndreas Gohr        ];
416*53585189SAndreas Gohr        $params = [strtotime("-1 year")];
417*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getRecentChanges', $params));
418*53585189SAndreas Gohr    }
419*53585189SAndreas Gohr
420*53585189SAndreas Gohr    public function testGetPageVersions()
421*53585189SAndreas Gohr    {
422*53585189SAndreas Gohr        /** @var $EVENT_HANDLER \dokuwiki\Extension\EventHandler */
423*53585189SAndreas Gohr        global $EVENT_HANDLER;
424*53585189SAndreas Gohr        $EVENT_HANDLER->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_write');
425*53585189SAndreas Gohr        global $conf;
426*53585189SAndreas Gohr
427*53585189SAndreas Gohr        $id = 'revpage';
428*53585189SAndreas Gohr        $file = wikiFN($id);
429*53585189SAndreas Gohr
430*53585189SAndreas Gohr        $rev = [];
431*53585189SAndreas Gohr        for ($i = 0; $i < 6; $i++) {
432*53585189SAndreas Gohr            $this->waitForTick();
433*53585189SAndreas Gohr            saveWikiText($id, "rev$i", "rev$i");
434*53585189SAndreas Gohr            clearstatcache(false, $file);
435*53585189SAndreas Gohr            $rev[$i] = filemtime($file);
436*53585189SAndreas Gohr        }
437*53585189SAndreas Gohr
438*53585189SAndreas Gohr        $params = [$id, 0];
439*53585189SAndreas Gohr        $versions = $this->remote->call('wiki.getPageVersions', $params);
440*53585189SAndreas Gohr        $this->assertEquals(6, count($versions));
441*53585189SAndreas Gohr        $this->assertEquals($rev[5], $versions[0]['version']);
442*53585189SAndreas Gohr        $this->assertEquals($rev[4], $versions[1]['version']);
443*53585189SAndreas Gohr        $this->assertEquals($rev[3], $versions[2]['version']);
444*53585189SAndreas Gohr        $this->assertEquals($rev[2], $versions[3]['version']);
445*53585189SAndreas Gohr        $this->assertEquals($rev[1], $versions[4]['version']);
446*53585189SAndreas Gohr        $this->assertEquals($rev[0], $versions[5]['version']);
447*53585189SAndreas Gohr
448*53585189SAndreas Gohr        $params = [$id, 1]; // offset 1
449*53585189SAndreas Gohr        $versions = $this->remote->call('wiki.getPageVersions', $params);
450*53585189SAndreas Gohr        $this->assertEquals(5, count($versions));
451*53585189SAndreas Gohr        $this->assertEquals($rev[4], $versions[0]['version']);
452*53585189SAndreas Gohr        $this->assertEquals($rev[3], $versions[1]['version']);
453*53585189SAndreas Gohr        $this->assertEquals($rev[2], $versions[2]['version']);
454*53585189SAndreas Gohr        $this->assertEquals($rev[1], $versions[3]['version']);
455*53585189SAndreas Gohr        $this->assertEquals($rev[0], $versions[4]['version']);
456*53585189SAndreas Gohr
457*53585189SAndreas Gohr        $conf['recent'] = 3; //set number of results per page
458*53585189SAndreas Gohr
459*53585189SAndreas Gohr        $params = [$id, 0]; // first page
460*53585189SAndreas Gohr        $versions = $this->remote->call('wiki.getPageVersions', $params);
461*53585189SAndreas Gohr        $this->assertEquals(3, count($versions));
462*53585189SAndreas Gohr        $this->assertEquals($rev[5], $versions[0]['version']);
463*53585189SAndreas Gohr        $this->assertEquals($rev[4], $versions[1]['version']);
464*53585189SAndreas Gohr        $this->assertEquals($rev[3], $versions[2]['version']);
465*53585189SAndreas Gohr
466*53585189SAndreas Gohr        $params = [$id, $conf['recent']]; // second page
467*53585189SAndreas Gohr        $versions = $this->remote->call('wiki.getPageVersions', $params);
468*53585189SAndreas Gohr        $this->assertEquals(3, count($versions));
469*53585189SAndreas Gohr        $this->assertEquals($rev[2], $versions[0]['version']);
470*53585189SAndreas Gohr        $this->assertEquals($rev[1], $versions[1]['version']);
471*53585189SAndreas Gohr        $this->assertEquals($rev[0], $versions[2]['version']);
472*53585189SAndreas Gohr
473*53585189SAndreas Gohr        $params = [$id, $conf['recent'] * 2]; // third page
474*53585189SAndreas Gohr        $versions = $this->remote->call('wiki.getPageVersions', $params);
475*53585189SAndreas Gohr        $this->assertEquals(0, count($versions));
476*53585189SAndreas Gohr    }
477*53585189SAndreas Gohr
478*53585189SAndreas Gohr    public function testDeleteUser()
479*53585189SAndreas Gohr    {
480*53585189SAndreas Gohr        global $conf, $auth;
481*53585189SAndreas Gohr        $auth = new AuthDeletePlugin();
482*53585189SAndreas Gohr        $conf['remote'] = 1;
483*53585189SAndreas Gohr        $conf['remoteuser'] = 'testuser';
484*53585189SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'testuser';
485*53585189SAndreas Gohr        $params = [
486*53585189SAndreas Gohr            ['testuser']
487*53585189SAndreas Gohr        ];
488*53585189SAndreas Gohr        $actualCallResult = $this->remote->call('dokuwiki.deleteUsers', $params);
489*53585189SAndreas Gohr        $this->assertTrue($actualCallResult);
490*53585189SAndreas Gohr    }
491*53585189SAndreas Gohr
492*53585189SAndreas Gohr    public function testAclCheck()
493*53585189SAndreas Gohr    {
494*53585189SAndreas Gohr        $id = 'aclpage';
495*53585189SAndreas Gohr
496*53585189SAndreas Gohr        $params = [$id];
497*53585189SAndreas Gohr        $this->assertEquals(AUTH_UPLOAD, $this->remote->call('wiki.aclCheck', $params));
498*53585189SAndreas Gohr
499*53585189SAndreas Gohr        global $conf;
500*53585189SAndreas Gohr        global $AUTH_ACL, $USERINFO;
501*53585189SAndreas Gohr        $conf['useacl'] = 1;
502*53585189SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'john';
503*53585189SAndreas Gohr        $USERINFO['grps'] = ['user'];
504*53585189SAndreas Gohr        $AUTH_ACL = [
505*53585189SAndreas Gohr            '*                  @ALL           0',
506*53585189SAndreas Gohr            '*                  @user          2', //edit
507*53585189SAndreas Gohr        ];
508*53585189SAndreas Gohr
509*53585189SAndreas Gohr        $params = [$id];
510*53585189SAndreas Gohr        $this->assertEquals(AUTH_EDIT, $this->remote->call('wiki.aclCheck', $params));
511*53585189SAndreas Gohr    }
512*53585189SAndreas Gohr
513*53585189SAndreas Gohr    public function testGetXMLRPCAPIVersion()
514*53585189SAndreas Gohr    {
515*53585189SAndreas Gohr        $this->assertEquals(ApiCore::API_VERSION, $this->remote->call('dokuwiki.getXMLRPCAPIVersion'));
516*53585189SAndreas Gohr    }
517*53585189SAndreas Gohr
518*53585189SAndreas Gohr    public function testGetRPCVersionSupported()
519*53585189SAndreas Gohr    {
520*53585189SAndreas Gohr        $this->assertEquals(2, $this->remote->call('wiki.getRPCVersionSupported'));
521*53585189SAndreas Gohr    }
522*53585189SAndreas Gohr
523*53585189SAndreas Gohr    public function testListLinks()
524*53585189SAndreas Gohr    {
525*53585189SAndreas Gohr        $localdoku = [
526*53585189SAndreas Gohr            'type' => 'local',
527*53585189SAndreas Gohr            'page' => 'DokuWiki',
528*53585189SAndreas Gohr            'href' => DOKU_BASE . DOKU_SCRIPT . '?id=DokuWiki'
529*53585189SAndreas Gohr        ];
530*53585189SAndreas Gohr        $expected = [  //no local links
531*53585189SAndreas Gohr            $localdoku,
532*53585189SAndreas Gohr            [
533*53585189SAndreas Gohr                'type' => 'extern',
534*53585189SAndreas Gohr                'page' => 'http://www.freelists.org',
535*53585189SAndreas Gohr                'href' => 'http://www.freelists.org'
536*53585189SAndreas Gohr            ],
537*53585189SAndreas Gohr            [
538*53585189SAndreas Gohr                'type' => 'extern',
539*53585189SAndreas Gohr                'page' => 'https://tools.ietf.org/html/rfc1855',
540*53585189SAndreas Gohr                'href' => 'https://tools.ietf.org/html/rfc1855'
541*53585189SAndreas Gohr            ],
542*53585189SAndreas Gohr            [
543*53585189SAndreas Gohr                'type' => 'extern',
544*53585189SAndreas Gohr                'page' => 'http://www.catb.org/~esr/faqs/smart-questions.html',
545*53585189SAndreas Gohr                'href' => 'http://www.catb.org/~esr/faqs/smart-questions.html'
546*53585189SAndreas Gohr            ],
547*53585189SAndreas Gohr            $localdoku,
548*53585189SAndreas Gohr            $localdoku
549*53585189SAndreas Gohr        ];
550*53585189SAndreas Gohr        $params = ['mailinglist'];
551*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.listLinks', $params));
552*53585189SAndreas Gohr    }
553*53585189SAndreas Gohr
554*53585189SAndreas Gohr    public function testCoreattachments()
555*53585189SAndreas Gohr    {
556*53585189SAndreas Gohr        global $conf;
557*53585189SAndreas Gohr        global $AUTH_ACL, $USERINFO;
558*53585189SAndreas Gohr
559*53585189SAndreas Gohr        $filecontent = io_readFile(mediaFN('wiki:dokuwiki-128.png'), false);
560*53585189SAndreas Gohr        $params = ['test:dokuwiki-128_2.png', $filecontent, ['ow' => false]];
561*53585189SAndreas Gohr        $this->assertEquals('test:dokuwiki-128_2.png', $this->remote->call('wiki.putAttachment', $params)); //prints a success div
562*53585189SAndreas Gohr
563*53585189SAndreas Gohr        $params = ['test:dokuwiki-128_2.png'];
564*53585189SAndreas Gohr        $this->assertEquals($filecontent, $this->remote->call('wiki.getAttachment', $params));
565*53585189SAndreas Gohr        $rev = filemtime(mediaFN('test:dokuwiki-128_2.png'));
566*53585189SAndreas Gohr
567*53585189SAndreas Gohr        $expected = [
568*53585189SAndreas Gohr            'lastModified' => $rev,
569*53585189SAndreas Gohr            'size' => 27895,
570*53585189SAndreas Gohr        ];
571*53585189SAndreas Gohr        $params = ['test:dokuwiki-128_2.png'];
572*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
573*53585189SAndreas Gohr
574*53585189SAndreas Gohr        $params = [strtotime("-5 year")];
575*53585189SAndreas Gohr        $expected = [
576*53585189SAndreas Gohr            [
577*53585189SAndreas Gohr                'name' => 'test:dokuwiki-128_2.png',
578*53585189SAndreas Gohr                'lastModified' => $rev,
579*53585189SAndreas Gohr                'author' => '',
580*53585189SAndreas Gohr                'version' => $rev,
581*53585189SAndreas Gohr                'perms' => 8,
582*53585189SAndreas Gohr                'size' => 27895 //actual size, not size change
583*53585189SAndreas Gohr            ]
584*53585189SAndreas Gohr        ];
585*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getRecentMediaChanges', $params));
586*53585189SAndreas Gohr
587*53585189SAndreas Gohr        $this->waitForTick(true);
588*53585189SAndreas Gohr        $conf['useacl'] = 1;
589*53585189SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'john';
590*53585189SAndreas Gohr        $USERINFO['grps'] = ['user'];
591*53585189SAndreas Gohr        $AUTH_ACL = [
592*53585189SAndreas Gohr            '*                  @ALL           0',
593*53585189SAndreas Gohr            '*                  @user          16',
594*53585189SAndreas Gohr        ];
595*53585189SAndreas Gohr
596*53585189SAndreas Gohr        $params = ['test:dokuwiki-128_2.png'];
597*53585189SAndreas Gohr        $this->assertEquals(0, $this->remote->call('wiki.deleteAttachment', $params));
598*53585189SAndreas Gohr
599*53585189SAndreas Gohr        $rev2 = filemtime($conf['media_changelog']);
600*53585189SAndreas Gohr        $expected = [
601*53585189SAndreas Gohr            'lastModified' => $rev2,
602*53585189SAndreas Gohr            'size' => 0,
603*53585189SAndreas Gohr        ];
604*53585189SAndreas Gohr        $params = ['test:dokuwiki-128_2.png'];
605*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
606*53585189SAndreas Gohr
607*53585189SAndreas Gohr        $expected = [
608*53585189SAndreas Gohr            'lastModified' => 0,
609*53585189SAndreas Gohr            'size' => 0,
610*53585189SAndreas Gohr        ];
611*53585189SAndreas Gohr        $params = ['test:nonexisting.png'];
612*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
613*53585189SAndreas Gohr
614*53585189SAndreas Gohr        $media1 = mediaFN('wiki:dokuwiki-128.png');
615*53585189SAndreas Gohr        $expected = [
616*53585189SAndreas Gohr            [
617*53585189SAndreas Gohr                'id' => 'wiki:dokuwiki-128.png',
618*53585189SAndreas Gohr                'file' => 'dokuwiki-128.png',
619*53585189SAndreas Gohr                'size' => filesize($media1),
620*53585189SAndreas Gohr                'mtime' => filemtime($media1),
621*53585189SAndreas Gohr                'writable' => 1,
622*53585189SAndreas Gohr                'isimg' => 1,
623*53585189SAndreas Gohr                'hash' => md5(io_readFile($media1, false)),
624*53585189SAndreas Gohr                'perms' => 16,
625*53585189SAndreas Gohr                'lastModified' => filemtime($media1)
626*53585189SAndreas Gohr            ]
627*53585189SAndreas Gohr        ];
628*53585189SAndreas Gohr        $params = [
629*53585189SAndreas Gohr            'wiki:',
630*53585189SAndreas Gohr            [
631*53585189SAndreas Gohr                'depth' => 0, // 0 for all
632*53585189SAndreas Gohr                'hash' => 1,
633*53585189SAndreas Gohr                'skipacl' => 1, // is ignored
634*53585189SAndreas Gohr                'showmsg' => true, //useless??
635*53585189SAndreas Gohr                'pattern' => '/128/' //filter
636*53585189SAndreas Gohr            ]
637*53585189SAndreas Gohr        ];
638*53585189SAndreas Gohr        $this->assertEquals($expected, $this->remote->call('wiki.getAttachments', $params));
639*53585189SAndreas Gohr    }
640*53585189SAndreas Gohr
641*53585189SAndreas Gohr}
642