WikiMapTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. use Wikimedia\Rdbms\DatabaseDomain;
  3. /**
  4. * @covers WikiMap
  5. *
  6. * @group Database
  7. */
  8. class WikiMapTest extends MediaWikiLangTestCase {
  9. public function setUp() {
  10. parent::setUp();
  11. $conf = new SiteConfiguration();
  12. $conf->settings = [
  13. 'wgServer' => [
  14. 'enwiki' => 'http://en.example.org',
  15. 'ruwiki' => '//ru.example.org',
  16. 'nopathwiki' => '//nopath.example.org',
  17. 'thiswiki' => '//this.wiki.org'
  18. ],
  19. 'wgArticlePath' => [
  20. 'enwiki' => '/w/$1',
  21. 'ruwiki' => '/wiki/$1',
  22. ],
  23. ];
  24. $conf->suffixes = [ 'wiki' ];
  25. $this->setMwGlobals( [
  26. 'wgConf' => $conf,
  27. 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
  28. 'wgCanonicalServer' => '//this.wiki.org',
  29. 'wgDBname' => 'thiswiki',
  30. 'wgDBprefix' => ''
  31. ] );
  32. TestSites::insertIntoDb();
  33. }
  34. public function provideGetWiki() {
  35. // As provided by $wgConf
  36. $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
  37. $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
  38. // Created from site objects
  39. $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
  40. // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
  41. $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
  42. return [
  43. 'unknown' => [ null, 'xyzzy' ],
  44. 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
  45. 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
  46. 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
  47. 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
  48. 'non MediaWiki site' => [ null, 'spam', false ],
  49. 'boguswiki' => [ null, 'boguswiki' ],
  50. 'nopathwiki' => [ null, 'nopathwiki' ],
  51. ];
  52. }
  53. /**
  54. * @dataProvider provideGetWiki
  55. */
  56. public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
  57. if ( !$useWgConf ) {
  58. $this->setMwGlobals( [
  59. 'wgConf' => new SiteConfiguration(),
  60. ] );
  61. }
  62. $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
  63. }
  64. public function provideGetWikiName() {
  65. return [
  66. 'unknown' => [ 'xyzzy', 'xyzzy' ],
  67. 'enwiki' => [ 'en.example.org', 'enwiki' ],
  68. 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
  69. 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
  70. ];
  71. }
  72. /**
  73. * @dataProvider provideGetWikiName
  74. */
  75. public function testGetWikiName( $expected, $wikiId ) {
  76. $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
  77. }
  78. public function provideMakeForeignLink() {
  79. return [
  80. 'unknown' => [ false, 'xyzzy', 'Foo' ],
  81. 'enwiki' => [
  82. '<a class="external" rel="nofollow" ' .
  83. 'href="http://en.example.org/w/Foo">Foo</a>',
  84. 'enwiki',
  85. 'Foo'
  86. ],
  87. 'ruwiki' => [
  88. '<a class="external" rel="nofollow" ' .
  89. 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
  90. 'ruwiki',
  91. 'Фу',
  92. 'вар'
  93. ],
  94. 'enwiktionary (sites)' => [
  95. '<a class="external" rel="nofollow" ' .
  96. 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
  97. 'enwiktionary',
  98. 'Kitten',
  99. 'Kittens!'
  100. ],
  101. ];
  102. }
  103. /**
  104. * @dataProvider provideMakeForeignLink
  105. */
  106. public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
  107. $this->assertEquals(
  108. $expected,
  109. WikiMap::makeForeignLink( $wikiId, $page, $text )
  110. );
  111. }
  112. public function provideForeignUserLink() {
  113. return [
  114. 'unknown' => [ false, 'xyzzy', 'Foo' ],
  115. 'enwiki' => [
  116. '<a class="external" rel="nofollow" ' .
  117. 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
  118. 'enwiki',
  119. 'Foo'
  120. ],
  121. 'ruwiki' => [
  122. '<a class="external" rel="nofollow" ' .
  123. 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
  124. 'ruwiki',
  125. 'Фу',
  126. 'вар'
  127. ],
  128. 'enwiktionary (sites)' => [
  129. '<a class="external" rel="nofollow" ' .
  130. 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
  131. 'enwiktionary',
  132. 'Dummy',
  133. 'Whatever'
  134. ],
  135. ];
  136. }
  137. /**
  138. * @dataProvider provideForeignUserLink
  139. */
  140. public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
  141. $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
  142. }
  143. public function provideGetForeignURL() {
  144. return [
  145. 'unknown' => [ false, 'xyzzy', 'Foo' ],
  146. 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
  147. 'enwiktionary (sites)' => [
  148. 'https://en.wiktionary.org/wiki/Testme',
  149. 'enwiktionary',
  150. 'Testme'
  151. ],
  152. 'ruwiki with fragment' => [
  153. '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
  154. 'ruwiki',
  155. 'Фу',
  156. 'вар'
  157. ],
  158. ];
  159. }
  160. /**
  161. * @dataProvider provideGetForeignURL
  162. */
  163. public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
  164. $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
  165. }
  166. /**
  167. * @covers WikiMap::getCanonicalServerInfoForAllWikis()
  168. */
  169. public function testGetCanonicalServerInfoForAllWikis() {
  170. $expected = [
  171. 'thiswiki' => [
  172. 'url' => '//this.wiki.org',
  173. 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
  174. ],
  175. 'enwiki' => [
  176. 'url' => 'http://en.example.org',
  177. 'parts' => [
  178. 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
  179. ],
  180. 'ruwiki' => [
  181. 'url' => '//ru.example.org',
  182. 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
  183. ]
  184. ];
  185. $this->assertArrayEquals(
  186. $expected,
  187. WikiMap::getCanonicalServerInfoForAllWikis(),
  188. true,
  189. true
  190. );
  191. }
  192. public function provideGetWikiFromUrl() {
  193. return [
  194. [ 'http://this.wiki.org', 'thiswiki' ],
  195. [ 'https://this.wiki.org', 'thiswiki' ],
  196. [ 'http://this.wiki.org/$1', 'thiswiki' ],
  197. [ 'https://this.wiki.org/$2', 'thiswiki' ],
  198. [ 'http://en.example.org', 'enwiki' ],
  199. [ 'https://en.example.org', 'enwiki' ],
  200. [ 'http://en.example.org/$1', 'enwiki' ],
  201. [ 'https://en.example.org/$2', 'enwiki' ],
  202. [ 'http://ru.example.org', 'ruwiki' ],
  203. [ 'https://ru.example.org', 'ruwiki' ],
  204. [ 'http://ru.example.org/$1', 'ruwiki' ],
  205. [ 'https://ru.example.org/$2', 'ruwiki' ],
  206. [ 'http://not.defined.org', false ]
  207. ];
  208. }
  209. /**
  210. * @dataProvider provideGetWikiFromUrl
  211. * @covers WikiMap::getWikiFromUrl()
  212. */
  213. public function testGetWikiFromUrl( $url, $wiki ) {
  214. $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
  215. }
  216. /**
  217. * @dataProvider provideGetWikiIdFromDomain
  218. * @covers WikiMap::getWikiIdFromDomain()
  219. */
  220. public function testGetWikiIdFromDomain( $domain, $wikiId ) {
  221. $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDomain( $domain ) );
  222. }
  223. public function provideGetWikiIdFromDomain() {
  224. return [
  225. [ 'db-prefix', 'db-prefix' ],
  226. [ wfWikiID(), wfWikiID() ],
  227. [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ]
  228. ];
  229. }
  230. }