WikiReferenceTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @covers WikiReference
  4. */
  5. class WikiReferenceTest extends PHPUnit\Framework\TestCase {
  6. use MediaWikiCoversValidator;
  7. public function provideGetDisplayName() {
  8. return [
  9. 'http' => [ 'foo.bar', 'http://foo.bar' ],
  10. 'https' => [ 'foo.bar', 'http://foo.bar' ],
  11. // apparently, this is the expected behavior
  12. 'invalid' => [ 'purple kittens', 'purple kittens' ],
  13. ];
  14. }
  15. /**
  16. * @dataProvider provideGetDisplayName
  17. */
  18. public function testGetDisplayName( $expected, $canonicalServer ) {
  19. $reference = new WikiReference( $canonicalServer, '/wiki/$1' );
  20. $this->assertEquals( $expected, $reference->getDisplayName() );
  21. }
  22. public function testGetCanonicalServer() {
  23. $reference = new WikiReference( 'https://acme.com', '/wiki/$1', '//acme.com' );
  24. $this->assertEquals( 'https://acme.com', $reference->getCanonicalServer() );
  25. }
  26. public function provideGetCanonicalUrl() {
  27. return [
  28. 'no fragment' => [
  29. 'https://acme.com/wiki/Foo',
  30. 'https://acme.com',
  31. '//acme.com',
  32. '/wiki/$1',
  33. 'Foo',
  34. null
  35. ],
  36. 'empty fragment' => [
  37. 'https://acme.com/wiki/Foo',
  38. 'https://acme.com',
  39. '//acme.com',
  40. '/wiki/$1',
  41. 'Foo',
  42. ''
  43. ],
  44. 'fragment' => [
  45. 'https://acme.com/wiki/Foo#Bar',
  46. 'https://acme.com',
  47. '//acme.com',
  48. '/wiki/$1',
  49. 'Foo',
  50. 'Bar'
  51. ],
  52. 'double fragment' => [
  53. 'https://acme.com/wiki/Foo#Bar%23Xus',
  54. 'https://acme.com',
  55. '//acme.com',
  56. '/wiki/$1',
  57. 'Foo',
  58. 'Bar#Xus'
  59. ],
  60. 'escaped fragment' => [
  61. 'https://acme.com/wiki/Foo%23Bar',
  62. 'https://acme.com',
  63. '//acme.com',
  64. '/wiki/$1',
  65. 'Foo#Bar',
  66. null
  67. ],
  68. 'empty path' => [
  69. 'https://acme.com/Foo',
  70. 'https://acme.com',
  71. '//acme.com',
  72. '/$1',
  73. 'Foo',
  74. null
  75. ],
  76. ];
  77. }
  78. /**
  79. * @dataProvider provideGetCanonicalUrl
  80. */
  81. public function testGetCanonicalUrl(
  82. $expected, $canonicalServer, $server, $path, $page, $fragmentId
  83. ) {
  84. $reference = new WikiReference( $canonicalServer, $path, $server );
  85. $this->assertEquals( $expected, $reference->getCanonicalUrl( $page, $fragmentId ) );
  86. }
  87. /**
  88. * @dataProvider provideGetCanonicalUrl
  89. * @note getUrl is an alias for getCanonicalUrl
  90. */
  91. public function testGetUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
  92. $reference = new WikiReference( $canonicalServer, $path, $server );
  93. $this->assertEquals( $expected, $reference->getUrl( $page, $fragmentId ) );
  94. }
  95. public function provideGetFullUrl() {
  96. return [
  97. 'no fragment' => [
  98. '//acme.com/wiki/Foo',
  99. 'https://acme.com',
  100. '//acme.com',
  101. '/wiki/$1',
  102. 'Foo',
  103. null
  104. ],
  105. 'empty fragment' => [
  106. '//acme.com/wiki/Foo',
  107. 'https://acme.com',
  108. '//acme.com',
  109. '/wiki/$1',
  110. 'Foo',
  111. ''
  112. ],
  113. 'fragment' => [
  114. '//acme.com/wiki/Foo#Bar',
  115. 'https://acme.com',
  116. '//acme.com',
  117. '/wiki/$1',
  118. 'Foo',
  119. 'Bar'
  120. ],
  121. 'double fragment' => [
  122. '//acme.com/wiki/Foo#Bar%23Xus',
  123. 'https://acme.com',
  124. '//acme.com',
  125. '/wiki/$1',
  126. 'Foo',
  127. 'Bar#Xus'
  128. ],
  129. 'escaped fragment' => [
  130. '//acme.com/wiki/Foo%23Bar',
  131. 'https://acme.com',
  132. '//acme.com',
  133. '/wiki/$1',
  134. 'Foo#Bar',
  135. null
  136. ],
  137. 'empty path' => [
  138. '//acme.com/Foo',
  139. 'https://acme.com',
  140. '//acme.com',
  141. '/$1',
  142. 'Foo',
  143. null
  144. ],
  145. ];
  146. }
  147. /**
  148. * @dataProvider provideGetFullUrl
  149. */
  150. public function testGetFullUrl( $expected, $canonicalServer, $server, $path, $page, $fragmentId ) {
  151. $reference = new WikiReference( $canonicalServer, $path, $server );
  152. $this->assertEquals( $expected, $reference->getFullUrl( $page, $fragmentId ) );
  153. }
  154. }