Debian Codegraph @

extensions/TextExtracts/tests/phpunit/ExtractFormatterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\TextExtracts\Test;
4
5 use MediaWiki\Extension\TextExtracts\ExtractFormatter;
6 use MediaWikiIntegrationTestCase;
7
8 /**
9 * @covers MediaWiki\Extension\TextExtracts\ExtractFormatter
10 * @group TextExtracts
11 *
12 * @license GPL-2.0-or-later
13 */
14 class ExtractFormatterTest extends MediaWikiIntegrationTestCase {
15
16 /**
17 * @dataProvider provideExtracts
18 */
19 public function testExtracts( $expected, $text, $plainText ) {
20 $fmt = new ExtractFormatter( $text, $plainText );
21 // .metadata class will be added via $wgExtractsRemoveClasses on WMF
22 $fmt->remove( [ 'div', 'figure', '.metadata' ] );
23 $text = $fmt->getText();
24 $this->assertSame( $expected, $text );
25 }
26
27 public static function provideExtracts() {
28 // phpcs:ignore Generic.Files.LineLength
29 $dutch = '<b>Dutch</b> (<span class="unicode haudio" style="white-space:nowrap;"><span class="fn"><a href="/wiki/File:Nl-Nederlands.ogg" title="About this sound"><img alt="About this sound" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/11px-Loudspeaker.svg.png" width="11" height="11" srcset="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/17px-Loudspeaker.svg.png 1.5x, https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/22px-Loudspeaker.svg.png 2x" /></a>&#160;<a href="https://upload.wikimedia.org/wikipedia/commons/d/db/Nl-Nederlands.ogg" class="internal" title="Nl-Nederlands.ogg"><i>Nederlands</i></a></span>&#160;<small class="metadata audiolinkinfo" style="cursor:help;">(<a href="/w/index.php?title=Wikipedia:Media_help&amp;action=edit&amp;redlink=1" class="new" title="Wikipedia:Media help (page does not exist)"><span style="cursor:help;">help</span></a>ยท<a href="/wiki/File:Nl-Nederlands.ogg" title="File:Nl-Nederlands.ogg"><span style="cursor:help;">info</span></a>)</small></span>) is a <a href="/w/index.php?title=West_Germanic_languages&amp;action=edit&amp;redlink=1" class="new" title="West Germanic languages (page does not exist)">West Germanic language</a> and the native language of most of the population of the <a href="/w/index.php?title=Netherlands&amp;action=edit&amp;redlink=1" class="new" title="Netherlands (page does not exist)">Netherlands</a>';
30 $tocText = 'Lead<div id="toc" class="toc">TOC goes here</div>
31 <h1>Section</h1>
32 <p>Section text</p>';
33 // phpcs:ignore Generic.Files.LineLength
34 $figureText = '<b>Test 123</b>beforetext<figure><span><img alt="About this sound" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/11px-Loudspeaker.svg.png" width="11" height="11" srcset="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/17px-Loudspeaker.svg.png 1.5x, https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/22px-Loudspeaker.svg.png 2x" /></span><figcaption>Very loud speaker</figcaption></figure>aftertext';
35
36 return [
37 [
38 'Dutch ( Nederlands ) is a West Germanic language and the native language of ' .
39 'most of the population of the Netherlands',
40 $dutch,
41 true,
42 ],
43
44 'HTML cleanup in HTML mode' => [
45 "\u{00A0}A &amp; <b>B</b>",
46 "&#x0A;&nbsp;<a>A</a> &amp; <b>&#x42;</b>\r\n",
47 false
48 ],
49 'HTML cleanup in plain text mode' => [
50 'A & B',
51 "&#x0A;&nbsp;<a>A</a> &amp; <b>&#x42;</b>\r\n",
52 true
53 ],
54
55 [
56 "<span><span lang=\"baz\">qux</span></span>",
57 '<span class="foo"><span lang="baz">qux</span></span>',
58 false,
59 ],
60 [
61 "<span><span lang=\"baz\">qux</span></span>",
62 '<span style="foo: bar;"><span lang="baz">qux</span></span>',
63 false,
64 ],
65 [
66 "<span><span lang=\"qux\">quux</span></span>",
67 '<span class="foo"><span style="bar: baz;" lang="qux">quux</span></span>',
68 false,
69 ],
70 [
71 // New heading markup from T13555 (T363445)
72 "<p>hello</p><h2 id=\"heading\">heading</h2><p>bye</p>",
73 '<p>hello</p>' .
74 '<div class="mw-heading mw-heading2">' .
75 '<h2 id="heading">heading</h2>' .
76 '<span class="mw-editsection">xxx</span>' .
77 '</div>' .
78 '<p>bye</p>',
79 false,
80 ],
81 [
82 // Verify that TOC is properly removed (HTML mode)
83 "Lead\n<h1>Section</h1>\n<p>Section text</p>",
84 $tocText,
85 false,
86 ],
87 [
88 // Verify that TOC is properly removed (plain text mode)
89 "Lead\n\n\x01\x021\2\1Section\nSection text",
90 $tocText,
91 true,
92 ],
93 [
94 // Verify that text in figures is removed (plain)
95 'Test 123beforetextaftertext',
96 $figureText,
97 true,
98 ],
99 [
100 // Verify that text in figures is removed (html)
101 '<b>Test 123</b>beforetextaftertext',
102 $figureText,
103 false,
104 ]
105 ];
106 }
107
108 }