| 1 |
<?php |
| 2 |
|
| 3 |
namespace PageImages\Tests\Hooks; |
| 4 |
|
| 5 |
use LocalFile; |
| 6 |
use LogicException; |
| 7 |
use MediaWiki\Page\PageIdentity; |
| 8 |
use MediaWiki\Page\PageIdentityValue; |
| 9 |
use MediaWiki\Page\PageProps; |
| 10 |
use MediaWiki\Search\SearchResultThumbnailProvider; |
| 11 |
use MediaWikiIntegrationTestCase; |
| 12 |
use PageImages\Hooks\SearchResultProvideThumbnailHookHandler; |
| 13 |
use PageImages\PageImages; |
| 14 |
use RepoGroup; |
| 15 |
use ThumbnailImage; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class SearchResultProvideThumbnailHookHandlerTest extends MediaWikiIntegrationTestCase { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private function getMockThumbnailImage( |
| 30 |
int $size, |
| 31 |
LocalFile $file |
| 32 |
): ThumbnailImage { |
| 33 |
$thumbnail = $this->getMockBuilder( ThumbnailImage::class ) |
| 34 |
->disableOriginalConstructor() |
| 35 |
->onlyMethods( [ |
| 36 |
'getWidth', |
| 37 |
'getFile', |
| 38 |
'getUrl' |
| 39 |
] ) |
| 40 |
->getMock(); |
| 41 |
|
| 42 |
$thumbnail->expects( $this->once() ) |
| 43 |
->method( 'getUrl' ) |
| 44 |
->willReturn( 'https://example.org/test.url' ); |
| 45 |
|
| 46 |
$thumbnail->expects( $this->once() ) |
| 47 |
->method( 'getWidth' ) |
| 48 |
->willReturn( $size ); |
| 49 |
|
| 50 |
$thumbnail->expects( $this->once() ) |
| 51 |
->method( 'getFile' ) |
| 52 |
->willReturn( $file ); |
| 53 |
|
| 54 |
return $thumbnail; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private function getMockLocalFile( int $size, $filename ): LocalFile { |
| 64 |
$file = $this->getMockBuilder( LocalFile::class ) |
| 65 |
->disableOriginalConstructor() |
| 66 |
->onlyMethods( [ |
| 67 |
'getName', |
| 68 |
'transform', |
| 69 |
'getMimeType' |
| 70 |
] ) |
| 71 |
->getMock(); |
| 72 |
|
| 73 |
$file->expects( $this->once() ) |
| 74 |
->method( 'getName' ) |
| 75 |
->willReturn( $filename ); |
| 76 |
|
| 77 |
$file->expects( $this->once() ) |
| 78 |
->method( 'transform' ) |
| 79 |
->willReturn( $this->getMockThumbnailImage( $size, $file ) ); |
| 80 |
|
| 81 |
$file->expects( $this->once() ) |
| 82 |
->method( 'getMimeType' ) |
| 83 |
->willReturn( 'image/jpg' ); |
| 84 |
|
| 85 |
return $file; |
| 86 |
} |
| 87 |
|
| 88 |
public function testProvideThumbnail() { |
| 89 |
$pageProps = $this->getMockBuilder( PageProps::class ) |
| 90 |
->disableOriginalConstructor() |
| 91 |
->onlyMethods( [ 'getProperties' ] ) |
| 92 |
->getMock(); |
| 93 |
|
| 94 |
$pageIdentities = [ |
| 95 |
1 => new PageIdentityValue( 1, NS_MAIN, 'dbKey1', PageIdentity::LOCAL ), |
| 96 |
2 => new PageIdentityValue( 2, NS_MAIN, 'dbKey2', PageIdentity::LOCAL ), |
| 97 |
3 => new PageIdentityValue( 3, NS_FILE, 'dbKey3', PageIdentity::LOCAL ), |
| 98 |
4 => new PageIdentityValue( 4, NS_FILE, 'dbKey4', PageIdentity::LOCAL ) |
| 99 |
]; |
| 100 |
|
| 101 |
$pageProps->expects( $this->once() ) |
| 102 |
->method( 'getProperties' ) |
| 103 |
->with( |
| 104 |
$this->anything(), |
| 105 |
(array)PageImages::getPropNames( PageImages::LICENSE_FREE ) |
| 106 |
)->willReturn( [ |
| 107 |
1 => [ |
| 108 |
PageImages::getPropName( true ) => 'File1_free.jpg' |
| 109 |
], |
| 110 |
2 => [ |
| 111 |
PageImages::getPropName( true ) => 'File2_free.jpg', |
| 112 |
] ] ); |
| 113 |
|
| 114 |
$repoGroup = $this->getMockBuilder( RepoGroup::class ) |
| 115 |
->disableOriginalConstructor() |
| 116 |
->onlyMethods( [ 'findFile' ] ) |
| 117 |
->getMock(); |
| 118 |
|
| 119 |
$repoGroup->expects( $this->exactly( 2 ) ) |
| 120 |
->method( 'findFile' ) |
| 121 |
->willReturnCallback( function ( $title ) { |
| 122 |
switch ( $title ) { |
| 123 |
case 'File1_free.jpg': |
| 124 |
return $this->getMockLocalFile( |
| 125 |
SearchResultThumbnailProvider::THUMBNAIL_SIZE, |
| 126 |
$title |
| 127 |
); |
| 128 |
case 'File2_free.jpg': |
| 129 |
return null; |
| 130 |
default: |
| 131 |
throw new LogicException( "Unexpected title $title" ); |
| 132 |
} |
| 133 |
} ); |
| 134 |
|
| 135 |
$provider = new SearchResultThumbnailProvider( $repoGroup, $this->createHookContainer() ); |
| 136 |
$handler = new SearchResultProvideThumbnailHookHandler( $provider, $pageProps, $repoGroup ); |
| 137 |
|
| 138 |
$results = [ 1 => null, 2 => null, 3 => null, 4 => null ]; |
| 139 |
$handler->onSearchResultProvideThumbnail( $pageIdentities, $results ); |
| 140 |
|
| 141 |
$this->assertNull( $results[ 2 ] ); |
| 142 |
$this->assertNull( $results[ 3 ] ); |
| 143 |
$this->assertNull( $results[ 4 ] ); |
| 144 |
|
| 145 |
$this->assertNotNull( $results[ 1 ] ); |
| 146 |
$this->assertSame( 'File1_free.jpg', $results[ 1 ]->getName() ); |
| 147 |
$this->assertSame( |
| 148 |
SearchResultThumbnailProvider::THUMBNAIL_SIZE, |
| 149 |
$results[ 1 ]->getWidth() |
| 150 |
); |
| 151 |
$this->assertSame( 'https://example.org/test.url', $results[ 1 ]->getUrl() ); |
| 152 |
} |
| 153 |
} |