12 use Wikimedia\TestingAccessWrapper;
35 $this->setMwGlobals(
'wgPageImagesLeadSectionOnly',
false );
46 $parserOutput->setExtensionData(
'pageImages', $images );
48 $parserOutputLead->setExtensionData(
'pageImages', $leadImages ?: $images );
50 $rev = $this->getMockBuilder(
'Revision' )
51 ->disableOriginalConstructor()
54 $content = $this->getMockBuilder(
'AbstractContent' )
55 ->disableOriginalConstructor()
58 $sectionContent = $this->getMockBuilder(
'AbstractContent' )
59 ->disableOriginalConstructor()
62 $linksUpdate = $this->getMockBuilder(
'LinksUpdate' )
63 ->disableOriginalConstructor()
66 $linksUpdate->expects( $this->any() )
67 ->method(
'getTitle' )
68 ->will( $this->returnValue(
new Title(
'LinksUpdateHandlerTest' ) ) );
70 $linksUpdate->expects( $this->any() )
71 ->method(
'getParserOutput' )
72 ->will( $this->returnValue( $parserOutput ) );
74 $linksUpdate->expects( $this->any() )
75 ->method(
'getRevision' )
76 ->will( $this->returnValue( $rev ) );
78 $rev->expects( $this->any() )
79 ->method(
'getContent' )
80 ->will( $this->returnValue(
$content ) );
83 ->method(
'getSection' )
84 ->will( $this->returnValue( $sectionContent ) );
86 $sectionContent->expects( $this->any() )
87 ->method(
'getParserOutput' )
88 ->will( $this->returnValue( $parserOutputLead ) );
98 $file = $this->getMockBuilder(
'File' )
99 ->disableOriginalConstructor()
102 $file->expects( $this->any() )
103 ->method(
'isDeleted' )
104 ->will( $this->returnValue(
true ) );
106 $repoGroup = $this->getMockBuilder(
'RepoGroup' )
107 ->disableOriginalConstructor()
109 $repoGroup->expects( $this->any() )
110 ->method(
'findFile' )
111 ->will( $this->returnValue(
$file ) );
122 $expectedFreeFileName,
123 $expectedNonFreeFileName
126 $mock = TestingAccessWrapper::newFromObject(
127 $this->getMockBuilder( LinksUpdateHookHandler::class )
128 ->setMethods( [
'getScore',
'isImageFree' ] )
135 foreach ( $images as $image ) {
136 array_push( $scoreMap, [ $image, $counter++, $image[
'score'] ] );
137 array_push( $isFreeMap, [ $image[
'filename'], $image[
'isFree'] ] );
140 $mock->expects( $this->any() )
141 ->method(
'getScore' )
142 ->will( $this->returnValueMap( $scoreMap ) );
144 $mock->expects( $this->any() )
145 ->method(
'isImageFree' )
146 ->will( $this->returnValueMap( $isFreeMap ) );
148 $mock->doLinksUpdate( $linksUpdate );
150 $this->assertTrue( property_exists( $linksUpdate,
'mProperties' ),
'precondition' );
151 if ( is_null( $expectedFreeFileName ) ) {
154 $this->assertSame( $expectedFreeFileName,
157 if ( is_null( $expectedNonFreeFileName ) ) {
169 [
'filename' =>
'A.jpg',
'score' => 100,
'isFree' => false ],
170 [
'filename' =>
'B.jpg',
'score' => 90,
'isFree' => false ],
178 [
'filename' =>
'A.jpg',
'score' => 100,
'isFree' =>
true ],
179 [
'filename' =>
'B.jpg',
'score' => 90,
'isFree' =>
true ],
187 [
'filename' =>
'A.jpg',
'score' => 100,
'isFree' =>
true ],
188 [
'filename' =>
'B.jpg',
'score' => 90,
'isFree' => false ],
196 [
'filename' =>
'A.jpg',
'score' => 100,
'isFree' => false ],
197 [
'filename' =>
'B.jpg',
'score' => 90,
'isFree' =>
true ],
210 [
'filename' =>
'A.jpg',
'score' => 100,
'isFree' => false ],
211 [
'filename' =>
'B.jpg',
'score' => 90,
'isFree' => false ],
213 $linksUpdate = $this->
getLinksUpdate( $candidates, array_slice( $candidates, 0, 1 ) );
217 $this->setMwGlobals(
'wgPageImagesLeadSectionOnly',
false );
218 $images = $handler->getPageImageCandidates( $linksUpdate );
219 $this->assertCount( 2, $images,
'All images are returned.' );
221 $this->setMwGlobals(
'wgPageImagesLeadSectionOnly',
true );
222 $images = $handler->getPageImageCandidates( $linksUpdate );
223 $this->assertCount( 1, $images,
'Only lead images are returned.' );
229 public function testGetScore( $image, $scoreFromTable, $position, $expected ) {
230 $mock = TestingAccessWrapper::newFromObject(
231 $this->getMockBuilder( LinksUpdateHookHandler::class )
232 ->setMethods( [
'scoreFromTable',
'getMetadata',
'getRatio',
'getBlacklist' ] )
235 $mock->expects( $this->any() )
236 ->method(
'scoreFromTable' )
237 ->will( $this->returnValue( $scoreFromTable ) );
238 $mock->expects( $this->any() )
239 ->method(
'getRatio' )
240 ->will( $this->returnValue( 0 ) );
241 $mock->expects( $this->any() )
242 ->method(
'getBlacklist' )
243 ->will( $this->returnValue( [
'blacklisted.jpg' => 1 ] ) );
245 $score = $mock->getScore( $image, $position );
246 $this->assertEquals( $expected, $score );
252 [
'filename' =>
'A.jpg',
'handler' => [
'width' => 100 ] ],
259 [
'filename' =>
'A.jpg',
'fullwidth' => 100 ],
266 [
'filename' =>
'A.jpg',
'fullwidth' => 100 ],
273 [
'filename' =>
'A.jpg',
'fullwidth' => 100 ],
280 [
'filename' =>
'blacklisted.jpg',
'fullwidth' => 100 ],
296 $score = $handlerWrapper->scoreFromTable( $value, $scores );
297 $this->assertEquals( $expected, $score );
301 global $wgPageImagesScores;
304 'no match' => [ [], 100, 0 ],
305 'float' => [ [ 0.5 ], 0, 0.5 ],
307 'always min when below range' => [ [ 200 => 2, 800 => 1 ], 0, 2 ],
308 'always max when above range' => [ [ 200 => 2, 800 => 1 ], 1000, 1 ],
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 ],
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 ],
319 [ $wgPageImagesScores[
'width'], 100, -100 ],
320 [ $wgPageImagesScores[
'width'], 119, -100 ],
321 [ $wgPageImagesScores[
'width'], 300, 10 ],
322 [ $wgPageImagesScores[
'width'], 400, 10 ],
323 [ $wgPageImagesScores[
'width'], 500, 5 ],
324 [ $wgPageImagesScores[
'width'], 600, 5 ],
325 [ $wgPageImagesScores[
'width'], 601, 0 ],
326 [ $wgPageImagesScores[
'width'], 999, 0 ],
327 [ $wgPageImagesScores[
'galleryImageWidth'], 99, -100 ],
328 [ $wgPageImagesScores[
'galleryImageWidth'], 100, 0 ],
329 [ $wgPageImagesScores[
'galleryImageWidth'], 500, 0 ],
330 [ $wgPageImagesScores[
'ratio'], 1, -100 ],
331 [ $wgPageImagesScores[
'ratio'], 3, -100 ],
332 [ $wgPageImagesScores[
'ratio'], 4, 0 ],
333 [ $wgPageImagesScores[
'ratio'], 5, 0 ],
334 [ $wgPageImagesScores[
'ratio'], 10, 5 ],
335 [ $wgPageImagesScores[
'ratio'], 20, 5 ],
336 [ $wgPageImagesScores[
'ratio'], 25, 0 ],
337 [ $wgPageImagesScores[
'ratio'], 30, 0 ],
338 [ $wgPageImagesScores[
'ratio'], 31, -100 ],
339 [ $wgPageImagesScores[
'ratio'], 40, -100 ],
341 'T212013' => [ $wgPageImagesScores[
'width'], 0, -100 ],
351 $mock = TestingAccessWrapper::newFromObject(
352 $this->getMockBuilder( LinksUpdateHookHandler::class )
353 ->setMethods( [
'fetchFileMetadata' ] )
356 $mock->expects( $this->any() )
357 ->method(
'fetchFileMetadata' )
358 ->will( $this->returnValue( $metadata ) );
359 $this->assertEquals( $expected, $mock->isImageFree( $fileName ) );
364 [
'A.jpg', [],
true ],
365 [
'A.jpg', [
'NonFree' => [
'value' =>
'0' ] ],
true ],
366 [
'A.jpg', [
'NonFree' => [
'value' => 0 ] ],
true ],
367 [
'A.jpg', [
'NonFree' => [
'value' => false ] ],
true ],
368 [
'A.jpg', [
'NonFree' => [
'value' =>
'something' ] ], false ],
369 [
'A.jpg', [
'something' => [
'value' =>
'something' ] ],
true ],