2017-05-19 13:37:26 +00:00
|
|
|
<?php
|
2019-02-11 16:32:20 +00:00
|
|
|
|
2017-05-19 13:37:26 +00:00
|
|
|
namespace TextExtracts\Test;
|
|
|
|
|
2018-01-23 22:54:12 +00:00
|
|
|
use MediaWikiCoversValidator;
|
2017-05-19 13:37:26 +00:00
|
|
|
use TextExtracts\ApiQueryExtracts;
|
2019-02-11 16:32:20 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2017-05-19 13:37:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-23 22:54:12 +00:00
|
|
|
* @covers \TextExtracts\ApiQueryExtracts
|
2017-05-19 13:37:26 +00:00
|
|
|
* @group TextExtracts
|
2019-04-24 16:26:53 +00:00
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
2017-05-19 13:37:26 +00:00
|
|
|
*/
|
2019-02-13 13:37:24 +00:00
|
|
|
class ApiQueryExtractsTest extends \MediaWikiTestCase {
|
2018-01-23 22:54:12 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
2017-05-19 13:37:26 +00:00
|
|
|
private function newInstance() {
|
2019-02-13 13:37:24 +00:00
|
|
|
$config = new \HashConfig( [
|
|
|
|
'ParserCacheExpireTime' => \IExpiringStore::TTL_INDEFINITE,
|
|
|
|
] );
|
2017-05-19 13:37:26 +00:00
|
|
|
|
2020-02-07 02:51:55 +00:00
|
|
|
$cache = new \WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
|
|
|
|
|
2019-02-13 13:37:24 +00:00
|
|
|
$context = $this->createMock( \IContextSource::class );
|
|
|
|
$context->method( 'getConfig' )
|
|
|
|
->willReturn( $config );
|
2021-01-08 08:03:05 +00:00
|
|
|
$context->method( 'msg' )
|
|
|
|
->willReturnCallback( function ( $key, ...$params ) {
|
|
|
|
$msg = $this->createMock( \Message::class );
|
|
|
|
$msg->method( 'text' )->willReturn( "($key)" );
|
|
|
|
return $msg;
|
|
|
|
} );
|
2019-02-13 13:37:24 +00:00
|
|
|
|
|
|
|
$main = $this->createMock( \ApiMain::class );
|
2017-05-19 13:37:26 +00:00
|
|
|
$main->expects( $this->once() )
|
|
|
|
->method( 'getContext' )
|
2019-02-13 13:37:24 +00:00
|
|
|
->willReturn( $context );
|
2017-05-19 13:37:26 +00:00
|
|
|
|
2019-02-13 13:37:24 +00:00
|
|
|
$query = $this->createMock( \ApiQuery::class );
|
2017-05-19 13:37:26 +00:00
|
|
|
$query->expects( $this->once() )
|
|
|
|
->method( 'getMain' )
|
2019-02-13 13:37:24 +00:00
|
|
|
->willReturn( $main );
|
2017-05-19 13:37:26 +00:00
|
|
|
|
2020-02-07 02:51:55 +00:00
|
|
|
return new ApiQueryExtracts( $query, '', $config, $cache );
|
2017-05-19 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:37:24 +00:00
|
|
|
public function testMemCacheHelpers() {
|
|
|
|
$title = $this->createMock( \Title::class );
|
|
|
|
$title->method( 'getPageLanguage' )
|
|
|
|
->willReturn( $this->createMock( \Language::class ) );
|
|
|
|
|
|
|
|
$page = $this->createMock( \WikiPage::class );
|
|
|
|
$page->method( 'getTitle' )
|
|
|
|
->willReturn( $title );
|
|
|
|
|
|
|
|
$text = 'Text to cache';
|
|
|
|
|
|
|
|
/** @var ApiQueryExtracts $instance */
|
|
|
|
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
|
2020-03-13 21:08:54 +00:00
|
|
|
// Default param values for this API module
|
|
|
|
$instance->params = [ 'intro' => false, 'plaintext' => false ];
|
|
|
|
|
2019-02-13 13:37:24 +00:00
|
|
|
$this->assertFalse( $instance->getFromCache( $page, false ), 'is not cached yet' );
|
2020-02-07 02:51:55 +00:00
|
|
|
|
2019-02-13 13:37:24 +00:00
|
|
|
$instance->setCache( $page, $text );
|
2020-02-07 02:51:55 +00:00
|
|
|
$instance->cache->clearProcessCache();
|
2019-02-13 13:37:24 +00:00
|
|
|
$this->assertSame( $text, $instance->getFromCache( $page, false ) );
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:32:20 +00:00
|
|
|
public function testSelfDocumentation() {
|
|
|
|
/** @var ApiQueryExtracts $instance */
|
|
|
|
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
|
|
|
|
|
2020-01-15 09:17:21 +00:00
|
|
|
$this->assertIsString( $instance->getCacheMode( [] ) );
|
2019-02-11 16:32:20 +00:00
|
|
|
$this->assertNotEmpty( $instance->getExamplesMessages() );
|
2020-01-15 09:17:21 +00:00
|
|
|
$this->assertIsString( $instance->getHelpUrls() );
|
2019-02-11 16:32:20 +00:00
|
|
|
|
2017-05-19 13:37:26 +00:00
|
|
|
$params = $instance->getAllowedParams();
|
2020-01-15 09:17:21 +00:00
|
|
|
$this->assertIsArray( $params );
|
2019-03-19 11:27:54 +00:00
|
|
|
|
|
|
|
$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 );
|
2017-05-19 13:37:26 +00:00
|
|
|
}
|
2019-02-11 16:32:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFirstSectionsToExtract
|
|
|
|
*/
|
|
|
|
public function testGetFirstSection( $text, $isPlainText, $expected ) {
|
|
|
|
/** @var ApiQueryExtracts $instance */
|
|
|
|
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
|
|
|
|
|
|
|
|
$this->assertSame( $expected, $instance->getFirstSection( $text, $isPlainText ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFirstSectionsToExtract() {
|
|
|
|
return [
|
|
|
|
'Plain text match' => [
|
|
|
|
"First\nsection \1\2... \1\2...",
|
|
|
|
true,
|
|
|
|
"First\nsection ",
|
|
|
|
],
|
|
|
|
'Plain text without a match' => [
|
|
|
|
'Example\1\2...',
|
|
|
|
true,
|
|
|
|
'Example\1\2...',
|
|
|
|
],
|
|
|
|
|
|
|
|
'HTML match' => [
|
|
|
|
"First\nsection <h1>...<h2>...",
|
|
|
|
false,
|
|
|
|
"First\nsection ",
|
|
|
|
],
|
|
|
|
'HTML without a match' => [
|
|
|
|
'Example <h11>...',
|
|
|
|
false,
|
|
|
|
'Example <h11>...',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-01-08 08:03:05 +00:00
|
|
|
/**
|
|
|
|
* @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.'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:32:20 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSectionsToFormat
|
|
|
|
*/
|
|
|
|
public function testDoSections( $text, $format, $expected ) {
|
|
|
|
/** @var ApiQueryExtracts $instance */
|
|
|
|
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() );
|
|
|
|
$instance->params = [ 'sectionformat' => $format ];
|
|
|
|
|
|
|
|
$this->assertSame( $expected, $instance->doSections( $text ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideSectionsToFormat() {
|
|
|
|
$level = 3;
|
|
|
|
$marker = "\1\2$level\2\1";
|
|
|
|
|
|
|
|
return [
|
|
|
|
'Raw' => [
|
|
|
|
"$marker Headline\t\nNext line",
|
|
|
|
'raw',
|
|
|
|
"$marker Headline\t\nNext line",
|
|
|
|
],
|
|
|
|
'Wiki text' => [
|
|
|
|
"$marker Headline\t\nNext line",
|
|
|
|
'wiki',
|
|
|
|
"\n=== Headline ===\nNext line",
|
|
|
|
],
|
|
|
|
'Plain text' => [
|
|
|
|
"$marker Headline\t\nNext line",
|
|
|
|
'plain',
|
|
|
|
"\nHeadline\nNext line",
|
|
|
|
],
|
|
|
|
|
|
|
|
'Multiple matches' => [
|
|
|
|
"${marker}First\n${marker}Second",
|
|
|
|
'wiki',
|
|
|
|
"\n=== First ===\n\n=== Second ===",
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2021-01-08 08:03:05 +00:00
|
|
|
|
2017-05-19 13:37:26 +00:00
|
|
|
}
|