1<?php
2
3namespace dokuwiki\test\Remote;
4
5
6use dokuwiki\test\Remote\Mock\JsonRpcServer;
7
8/**
9 * @todo test different request formats
10 */
11class JsonRpcServerTest extends \DokuWikiTest
12{
13    protected $server;
14
15    function setUp(): void
16    {
17        parent::setUp();
18        global $conf;
19
20        $conf['remote'] = 1;
21        $conf['remoteuser'] = '';
22        $conf['useacl'] = 0;
23
24        $this->server = new JsonRpcServer();
25    }
26
27
28    function testFullArgs()
29    {
30        $_SERVER['CONTENT_TYPE'] = 'application/json';
31        $_SERVER['REQUEST_METHOD'] = 'POST';
32        $_SERVER['PATH_INFO'] = '/wiki.twoArgWithDefaultArg';
33
34        $positional = json_encode(['arg1', 'arg2']);
35        $named = json_encode(['string1' => 'arg1', 'string2' => 'arg2']);
36        $expect = json_encode(['arg1', 'arg2']);
37
38        $response = json_encode($this->server->serve($positional)['result']);
39        $this->assertJsonStringEqualsJsonString($expect, $response);
40
41        $response = json_encode($this->server->serve($named)['result']);
42        $this->assertJsonStringEqualsJsonString($expect, $response);
43    }
44
45    function testDefaultArgs()
46    {
47        $_SERVER['CONTENT_TYPE'] = 'application/json';
48        $_SERVER['REQUEST_METHOD'] = 'POST';
49        $_SERVER['PATH_INFO'] = '/wiki.twoArgWithDefaultArg';
50
51        $positional = json_encode(['arg1']);
52        $named = json_encode(['string1' => 'arg1']);
53        $expect = json_encode(['arg1', 'default']);
54
55        $response = json_encode($this->server->serve($positional)['result']);
56        $this->assertJsonStringEqualsJsonString($expect, $response);
57
58        $response = json_encode($this->server->serve($named)['result']);
59        $this->assertJsonStringEqualsJsonString($expect, $response);
60    }
61
62    function testStructResponse()
63    {
64        $_SERVER['CONTENT_TYPE'] = 'application/json';
65        $_SERVER['REQUEST_METHOD'] = 'POST';
66        $_SERVER['PATH_INFO'] = '/wiki.getStructuredData';
67
68        $expect = json_encode([
69            'type' => 'internal',
70            'page' => 'wiki:dokuwiki',
71            'href' => 'https://www.dokuwiki.org/wiki:dokuwiki'
72        ]);
73
74        $response = json_encode($this->server->serve('[]')['result']);
75        $this->assertJsonStringEqualsJsonString($expect, $response);
76    }
77}
78