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