Debian Codegraph @

extensions/PageImages/tests/phpunit/Hooks/ParserFileProcessingHookHandlersTest.php

raw symbols SCIP
1 <?php
2
3 namespace PageImages\Tests\Hooks;
4
5 use File;
6 use MediaWiki\Config\Config;
7 use MediaWiki\Config\HashConfig;
8 use MediaWiki\Http\HttpRequestFactory;
9 use MediaWiki\Linker\LinksMigration;
10 use MediaWiki\Parser\Parser;
11 use MediaWiki\Parser\ParserOptions;
12 use MediaWiki\Title\Title;
13 use MediaWiki\Title\TitleFactory;
14 use MediaWikiIntegrationTestCase;
15 use PageImages\Hooks\ParserFileProcessingHookHandlers;
16 use PageImages\PageImageCandidate;
17 use PageImages\PageImages;
18 use RepoGroup;
19 use Wikimedia\ObjectCache\WANObjectCache;
20 use Wikimedia\Rdbms\IConnectionProvider;
21 use Wikimedia\TestingAccessWrapper;
22
23 /**
24 * @covers \PageImages\Hooks\ParserFileProcessingHookHandlers
25 *
26 * @group PageImages
27 *
28 * @license WTFPL
29 * @author Thiemo Kreuz
30 */
31 class ParserFileProcessingHookHandlersTest extends MediaWikiIntegrationTestCase {
32
33 /**
34 * @param array[] $images
35 *
36 * @return Parser
37 */
38 private function getParser( array $images ) {
39 $parser = $this->getServiceContainer()->getParser();
40 $title = Title::newFromText( 'test' );
41 $options = ParserOptions::newFromAnon();
42 $parser->startExternalParse( $title, $options, Parser::OT_HTML );
43 $parser->getOutput()->setExtensionData( 'pageImages', $images );
44 return $parser;
45 }
46
47 private function getHtml( $indexes, $nonLeadIndex = INF ) {
48 $html = '';
49 $doneSectionBreak = false;
50 foreach ( $indexes as $index ) {
51 if ( $index >= $nonLeadIndex && !$doneSectionBreak ) {
52 $html .= '<mw:editsection page="Test" section="1"/>';
53 $doneSectionBreak = true;
54 }
55 $html .= "<!--MW-PAGEIMAGES-CANDIDATE-$index-->";
56 }
57 return $html;
58 }
59
60 /**
61 * Required to make RepoGroup::findFile in ParserFileProcessingHookHandlers::getScore return something.
62 * @return RepoGroup
63 */
64 private function getRepoGroup() {
65 $file = $this->createMock( File::class );
66 // ugly hack to avoid all the unmockable crap in FormatMetadata
67 $file->method( 'isDeleted' )
68 ->willReturn( true );
69
70 $repoGroup = $this->createMock( RepoGroup::class );
71 $repoGroup->method( 'findFile' )
72 ->willReturn( $file );
73
74 return $repoGroup;
75 }
76
77 private function getHandler( $images, $leadOnly = false ) {
78 return new class ( $images, $leadOnly ) extends ParserFileProcessingHookHandlers {
79 private $images;
80 private $isFreeMap;
81
82 public function __construct( $images, $leadOnly ) {
83 $this->config = new HashConfig( [
84 'PageImagesLeadSectionOnly' => $leadOnly,
85 ] );
86 $this->images = $images;
87 foreach ( $images as $image ) {
88 $this->isFreeMap[$image['filename']] = $image['isFree'];
89 }
90 }
91
92 protected function isImageFree( $fileName ) {
93 return $this->isFreeMap[$fileName] ?? false;
94 }
95
96 protected function getScore( PageImageCandidate $image, $position ) {
97 return $this->images[$position]['score'];
98 }
99 };
100 }
101
102 /**
103 * @dataProvider provideDoParserAfterTidy
104 * @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::onParserAfterTidy
105 */
106 public function testDoParserAfterTidy(
107 array $images,
108 $expectedFreeFileName,
109 $expectedNonFreeFileName
110 ) {
111 $parser = $this->getParser( $images );
112 $html = $this->getHtml( array_keys( $images ) );
113 $handler = $this->getHandler( $images );
114 $handler->onParserAfterTidy( $parser, $html );
115 $properties = $parser->getOutput()->getPageProperties();
116
117 if ( $expectedFreeFileName === null ) {
118 $this->assertArrayNotHasKey( PageImages::PROP_NAME_FREE, $properties );
119 } else {
120 $this->assertSame( $expectedFreeFileName,
121 $properties[PageImages::PROP_NAME_FREE] );
122 }
123 if ( $expectedNonFreeFileName === null ) {
124 $this->assertArrayNotHasKey( PageImages::PROP_NAME, $properties );
125 } else {
126 $this->assertSame( $expectedNonFreeFileName, $properties[PageImages::PROP_NAME] );
127 }
128 }
129
130 public static function provideDoParserAfterTidy() {
131 return [
132 // both images are non-free
133 [
134 [
135 [ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
136 [ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => false ],
137 ],
138 null,
139 'A.jpg'
140 ],
141 // both images are free
142 [
143 [
144 [ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => true ],
145 [ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
146 ],
147 'A.jpg',
148 null
149 ],
150 // one free (with a higher score), one non-free image
151 [
152 [
153 [ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => true ],
154 [ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => false ],
155 ],
156 'A.jpg',
157 null
158 ],
159 // one non-free (with a higher score), one free image
160 [
161 [
162 [ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
163 [ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
164 ],
165 'B.jpg',
166 'A.jpg'
167 ]
168 ];
169 }
170
171 /**
172 * @dataProvider provideDoParserAfterTidy_lead
173 * @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::onParserAfterTidy
174 */
175 public function testDoParserAfterTidy_lead( $leadOnly ) {
176 $candidates = [
177 [ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
178 [ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
179 ];
180
181 $parser = $this->getParser( $candidates );
182 $html = $this->getHtml( array_keys( $candidates ), 1 );
183 $handler = $this->getHandler( $candidates, $leadOnly );
184 $handler->onParserAfterTidy( $parser, $html );
185 if ( $leadOnly ) {
186 $this->assertNull(
187 $parser->getOutput()->getPageProperty( PageImages::PROP_NAME_FREE ),
188 'Only lead images are returned.' );
189 } else {
190 $this->assertIsString(
191 $parser->getOutput()->getPageProperty( PageImages::PROP_NAME_FREE ),
192 'All images are returned'
193 );
194
195 }
196 }
197
198 public static function provideDoParserAfterTidy_lead() {
199 return [
200 [ false ],
201 [ true ]
202 ];
203 }
204
205 /**
206 * @dataProvider provideGetScore
207 */
208 public function testGetScore( $image, $scoreFromTable, $position, $expected ) {
209 $mock = TestingAccessWrapper::newFromObject(
210 $this->getMockBuilder( ParserFileProcessingHookHandlers::class )
211 ->setConstructorArgs( [
212 $this->getServiceContainer()->getMainConfig(),
213 $this->getRepoGroup(),
214 $this->createMock( WANObjectCache::class ),
215 $this->createMock( HttpRequestFactory::class ),
216 $this->createMock( IConnectionProvider::class ),
217 $this->createMock( TitleFactory::class ),
218 $this->createMock( LinksMigration::class ),
219 ] )
220 ->onlyMethods( [ 'scoreFromTable', 'fetchFileMetadata', 'getRatio', 'getDenylist' ] )
221 ->getMock()
222 );
223 $mock->method( 'scoreFromTable' )
224 ->willReturn( $scoreFromTable );
225 $mock->method( 'getRatio' )
226 ->willReturn( 0 );
227 $mock->method( 'getDenylist' )
228 ->willReturn( [ 'denylisted.jpg' => 1 ] );
229
230 $score = $mock->getScore( PageImageCandidate::newFromArray( $image ), $position );
231 $this->assertSame( $expected, $score );
232 }
233
234 public static function provideGetScore() {
235 return [
236 [
237 [ 'filename' => 'A.jpg', 'handler' => [ 'width' => 100 ] ],
238 100,
239 0,
240 // width score + ratio score + position score
241 100 + 100 + 8
242 ],
243 [
244 [ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
245 50,
246 1,
247 // width score + ratio score + position score
248 106
249 ],
250 [
251 [ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
252 50,
253 2,
254 // width score + ratio score + position score
255 104
256 ],
257 [
258 [ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
259 50,
260 3,
261 // width score + ratio score + position score
262 103
263 ],
264 [
265 [ 'filename' => 'denylisted.jpg', 'fullwidth' => 100 ],
266 50,
267 3,
268 // denylist score
269 - 1000
270 ],
271 [
272 [ 'filename' => 'A.jpg', 'frame' => [ 'class' => 'notpageimage' ] ],
273 0,
274 0,
275 -1000
276 ],
277 ];
278 }
279
280 /**
281 * @dataProvider provideScoreFromTable
282 * @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::scoreFromTable
283 */
284 public function testScoreFromTable( array $scores, $value, $expected ) {
285 /** @var ParserFileProcessingHookHandlers $handlerWrapper */
286 $handlerWrapper = TestingAccessWrapper::newFromObject(
287 new ParserFileProcessingHookHandlers(
288 $this->createMock( Config::class ),
289 $this->getRepoGroup(),
290 $this->createMock( WANObjectCache::class ),
291 $this->createMock( HttpRequestFactory::class ),
292 $this->createMock( IConnectionProvider::class ),
293 $this->createMock( TitleFactory::class ),
294 $this->createMock( LinksMigration::class )
295 )
296 );
297
298 $score = $handlerWrapper->scoreFromTable( $value, $scores );
299 $this->assertEquals( $expected, $score );
300 }
301
302 public static function provideScoreFromTable() {
303 return [
304 'no match' => [ [], 100, 0 ],
305 'float' => [ [ 0.5 ], 0, 0.5 ],
306
307 'always min when below range' => [ [ 200 => 2, 800 => 1 ], 0, 2 ],
308 'always max when above range' => [ [ 200 => 2, 800 => 1 ], 1000, 1 ],
309
310 'always min when below range (reversed)' => [ [ 800 => 1, 200 => 2 ], 0, 2 ],
311 'always max when above range (reversed)' => [ [ 800 => 1, 200 => 2 ], 1000, 1 ],
312
313 'min match' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 200, 2 ],
314 'above min' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 201, 3 ],
315 'second last match' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 400, 3 ],
316 'above second last' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 401, 1 ],
317
318 // These test cases use the default values from extension.json
319 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 100, -100 ],
320 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 119, -100 ],
321 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 300, 10 ],
322 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 400, 10 ],
323 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 500, 5 ],
324 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 600, 5 ],
325 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 601, 0 ],
326 [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 999, 0 ],
327 [ [ 99 => -100, 100 => 0 ], 99, -100 ],
328 [ [ 99 => -100, 100 => 0 ], 100, 0 ],
329 [ [ 99 => -100, 100 => 0 ], 500, 0 ],
330 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 1, -100 ],
331 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 3, -100 ],
332 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 4, 0 ],
333 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 5, 0 ],
334 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 10, 5 ],
335 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 20, 5 ],
336 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 25, 0 ],
337 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 30, 0 ],
338 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 31, -100 ],
339 [ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 40, -100 ],
340
341 'T212013' => [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 0, -100 ],
342 ];
343 }
344
345 /**
346 * @dataProvider provideIsFreeImage
347 * @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::isImageFree
348 */
349 public function testIsFreeImage( $fileName, $metadata, $expected ) {
350 $mock = TestingAccessWrapper::newFromObject(
351 $this->getMockBuilder( ParserFileProcessingHookHandlers::class )
352 ->setConstructorArgs( [
353 $this->getServiceContainer()->getMainConfig(),
354 $this->getRepoGroup(),
355 $this->createMock( WANObjectCache::class ),
356 $this->createMock( HttpRequestFactory::class ),
357 $this->createMock( IConnectionProvider::class ),
358 $this->createMock( TitleFactory::class ),
359 $this->createMock( LinksMigration::class ),
360 ] )
361 ->onlyMethods( [ 'fetchFileMetadata' ] )
362 ->getMock()
363 );
364 $mock->method( 'fetchFileMetadata' )
365 ->willReturn( $metadata );
366 /** @var ParserFileProcessingHookHandlers $mock */
367 $this->assertSame( $expected, $mock->isImageFree( $fileName ) );
368 }
369
370 public static function provideIsFreeImage() {
371 return [
372 [ 'A.jpg', [], true ],
373 [ 'A.jpg', [ 'NonFree' => [ 'value' => '0' ] ], true ],
374 [ 'A.jpg', [ 'NonFree' => [ 'value' => 0 ] ], true ],
375 [ 'A.jpg', [ 'NonFree' => [ 'value' => false ] ], true ],
376 [ 'A.jpg', [ 'NonFree' => [ 'value' => 'something' ] ], false ],
377 [ 'A.jpg', [ 'something' => [ 'value' => 'something' ] ], true ],
378 ];
379 }
380 }