1<?php 2 3namespace dokuwiki\test\Parsing\Helpers; 4 5use dokuwiki\Parsing\Helpers\Link; 6 7/** 8 * Tests for URL classification shared between Internallink and GfmLink. 9 */ 10class LinkTest extends \DokuWikiTest 11{ 12 function testClassifyInternalPageDefault() 13 { 14 $this->assertSame( 15 ['internallink', ['some:page', 'Label']], 16 Link::classify('some:page', 'Label') 17 ); 18 } 19 20 function testClassifyExternalHttp() 21 { 22 $this->assertSame( 23 ['externallink', ['http://example.com', null]], 24 Link::classify('http://example.com', null) 25 ); 26 } 27 28 function testClassifyExternalCustomScheme() 29 { 30 // Any `scheme://...` matches — the ladder does not validate against 31 // the configured schemes list; that's the renderer's job. 32 $this->assertSame( 33 ['externallink', ['ftp://files.example.com/x', 'F']], 34 Link::classify('ftp://files.example.com/x', 'F') 35 ); 36 } 37 38 function testClassifyInterwikiLink() 39 { 40 $this->assertSame( 41 ['interwikilink', ['wp>Callback', 'cb', 'wp', 'Callback']], 42 Link::classify('wp>Callback', 'cb') 43 ); 44 } 45 46 function testClassifyInterwikiPrefixLowercased() 47 { 48 [$call, $args] = Link::classify('IW>SomePage', 'T'); 49 $this->assertSame('interwikilink', $call); 50 $this->assertSame('iw', $args[2], 'interwiki prefix must be lowercased'); 51 $this->assertSame('SomePage', $args[3], 'interwiki target must be preserved'); 52 } 53 54 function testClassifyWindowsShare() 55 { 56 $this->assertSame( 57 ['windowssharelink', ['\\\\server\\share', null]], 58 Link::classify('\\\\server\\share', null) 59 ); 60 } 61 62 function testClassifyEmail() 63 { 64 $this->assertSame( 65 ['emaillink', ['user@example.com', 'Mail']], 66 Link::classify('user@example.com', 'Mail') 67 ); 68 } 69 70 function testClassifyLocalAnchorStripsHash() 71 { 72 $this->assertSame( 73 ['locallink', ['section', 'Here']], 74 Link::classify('#section', 'Here') 75 ); 76 } 77 78 function testClassifyInterwikiWinsOverEmail() 79 { 80 // An interwiki prefix containing `>` before an `@` still goes 81 // interwiki. Order of the ladder is load-bearing. 82 [$call, ] = Link::classify('wiki>user@host', 'x'); 83 $this->assertSame('interwikilink', $call); 84 } 85 86 function testClassifyArrayLabelForMediaInTitle() 87 { 88 // Internallink supports a parsed-media array as the label; the 89 // helper must pass it through untouched. 90 $media = ['type' => 'internalmedia', 'src' => 'img.gif']; 91 [, $args] = Link::classify('some:page', $media); 92 $this->assertSame($media, $args[1]); 93 } 94} 95