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