Merge "Update README"
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlFormatterTest.php
1 <?php
2
3 /**
4 * @group HtmlFormatter
5 */
6 class HtmlFormatterTest extends MediaWikiTestCase {
7 /**
8 * @dataProvider getHtmlData
9 */
10 public function testTransform( $input, $expected, $callback = false ) {
11 $input = self::normalize( $input );
12 $formatter = new HtmlFormatter( HtmlFormatter::wrapHTML( $input ) );
13 if ( $callback ) {
14 $callback( $formatter );
15 }
16 $formatter->filterContent();
17 $html = $formatter->getText();
18 $this->assertEquals( self::normalize( $expected ), self::normalize( $html ) );
19 }
20
21 private static function normalize( $s ) {
22 return str_replace( "\n", '',
23 str_replace( "\r", '', $s ) // "yay" to Windows!
24 );
25 }
26
27 public function getHtmlData() {
28 $removeImages = function( HtmlFormatter $f ) {
29 $f->setRemoveMedia();
30 };
31 $removeTags = function( HtmlFormatter $f ) {
32 $f->remove( array( 'table', '.foo', '#bar', 'div.baz' ) );
33 };
34 $flattenSomeStuff = function( HtmlFormatter $f ) {
35 $f->flatten( array( 's', 'div' ) );
36 };
37 $flattenEverything = function( HtmlFormatter $f ) {
38 $f->flattenAllTags();
39 };
40 return array(
41 // remove images if asked
42 array(
43 '<img src="/foo/bar.jpg" alt="Blah"/>',
44 '',
45 $removeImages,
46 ),
47 // basic tag removal
48 array(
49 '<table><tr><td>foo</td></tr></table><div class="foo">foo</div><div class="foo quux">foo</div><span id="bar">bar</span>
50 <strong class="foo" id="bar">foobar</strong><div class="notfoo">test</div><div class="baz"/>
51 <span class="baz">baz</span>',
52
53 '<div class="notfoo">test</div>
54 <span class="baz">baz</span>',
55 $removeTags,
56 ),
57 // don't flatten tags that start like chosen ones
58 array(
59 '<div><s>foo</s> <span>bar</span></div>',
60 'foo <span>bar</span>',
61 $flattenSomeStuff,
62 ),
63 // total flattening
64 array(
65 '<div style="foo">bar<sup>2</sup></div>',
66 'bar2',
67 $flattenEverything,
68 ),
69 // UTF-8 preservation and security
70 array(
71 '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&#38;&#0038;&#x26;&#x026;',
72 '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&amp;&amp;&amp;&amp;',
73 ),
74 // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
75 array(
76 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup> <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
77 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup> <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
78 ),
79 );
80 }
81 }