1<?php
2
3use dokuwiki\test\mock\Doku_Renderer;
4
5/**
6 * Tests for Doku_Renderer::_resolveInterWiki()
7 */
8class Test_resolveInterwiki extends DokuWikiTest {
9
10    function testDefaults() {
11        $Renderer = new Doku_Renderer();
12        $Renderer->interwiki = getInterwiki();
13        $Renderer->interwiki['scheme'] = '{SCHEME}://example.com';
14        $Renderer->interwiki['withslash'] = '/test';
15        $Renderer->interwiki['onlytext'] = ':onlytext{NAME}'; //with {URL} double urlencoded
16        $Renderer->interwiki['withquery'] = ':anyns:{NAME}?do=edit';
17        //this was the only link with host/port/path/query. Keep it here for regression
18        $Renderer->interwiki['coral'] = 'http://{HOST}.{PORT}.nyud.net:8090{PATH}?{QUERY}';
19
20        $tests = array(
21            // shortcut, reference and expected
22            array('wp', 'foo [\\]^`{|}~@+#%?/#txt', 'https://en.wikipedia.org/wiki/foo %5B%5C%5D%5E%60%7B%7C%7D~@+%23%25?/#txt'),
23            array('amazon', 'foo [\\]^`{|}~@+#%?/#txt', 'https://www.amazon.com/dp/foo%20%5B%5C%5D%5E%60%7B%7C%7D~%40%2B%23%25%3F%2F?tag=splitbrain-20#txt'),
24            array('doku', 'foo [\\]^`{|}~@+#%?/#txt', 'https://www.dokuwiki.org/foo%20%5B%5C%5D%5E%60%7B%7C%7D~%40%2B%23%25%3F%2F#txt'),
25            array('coral', 'http://example.com:83/path/naar/?query=foo%20%40%2B%25%3F%2F', 'http://example.com.83.nyud.net:8090/path/naar/?query=foo%20%40%2B%25%3F%2F'),
26            array('scheme', 'ftp://foo @+%/#txt', 'ftp://example.com#txt'),
27            //relative url
28            array('withslash', 'foo [\\]^`{|}~@+#%?/#txt', '/testfoo%20%5B%5C%5D%5E%60%7B%7C%7D~%40%2B%23%25%3F%2F#txt'),
29            array('skype',  'foo [\\]^`{|}~@+#%?/#txt', 'skype:foo %5B%5C%5D%5E%60%7B%7C%7D~@+%23%25?/#txt'),
30            //dokuwiki id's
31            array('onlytext', 'foo [\\]^`{|}~@+#%/#txt', DOKU_BASE.'doku.php?id=onlytextfoo#txt'),
32            array('user', 'foo [\\]^`{|}~@+#%/#txt', DOKU_BASE.'doku.php?id=user:foo#txt'),
33            array('withquery', 'foo [\\]^`{|}~@+#%/#txt', DOKU_BASE.'doku.php?id=anyns:foo&amp;do=edit#txt')
34        );
35
36        foreach($tests as $test) {
37            $url = $Renderer->_resolveInterWiki($test[0], $test[1]);
38
39            $this->assertEquals($test[2], $url);
40        }
41    }
42
43    function testNonexisting() {
44        $Renderer = new Doku_Renderer();
45        $Renderer->interwiki = getInterwiki();
46        unset($Renderer->interwiki['default']);
47
48        $shortcut = 'nonexisting';
49        $reference = 'foo @+%/';
50        $url = $Renderer->_resolveInterWiki($shortcut, $reference);
51
52        $this->assertEquals('', $url);
53        $this->assertEquals('', $shortcut);
54    }
55
56    function testNonexistingWithDefault() {
57        $Renderer = new Doku_Renderer();
58        $Renderer->interwiki = getInterwiki();
59        $Renderer->interwiki['default'] = 'https://en.wikipedia.org/wiki/{NAME}';
60
61        $shortcut = 'nonexisting';
62        $reference = 'foo';
63        $url = $Renderer->_resolveInterWiki($shortcut, $reference);
64
65        $this->assertEquals('https://en.wikipedia.org/wiki/foo', $url);
66        $this->assertEquals('default', $shortcut);
67    }
68
69}
70