SanitizerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /**
  3. * @todo Tests covering decodeCharReferences can be refactored into a single
  4. * method and dataprovider.
  5. *
  6. * @group Sanitizer
  7. */
  8. class SanitizerTest extends MediaWikiTestCase {
  9. protected function tearDown() {
  10. MWTidy::destroySingleton();
  11. parent::tearDown();
  12. }
  13. /**
  14. * @covers Sanitizer::decodeCharReferences
  15. */
  16. public function testDecodeNamedEntities() {
  17. $this->assertEquals(
  18. "\xc3\xa9cole",
  19. Sanitizer::decodeCharReferences( '&eacute;cole' ),
  20. 'decode named entities'
  21. );
  22. }
  23. /**
  24. * @covers Sanitizer::decodeCharReferences
  25. */
  26. public function testDecodeNumericEntities() {
  27. $this->assertEquals(
  28. "\xc4\x88io bonas dans l'\xc3\xa9cole!",
  29. Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
  30. 'decode numeric entities'
  31. );
  32. }
  33. /**
  34. * @covers Sanitizer::decodeCharReferences
  35. */
  36. public function testDecodeMixedEntities() {
  37. $this->assertEquals(
  38. "\xc4\x88io bonas dans l'\xc3\xa9cole!",
  39. Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
  40. 'decode mixed numeric/named entities'
  41. );
  42. }
  43. /**
  44. * @covers Sanitizer::decodeCharReferences
  45. */
  46. public function testDecodeMixedComplexEntities() {
  47. $this->assertEquals(
  48. "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
  49. Sanitizer::decodeCharReferences(
  50. "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)"
  51. ),
  52. 'decode mixed complex entities'
  53. );
  54. }
  55. /**
  56. * @covers Sanitizer::decodeCharReferences
  57. */
  58. public function testInvalidAmpersand() {
  59. $this->assertEquals(
  60. 'a & b',
  61. Sanitizer::decodeCharReferences( 'a & b' ),
  62. 'Invalid ampersand'
  63. );
  64. }
  65. /**
  66. * @covers Sanitizer::decodeCharReferences
  67. */
  68. public function testInvalidEntities() {
  69. $this->assertEquals(
  70. '&foo;',
  71. Sanitizer::decodeCharReferences( '&foo;' ),
  72. 'Invalid named entity'
  73. );
  74. }
  75. /**
  76. * @covers Sanitizer::decodeCharReferences
  77. */
  78. public function testInvalidNumberedEntities() {
  79. $this->assertEquals(
  80. UtfNormal\Constants::UTF8_REPLACEMENT,
  81. Sanitizer::decodeCharReferences( "&#88888888888888;" ),
  82. 'Invalid numbered entity'
  83. );
  84. }
  85. /**
  86. * @covers Sanitizer::removeHTMLtags
  87. * @dataProvider provideHtml5Tags
  88. *
  89. * @param string $tag Name of an HTML5 element (ie: 'video')
  90. * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
  91. */
  92. public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
  93. MWTidy::setInstance( false );
  94. if ( $escaped ) {
  95. $this->assertEquals( "&lt;$tag&gt;",
  96. Sanitizer::removeHTMLtags( "<$tag>" )
  97. );
  98. } else {
  99. $this->assertEquals( "<$tag></$tag>\n",
  100. Sanitizer::removeHTMLtags( "<$tag>" )
  101. );
  102. }
  103. }
  104. /**
  105. * Provide HTML5 tags
  106. */
  107. public static function provideHtml5Tags() {
  108. $ESCAPED = true; # We want tag to be escaped
  109. $VERBATIM = false; # We want to keep the tag
  110. return [
  111. [ 'data', $VERBATIM ],
  112. [ 'mark', $VERBATIM ],
  113. [ 'time', $VERBATIM ],
  114. [ 'video', $ESCAPED ],
  115. ];
  116. }
  117. function dataRemoveHTMLtags() {
  118. return [
  119. // former testSelfClosingTag
  120. [
  121. '<div>Hello world</div />',
  122. '<div>Hello world</div>',
  123. 'Self-closing closing div'
  124. ],
  125. // Make sure special nested HTML5 semantics are not broken
  126. // https://html.spec.whatwg.org/multipage/semantics.html#the-kbd-element
  127. [
  128. '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
  129. '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
  130. 'Nested <kbd>.'
  131. ],
  132. // https://html.spec.whatwg.org/multipage/semantics.html#the-sub-and-sup-elements
  133. [
  134. '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
  135. '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
  136. 'Nested <var>.'
  137. ],
  138. // https://html.spec.whatwg.org/multipage/semantics.html#the-dfn-element
  139. [
  140. '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
  141. '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
  142. '<abbr> inside <dfn>',
  143. ],
  144. ];
  145. }
  146. /**
  147. * @dataProvider dataRemoveHTMLtags
  148. * @covers Sanitizer::removeHTMLtags
  149. */
  150. public function testRemoveHTMLtags( $input, $output, $msg = null ) {
  151. MWTidy::setInstance( false );
  152. $this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
  153. }
  154. /**
  155. * @dataProvider provideTagAttributesToDecode
  156. * @covers Sanitizer::decodeTagAttributes
  157. */
  158. public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
  159. $this->assertEquals( $expected,
  160. Sanitizer::decodeTagAttributes( $attributes ),
  161. $message
  162. );
  163. }
  164. public static function provideTagAttributesToDecode() {
  165. return [
  166. [ [ 'foo' => 'bar' ], 'foo=bar', 'Unquoted attribute' ],
  167. [ [ 'עברית' => 'bar' ], 'עברית=bar', 'Non-Latin attribute' ],
  168. [ [ '६' => 'bar' ], '६=bar', 'Devanagari number' ],
  169. [ [ '搭𨋢' => 'bar' ], '搭𨋢=bar', 'Non-BMP character' ],
  170. [ [], 'ńgh=bar', 'Combining accent is not allowed' ],
  171. [ [ 'foo' => 'bar' ], ' foo = bar ', 'Spaced attribute' ],
  172. [ [ 'foo' => 'bar' ], 'foo="bar"', 'Double-quoted attribute' ],
  173. [ [ 'foo' => 'bar' ], 'foo=\'bar\'', 'Single-quoted attribute' ],
  174. [
  175. [ 'foo' => 'bar', 'baz' => 'foo' ],
  176. 'foo=\'bar\' baz="foo"',
  177. 'Several attributes'
  178. ],
  179. [
  180. [ 'foo' => 'bar', 'baz' => 'foo' ],
  181. 'foo=\'bar\' baz="foo"',
  182. 'Several attributes'
  183. ],
  184. [
  185. [ 'foo' => 'bar', 'baz' => 'foo' ],
  186. 'foo=\'bar\' baz="foo"',
  187. 'Several attributes'
  188. ],
  189. [ [ ':foo' => 'bar' ], ':foo=\'bar\'', 'Leading :' ],
  190. [ [ '_foo' => 'bar' ], '_foo=\'bar\'', 'Leading _' ],
  191. [ [ 'foo' => 'bar' ], 'Foo=\'bar\'', 'Leading capital' ],
  192. [ [ 'foo' => 'BAR' ], 'FOO=BAR', 'Attribute keys are normalized to lowercase' ],
  193. # Invalid beginning
  194. [ [], '-foo=bar', 'Leading - is forbidden' ],
  195. [ [], '.foo=bar', 'Leading . is forbidden' ],
  196. [ [ 'foo-bar' => 'bar' ], 'foo-bar=bar', 'A - is allowed inside the attribute' ],
  197. [ [ 'foo-' => 'bar' ], 'foo-=bar', 'A - is allowed inside the attribute' ],
  198. [ [ 'foo.bar' => 'baz' ], 'foo.bar=baz', 'A . is allowed inside the attribute' ],
  199. [ [ 'foo.' => 'baz' ], 'foo.=baz', 'A . is allowed as last character' ],
  200. [ [ 'foo6' => 'baz' ], 'foo6=baz', 'Numbers are allowed' ],
  201. # This bit is more relaxed than XML rules, but some extensions use
  202. # it, like ProofreadPage (see T29539)
  203. [ [ '1foo' => 'baz' ], '1foo=baz', 'Leading numbers are allowed' ],
  204. [ [], 'foo$=baz', 'Symbols are not allowed' ],
  205. [ [], 'foo@=baz', 'Symbols are not allowed' ],
  206. [ [], 'foo~=baz', 'Symbols are not allowed' ],
  207. [
  208. [ 'foo' => '1[#^`*%w/(' ],
  209. 'foo=1[#^`*%w/(',
  210. 'All kind of characters are allowed as values'
  211. ],
  212. [
  213. [ 'foo' => '1[#^`*%\'w/(' ],
  214. 'foo="1[#^`*%\'w/("',
  215. 'Double quotes are allowed if quoted by single quotes'
  216. ],
  217. [
  218. [ 'foo' => '1[#^`*%"w/(' ],
  219. 'foo=\'1[#^`*%"w/(\'',
  220. 'Single quotes are allowed if quoted by double quotes'
  221. ],
  222. [ [ 'foo' => '&"' ], 'foo=&amp;&quot;', 'Special chars can be provided as entities' ],
  223. [ [ 'foo' => '&foobar;' ], 'foo=&foobar;', 'Entity-like items are accepted' ],
  224. ];
  225. }
  226. /**
  227. * @dataProvider provideDeprecatedAttributes
  228. * @covers Sanitizer::fixTagAttributes
  229. */
  230. public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
  231. $this->assertEquals( " $inputAttr",
  232. Sanitizer::fixTagAttributes( $inputAttr, $inputEl ),
  233. $message
  234. );
  235. }
  236. public static function provideDeprecatedAttributes() {
  237. /** [ <attribute>, <element>, [message] ] */
  238. return [
  239. [ 'clear="left"', 'br' ],
  240. [ 'clear="all"', 'br' ],
  241. [ 'width="100"', 'td' ],
  242. [ 'nowrap="true"', 'td' ],
  243. [ 'nowrap=""', 'td' ],
  244. [ 'align="right"', 'td' ],
  245. [ 'align="center"', 'table' ],
  246. [ 'align="left"', 'tr' ],
  247. [ 'align="center"', 'div' ],
  248. [ 'align="left"', 'h1' ],
  249. [ 'align="left"', 'p' ],
  250. ];
  251. }
  252. /**
  253. * @dataProvider provideCssCommentsFixtures
  254. * @covers Sanitizer::checkCss
  255. */
  256. public function testCssCommentsChecking( $expected, $css, $message = '' ) {
  257. $this->assertEquals( $expected,
  258. Sanitizer::checkCss( $css ),
  259. $message
  260. );
  261. }
  262. public static function provideCssCommentsFixtures() {
  263. /** [ <expected>, <css>, [message] ] */
  264. return [
  265. // Valid comments spanning entire input
  266. [ '/**/', '/**/' ],
  267. [ '/* comment */', '/* comment */' ],
  268. // Weird stuff
  269. [ ' ', '/****/' ],
  270. [ ' ', '/* /* */' ],
  271. [ 'display: block;', "display:/* foo */block;" ],
  272. [ 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
  273. 'Backslash-escaped comments must be stripped (T30450)' ],
  274. [ '', '/* unfinished comment structure',
  275. 'Remove anything after a comment-start token' ],
  276. [ '', "\\2f\\2a unifinished comment'",
  277. 'Remove anything after a backslash-escaped comment-start token' ],
  278. [
  279. '/* insecure input */',
  280. 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader'
  281. . '(src=\'asdf.png\',sizingMethod=\'scale\');'
  282. ],
  283. [
  284. '/* insecure input */',
  285. '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader'
  286. . '(src=\'asdf.png\',sizingMethod=\'scale\')";'
  287. ],
  288. [ '/* insecure input */', 'width: expression(1+1);' ],
  289. [ '/* insecure input */', 'background-image: image(asdf.png);' ],
  290. [ '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ],
  291. [ '/* insecure input */', 'background-image: -moz-image(asdf.png);' ],
  292. [ '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ],
  293. [
  294. '/* insecure input */',
  295. 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
  296. ],
  297. [
  298. '/* insecure input */',
  299. 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
  300. ],
  301. [ '/* insecure input */', 'foo: attr( title, url );' ],
  302. [ '/* insecure input */', 'foo: attr( title url );' ],
  303. ];
  304. }
  305. /**
  306. * @dataProvider provideEscapeHtmlAllowEntities
  307. * @covers Sanitizer::escapeHtmlAllowEntities
  308. */
  309. public function testEscapeHtmlAllowEntities( $expected, $html ) {
  310. $this->assertEquals(
  311. $expected,
  312. Sanitizer::escapeHtmlAllowEntities( $html )
  313. );
  314. }
  315. public static function provideEscapeHtmlAllowEntities() {
  316. return [
  317. [ 'foo', 'foo' ],
  318. [ 'a¡b', 'a&#161;b' ],
  319. [ 'foo&#039;bar', "foo'bar" ],
  320. [ '&lt;script&gt;foo&lt;/script&gt;', '<script>foo</script>' ],
  321. ];
  322. }
  323. /**
  324. * Test Sanitizer::escapeId
  325. *
  326. * @dataProvider provideEscapeId
  327. * @covers Sanitizer::escapeId
  328. */
  329. public function testEscapeId( $input, $output ) {
  330. $this->assertEquals(
  331. $output,
  332. Sanitizer::escapeId( $input, [ 'noninitial', 'legacy' ] )
  333. );
  334. }
  335. public static function provideEscapeId() {
  336. return [
  337. [ '+', '.2B' ],
  338. [ '&', '.26' ],
  339. [ '=', '.3D' ],
  340. [ ':', ':' ],
  341. [ ';', '.3B' ],
  342. [ '@', '.40' ],
  343. [ '$', '.24' ],
  344. [ '-_.', '-_.' ],
  345. [ '!', '.21' ],
  346. [ '*', '.2A' ],
  347. [ '/', '.2F' ],
  348. [ '[]', '.5B.5D' ],
  349. [ '<>', '.3C.3E' ],
  350. [ '\'', '.27' ],
  351. [ '§', '.C2.A7' ],
  352. [ 'Test:A & B/Here', 'Test:A_.26_B.2FHere' ],
  353. [ 'A&B&amp;C&amp;amp;D&amp;amp;amp;E', 'A.26B.26amp.3BC.26amp.3Bamp.3BD.26amp.3Bamp.3Bamp.3BE' ],
  354. ];
  355. }
  356. /**
  357. * Test escapeIdReferenceList for consistency with escapeIdForAttribute
  358. *
  359. * @dataProvider provideEscapeIdReferenceList
  360. * @covers Sanitizer::escapeIdReferenceList
  361. */
  362. public function testEscapeIdReferenceList( $referenceList, $id1, $id2 ) {
  363. $this->assertEquals(
  364. Sanitizer::escapeIdReferenceList( $referenceList ),
  365. Sanitizer::escapeIdForAttribute( $id1 )
  366. . ' '
  367. . Sanitizer::escapeIdForAttribute( $id2 )
  368. );
  369. }
  370. public static function provideEscapeIdReferenceList() {
  371. /** [ <reference list>, <individual id 1>, <individual id 2> ] */
  372. return [
  373. [ 'foo bar', 'foo', 'bar' ],
  374. [ '#1 #2', '#1', '#2' ],
  375. [ '+1 +2', '+1', '+2' ],
  376. ];
  377. }
  378. /**
  379. * @dataProvider provideIsReservedDataAttribute
  380. * @covers Sanitizer::isReservedDataAttribute
  381. */
  382. public function testIsReservedDataAttribute( $attr, $expected ) {
  383. $this->assertSame( $expected, Sanitizer::isReservedDataAttribute( $attr ) );
  384. }
  385. public static function provideIsReservedDataAttribute() {
  386. return [
  387. [ 'foo', false ],
  388. [ 'data', false ],
  389. [ 'data-foo', false ],
  390. [ 'data-mw', true ],
  391. [ 'data-ooui', true ],
  392. [ 'data-parsoid', true ],
  393. [ 'data-mw-foo', true ],
  394. [ 'data-ooui-foo', true ],
  395. [ 'data-mwfoo', true ], // could be false but this is how it's implemented currently
  396. ];
  397. }
  398. /**
  399. * @dataProvider provideEscapeIdForStuff
  400. *
  401. * @covers Sanitizer::escapeIdForAttribute()
  402. * @covers Sanitizer::escapeIdForLink()
  403. * @covers Sanitizer::escapeIdForExternalInterwiki()
  404. * @covers Sanitizer::escapeIdInternal()
  405. *
  406. * @param string $stuff
  407. * @param string[] $config
  408. * @param string $id
  409. * @param string|false $expected
  410. * @param int|null $mode
  411. */
  412. public function testEscapeIdForStuff( $stuff, array $config, $id, $expected, $mode = null ) {
  413. $func = "Sanitizer::escapeIdFor{$stuff}";
  414. $iwFlavor = array_pop( $config );
  415. $this->setMwGlobals( [
  416. 'wgFragmentMode' => $config,
  417. 'wgExternalInterwikiFragmentMode' => $iwFlavor,
  418. ] );
  419. $escaped = call_user_func( $func, $id, $mode );
  420. self::assertEquals( $expected, $escaped );
  421. }
  422. public function provideEscapeIdForStuff() {
  423. // Test inputs and outputs
  424. $text = 'foo тест_#%!\'()[]:<>&&amp;&amp;amp;';
  425. $legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E' .
  426. '.26.26amp.3B.26amp.3Bamp.3B';
  427. $html5Encoded = 'foo_тест_#%!\'()[]:<>&&amp;&amp;amp;';
  428. // Settings: last element is $wgExternalInterwikiFragmentMode, the rest is $wgFragmentMode
  429. $legacy = [ 'legacy', 'legacy' ];
  430. $legacyNew = [ 'legacy', 'html5', 'legacy' ];
  431. $newLegacy = [ 'html5', 'legacy', 'legacy' ];
  432. $new = [ 'html5', 'legacy' ];
  433. $allNew = [ 'html5', 'html5' ];
  434. return [
  435. // Pure legacy: how MW worked before 2017
  436. [ 'Attribute', $legacy, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
  437. [ 'Attribute', $legacy, $text, false, Sanitizer::ID_FALLBACK ],
  438. [ 'Link', $legacy, $text, $legacyEncoded ],
  439. [ 'ExternalInterwiki', $legacy, $text, $legacyEncoded ],
  440. // Transition to a new world: legacy links with HTML5 fallback
  441. [ 'Attribute', $legacyNew, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
  442. [ 'Attribute', $legacyNew, $text, $html5Encoded, Sanitizer::ID_FALLBACK ],
  443. [ 'Link', $legacyNew, $text, $legacyEncoded ],
  444. [ 'ExternalInterwiki', $legacyNew, $text, $legacyEncoded ],
  445. // New world: HTML5 links, legacy fallbacks
  446. [ 'Attribute', $newLegacy, $text, $html5Encoded, Sanitizer::ID_PRIMARY ],
  447. [ 'Attribute', $newLegacy, $text, $legacyEncoded, Sanitizer::ID_FALLBACK ],
  448. [ 'Link', $newLegacy, $text, $html5Encoded ],
  449. [ 'ExternalInterwiki', $newLegacy, $text, $legacyEncoded ],
  450. // Distant future: no legacy fallbacks, but still linking to leagacy wikis
  451. [ 'Attribute', $new, $text, $html5Encoded, Sanitizer::ID_PRIMARY ],
  452. [ 'Attribute', $new, $text, false, Sanitizer::ID_FALLBACK ],
  453. [ 'Link', $new, $text, $html5Encoded ],
  454. [ 'ExternalInterwiki', $new, $text, $legacyEncoded ],
  455. // Just before the heat death of universe: external interwikis are also HTML5 \m/
  456. [ 'Attribute', $allNew, $text, $html5Encoded, Sanitizer::ID_PRIMARY ],
  457. [ 'Attribute', $allNew, $text, false, Sanitizer::ID_FALLBACK ],
  458. [ 'Link', $allNew, $text, $html5Encoded ],
  459. [ 'ExternalInterwiki', $allNew, $text, $html5Encoded ],
  460. ];
  461. }
  462. /**
  463. * @dataProvider provideStripAllTags
  464. *
  465. * @covers Sanitizer::stripAllTags()
  466. * @covers RemexStripTagHandler
  467. *
  468. * @param string $input
  469. * @param string $expected
  470. */
  471. public function testStripAllTags( $input, $expected ) {
  472. $this->assertEquals( $expected, Sanitizer::stripAllTags( $input ) );
  473. }
  474. public function provideStripAllTags() {
  475. return [
  476. [ '<p>Foo</p>', 'Foo' ],
  477. [ '<p id="one">Foo</p><p id="two">Bar</p>', 'FooBar' ],
  478. [ "<p>Foo</p>\n<p>Bar</p>", 'Foo Bar' ],
  479. [ '<p>Hello &lt;strong&gt; wor&#x6c;&#100; caf&eacute;</p>', 'Hello <strong> world café' ],
  480. [
  481. '<p><small data-foo=\'bar"&lt;baz>quux\'><a href="./Foo">Bar</a></small> Whee!</p>',
  482. 'Bar Whee!'
  483. ],
  484. [ '1<span class="<?php">2</span>3', '123' ],
  485. [ '1<span class="<?">2</span>3', '123' ],
  486. ];
  487. }
  488. /**
  489. * @expectedException InvalidArgumentException
  490. * @covers Sanitizer::escapeIdInternal()
  491. */
  492. public function testInvalidFragmentThrows() {
  493. $this->setMwGlobals( 'wgFragmentMode', [ 'boom!' ] );
  494. Sanitizer::escapeIdForAttribute( 'This should throw' );
  495. }
  496. /**
  497. * @expectedException UnexpectedValueException
  498. * @covers Sanitizer::escapeIdForAttribute()
  499. */
  500. public function testNoPrimaryFragmentModeThrows() {
  501. $this->setMwGlobals( 'wgFragmentMode', [ 666 => 'html5' ] );
  502. Sanitizer::escapeIdForAttribute( 'This should throw' );
  503. }
  504. /**
  505. * @expectedException UnexpectedValueException
  506. * @covers Sanitizer::escapeIdForLink()
  507. */
  508. public function testNoPrimaryFragmentModeThrows2() {
  509. $this->setMwGlobals( 'wgFragmentMode', [ 666 => 'html5' ] );
  510. Sanitizer::escapeIdForLink( 'This should throw' );
  511. }
  512. }