| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\TextExtracts\Test; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Api\ApiQuery; |
| 8 |
use MediaWiki\Config\ConfigFactory; |
| 9 |
use MediaWiki\Config\HashConfig; |
| 10 |
use MediaWiki\Context\IContextSource; |
| 11 |
use MediaWiki\Extension\TextExtracts\ApiQueryExtracts; |
| 12 |
use MediaWiki\Language\ILanguageConverter; |
| 13 |
use MediaWiki\Language\Language; |
| 14 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 15 |
use MediaWiki\Message\Message; |
| 16 |
use MediaWiki\Title\Title; |
| 17 |
use MediaWikiCoversValidator; |
| 18 |
use Wikimedia\LightweightObjectStore\ExpirationAwareness; |
| 19 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 20 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 21 |
use Wikimedia\TestingAccessWrapper; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class ApiQueryExtractsTest extends \MediaWikiIntegrationTestCase { |
| 30 |
use MediaWikiCoversValidator; |
| 31 |
|
| 32 |
private function newInstance() { |
| 33 |
$config = new HashConfig( [ |
| 34 |
'ParserCacheExpireTime' => ExpirationAwareness::TTL_INDEFINITE, |
| 35 |
] ); |
| 36 |
|
| 37 |
$configFactory = $this->createMock( ConfigFactory::class ); |
| 38 |
$configFactory->method( 'makeConfig' ) |
| 39 |
->with( 'textextracts' ) |
| 40 |
->willReturn( $config ); |
| 41 |
|
| 42 |
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); |
| 43 |
|
| 44 |
$context = $this->createMock( IContextSource::class ); |
| 45 |
$context->method( 'getConfig' ) |
| 46 |
->willReturn( $config ); |
| 47 |
$context->method( 'msg' ) |
| 48 |
->willReturnCallback( function ( $key, ...$params ) { |
| 49 |
$msg = $this->createMock( Message::class ); |
| 50 |
$msg->method( 'text' )->willReturn( "($key)" ); |
| 51 |
return $msg; |
| 52 |
} ); |
| 53 |
|
| 54 |
$main = $this->createMock( ApiMain::class ); |
| 55 |
$main->expects( $this->once() ) |
| 56 |
->method( 'getContext' ) |
| 57 |
->willReturn( $context ); |
| 58 |
|
| 59 |
$query = $this->createMock( ApiQuery::class ); |
| 60 |
$query->expects( $this->once() ) |
| 61 |
->method( 'getMain' ) |
| 62 |
->willReturn( $main ); |
| 63 |
|
| 64 |
$langConv = $this->createMock( ILanguageConverter::class ); |
| 65 |
$langConv->method( 'getPreferredVariant' ) |
| 66 |
->willReturn( 'en' ); |
| 67 |
$langConvFactory = $this->createMock( LanguageConverterFactory::class ); |
| 68 |
$langConvFactory->method( 'getLanguageConverter' ) |
| 69 |
->willReturn( $langConv ); |
| 70 |
|
| 71 |
return new ApiQueryExtracts( |
| 72 |
$query, |
| 73 |
'', |
| 74 |
$configFactory, |
| 75 |
$cache, |
| 76 |
$langConvFactory, |
| 77 |
$this->getServiceContainer()->getWikiPageFactory(), |
| 78 |
$this->getServiceContainer()->getTitleFormatter() |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public function testMemCacheHelpers() { |
| 83 |
$title = $this->createMock( Title::class ); |
| 84 |
$title->method( 'getPageLanguage' ) |
| 85 |
->willReturn( $this->createMock( Language::class ) ); |
| 86 |
|
| 87 |
$page = $this->createMock( \WikiPage::class ); |
| 88 |
$page->method( 'getTitle' ) |
| 89 |
->willReturn( $title ); |
| 90 |
$page->method( 'getId' ) |
| 91 |
->willReturn( 123 ); |
| 92 |
$page->method( 'getTouched' ) |
| 93 |
->willReturn( '20010101000000' ); |
| 94 |
|
| 95 |
$text = 'Text to cache'; |
| 96 |
|
| 97 |
|
| 98 |
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() ); |
| 99 |
|
| 100 |
$instance->params = [ 'intro' => false, 'plaintext' => false ]; |
| 101 |
|
| 102 |
$this->assertFalse( $instance->getFromCache( $page, false ), 'is not cached yet' ); |
| 103 |
|
| 104 |
$instance->setCache( $page, $text ); |
| 105 |
$instance->cache->clearProcessCache(); |
| 106 |
$this->assertSame( $text, $instance->getFromCache( $page, false ) ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testSelfDocumentation() { |
| 110 |
|
| 111 |
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() ); |
| 112 |
|
| 113 |
$this->assertIsString( $instance->getCacheMode( [] ) ); |
| 114 |
$this->assertNotEmpty( $instance->getExamplesMessages() ); |
| 115 |
$this->assertIsString( $instance->getHelpUrls() ); |
| 116 |
|
| 117 |
$params = $instance->getAllowedParams(); |
| 118 |
$this->assertIsArray( $params ); |
| 119 |
|
| 120 |
$this->assertSame( 1, $params['chars'][ApiBase::PARAM_MIN] ); |
| 121 |
$this->assertSame( 1200, $params['chars'][ApiBase::PARAM_MAX] ); |
| 122 |
|
| 123 |
$this->assertSame( 20, $params['limit'][ApiBase::PARAM_DFLT] ); |
| 124 |
$this->assertSame( 'limit', $params['limit'][ApiBase::PARAM_TYPE] ); |
| 125 |
$this->assertSame( 1, $params['limit'][ApiBase::PARAM_MIN] ); |
| 126 |
$this->assertSame( 20, $params['limit'][ApiBase::PARAM_MAX] ); |
| 127 |
$this->assertSame( 20, $params['limit'][ApiBase::PARAM_MAX2] ); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function testGetFirstSection( $text, $isPlainText, $expected ) { |
| 134 |
|
| 135 |
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() ); |
| 136 |
|
| 137 |
$this->assertSame( $expected, $instance->getFirstSection( $text, $isPlainText ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public static function provideFirstSectionsToExtract() { |
| 141 |
return [ |
| 142 |
'Plain text match' => [ |
| 143 |
"First\nsection \1\2... \1\2...", |
| 144 |
true, |
| 145 |
"First\nsection ", |
| 146 |
], |
| 147 |
'Plain text without a match' => [ |
| 148 |
'Example\1\2...', |
| 149 |
true, |
| 150 |
'Example\1\2...', |
| 151 |
], |
| 152 |
|
| 153 |
'HTML match' => [ |
| 154 |
"First\nsection <h1>...<h2>...", |
| 155 |
false, |
| 156 |
"First\nsection ", |
| 157 |
], |
| 158 |
'HTML without a match' => [ |
| 159 |
'Example <h11>...', |
| 160 |
false, |
| 161 |
'Example <h11>...', |
| 162 |
], |
| 163 |
'__TOC__ before intro (HTML)' => [ |
| 164 |
'<h2 id="mw-toc-heading">Contents</h2>Intro<h2>Actual heading</h2>...', |
| 165 |
false, |
| 166 |
'<h2 id="mw-toc-heading">Contents</h2>Intro', |
| 167 |
], |
| 168 |
'__TOC__ before intro (plaintext)' => [ |
| 169 |
"\1\2_\2\1<h2 id=\"mw-toc-heading\">Contents</h2>Intro\1\2_\2\1<h2>Actual heading</h2>...", |
| 170 |
true, |
| 171 |
"\1\2_\2\1<h2 id=\"mw-toc-heading\">Contents</h2>Intro", |
| 172 |
], |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function testTruncate( $text, array $params, $expected ) { |
| 180 |
|
| 181 |
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() ); |
| 182 |
$instance->params = $params + [ 'chars' => null, 'sentences' => null, 'plaintext' => true ]; |
| 183 |
|
| 184 |
$this->assertSame( $expected, $instance->truncate( $text ) ); |
| 185 |
} |
| 186 |
|
| 187 |
public static function provideTextsToTruncate() { |
| 188 |
return [ |
| 189 |
[ '', [], '' ], |
| 190 |
[ 'abc', [], 'abc' ], |
| 191 |
[ |
| 192 |
'abc', |
| 193 |
[ 'chars' => 1 ], |
| 194 |
'abc' |
| 195 |
], |
| 196 |
[ |
| 197 |
'abc', |
| 198 |
[ 'chars' => 1, 'plaintext' => false ], |
| 199 |
'abc' |
| 200 |
], |
| 201 |
[ |
| 202 |
'abc', |
| 203 |
[ 'sentences' => 1 ], |
| 204 |
'abc' |
| 205 |
], |
| 206 |
[ |
| 207 |
'abc abc. xyz xyz.', |
| 208 |
[ 'chars' => 1 ], |
| 209 |
'abc(ellipsis)' |
| 210 |
], |
| 211 |
[ |
| 212 |
'abc abc. xyz xyz.', |
| 213 |
[ 'sentences' => 1 ], |
| 214 |
'abc abc.' |
| 215 |
], |
| 216 |
[ |
| 217 |
'abc abc. xyz xyz.', |
| 218 |
[ 'chars' => 1000 ], |
| 219 |
'abc abc. xyz xyz.' |
| 220 |
], |
| 221 |
[ |
| 222 |
'abc abc. xyz xyz.', |
| 223 |
[ 'chars' => 1000, 'plaintext' => false ], |
| 224 |
'abc abc. xyz xyz.' |
| 225 |
], |
| 226 |
[ |
| 227 |
'abc abc. xyz xyz.', |
| 228 |
[ 'sentences' => 10 ], |
| 229 |
'abc abc. xyz xyz.' |
| 230 |
], |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function testDoSections( $text, $format, $expected ) { |
| 238 |
|
| 239 |
$instance = TestingAccessWrapper::newFromObject( $this->newInstance() ); |
| 240 |
$instance->params = [ 'sectionformat' => $format ]; |
| 241 |
|
| 242 |
$this->assertSame( $expected, $instance->doSections( $text ) ); |
| 243 |
} |
| 244 |
|
| 245 |
public static function provideSectionsToFormat() { |
| 246 |
$level = 3; |
| 247 |
$marker = "\1\2$level\2\1"; |
| 248 |
|
| 249 |
return [ |
| 250 |
'Raw' => [ |
| 251 |
"$marker Headline\t\nNext line", |
| 252 |
'raw', |
| 253 |
"$marker Headline\t\nNext line", |
| 254 |
], |
| 255 |
'Wiki text' => [ |
| 256 |
"$marker Headline\t\nNext line", |
| 257 |
'wiki', |
| 258 |
"\n=== Headline ===\nNext line", |
| 259 |
], |
| 260 |
'Plain text' => [ |
| 261 |
"$marker Headline\t\nNext line", |
| 262 |
'plain', |
| 263 |
"\nHeadline\nNext line", |
| 264 |
], |
| 265 |
|
| 266 |
'Multiple matches' => [ |
| 267 |
"{$marker}First\n{$marker}Second", |
| 268 |
'wiki', |
| 269 |
"\n=== First ===\n\n=== Second ===", |
| 270 |
], |
| 271 |
]; |
| 272 |
} |
| 273 |
|
| 274 |
} |