xref: /dokuwiki/_test/tests/Parsing/ParserMode/InternallinkTest.php (revision 465aec673bd9c2018b4df5364f08c816a70f03a7)
1*465aec67SAndreas Gohr<?php
2*465aec67SAndreas Gohr
3*465aec67SAndreas Gohrnamespace dokuwiki\test\Parsing\ParserMode;
4*465aec67SAndreas Gohr
5*465aec67SAndreas Gohruse dokuwiki\Parsing\ParserMode\Internallink;
6*465aec67SAndreas Gohr
7*465aec67SAndreas Gohr/**
8*465aec67SAndreas Gohr * Tests for the {@see Internallink} parser mode: `[[target|title]]` dispatch.
9*465aec67SAndreas Gohr *
10*465aec67SAndreas Gohr * Covers internal pages, namespaces, section refs, interwiki labels, and the cases where the target
11*465aec67SAndreas Gohr * dispatches to externallink (URL inside [[ ]]), emaillink (email inside [[ ]]), windowssharelink, or
12*465aec67SAndreas Gohr * a media payload as the link title.
13*465aec67SAndreas Gohr *
14*465aec67SAndreas Gohr * @group parser_links
15*465aec67SAndreas Gohr */
16*465aec67SAndreas Gohrclass InternallinkTest extends ParserTestBase
17*465aec67SAndreas Gohr{
18*465aec67SAndreas Gohr    function testOneChar() {
19*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
20*465aec67SAndreas Gohr        $this->P->parse("Foo [[l]] Bar");
21*465aec67SAndreas Gohr        $calls = [
22*465aec67SAndreas Gohr            ['document_start', []],
23*465aec67SAndreas Gohr            ['p_open', []],
24*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
25*465aec67SAndreas Gohr            ['internallink', ['l', null]],
26*465aec67SAndreas Gohr            ['cdata', [' Bar']],
27*465aec67SAndreas Gohr            ['p_close', []],
28*465aec67SAndreas Gohr            ['document_end', []],
29*465aec67SAndreas Gohr        ];
30*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
31*465aec67SAndreas Gohr    }
32*465aec67SAndreas Gohr
33*465aec67SAndreas Gohr    function testNoChar() {
34*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
35*465aec67SAndreas Gohr        $this->P->parse("Foo [[]] Bar");
36*465aec67SAndreas Gohr        $calls = [
37*465aec67SAndreas Gohr            ['document_start', []],
38*465aec67SAndreas Gohr            ['p_open', []],
39*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
40*465aec67SAndreas Gohr            ['internallink', ['', null]],
41*465aec67SAndreas Gohr            ['cdata', [' Bar']],
42*465aec67SAndreas Gohr            ['p_close', []],
43*465aec67SAndreas Gohr            ['document_end', []],
44*465aec67SAndreas Gohr        ];
45*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
46*465aec67SAndreas Gohr    }
47*465aec67SAndreas Gohr
48*465aec67SAndreas Gohr    function testNamespaceNoTitle() {
49*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
50*465aec67SAndreas Gohr        $this->P->parse("Foo [[foo:bar]] Bar");
51*465aec67SAndreas Gohr        $calls = [
52*465aec67SAndreas Gohr            ['document_start', []],
53*465aec67SAndreas Gohr            ['p_open', []],
54*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
55*465aec67SAndreas Gohr            ['internallink', ['foo:bar', null]],
56*465aec67SAndreas Gohr            ['cdata', [' Bar']],
57*465aec67SAndreas Gohr            ['p_close', []],
58*465aec67SAndreas Gohr            ['document_end', []],
59*465aec67SAndreas Gohr        ];
60*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
61*465aec67SAndreas Gohr    }
62*465aec67SAndreas Gohr
63*465aec67SAndreas Gohr    function testNamespace() {
64*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
65*465aec67SAndreas Gohr        $this->P->parse("Foo [[x:1:y:foo_bar:z|Test]] Bar");
66*465aec67SAndreas Gohr        $calls = [
67*465aec67SAndreas Gohr            ['document_start', []],
68*465aec67SAndreas Gohr            ['p_open', []],
69*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
70*465aec67SAndreas Gohr            ['internallink', ['x:1:y:foo_bar:z', 'Test']],
71*465aec67SAndreas Gohr            ['cdata', [' Bar']],
72*465aec67SAndreas Gohr            ['p_close', []],
73*465aec67SAndreas Gohr            ['document_end', []],
74*465aec67SAndreas Gohr        ];
75*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
76*465aec67SAndreas Gohr    }
77*465aec67SAndreas Gohr
78*465aec67SAndreas Gohr    function testSectionRef() {
79*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
80*465aec67SAndreas Gohr        $this->P->parse("Foo [[wiki:syntax#internal|Syntax]] Bar");
81*465aec67SAndreas Gohr        $calls = [
82*465aec67SAndreas Gohr            ['document_start', []],
83*465aec67SAndreas Gohr            ['p_open', []],
84*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
85*465aec67SAndreas Gohr            ['internallink', ['wiki:syntax#internal', 'Syntax']],
86*465aec67SAndreas Gohr            ['cdata', [' Bar']],
87*465aec67SAndreas Gohr            ['p_close', []],
88*465aec67SAndreas Gohr            ['document_end', []],
89*465aec67SAndreas Gohr        ];
90*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
91*465aec67SAndreas Gohr    }
92*465aec67SAndreas Gohr
93*465aec67SAndreas Gohr    function testCodeFollows() {
94*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
95*465aec67SAndreas Gohr        $this->P->parse("Foo [[wiki:internal:link|Test]] Bar <code>command [arg1 [arg2 [arg3]]]</code>");
96*465aec67SAndreas Gohr        $calls = [
97*465aec67SAndreas Gohr            ['document_start', []],
98*465aec67SAndreas Gohr            ['p_open', []],
99*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
100*465aec67SAndreas Gohr            ['internallink', ['wiki:internal:link', 'Test']],
101*465aec67SAndreas Gohr            ['cdata', [' Bar <code>command [arg1 [arg2 [arg3]]]</code>']],
102*465aec67SAndreas Gohr            ['p_close', []],
103*465aec67SAndreas Gohr            ['document_end', []],
104*465aec67SAndreas Gohr        ];
105*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
106*465aec67SAndreas Gohr    }
107*465aec67SAndreas Gohr
108*465aec67SAndreas Gohr    function testCodeFollows2() {
109*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
110*465aec67SAndreas Gohr        $this->P->parse("Foo [[wiki:internal:link|[Square brackets in title] Test]] Bar <code>command [arg1 [arg2 [arg3]]]</code>");
111*465aec67SAndreas Gohr        $calls = [
112*465aec67SAndreas Gohr            ['document_start', []],
113*465aec67SAndreas Gohr            ['p_open', []],
114*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
115*465aec67SAndreas Gohr            ['internallink', ['wiki:internal:link', '[Square brackets in title] Test']],
116*465aec67SAndreas Gohr            ['cdata', [' Bar <code>command [arg1 [arg2 [arg3]]]</code>']],
117*465aec67SAndreas Gohr            ['p_close', []],
118*465aec67SAndreas Gohr            ['document_end', []],
119*465aec67SAndreas Gohr        ];
120*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
121*465aec67SAndreas Gohr    }
122*465aec67SAndreas Gohr
123*465aec67SAndreas Gohr    function testTwoLinks() {
124*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
125*465aec67SAndreas Gohr        $this->P->parse("Foo [[foo:bar|one]] and [[bar:foo|two]] Bar");
126*465aec67SAndreas Gohr        $calls = [
127*465aec67SAndreas Gohr            ['document_start', []],
128*465aec67SAndreas Gohr            ['p_open', []],
129*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
130*465aec67SAndreas Gohr            ['internallink', ['foo:bar', 'one']],
131*465aec67SAndreas Gohr            ['cdata', [' and ']],
132*465aec67SAndreas Gohr            ['internallink', ['bar:foo', 'two']],
133*465aec67SAndreas Gohr            ['cdata', [' Bar']],
134*465aec67SAndreas Gohr            ['p_close', []],
135*465aec67SAndreas Gohr            ['document_end', []],
136*465aec67SAndreas Gohr        ];
137*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
138*465aec67SAndreas Gohr    }
139*465aec67SAndreas Gohr
140*465aec67SAndreas Gohr    // ----- dispatch to externallink -----
141*465aec67SAndreas Gohr
142*465aec67SAndreas Gohr    function testExternalUrlInside() {
143*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
144*465aec67SAndreas Gohr        $this->P->parse("Foo [[http://www.google.com|Google]] Bar");
145*465aec67SAndreas Gohr        $calls = [
146*465aec67SAndreas Gohr            ['document_start', []],
147*465aec67SAndreas Gohr            ['p_open', []],
148*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
149*465aec67SAndreas Gohr            ['externallink', ['http://www.google.com', 'Google']],
150*465aec67SAndreas Gohr            ['cdata', [' Bar']],
151*465aec67SAndreas Gohr            ['p_close', []],
152*465aec67SAndreas Gohr            ['document_end', []],
153*465aec67SAndreas Gohr        ];
154*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
155*465aec67SAndreas Gohr    }
156*465aec67SAndreas Gohr
157*465aec67SAndreas Gohr    function testExternalUrlWithBracketsInside() {
158*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
159*465aec67SAndreas Gohr        $this->P->parse("Foo [[http://www.google.com?test[]=squarebracketsinurl|Google]] Bar");
160*465aec67SAndreas Gohr        $calls = [
161*465aec67SAndreas Gohr            ['document_start', []],
162*465aec67SAndreas Gohr            ['p_open', []],
163*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
164*465aec67SAndreas Gohr            ['externallink', ['http://www.google.com?test[]=squarebracketsinurl', 'Google']],
165*465aec67SAndreas Gohr            ['cdata', [' Bar']],
166*465aec67SAndreas Gohr            ['p_close', []],
167*465aec67SAndreas Gohr            ['document_end', []],
168*465aec67SAndreas Gohr        ];
169*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
170*465aec67SAndreas Gohr    }
171*465aec67SAndreas Gohr
172*465aec67SAndreas Gohr    function testExternalUrlWithBracketsInsideCodeFollows() {
173*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
174*465aec67SAndreas Gohr        $this->P->parse("Foo [[http://www.google.com?test[]=squarebracketsinurl|Google]] Bar <code>command [arg1 [arg2 [arg3]]]</code>");
175*465aec67SAndreas Gohr        $calls = [
176*465aec67SAndreas Gohr            ['document_start', []],
177*465aec67SAndreas Gohr            ['p_open', []],
178*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
179*465aec67SAndreas Gohr            ['externallink', ['http://www.google.com?test[]=squarebracketsinurl', 'Google']],
180*465aec67SAndreas Gohr            ['cdata', [' Bar <code>command [arg1 [arg2 [arg3]]]</code>']],
181*465aec67SAndreas Gohr            ['p_close', []],
182*465aec67SAndreas Gohr            ['document_end', []],
183*465aec67SAndreas Gohr        ];
184*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
185*465aec67SAndreas Gohr    }
186*465aec67SAndreas Gohr
187*465aec67SAndreas Gohr    function testFileSchemeInside() {
188*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
189*465aec67SAndreas Gohr        $this->P->parse('Foo [[file://temp/file.txt|Some File]] Bar');
190*465aec67SAndreas Gohr        $calls = [
191*465aec67SAndreas Gohr            ['document_start', []],
192*465aec67SAndreas Gohr            ['p_open', []],
193*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
194*465aec67SAndreas Gohr            ['externallink', ['file://temp/file.txt', 'Some File']],
195*465aec67SAndreas Gohr            ['cdata', [' Bar']],
196*465aec67SAndreas Gohr            ['p_close', []],
197*465aec67SAndreas Gohr            ['document_end', []],
198*465aec67SAndreas Gohr        ];
199*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
200*465aec67SAndreas Gohr    }
201*465aec67SAndreas Gohr
202*465aec67SAndreas Gohr    // ----- dispatch to interwikilink -----
203*465aec67SAndreas Gohr
204*465aec67SAndreas Gohr    function testInterwikiLink() {
205*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
206*465aec67SAndreas Gohr        $this->P->parse("Foo [[iw>somepage|Some Page]] Bar");
207*465aec67SAndreas Gohr        $calls = [
208*465aec67SAndreas Gohr            ['document_start', []],
209*465aec67SAndreas Gohr            ['p_open', []],
210*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
211*465aec67SAndreas Gohr            ['interwikilink', ['iw>somepage', 'Some Page', 'iw', 'somepage']],
212*465aec67SAndreas Gohr            ['cdata', [' Bar']],
213*465aec67SAndreas Gohr            ['p_close', []],
214*465aec67SAndreas Gohr            ['document_end', []],
215*465aec67SAndreas Gohr        ];
216*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
217*465aec67SAndreas Gohr    }
218*465aec67SAndreas Gohr
219*465aec67SAndreas Gohr    function testInterwikiLinkCase() {
220*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
221*465aec67SAndreas Gohr        $this->P->parse("Foo [[IW>somepage|Some Page]] Bar");
222*465aec67SAndreas Gohr        $calls = [
223*465aec67SAndreas Gohr            ['document_start', []],
224*465aec67SAndreas Gohr            ['p_open', []],
225*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
226*465aec67SAndreas Gohr            ['interwikilink', ['IW>somepage', 'Some Page', 'iw', 'somepage']],
227*465aec67SAndreas Gohr            ['cdata', [' Bar']],
228*465aec67SAndreas Gohr            ['p_close', []],
229*465aec67SAndreas Gohr            ['document_end', []],
230*465aec67SAndreas Gohr        ];
231*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
232*465aec67SAndreas Gohr    }
233*465aec67SAndreas Gohr
234*465aec67SAndreas Gohr    function testInterwikiPedia() {
235*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
236*465aec67SAndreas Gohr        $this->P->parse("Foo [[wp>Callback_(computer_science)|callbacks]] Bar");
237*465aec67SAndreas Gohr        $calls = [
238*465aec67SAndreas Gohr            ['document_start', []],
239*465aec67SAndreas Gohr            ['p_open', []],
240*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
241*465aec67SAndreas Gohr            ['interwikilink', ['wp>Callback_(computer_science)', 'callbacks', 'wp', 'Callback_(computer_science)']],
242*465aec67SAndreas Gohr            ['cdata', [' Bar']],
243*465aec67SAndreas Gohr            ['p_close', []],
244*465aec67SAndreas Gohr            ['document_end', []],
245*465aec67SAndreas Gohr        ];
246*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
247*465aec67SAndreas Gohr    }
248*465aec67SAndreas Gohr
249*465aec67SAndreas Gohr    // ----- media payload as link title -----
250*465aec67SAndreas Gohr
251*465aec67SAndreas Gohr    function testMediaImageAsTitle() {
252*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
253*465aec67SAndreas Gohr        $this->P->parse("Foo [[x:1:y:foo_bar:z|{{img.gif?10x20nocache|Some Image}}]] Bar");
254*465aec67SAndreas Gohr
255*465aec67SAndreas Gohr        $image = [
256*465aec67SAndreas Gohr            'type'    => 'internalmedia',
257*465aec67SAndreas Gohr            'src'     => 'img.gif',
258*465aec67SAndreas Gohr            'title'   => 'Some Image',
259*465aec67SAndreas Gohr            'align'   => null,
260*465aec67SAndreas Gohr            'width'   => 10,
261*465aec67SAndreas Gohr            'height'  => 20,
262*465aec67SAndreas Gohr            'cache'   => 'nocache',
263*465aec67SAndreas Gohr            'linking' => 'details',
264*465aec67SAndreas Gohr        ];
265*465aec67SAndreas Gohr
266*465aec67SAndreas Gohr        $calls = [
267*465aec67SAndreas Gohr            ['document_start', []],
268*465aec67SAndreas Gohr            ['p_open', []],
269*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
270*465aec67SAndreas Gohr            ['internallink', ['x:1:y:foo_bar:z', $image]],
271*465aec67SAndreas Gohr            ['cdata', [' Bar']],
272*465aec67SAndreas Gohr            ['p_close', []],
273*465aec67SAndreas Gohr            ['document_end', []],
274*465aec67SAndreas Gohr        ];
275*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
276*465aec67SAndreas Gohr    }
277*465aec67SAndreas Gohr
278*465aec67SAndreas Gohr    function testMediaNonImageAsTitle() {
279*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
280*465aec67SAndreas Gohr        $this->P->parse("Foo [[x:1:y:foo_bar:z|{{foo.txt?10x20nocache|Some Image}}]] Bar");
281*465aec67SAndreas Gohr
282*465aec67SAndreas Gohr        $image = [
283*465aec67SAndreas Gohr            'type'    => 'internalmedia',
284*465aec67SAndreas Gohr            'src'     => 'foo.txt',
285*465aec67SAndreas Gohr            'title'   => 'Some Image',
286*465aec67SAndreas Gohr            'align'   => null,
287*465aec67SAndreas Gohr            'width'   => 10,
288*465aec67SAndreas Gohr            'height'  => 20,
289*465aec67SAndreas Gohr            'cache'   => 'nocache',
290*465aec67SAndreas Gohr            'linking' => 'details',
291*465aec67SAndreas Gohr        ];
292*465aec67SAndreas Gohr
293*465aec67SAndreas Gohr        $calls = [
294*465aec67SAndreas Gohr            ['document_start', []],
295*465aec67SAndreas Gohr            ['p_open', []],
296*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
297*465aec67SAndreas Gohr            ['internallink', ['x:1:y:foo_bar:z', $image]],
298*465aec67SAndreas Gohr            ['cdata', [' Bar']],
299*465aec67SAndreas Gohr            ['p_close', []],
300*465aec67SAndreas Gohr            ['document_end', []],
301*465aec67SAndreas Gohr        ];
302*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
303*465aec67SAndreas Gohr    }
304*465aec67SAndreas Gohr
305*465aec67SAndreas Gohr    function testMediaAsEmailLinkTitle() {
306*465aec67SAndreas Gohr        $this->P->addMode('internallink', new Internallink());
307*465aec67SAndreas Gohr        $this->P->parse("Foo [[foo@example.com|{{img.gif?10x20nocache|Some Image}}]] Bar");
308*465aec67SAndreas Gohr
309*465aec67SAndreas Gohr        $image = [
310*465aec67SAndreas Gohr            'type'    => 'internalmedia',
311*465aec67SAndreas Gohr            'src'     => 'img.gif',
312*465aec67SAndreas Gohr            'title'   => 'Some Image',
313*465aec67SAndreas Gohr            'align'   => null,
314*465aec67SAndreas Gohr            'width'   => 10,
315*465aec67SAndreas Gohr            'height'  => 20,
316*465aec67SAndreas Gohr            'cache'   => 'nocache',
317*465aec67SAndreas Gohr            'linking' => 'details',
318*465aec67SAndreas Gohr        ];
319*465aec67SAndreas Gohr
320*465aec67SAndreas Gohr        $calls = [
321*465aec67SAndreas Gohr            ['document_start', []],
322*465aec67SAndreas Gohr            ['p_open', []],
323*465aec67SAndreas Gohr            ['cdata', ["\n" . 'Foo ']],
324*465aec67SAndreas Gohr            ['emaillink', ['foo@example.com', $image]],
325*465aec67SAndreas Gohr            ['cdata', [' Bar']],
326*465aec67SAndreas Gohr            ['p_close', []],
327*465aec67SAndreas Gohr            ['document_end', []],
328*465aec67SAndreas Gohr        ];
329*465aec67SAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
330*465aec67SAndreas Gohr    }
331*465aec67SAndreas Gohr}
332