Add test for ApiQueryExtracts::truncate()

Change-Id: Ia39188fe3ff1b87d82b4c573f8d27629e75c0aa4
This commit is contained in:
Thiemo Kreuz 2021-01-08 09:03:05 +01:00
parent f93fcf6d26
commit 471fdd0f89
2 changed files with 57 additions and 5 deletions

View file

@ -327,11 +327,8 @@ class ApiQueryExtracts extends ApiQueryBase {
* @return string
*/
private function truncate( $text ) {
if ( !$this->params['plaintext'] ) {
$truncator = new TextTruncator( true );
} else {
$truncator = new TextTruncator( false );
}
$useTidy = !$this->params['plaintext'];
$truncator = new TextTruncator( $useTidy );
if ( $this->params['chars'] ) {
$text = $truncator->getFirstChars( $text, $this->params['chars'] ) .

View file

@ -25,6 +25,12 @@ class ApiQueryExtractsTest extends \MediaWikiTestCase {
$context = $this->createMock( \IContextSource::class );
$context->method( 'getConfig' )
->willReturn( $config );
$context->method( 'msg' )
->willReturnCallback( function ( $key, ...$params ) {
$msg = $this->createMock( \Message::class );
$msg->method( 'text' )->willReturn( "($key)" );
return $msg;
} );
$main = $this->createMock( \ApiMain::class );
$main->expects( $this->once() )
@ -119,6 +125,54 @@ class ApiQueryExtractsTest extends \MediaWikiTestCase {
];
}
/**
* @dataProvider provideTextsToTruncate
*/
public function testTruncate( $text, array $params, $expected ) {
/** @var ApiQueryExtracts $instance */
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
$instance->params = $params + [ 'chars' => null, 'sentences' => null, 'plaintext' => true ];
$this->assertSame( $expected, $instance->truncate( $text ) );
}
public function provideTextsToTruncate() {
return [
[ '', [], '' ],
[ 'abc', [], 'abc' ],
[
'abc',
[ 'chars' => 1 ],
'abc(ellipsis)'
],
[
'abc',
[ 'sentences' => 1 ],
'abc'
],
[
'abc abc. xyz xyz.',
[ 'chars' => 1 ],
'abc(ellipsis)'
],
[
'abc abc. xyz xyz.',
[ 'sentences' => 1 ],
'abc abc.'
],
[
'abc abc. xyz xyz.',
[ 'chars' => 1000 ],
'abc abc. xyz xyz.(ellipsis)'
],
[
'abc abc. xyz xyz.',
[ 'sentences' => 10 ],
'abc abc. xyz xyz.'
],
];
}
/**
* @dataProvider provideSectionsToFormat
*/
@ -158,4 +212,5 @@ class ApiQueryExtractsTest extends \MediaWikiTestCase {
],
];
}
}