summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php
blob: bdba6a355e245e419e1f8e3776b23ee022471c4d (plain)
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
<?php

use MediaWiki\MediaWikiServices;

/**
 * @group GlobalFunctions
 * @covers ::wfThumbIsStandard
 */
class WfThumbIsStandardTest extends MediaWikiTestCase {

	protected function setUp() {
		parent::setUp();

		$this->setMwGlobals( [
			'wgThumbLimits' => [
				100,
				401
			],
			'wgImageLimits' => [
				[ 300, 225 ],
				[ 800, 600 ],
			],
		] );
	}

	public static function provideThumbParams() {
		return [
			// Thumb limits
			[
				'Standard thumb width',
				true,
				[ 'width' => 100 ],
			],
			[
				'Standard thumb width',
				true,
				[ 'width' => 401 ],
			],
			// wfThumbIsStandard should match Linker::processResponsiveImages
			// in its rounding behaviour.
			[
				'Standard thumb width (HiDPI 1.5x) - incorrect rounding',
				false,
				[ 'width' => 601 ],
			],
			[
				'Standard thumb width (HiDPI 1.5x)',
				true,
				[ 'width' => 602 ],
			],
			[
				'Standard thumb width (HiDPI 2x)',
				true,
				[ 'width' => 802 ],
			],
			[
				'Non-standard thumb width',
				false,
				[ 'width' => 300 ],
			],
			// Image limits
			// Note: Image limits are measured as pairs. Individual values
			// may be non-standard based on the aspect ratio.
			[
				'Standard image width/height pair',
				true,
				[ 'width' => 250, 'height' => 225 ],
			],
			[
				'Standard image width/height pair',
				true,
				[ 'width' => 667, 'height' => 600 ],
			],
			[
				'Standard image width where image does not fit aspect ratio',
				false,
				[ 'width' => 300 ],
			],
			[
				'Implicit width from image width/height pair aspect ratio fit',
				true,
				// 2000x1800 fit inside 300x225 makes w=250
				[ 'width' => 250 ],
			],
			[
				'Height-only is always non-standard',
				false,
				[ 'height' => 225 ],
			],
		];
	}

	/**
	 * @dataProvider provideThumbParams
	 */
	public function testIsStandard( $message, $expected, $params ) {
		$handlers = MediaWikiServices::getInstance()->getMainConfig()->get( 'ParserTestMediaHandlers' );
		$this->setService( 'MediaHandlerFactory', new MediaHandlerFactory( $handlers ) );
		$this->assertSame(
			$expected,
			wfThumbIsStandard( new FakeDimensionFile( [ 2000, 1800 ], 'image/jpeg' ), $params ),
			$message
		);
	}
}