| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Search\SearchResultThumbnailProvider; |
| 4 |
use MediaWiki\Tests\Rest\Handler\MediaTestTrait; |
| 5 |
use MediaWiki\Title\Title; |
| 6 |
|
| 7 |
class SearchResultThumbnailProviderTest extends MediaWikiIntegrationTestCase { |
| 8 |
use MediaTestTrait; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
protected const TITLES = [ |
| 14 |
'file' => [ |
| 15 |
'id' => 1, |
| 16 |
'text' => 'File_1.jpg', |
| 17 |
'namespace' => NS_FILE, |
| 18 |
], |
| 19 |
'article_with_thumb' => [ |
| 20 |
'id' => 2, |
| 21 |
'text' => 'Title_2', |
| 22 |
'namespace' => NS_MAIN, |
| 23 |
], |
| 24 |
'article_without_thumb' => [ |
| 25 |
'id' => 3, |
| 26 |
'text' => 'Title_3', |
| 27 |
'namespace' => NS_MAIN, |
| 28 |
], |
| 29 |
]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected const HOOK_PROVIDED_THUMBNAILS_BY_ID = [ |
| 36 |
self::TITLES['article_with_thumb']['id'] => self::TITLES['file']['id'], |
| 37 |
]; |
| 38 |
|
| 39 |
|
| 40 |
private $thumbnailProvider; |
| 41 |
|
| 42 |
|
| 43 |
private $titles = []; |
| 44 |
|
| 45 |
public static function articleThumbnailsProvider(): array { |
| 46 |
return [ |
| 47 |
|
| 48 |
[ |
| 49 |
|
| 50 |
[ self::TITLES['file']['id'] ], |
| 51 |
|
| 52 |
[ self::TITLES['file']['id'] ], |
| 53 |
|
| 54 |
500 |
| 55 |
], |
| 56 |
|
| 57 |
[ |
| 58 |
[ self::TITLES['article_with_thumb']['id'] ], |
| 59 |
[ self::TITLES['file']['id'] ], |
| 60 |
500 |
| 61 |
], |
| 62 |
|
| 63 |
[ |
| 64 |
[ self::TITLES['article_without_thumb']['id'] ], |
| 65 |
[], |
| 66 |
500 |
| 67 |
], |
| 68 |
|
| 69 |
[ |
| 70 |
[ self::TITLES['file']['id'] ], |
| 71 |
[ self::TITLES['file']['id'] ], |
| 72 |
null |
| 73 |
] |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
public function setUp(): void { |
| 78 |
parent::setUp(); |
| 79 |
|
| 80 |
|
| 81 |
$this->titles = []; |
| 82 |
foreach ( self::TITLES as $data ) { |
| 83 |
$this->titles[$data['id']] = $this->makeMockTitle( |
| 84 |
$data['text'], |
| 85 |
[ 'id' => $data['id'], 'namespace' => $data['namespace'] ] |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$thumbnails = array_map( |
| 91 |
static fn ( Title $title ) => $title->getDBkey(), |
| 92 |
array_filter( |
| 93 |
$this->titles, |
| 94 |
static fn ( Title $title ) => $title->inNamespace( NS_FILE ) |
| 95 |
) |
| 96 |
); |
| 97 |
$mockRepoGroup = $this->makeMockRepoGroup( $thumbnails ); |
| 98 |
|
| 99 |
|
| 100 |
$hookContainer = $this->createHookContainer( [ |
| 101 |
'SearchResultProvideThumbnail' => function ( $pageIdentities, &$results, $size ) use ( $mockRepoGroup ) { |
| 102 |
foreach ( self::HOOK_PROVIDED_THUMBNAILS_BY_ID as $pageId => $thumbnailPageId ) { |
| 103 |
if ( !isset( $pageIdentities[$pageId] ) ) { |
| 104 |
|
| 105 |
continue; |
| 106 |
} |
| 107 |
$articleTitle = $this->titles[$pageId]; |
| 108 |
if ( !$articleTitle ) { |
| 109 |
throw new InvalidArgumentException( |
| 110 |
'self::HOOK_PROVIDED_THUMBNAILS_BY_ID key references a page missing from in self::TITLES' |
| 111 |
); |
| 112 |
} |
| 113 |
$thumbnailTitle = $this->titles[$thumbnailPageId]; |
| 114 |
if ( !$thumbnailTitle ) { |
| 115 |
throw new InvalidArgumentException( |
| 116 |
'self::HOOK_PROVIDED_THUMBNAILS_BY_ID value references a page missing from in self::TITLES' |
| 117 |
); |
| 118 |
} |
| 119 |
$results[$pageId] = $this->thumbnailProvider->buildSearchResultThumbnailFromFile( |
| 120 |
$mockRepoGroup->findFile( $thumbnailTitle ), |
| 121 |
$size |
| 122 |
); |
| 123 |
} |
| 124 |
}, |
| 125 |
] ); |
| 126 |
$this->thumbnailProvider = new SearchResultThumbnailProvider( $mockRepoGroup, $hookContainer ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function testGetThumbnails( array $pageIds, array $thumbnailIds, ?int $size = null ) { |
| 138 |
$pageIdentities = array_intersect_key( $this->titles, array_fill_keys( $pageIds, null ) ); |
| 139 |
|
| 140 |
$thumbnails = $this->thumbnailProvider->getThumbnails( $pageIdentities, $size ); |
| 141 |
|
| 142 |
|
| 143 |
$missingThumbnails = array_diff_key( $pageIdentities, $thumbnails ); |
| 144 |
foreach ( $missingThumbnails as $pageId => $pageIdentity ) { |
| 145 |
$this->assertArrayNotHasKey( $pageId, $thumbnails ); |
| 146 |
} |
| 147 |
|
| 148 |
foreach ( $thumbnails as $pageId => $thumbnail ) { |
| 149 |
|
| 150 |
$expectedName = $this->titles[$pageId]->inNamespace( NS_FILE ) |
| 151 |
? $this->titles[$pageId]->getDBkey() |
| 152 |
: $this->titles[self::HOOK_PROVIDED_THUMBNAILS_BY_ID[$pageId]]->getDBkey(); |
| 153 |
$this->assertEquals( $expectedName, $thumbnail->getName() ); |
| 154 |
|
| 155 |
|
| 156 |
$expectedSize = $size ?? SearchResultThumbnailProvider::THUMBNAIL_SIZE; |
| 157 |
$this->assertLessThanOrEqual( $expectedSize, $thumbnail->getWidth() ); |
| 158 |
} |
| 159 |
} |
| 160 |
} |