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