Add missing test cases for ExtractFormatter

This is especially covering:
* The behavior on different kinds of HTML encoded characters.
* Trimming, or more precisely the fact that the formatter currently does
  not do all the trimming. There is a trim() call in the test that should
  not be there. This will be reworked in a following patch.

Additionally:
* Prefer strict assertSame() where possible.
* Remove two assertions that don't help making the test more safe.
* Utilize the new `// phpcs:…` syntax.

Change-Id: Ic361f2644300dcef3fc972a2dc2ae19ad34fa513
This commit is contained in:
Thiemo Kreuz 2019-03-19 12:27:54 +01:00
parent e234409518
commit b6250d3791
2 changed files with 24 additions and 14 deletions

View file

@ -66,15 +66,15 @@ class ApiQueryExtractsTest extends \MediaWikiTestCase {
$params = $instance->getAllowedParams();
$this->assertInternalType( 'array', $params );
$this->assertArrayHasKey( 'chars', $params );
$this->assertEquals( $params['chars'][\ApiBase::PARAM_MIN], 1 );
$this->assertEquals( $params['chars'][\ApiBase::PARAM_MAX], 1200 );
$this->assertArrayHasKey( 'limit', $params );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_DFLT], 20 );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_TYPE], 'limit' );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_MIN], 1 );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX], 20 );
$this->assertEquals( $params['limit'][\ApiBase::PARAM_MAX2], 20 );
$this->assertSame( $params['chars'][\ApiBase::PARAM_MIN], 1 );
$this->assertSame( $params['chars'][\ApiBase::PARAM_MAX], 1200 );
$this->assertSame( $params['limit'][\ApiBase::PARAM_DFLT], 20 );
$this->assertSame( $params['limit'][\ApiBase::PARAM_TYPE], 'limit' );
$this->assertSame( $params['limit'][\ApiBase::PARAM_MIN], 1 );
$this->assertSame( $params['limit'][\ApiBase::PARAM_MAX], 20 );
$this->assertSame( $params['limit'][\ApiBase::PARAM_MAX2], 20 );
}
/**

View file

@ -18,16 +18,15 @@ class ExtractFormatterTest extends MediaWikiTestCase {
// .metadata class will be added via $wgExtractsRemoveClasses on WMF
$fmt->remove( [ 'div', '.metadata' ] );
$text = trim( $fmt->getText() );
$this->assertEquals( $expected, $text );
$this->assertSame( $expected, $text );
}
public function provideExtracts() {
// @codingStandardsIgnoreStart
// phpcs:ignore Generic.Files.LineLength
$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>';
$tocText = 'Lead<div id="toc" class="toc">TOC goes here</div>
<h1>Section</h1>
<p>Section text</p>';
// @codingStandardsIgnoreEnd
return [
[
@ -37,6 +36,17 @@ class ExtractFormatterTest extends MediaWikiTestCase {
true,
],
'HTML cleanup in HTML mode' => [
"\u{00A0}A &amp; <b>B</b>",
"&#x0A;&nbsp;<a>A</a> &amp; <b>&#x42;</b>\r\n",
false
],
'HTML cleanup in plain text mode' => [
'A & B',
"&#x0A;&nbsp;<a>A</a> &amp; <b>&#x42;</b>\r\n",
true
],
[
"<span><span lang=\"baz\">qux</span></span>",
'<span class="foo"><span lang="baz">qux</span></span>',
@ -74,7 +84,7 @@ class ExtractFormatterTest extends MediaWikiTestCase {
* @param string $expected
*/
public function testGetFirstSentences( $text, $sentences, $expected ) {
$this->assertEquals( $expected, ExtractFormatter::getFirstSentences( $text, $sentences ) );
$this->assertSame( $expected, ExtractFormatter::getFirstSentences( $text, $sentences ) );
}
public function provideGetFirstSentences() {
@ -169,7 +179,7 @@ class ExtractFormatterTest extends MediaWikiTestCase {
* @param string $expected
*/
public function testGetFirstChars( $text, $chars, $expected ) {
$this->assertEquals( $expected, ExtractFormatter::getFirstChars( $text, $chars ) );
$this->assertSame( $expected, ExtractFormatter::getFirstChars( $text, $chars ) );
}
public function provideGetFirstChars() {