another set of api tests

This commit is contained in:
Andreas Gohr 2023-12-01 15:11:34 +01:00
parent 1468a1289a
commit 535851891f
3 changed files with 214 additions and 184 deletions

View File

@ -1,23 +1,28 @@
<?php
namespace dokuwiki\test\Remote;
use dokuwiki\Extension\Event;
use dokuwiki\Remote\Api;
use dokuwiki\Remote\ApiCore;
use dokuwiki\test\mock\AuthPlugin;
use dokuwiki\test\mock\AuthDeletePlugin;
use dokuwiki\test\mock\AuthPlugin;
/**
* Class remoteapicore_test
*/
class remoteapicore_test extends DokuWikiTest {
class ApiCoreTest extends \DokuWikiTest
{
protected $userinfo;
protected $oldAuthAcl;
/** @var Api */
protected $remote;
public function setUp() : void {
public function setUp(): void
{
// we need a clean setup before each single test:
DokuWikiTest::setUpBeforeClass();
\DokuWikiTest::setUpBeforeClass();
parent::setUp();
global $conf;
@ -35,7 +40,8 @@ class remoteapicore_test extends DokuWikiTest {
$this->remote = new Api();
}
public function tearDown() : void {
public function tearDown(): void
{
parent::tearDown();
global $USERINFO;
@ -46,53 +52,57 @@ class remoteapicore_test extends DokuWikiTest {
}
/** Delay writes of old revisions by a second. */
public function handle_write(Doku_Event $event, $param) {
public function handle_write(Event $event, $param)
{
if ($event->data[3] !== false) {
$this->waitForTick();
}
}
public function test_getVersion() {
public function testGetVersion()
{
$this->assertEquals(getVersion(), $this->remote->call('dokuwiki.getVersion'));
}
public function test_getPageList() {
public function testGetPageList()
{
$file1 = wikiFN('wiki:dokuwiki');
$file2 = wikiFN('wiki:syntax');
$expected = array(
array(
$expected = [
[
'id' => 'wiki:dokuwiki',
'rev' => filemtime($file1),
'mtime' => filemtime($file1),
'size' => filesize($file1),
'hash' => md5(trim(rawWiki('wiki:dokuwiki')))
),
array(
],
[
'id' => 'wiki:syntax',
'rev' => filemtime($file2),
'mtime' => filemtime($file2),
'size' => filesize($file2),
'hash' => md5(trim(rawWiki('wiki:syntax')))
)
);
$params = array(
]
];
$params = [
'wiki:',
array(
[
'depth' => 0, // 0 for all
'hash' => 1,
'skipacl' => 1 // is ignored
)
);
]
];
$this->assertEquals($expected, $this->remote->call('dokuwiki.getPagelist', $params));
}
public function test_search() {
public function testSearch()
{
$id = 'wiki:syntax';
$file = wikiFN($id);
idx_addPage($id); //full text search depends on index
$expected = array(
array(
$expected = [
[
'id' => $id,
'score' => 1,
'rev' => filemtime($file),
@ -104,105 +114,112 @@ class remoteapicore_test extends DokuWikiTest {
You can use up to five different levels of',
'title' => 'wiki:syntax'
)
);
$params = array('Sectioning');
]
];
$params = ['Sectioning'];
$this->assertEquals($expected, $this->remote->call('dokuwiki.search', $params));
}
public function test_getTime() {
public function testGetTime()
{
$timeexpect = time();
$timeactual = $this->remote->call('dokuwiki.getTime');
$this->assertTrue(($timeexpect <= $timeactual) && ($timeactual <= $timeexpect + 1));
}
public function test_setLocks() {
$expected = array(
'locked' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
'lockfail' => array(),
'unlocked' => array(),
'unlockfail' => array(),
);
$params = array(
array(
'lock' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
'unlock' => array()
)
);
public function testSetLocks()
{
$expected = [
'locked' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
'lockfail' => [],
'unlocked' => [],
'unlockfail' => [],
];
$params = [
[
'lock' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
'unlock' => []
]
];
$this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
$expected = array(
'locked' => array(),
'lockfail' => array(),
'unlocked' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting'),
'unlockfail' => array('nonexisting2'),
);
$params = array(
array(
'lock' => array(),
'unlock' => array('wiki:dokuwiki', 'wiki:syntax', 'nonexisting', 'nonexisting2')
)
);
$expected = [
'locked' => [],
'lockfail' => [],
'unlocked' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting'],
'unlockfail' => ['nonexisting2'],
];
$params = [
[
'lock' => [],
'unlock' => ['wiki:dokuwiki', 'wiki:syntax', 'nonexisting', 'nonexisting2']
]
];
$this->assertEquals($expected, $this->remote->call('dokuwiki.setLocks', $params));
}
public function test_getTitle() {
public function testGetTitle()
{
global $conf;
$this->assertEquals($conf['title'], $this->remote->call('dokuwiki.getTitle'));
}
public function test_putPage() {
public function testPutPage()
{
$id = 'putpage';
$content = "====Title====\nText";
$params = array(
$params = [
$id,
$content,
array(
[
'minor' => false,
'sum' => 'Summary of nice text'
)
);
]
];
$this->assertTrue($this->remote->call('wiki.putPage', $params));
$this->assertEquals($content, rawWiki($id));
//remove page
$params = array(
$params = [
$id,
'',
array(
[
'minor' => false,
)
);
]
];
$this->assertTrue($this->remote->call('wiki.putPage', $params));
$this->assertFileNotExists(wikiFN($id));
}
public function test_getPage() {
public function testGetPage()
{
$id = 'getpage';
$content = 'a test';
saveWikiText($id, $content, 'test for getpage');
$params = array($id);
$params = [$id];
$this->assertEquals($content, $this->remote->call('wiki.getPage', $params));
}
public function test_appendPage() {
public function testAppendPage()
{
$id = 'appendpage';
$content = 'a test';
$morecontent = "\nOther text";
saveWikiText($id, $content, 'local');
$params = array(
$params = [
$id,
$morecontent,
array()
);
[]
];
$this->assertEquals(true, $this->remote->call('dokuwiki.appendPage', $params));
$this->assertEquals($content . $morecontent, rawWiki($id));
}
public function test_getPageVersion() {
public function testGetPageVersion()
{
$id = 'pageversion';
$file = wikiFN($id);
@ -213,34 +230,36 @@ You can use up to five different levels of',
saveWikiText($id, 'second version', 'second');
$rev2 = filemtime($file);
$params = array($id, '');
$params = [$id, ''];
$this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), 'no revision given');
$params = array($id, $rev1);
$params = [$id, $rev1];
$this->assertEquals('first version', $this->remote->call('wiki.getPageVersion', $params), '1st revision given');
$params = array($id, $rev2);
$params = [$id, $rev2];
$this->assertEquals('second version', $this->remote->call('wiki.getPageVersion', $params), '2nd revision given');
$params = array($id, 1234);
$params = [$id, 1234];
$this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing revision given');
$params = array('foobar', 1234);
$params = ['foobar', 1234];
$this->assertEquals('', $this->remote->call('wiki.getPageVersion', $params), 'Non existing page given');
}
public function test_getPageHTML() {
public function testGetPageHTML()
{
$id = 'htmltest';
$content = "====Title====\nText";
$html = "\n<h3 class=\"sectionedit1\" id=\"title\">Title</h3>\n<div class=\"level3\">\n\n<p>\nText\n</p>\n\n</div>\n";
saveWikiText($id, $content, 'htmltest');
$params = array($id);
$params = [$id];
$this->assertEquals($html, $this->remote->call('wiki.getPageHTML', $params));
}
public function test_getPageHTMLVersion() {
public function testGetPageHTMLVersion()
{
$id = 'htmltest';
$file = wikiFN($id);
@ -256,23 +275,24 @@ You can use up to five different levels of',
saveWikiText($id, $content2, 'second');
$rev2 = filemtime($file);
$params = array($id, '');
$params = [$id, ''];
$this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), 'no revision given');
$params = array($id, $rev1);
$params = [$id, $rev1];
$this->assertEquals($html1, $this->remote->call('wiki.getPageHTMLVersion', $params), '1st revision given');
$params = array($id, $rev2);
$params = [$id, $rev2];
$this->assertEquals($html2, $this->remote->call('wiki.getPageHTMLVersion', $params), '2nd revision given');
$params = array($id, 1234);
$params = [$id, 1234];
$this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing revision given');
$params = array('foobar', 1234);
$params = ['foobar', 1234];
$this->assertEquals('', $this->remote->call('wiki.getPageHTMLVersion', $params), 'Non existing page given');
}
public function test_getAllPages() {
public function testGetAllPages()
{
// all pages depends on index
idx_addPage('wiki:syntax');
idx_addPage('wiki:dokuwiki');
@ -280,52 +300,55 @@ You can use up to five different levels of',
$file1 = wikiFN('wiki:syntax');
$file2 = wikiFN('wiki:dokuwiki');
$expected = array(
array(
$expected = [
[
'id' => 'wiki:syntax',
'perms' => 8,
'size' => filesize($file1),
'lastModified' => filemtime($file1)
),
array(
],
[
'id' => 'wiki:dokuwiki',
'perms' => 8,
'size' => filesize($file2),
'lastModified' => filemtime($file2)
)
);
]
];
$this->assertEquals($expected, $this->remote->call('wiki.getAllPages'));
}
public function test_getBacklinks() {
public function testGetBacklinks()
{
saveWikiText('linky', '[[wiki:syntax]]', 'test');
// backlinks need index
idx_addPage('wiki:syntax');
idx_addPage('linky');
$params = array('wiki:syntax');
$params = ['wiki:syntax'];
$result = $this->remote->call('wiki.getBackLinks', $params);
$this->assertTrue(count($result) > 0);
$this->assertEquals(ft_backlinks('wiki:syntax'), $result);
}
public function test_getPageInfo() {
public function testGetPageInfo()
{
$id = 'pageinfo';
$file = wikiFN($id);
saveWikiText($id, 'test', 'test');
$expected = array(
$expected = [
'name' => $id,
'lastModified' => filemtime($file),
'author' => clientIP(),
'version' => filemtime($file)
);
$params = array($id);
];
$params = [$id];
$this->assertEquals($expected, $this->remote->call('wiki.getPageInfo', $params));
}
public function test_getPageInfoVersion() {
public function testGetPageInfoVersion()
{
$id = 'pageinfo';
$file = wikiFN($id);
@ -336,64 +359,66 @@ You can use up to five different levels of',
saveWikiText($id, 'second version', 'second');
$rev2 = filemtime($file);
$expected = array(
$expected = [
'name' => $id,
'lastModified' => $rev2,
'author' => clientIP(),
'version' => $rev2
);
$params = array($id, '');
];
$params = [$id, ''];
$this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), 'no revision given');
$expected = array(
$expected = [
'name' => $id,
'lastModified' => $rev1,
'author' => clientIP(),
'version' => $rev1
);
$params = array($id, $rev1);
];
$params = [$id, $rev1];
$this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '1st revision given');
$expected = array(
$expected = [
'name' => $id,
'lastModified' => $rev2,
'author' => clientIP(),
'version' => $rev2
);
$params = array($id, $rev2);
];
$params = [$id, $rev2];
$this->assertEquals($expected, $this->remote->call('wiki.getPageInfoVersion', $params), '2nd revision given');
}
public function test_getRecentChanges() {
public function testGetRecentChanges()
{
saveWikiText('pageone', 'test', 'test');
$rev1 = filemtime(wikiFN('pageone'));
saveWikiText('pagetwo', 'test', 'test');
$rev2 = filemtime(wikiFN('pagetwo'));
$expected = array(
array(
$expected = [
[
'name' => 'pageone',
'lastModified' => $rev1,
'author' => '',
'version' => $rev1,
'perms' => 8,
'size' => 4
),
array(
],
[
'name' => 'pagetwo',
'lastModified' => $rev2,
'author' => '',
'version' => $rev2,
'perms' => 8,
'size' => 4
)
);
$params = array(strtotime("-1 year"));
]
];
$params = [strtotime("-1 year")];
$this->assertEquals($expected, $this->remote->call('wiki.getRecentChanges', $params));
}
public function test_getPageVersions() {
public function testGetPageVersions()
{
/** @var $EVENT_HANDLER \dokuwiki\Extension\EventHandler */
global $EVENT_HANDLER;
$EVENT_HANDLER->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_write');
@ -402,15 +427,15 @@ You can use up to five different levels of',
$id = 'revpage';
$file = wikiFN($id);
$rev = array();
for($i = 0; $i < 6; $i++) {
$rev = [];
for ($i = 0; $i < 6; $i++) {
$this->waitForTick();
saveWikiText($id, "rev$i", "rev$i");
clearstatcache(false, $file);
$rev[$i] = filemtime($file);
}
$params = array($id, 0);
$params = [$id, 0];
$versions = $this->remote->call('wiki.getPageVersions', $params);
$this->assertEquals(6, count($versions));
$this->assertEquals($rev[5], $versions[0]['version']);
@ -420,7 +445,7 @@ You can use up to five different levels of',
$this->assertEquals($rev[1], $versions[4]['version']);
$this->assertEquals($rev[0], $versions[5]['version']);
$params = array($id, 1); // offset 1
$params = [$id, 1]; // offset 1
$versions = $this->remote->call('wiki.getPageVersions', $params);
$this->assertEquals(5, count($versions));
$this->assertEquals($rev[4], $versions[0]['version']);
@ -431,26 +456,26 @@ You can use up to five different levels of',
$conf['recent'] = 3; //set number of results per page
$params = array($id, 0); // first page
$params = [$id, 0]; // first page
$versions = $this->remote->call('wiki.getPageVersions', $params);
$this->assertEquals(3, count($versions));
$this->assertEquals($rev[5], $versions[0]['version']);
$this->assertEquals($rev[4], $versions[1]['version']);
$this->assertEquals($rev[3], $versions[2]['version']);
$params = array($id, $conf['recent']); // second page
$params = [$id, $conf['recent']]; // second page
$versions = $this->remote->call('wiki.getPageVersions', $params);
$this->assertEquals(3, count($versions));
$this->assertEquals($rev[2], $versions[0]['version']);
$this->assertEquals($rev[1], $versions[1]['version']);
$this->assertEquals($rev[0], $versions[2]['version']);
$params = array($id, $conf['recent'] * 2); // third page
$params = [$id, $conf['recent'] * 2]; // third page
$versions = $this->remote->call('wiki.getPageVersions', $params);
$this->assertEquals(0, count($versions));
}
public function test_deleteUser()
public function testDeleteUser()
{
global $conf, $auth;
$auth = new AuthDeletePlugin();
@ -464,126 +489,131 @@ You can use up to five different levels of',
$this->assertTrue($actualCallResult);
}
public function test_aclCheck() {
public function testAclCheck()
{
$id = 'aclpage';
$params = array($id);
$params = [$id];
$this->assertEquals(AUTH_UPLOAD, $this->remote->call('wiki.aclCheck', $params));
global $conf;
global $AUTH_ACL, $USERINFO;
$conf['useacl'] = 1;
$_SERVER['REMOTE_USER'] = 'john';
$USERINFO['grps'] = array('user');
$AUTH_ACL = array(
$USERINFO['grps'] = ['user'];
$AUTH_ACL = [
'* @ALL 0',
'* @user 2', //edit
);
];
$params = array($id);
$params = [$id];
$this->assertEquals(AUTH_EDIT, $this->remote->call('wiki.aclCheck', $params));
}
public function test_getXMLRPCAPIVersion() {
public function testGetXMLRPCAPIVersion()
{
$this->assertEquals(ApiCore::API_VERSION, $this->remote->call('dokuwiki.getXMLRPCAPIVersion'));
}
public function test_getRPCVersionSupported() {
public function testGetRPCVersionSupported()
{
$this->assertEquals(2, $this->remote->call('wiki.getRPCVersionSupported'));
}
public function test_listLinks() {
$localdoku = array(
public function testListLinks()
{
$localdoku = [
'type' => 'local',
'page' => 'DokuWiki',
'href' => DOKU_BASE . DOKU_SCRIPT . '?id=DokuWiki'
);
$expected = array( //no local links
$localdoku,
array(
'type' => 'extern',
'page' => 'http://www.freelists.org',
'href' => 'http://www.freelists.org'
),
array(
'type' => 'extern',
'page' => 'https://tools.ietf.org/html/rfc1855',
'href' => 'https://tools.ietf.org/html/rfc1855'
),
array(
'type' => 'extern',
'page' => 'http://www.catb.org/~esr/faqs/smart-questions.html',
'href' => 'http://www.catb.org/~esr/faqs/smart-questions.html'
),
$localdoku,
$localdoku
);
$params = array('mailinglist');
];
$expected = [ //no local links
$localdoku,
[
'type' => 'extern',
'page' => 'http://www.freelists.org',
'href' => 'http://www.freelists.org'
],
[
'type' => 'extern',
'page' => 'https://tools.ietf.org/html/rfc1855',
'href' => 'https://tools.ietf.org/html/rfc1855'
],
[
'type' => 'extern',
'page' => 'http://www.catb.org/~esr/faqs/smart-questions.html',
'href' => 'http://www.catb.org/~esr/faqs/smart-questions.html'
],
$localdoku,
$localdoku
];
$params = ['mailinglist'];
$this->assertEquals($expected, $this->remote->call('wiki.listLinks', $params));
}
public function test_coreattachments() {
public function testCoreattachments()
{
global $conf;
global $AUTH_ACL, $USERINFO;
$filecontent = io_readFile(mediaFN('wiki:dokuwiki-128.png'), false);
$params = array('test:dokuwiki-128_2.png', $filecontent, array('ow' => false));
$params = ['test:dokuwiki-128_2.png', $filecontent, ['ow' => false]];
$this->assertEquals('test:dokuwiki-128_2.png', $this->remote->call('wiki.putAttachment', $params)); //prints a success div
$params = array('test:dokuwiki-128_2.png');
$params = ['test:dokuwiki-128_2.png'];
$this->assertEquals($filecontent, $this->remote->call('wiki.getAttachment', $params));
$rev = filemtime(mediaFN('test:dokuwiki-128_2.png'));
$expected = array(
$expected = [
'lastModified' => $rev,
'size' => 27895,
);
$params = array('test:dokuwiki-128_2.png');
];
$params = ['test:dokuwiki-128_2.png'];
$this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
$params = array(strtotime("-5 year"));
$expected = array(
array(
$params = [strtotime("-5 year")];
$expected = [
[
'name' => 'test:dokuwiki-128_2.png',
'lastModified' => $rev,
'author' => '',
'version' => $rev,
'perms' => 8,
'size' => 27895 //actual size, not size change
)
);
]
];
$this->assertEquals($expected, $this->remote->call('wiki.getRecentMediaChanges', $params));
$this->waitForTick(true);
$conf['useacl'] = 1;
$_SERVER['REMOTE_USER'] = 'john';
$USERINFO['grps'] = array('user');
$AUTH_ACL = array(
$USERINFO['grps'] = ['user'];
$AUTH_ACL = [
'* @ALL 0',
'* @user 16',
);
];
$params = array('test:dokuwiki-128_2.png');
$params = ['test:dokuwiki-128_2.png'];
$this->assertEquals(0, $this->remote->call('wiki.deleteAttachment', $params));
$rev2 = filemtime($conf['media_changelog']);
$expected = array(
$expected = [
'lastModified' => $rev2,
'size' => 0,
);
$params = array('test:dokuwiki-128_2.png');
];
$params = ['test:dokuwiki-128_2.png'];
$this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
$expected = array(
$expected = [
'lastModified' => 0,
'size' => 0,
);
$params = array('test:nonexisting.png');
];
$params = ['test:nonexisting.png'];
$this->assertEquals($expected, $this->remote->call('wiki.getAttachmentInfo', $params));
$media1 = mediaFN('wiki:dokuwiki-128.png');
$expected = array(
array(
$expected = [
[
'id' => 'wiki:dokuwiki-128.png',
'file' => 'dokuwiki-128.png',
'size' => filesize($media1),
@ -593,18 +623,18 @@ You can use up to five different levels of',
'hash' => md5(io_readFile($media1, false)),
'perms' => 16,
'lastModified' => filemtime($media1)
)
);
$params = array(
]
];
$params = [
'wiki:',
array(
[
'depth' => 0, // 0 for all
'hash' => 1,
'skipacl' => 1, // is ignored
'showmsg' => true, //useless??
'pattern' => '/128/' //filter
)
);
]
];
$this->assertEquals($expected, $this->remote->call('wiki.getAttachments', $params));
}

View File

@ -395,8 +395,8 @@ class ApiCore
$id = $this->resolvePageId($id);
$TEXT = cleanText($text);
$sum = $params['sum'];
$minor = $params['minor'];
$sum = $params['sum'] ?? '';
$minor = $params['minor'] ?? false;
if (empty($id)) {
throw new RemoteException('Empty page ID', 131);

View File

@ -13,7 +13,7 @@ class remote_plugin_acl extends RemotePlugin
*
* @return array Information about all provided methods. {@see dokuwiki\Remote\RemoteAPI}
*/
public function _getMethods()
public function getMethods()
{
return [
'listAcls' => [