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