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