2015-11-16 15:40:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Tests\Hooks;
|
|
|
|
|
2020-03-31 17:24:33 +00:00
|
|
|
use File;
|
2024-04-10 18:42:02 +00:00
|
|
|
use MediaWiki\Config\Config;
|
|
|
|
use MediaWiki\Config\HashConfig;
|
2022-12-11 13:00:11 +00:00
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
2023-11-01 17:51:38 +00:00
|
|
|
use MediaWiki\Linker\LinksMigration;
|
2024-06-09 17:03:17 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2024-10-20 09:10:45 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2023-08-19 04:18:19 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-01-05 21:37:40 +00:00
|
|
|
use MediaWiki\Title\TitleFactory;
|
2021-10-11 22:57:52 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2021-12-02 00:18:50 +00:00
|
|
|
use PageImages\Hooks\ParserFileProcessingHookHandlers;
|
2020-10-22 20:04:23 +00:00
|
|
|
use PageImages\PageImageCandidate;
|
2020-04-19 22:41:19 +00:00
|
|
|
use PageImages\PageImages;
|
2016-03-07 22:48:15 +00:00
|
|
|
use RepoGroup;
|
2024-10-20 09:10:45 +00:00
|
|
|
use Wikimedia\ObjectCache\WANObjectCache;
|
2023-06-21 19:54:03 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2017-04-20 07:48:48 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2015-11-16 15:40:07 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-02 00:18:50 +00:00
|
|
|
* @covers \PageImages\Hooks\ParserFileProcessingHookHandlers
|
2015-11-16 15:40:07 +00:00
|
|
|
*
|
|
|
|
* @group PageImages
|
|
|
|
*
|
2018-05-25 04:42:29 +00:00
|
|
|
* @license WTFPL
|
2017-11-24 07:33:49 +00:00
|
|
|
* @author Thiemo Kreuz
|
2015-11-16 15:40:07 +00:00
|
|
|
*/
|
2021-12-02 00:18:50 +00:00
|
|
|
class ParserFileProcessingHookHandlersTest extends MediaWikiIntegrationTestCase {
|
2015-11-16 15:40:07 +00:00
|
|
|
|
2016-03-10 15:27:40 +00:00
|
|
|
/**
|
2017-11-23 16:06:51 +00:00
|
|
|
* @param array[] $images
|
|
|
|
*
|
2021-12-02 00:18:50 +00:00
|
|
|
* @return Parser
|
2016-03-10 15:27:40 +00:00
|
|
|
*/
|
2021-12-02 00:18:50 +00:00
|
|
|
private function getParser( array $images ) {
|
|
|
|
$parser = $this->getServiceContainer()->getParser();
|
|
|
|
$title = Title::newFromText( 'test' );
|
|
|
|
$options = ParserOptions::newFromAnon();
|
|
|
|
$parser->startExternalParse( $title, $options, Parser::OT_HTML );
|
|
|
|
$parser->getOutput()->setExtensionData( 'pageImages', $images );
|
|
|
|
return $parser;
|
|
|
|
}
|
2016-12-06 01:19:06 +00:00
|
|
|
|
2021-12-02 00:18:50 +00:00
|
|
|
private function getHtml( $indexes, $nonLeadIndex = INF ) {
|
|
|
|
$html = '';
|
|
|
|
$doneSectionBreak = false;
|
|
|
|
foreach ( $indexes as $index ) {
|
|
|
|
if ( $index >= $nonLeadIndex && !$doneSectionBreak ) {
|
|
|
|
$html .= '<mw:editsection page="Test" section="1"/>';
|
|
|
|
$doneSectionBreak = true;
|
|
|
|
}
|
|
|
|
$html .= "<!--MW-PAGEIMAGES-CANDIDATE-$index-->";
|
|
|
|
}
|
|
|
|
return $html;
|
2015-11-16 15:40:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 15:27:40 +00:00
|
|
|
/**
|
2021-12-02 00:18:50 +00:00
|
|
|
* Required to make RepoGroup::findFile in ParserFileProcessingHookHandlers::getScore return something.
|
2016-03-10 15:27:40 +00:00
|
|
|
* @return RepoGroup
|
|
|
|
*/
|
|
|
|
private function getRepoGroup() {
|
2024-02-08 11:07:29 +00:00
|
|
|
$file = $this->createMock( File::class );
|
2016-03-07 22:48:15 +00:00
|
|
|
// ugly hack to avoid all the unmockable crap in FormatMetadata
|
2021-05-04 14:48:45 +00:00
|
|
|
$file->method( 'isDeleted' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( true );
|
2016-03-07 22:48:15 +00:00
|
|
|
|
2024-02-08 11:07:29 +00:00
|
|
|
$repoGroup = $this->createMock( RepoGroup::class );
|
2021-05-04 14:48:45 +00:00
|
|
|
$repoGroup->method( 'findFile' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $file );
|
2016-03-07 22:48:15 +00:00
|
|
|
|
2016-03-10 15:27:40 +00:00
|
|
|
return $repoGroup;
|
|
|
|
}
|
2016-03-07 22:48:15 +00:00
|
|
|
|
2024-04-10 18:42:02 +00:00
|
|
|
private function getHandler( $images, $leadOnly = false ) {
|
|
|
|
return new class ( $images, $leadOnly ) extends ParserFileProcessingHookHandlers {
|
2021-12-02 00:18:50 +00:00
|
|
|
private $images;
|
|
|
|
private $isFreeMap;
|
|
|
|
|
2024-04-10 18:42:02 +00:00
|
|
|
public function __construct( $images, $leadOnly ) {
|
|
|
|
$this->config = new HashConfig( [
|
|
|
|
'PageImagesLeadSectionOnly' => $leadOnly,
|
|
|
|
] );
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->images = $images;
|
|
|
|
foreach ( $images as $image ) {
|
|
|
|
$this->isFreeMap[$image['filename']] = $image['isFree'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isImageFree( $fileName ) {
|
|
|
|
return $this->isFreeMap[$fileName] ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getScore( PageImageCandidate $image, $position ) {
|
|
|
|
return $this->images[$position]['score'];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
/**
|
2021-12-02 00:18:50 +00:00
|
|
|
* @dataProvider provideDoParserAfterTidy
|
2022-09-02 22:15:57 +00:00
|
|
|
* @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::onParserAfterTidy
|
2016-11-10 00:02:00 +00:00
|
|
|
*/
|
2021-12-02 00:18:50 +00:00
|
|
|
public function testDoParserAfterTidy(
|
2017-05-30 20:36:57 +00:00
|
|
|
array $images,
|
|
|
|
$expectedFreeFileName,
|
|
|
|
$expectedNonFreeFileName
|
|
|
|
) {
|
2021-12-02 00:18:50 +00:00
|
|
|
$parser = $this->getParser( $images );
|
|
|
|
$html = $this->getHtml( array_keys( $images ) );
|
|
|
|
$handler = $this->getHandler( $images );
|
2022-09-02 22:15:57 +00:00
|
|
|
$handler->onParserAfterTidy( $parser, $html );
|
2021-12-02 00:18:50 +00:00
|
|
|
$properties = $parser->getOutput()->getPageProperties();
|
2016-11-10 00:02:00 +00:00
|
|
|
|
2020-01-14 08:47:58 +00:00
|
|
|
if ( $expectedFreeFileName === null ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->assertArrayNotHasKey( PageImages::PROP_NAME_FREE, $properties );
|
2016-11-10 00:02:00 +00:00
|
|
|
} else {
|
2017-05-30 19:49:44 +00:00
|
|
|
$this->assertSame( $expectedFreeFileName,
|
2021-12-02 00:18:50 +00:00
|
|
|
$properties[PageImages::PROP_NAME_FREE] );
|
2016-11-10 00:02:00 +00:00
|
|
|
}
|
2020-01-14 08:47:58 +00:00
|
|
|
if ( $expectedNonFreeFileName === null ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->assertArrayNotHasKey( PageImages::PROP_NAME, $properties );
|
2016-11-10 00:02:00 +00:00
|
|
|
} else {
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->assertSame( $expectedNonFreeFileName, $properties[PageImages::PROP_NAME] );
|
2016-11-10 00:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 15:29:11 +00:00
|
|
|
public static function provideDoParserAfterTidy() {
|
2016-11-10 00:02:00 +00:00
|
|
|
return [
|
|
|
|
// both images are non-free
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
|
|
|
|
[ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => false ],
|
|
|
|
],
|
|
|
|
null,
|
|
|
|
'A.jpg'
|
|
|
|
],
|
|
|
|
// both images are free
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => true ],
|
|
|
|
[ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
|
|
|
|
],
|
|
|
|
'A.jpg',
|
|
|
|
null
|
|
|
|
],
|
|
|
|
// one free (with a higher score), one non-free image
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => true ],
|
|
|
|
[ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => false ],
|
|
|
|
],
|
|
|
|
'A.jpg',
|
|
|
|
null
|
|
|
|
],
|
|
|
|
// one non-free (with a higher score), one free image
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
|
|
|
|
[ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
|
|
|
|
],
|
|
|
|
'B.jpg',
|
|
|
|
'A.jpg'
|
|
|
|
]
|
|
|
|
];
|
2016-03-10 15:27:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:19:06 +00:00
|
|
|
/**
|
2021-12-02 00:18:50 +00:00
|
|
|
* @dataProvider provideDoParserAfterTidy_lead
|
2022-09-02 22:15:57 +00:00
|
|
|
* @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::onParserAfterTidy
|
2016-12-06 01:19:06 +00:00
|
|
|
*/
|
2021-12-02 00:18:50 +00:00
|
|
|
public function testDoParserAfterTidy_lead( $leadOnly ) {
|
2016-12-06 01:19:06 +00:00
|
|
|
$candidates = [
|
2021-03-20 05:22:04 +00:00
|
|
|
[ 'filename' => 'A.jpg', 'score' => 100, 'isFree' => false ],
|
2021-12-02 00:18:50 +00:00
|
|
|
[ 'filename' => 'B.jpg', 'score' => 90, 'isFree' => true ],
|
2016-12-06 01:19:06 +00:00
|
|
|
];
|
|
|
|
|
2021-12-02 00:18:50 +00:00
|
|
|
$parser = $this->getParser( $candidates );
|
|
|
|
$html = $this->getHtml( array_keys( $candidates ), 1 );
|
2024-04-10 18:42:02 +00:00
|
|
|
$handler = $this->getHandler( $candidates, $leadOnly );
|
2022-09-02 22:15:57 +00:00
|
|
|
$handler->onParserAfterTidy( $parser, $html );
|
2021-12-02 00:18:50 +00:00
|
|
|
if ( $leadOnly ) {
|
2022-02-16 23:37:34 +00:00
|
|
|
$this->assertNull(
|
|
|
|
$parser->getOutput()->getPageProperty( PageImages::PROP_NAME_FREE ),
|
2021-12-02 00:18:50 +00:00
|
|
|
'Only lead images are returned.' );
|
|
|
|
} else {
|
|
|
|
$this->assertIsString(
|
2022-02-16 23:37:34 +00:00
|
|
|
$parser->getOutput()->getPageProperty( PageImages::PROP_NAME_FREE ),
|
2021-12-02 00:18:50 +00:00
|
|
|
'All images are returned'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 01:19:06 +00:00
|
|
|
|
2021-12-02 00:18:50 +00:00
|
|
|
public static function provideDoParserAfterTidy_lead() {
|
|
|
|
return [
|
|
|
|
[ false ],
|
|
|
|
[ true ]
|
|
|
|
];
|
2016-12-06 01:19:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 21:29:03 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideGetScore
|
|
|
|
*/
|
2016-11-10 00:02:00 +00:00
|
|
|
public function testGetScore( $image, $scoreFromTable, $position, $expected ) {
|
|
|
|
$mock = TestingAccessWrapper::newFromObject(
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->getMockBuilder( ParserFileProcessingHookHandlers::class )
|
2022-12-11 13:00:11 +00:00
|
|
|
->setConstructorArgs( [
|
2024-04-10 18:42:02 +00:00
|
|
|
$this->getServiceContainer()->getMainConfig(),
|
2022-12-11 13:00:11 +00:00
|
|
|
$this->getRepoGroup(),
|
|
|
|
$this->createMock( WANObjectCache::class ),
|
|
|
|
$this->createMock( HttpRequestFactory::class ),
|
2023-06-21 19:54:03 +00:00
|
|
|
$this->createMock( IConnectionProvider::class ),
|
2023-06-21 21:03:44 +00:00
|
|
|
$this->createMock( TitleFactory::class ),
|
2023-11-01 17:51:38 +00:00
|
|
|
$this->createMock( LinksMigration::class ),
|
2022-12-11 13:00:11 +00:00
|
|
|
] )
|
2021-05-12 09:23:38 +00:00
|
|
|
->onlyMethods( [ 'scoreFromTable', 'fetchFileMetadata', 'getRatio', 'getDenylist' ] )
|
2016-11-10 00:02:00 +00:00
|
|
|
->getMock()
|
|
|
|
);
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'scoreFromTable' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $scoreFromTable );
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'getRatio' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( 0 );
|
2021-05-07 20:19:07 +00:00
|
|
|
$mock->method( 'getDenylist' )
|
|
|
|
->willReturn( [ 'denylisted.jpg' => 1 ] );
|
2016-11-16 21:29:03 +00:00
|
|
|
|
2020-10-22 20:04:23 +00:00
|
|
|
$score = $mock->getScore( PageImageCandidate::newFromArray( $image ), $position );
|
2021-05-04 14:48:45 +00:00
|
|
|
$this->assertSame( $expected, $score );
|
2016-11-16 21:29:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 15:29:11 +00:00
|
|
|
public static function provideGetScore() {
|
2016-11-16 21:29:03 +00:00
|
|
|
return [
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'filename' => 'A.jpg', 'handler' => [ 'width' => 100 ] ],
|
2016-11-16 21:29:03 +00:00
|
|
|
100,
|
|
|
|
0,
|
|
|
|
// width score + ratio score + position score
|
|
|
|
100 + 100 + 8
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
|
2016-11-16 21:29:03 +00:00
|
|
|
50,
|
|
|
|
1,
|
|
|
|
// width score + ratio score + position score
|
|
|
|
106
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
|
2016-11-16 21:29:03 +00:00
|
|
|
50,
|
|
|
|
2,
|
|
|
|
// width score + ratio score + position score
|
|
|
|
104
|
|
|
|
],
|
|
|
|
[
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'filename' => 'A.jpg', 'fullwidth' => 100 ],
|
2016-11-16 21:29:03 +00:00
|
|
|
50,
|
|
|
|
3,
|
|
|
|
// width score + ratio score + position score
|
|
|
|
103
|
|
|
|
],
|
|
|
|
[
|
2021-05-07 20:19:07 +00:00
|
|
|
[ 'filename' => 'denylisted.jpg', 'fullwidth' => 100 ],
|
2016-11-16 21:29:03 +00:00
|
|
|
50,
|
|
|
|
3,
|
2021-05-07 20:19:07 +00:00
|
|
|
// denylist score
|
2017-08-11 04:15:33 +00:00
|
|
|
- 1000
|
2016-11-16 21:29:03 +00:00
|
|
|
],
|
2021-11-04 23:15:39 +00:00
|
|
|
[
|
|
|
|
[ 'filename' => 'A.jpg', 'frame' => [ 'class' => 'notpageimage' ] ],
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
-1000
|
|
|
|
],
|
2016-11-16 21:29:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideScoreFromTable
|
2021-12-02 00:18:50 +00:00
|
|
|
* @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::scoreFromTable
|
2016-11-16 21:29:03 +00:00
|
|
|
*/
|
2019-01-09 10:42:44 +00:00
|
|
|
public function testScoreFromTable( array $scores, $value, $expected ) {
|
2021-12-02 00:18:50 +00:00
|
|
|
/** @var ParserFileProcessingHookHandlers $handlerWrapper */
|
2022-09-02 22:15:57 +00:00
|
|
|
$handlerWrapper = TestingAccessWrapper::newFromObject(
|
2022-12-11 13:00:11 +00:00
|
|
|
new ParserFileProcessingHookHandlers(
|
2024-04-10 18:42:02 +00:00
|
|
|
$this->createMock( Config::class ),
|
2022-12-11 13:00:11 +00:00
|
|
|
$this->getRepoGroup(),
|
|
|
|
$this->createMock( WANObjectCache::class ),
|
2023-06-21 19:54:03 +00:00
|
|
|
$this->createMock( HttpRequestFactory::class ),
|
2023-06-21 21:03:44 +00:00
|
|
|
$this->createMock( IConnectionProvider::class ),
|
2023-11-01 17:51:38 +00:00
|
|
|
$this->createMock( TitleFactory::class ),
|
|
|
|
$this->createMock( LinksMigration::class )
|
2022-12-11 13:00:11 +00:00
|
|
|
)
|
2022-09-02 22:15:57 +00:00
|
|
|
);
|
2016-11-16 21:29:03 +00:00
|
|
|
|
2019-01-09 10:42:44 +00:00
|
|
|
$score = $handlerWrapper->scoreFromTable( $value, $scores );
|
2016-11-16 21:29:03 +00:00
|
|
|
$this->assertEquals( $expected, $score );
|
|
|
|
}
|
|
|
|
|
2023-05-20 15:29:11 +00:00
|
|
|
public static function provideScoreFromTable() {
|
2016-11-16 21:29:03 +00:00
|
|
|
return [
|
2019-01-09 10:42:44 +00:00
|
|
|
'no match' => [ [], 100, 0 ],
|
2019-01-09 10:49:50 +00:00
|
|
|
'float' => [ [ 0.5 ], 0, 0.5 ],
|
2019-01-09 10:42:44 +00:00
|
|
|
|
|
|
|
'always min when below range' => [ [ 200 => 2, 800 => 1 ], 0, 2 ],
|
|
|
|
'always max when above range' => [ [ 200 => 2, 800 => 1 ], 1000, 1 ],
|
|
|
|
|
|
|
|
'always min when below range (reversed)' => [ [ 800 => 1, 200 => 2 ], 0, 2 ],
|
|
|
|
'always max when above range (reversed)' => [ [ 800 => 1, 200 => 2 ], 1000, 1 ],
|
|
|
|
|
|
|
|
'min match' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 200, 2 ],
|
|
|
|
'above min' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 201, 3 ],
|
|
|
|
'second last match' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 400, 3 ],
|
|
|
|
'above second last' => [ [ 200 => 2, 400 => 3, 800 => 1 ], 401, 1 ],
|
|
|
|
|
|
|
|
// These test cases use the default values from extension.json
|
2024-04-10 18:42:02 +00:00
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 100, -100 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 119, -100 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 300, 10 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 400, 10 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 500, 5 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 600, 5 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 601, 0 ],
|
|
|
|
[ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 999, 0 ],
|
|
|
|
[ [ 99 => -100, 100 => 0 ], 99, -100 ],
|
|
|
|
[ [ 99 => -100, 100 => 0 ], 100, 0 ],
|
|
|
|
[ [ 99 => -100, 100 => 0 ], 500, 0 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 1, -100 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 3, -100 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 4, 0 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 5, 0 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 10, 5 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 20, 5 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 25, 0 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 30, 0 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 31, -100 ],
|
|
|
|
[ [ 3 => -100, 5 => 0, 20 => 5, 30 => 0, 31 => -100 ], 40, -100 ],
|
|
|
|
|
|
|
|
'T212013' => [ [ 119 => -100, 400 => 10, 600 => 5, 601 => 0 ], 0, -100 ],
|
2016-11-16 21:29:03 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideIsFreeImage
|
2021-12-02 00:18:50 +00:00
|
|
|
* @covers \PageImages\Hooks\ParserFileProcessingHookHandlers::isImageFree
|
2016-11-10 00:02:00 +00:00
|
|
|
*/
|
|
|
|
public function testIsFreeImage( $fileName, $metadata, $expected ) {
|
|
|
|
$mock = TestingAccessWrapper::newFromObject(
|
2021-12-02 00:18:50 +00:00
|
|
|
$this->getMockBuilder( ParserFileProcessingHookHandlers::class )
|
2022-12-11 13:00:11 +00:00
|
|
|
->setConstructorArgs( [
|
2024-04-10 18:42:02 +00:00
|
|
|
$this->getServiceContainer()->getMainConfig(),
|
2022-12-11 13:00:11 +00:00
|
|
|
$this->getRepoGroup(),
|
|
|
|
$this->createMock( WANObjectCache::class ),
|
|
|
|
$this->createMock( HttpRequestFactory::class ),
|
2023-06-21 19:54:03 +00:00
|
|
|
$this->createMock( IConnectionProvider::class ),
|
2023-06-21 21:03:44 +00:00
|
|
|
$this->createMock( TitleFactory::class ),
|
2023-11-01 17:51:38 +00:00
|
|
|
$this->createMock( LinksMigration::class ),
|
2022-12-11 13:00:11 +00:00
|
|
|
] )
|
2021-05-12 09:23:38 +00:00
|
|
|
->onlyMethods( [ 'fetchFileMetadata' ] )
|
2016-11-10 00:02:00 +00:00
|
|
|
->getMock()
|
|
|
|
);
|
2021-05-04 14:48:45 +00:00
|
|
|
$mock->method( 'fetchFileMetadata' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $metadata );
|
2021-12-02 00:18:50 +00:00
|
|
|
/** @var ParserFileProcessingHookHandlers $mock */
|
2021-05-04 14:48:45 +00:00
|
|
|
$this->assertSame( $expected, $mock->isImageFree( $fileName ) );
|
2016-03-07 22:48:15 +00:00
|
|
|
}
|
2016-03-10 15:27:40 +00:00
|
|
|
|
2023-05-20 15:29:11 +00:00
|
|
|
public static function provideIsFreeImage() {
|
2016-11-10 00:02:00 +00:00
|
|
|
return [
|
2016-12-02 00:49:13 +00:00
|
|
|
[ 'A.jpg', [], true ],
|
|
|
|
[ 'A.jpg', [ 'NonFree' => [ 'value' => '0' ] ], true ],
|
|
|
|
[ 'A.jpg', [ 'NonFree' => [ 'value' => 0 ] ], true ],
|
|
|
|
[ 'A.jpg', [ 'NonFree' => [ 'value' => false ] ], true ],
|
|
|
|
[ 'A.jpg', [ 'NonFree' => [ 'value' => 'something' ] ], false ],
|
|
|
|
[ 'A.jpg', [ 'something' => [ 'value' => 'something' ] ], true ],
|
2016-11-10 00:02:00 +00:00
|
|
|
];
|
|
|
|
}
|
2015-11-16 15:40:07 +00:00
|
|
|
}
|