Merge "Add test for ApiQueryExtracts::truncate()"

This commit is contained in:
jenkins-bot 2021-03-11 17:57:42 +00:00 committed by Gerrit Code Review
commit 0262d5409a
2 changed files with 57 additions and 5 deletions

View file

@ -343,11 +343,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

@ -27,6 +27,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() )
@ -125,6 +131,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
*/
@ -164,4 +218,5 @@ class ApiQueryExtractsTest extends \MediaWikiTestCase {
],
];
}
}