mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-27 09:30:09 +00:00
Merge "Add test for ApiQueryExtracts::truncate()"
This commit is contained in:
commit
0262d5409a
|
@ -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'] ) .
|
||||
|
|
|
@ -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 {
|
|||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue