File: SearchResultThumbnailProviderTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (160 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

use MediaWiki\Search\SearchResultThumbnailProvider;
use MediaWiki\Tests\Rest\Handler\MediaTestTrait;
use MediaWiki\Title\Title;

class SearchResultThumbnailProviderTest extends MediaWikiIntegrationTestCase {
	use MediaTestTrait;

	/**
	 * List of titles to create.
	 */
	protected const TITLES = [
		'file' => [
			'id' => 1,
			'text' => 'File_1.jpg',
			'namespace' => NS_FILE,
		],
		'article_with_thumb' => [
			'id' => 2,
			'text' => 'Title_2',
			'namespace' => NS_MAIN,
		],
		'article_without_thumb' => [
			'id' => 3,
			'text' => 'Title_3',
			'namespace' => NS_MAIN,
		],
	];

	/**
	 * Map of page id to thumbnail page id, both of which are expected to be present in self::TITLES.
	 * This map will be used to build a mock response for the SearchResultProvideThumbnail hook.
	 */
	protected const HOOK_PROVIDED_THUMBNAILS_BY_ID = [
		self::TITLES['article_with_thumb']['id'] => self::TITLES['file']['id'],
	];

	/** @var SearchResultThumbnailProvider */
	private $thumbnailProvider;

	/** @var Title[] */
	private $titles = [];

	public static function articleThumbnailsProvider(): array {
		return [
			// assert that NS_FILE pages provide their own file
			[
				// page ids
				[ self::TITLES['file']['id'] ],
				// thumbnail ids
				[ self::TITLES['file']['id'] ],
				// size
				500
			],
			// assert that hook provides thumbnails for non-file pages
			[
				[ self::TITLES['article_with_thumb']['id'] ],
				[ self::TITLES['file']['id'] ],
				500
			],
			// assert thumbnail is missing when not NS_FILE & hook doesn't provide
			[
				[ self::TITLES['article_without_thumb']['id'] ],
				[],
				500
			],
			// assert that size is optional and defaults to something functional
			[
				[ self::TITLES['file']['id'] ],
				[ self::TITLES['file']['id'] ],
				null
			]
		];
	}

	public function setUp(): void {
		parent::setUp();

		// build mock titles based on descriptions in self::TITLES
		$this->titles = [];
		foreach ( self::TITLES as $data ) {
			$this->titles[$data['id']] = $this->makeMockTitle(
				$data['text'],
				[ 'id' => $data['id'], 'namespace' => $data['namespace'] ]
			);
		}

		// compile a mock repo with all NS_FILE pages known in self::TITLES
		$thumbnails = array_map(
			static fn ( Title $title ) => $title->getDBkey(),
			array_filter(
				$this->titles,
				static fn ( Title $title ) => $title->inNamespace( NS_FILE )
			)
		);
		$mockRepoGroup = $this->makeMockRepoGroup( $thumbnails );

		// create a hook that provides all thumbnails defined in HOOK_PROVIDED_THUMBNAILS_BY_ID
		$hookContainer = $this->createHookContainer( [
			'SearchResultProvideThumbnail' => function ( $pageIdentities, &$results, $size ) use ( $mockRepoGroup ) {
				foreach ( self::HOOK_PROVIDED_THUMBNAILS_BY_ID as $pageId => $thumbnailPageId ) {
					if ( !isset( $pageIdentities[$pageId] ) ) {
						// skip this thumbnail, it was not requested
						continue;
					}
					$articleTitle = $this->titles[$pageId];
					if ( !$articleTitle ) {
						throw new InvalidArgumentException(
							'self::HOOK_PROVIDED_THUMBNAILS_BY_ID key references a page missing from in self::TITLES'
						);
					}
					$thumbnailTitle = $this->titles[$thumbnailPageId];
					if ( !$thumbnailTitle ) {
						throw new InvalidArgumentException(
							'self::HOOK_PROVIDED_THUMBNAILS_BY_ID value references a page missing from in self::TITLES'
						);
					}
					$results[$pageId] = $this->thumbnailProvider->buildSearchResultThumbnailFromFile(
						$mockRepoGroup->findFile( $thumbnailTitle ),
						$size
					);
				}
			},
		] );
		$this->thumbnailProvider = new SearchResultThumbnailProvider( $mockRepoGroup, $hookContainer );
	}

	/**
	 * @dataProvider articleThumbnailsProvider
	 * @covers \MediaWiki\Search\SearchResultThumbnailProvider::getThumbnails
	 * @covers \MediaWiki\Search\SearchResultThumbnailProvider::buildSearchResultThumbnailFromFile
	 * @param int[] $pageIds
	 * @param int[] $thumbnailIds
	 * @param int|null $size
	 */
	public function testGetThumbnails( array $pageIds, array $thumbnailIds, ?int $size = null ) {
		$pageIdentities = array_intersect_key( $this->titles, array_fill_keys( $pageIds, null ) );

		$thumbnails = $this->thumbnailProvider->getThumbnails( $pageIdentities, $size );

		// confirm that titles for which there is no thumbnail are missing
		$missingThumbnails = array_diff_key( $pageIdentities, $thumbnails );
		foreach ( $missingThumbnails as $pageId => $pageIdentity ) {
			$this->assertArrayNotHasKey( $pageId, $thumbnails );
		}

		foreach ( $thumbnails as $pageId => $thumbnail ) {
			// confirm thumbnail matches what we expect
			$expectedName = $this->titles[$pageId]->inNamespace( NS_FILE )
				? $this->titles[$pageId]->getDBkey()
				: $this->titles[self::HOOK_PROVIDED_THUMBNAILS_BY_ID[$pageId]]->getDBkey();
			$this->assertEquals( $expectedName, $thumbnail->getName() );

			// confirm thumbnail dimensions
			$expectedSize = $size ?? SearchResultThumbnailProvider::THUMBNAIL_SIZE;
			$this->assertLessThanOrEqual( $expectedSize, $thumbnail->getWidth() );
		}
	}
}