Store original media dimensions as additional header
[lhc/web/wiklou.git] / tests / phpunit / includes / media / MediaHandlerTest.php
1 <?php
2
3 /**
4 * @group Media
5 */
6 class MediaHandlerTest extends MediaWikiTestCase {
7
8 /**
9 * @covers MediaHandler::fitBoxWidth
10 *
11 * @dataProvider provideTestFitBoxWidth
12 */
13 public function testFitBoxWidth( $width, $height, $max, $expected ) {
14 $y = round( $expected * $height / $width );
15 $result = MediaHandler::fitBoxWidth( $width, $height, $max );
16 $y2 = round( $result * $height / $width );
17 $this->assertEquals( $expected,
18 $result,
19 "($width, $height, $max) wanted: {$expected}x$y, got: {z$result}x$y2" );
20 }
21
22 public static function provideTestFitBoxWidth() {
23 return array_merge(
24 static::generateTestFitBoxWidthData( 50, 50, [
25 50 => 50,
26 17 => 17,
27 18 => 18 ]
28 ),
29 static::generateTestFitBoxWidthData( 366, 300, [
30 50 => 61,
31 17 => 21,
32 18 => 22 ]
33 ),
34 static::generateTestFitBoxWidthData( 300, 366, [
35 50 => 41,
36 17 => 14,
37 18 => 15 ]
38 ),
39 static::generateTestFitBoxWidthData( 100, 400, [
40 50 => 12,
41 17 => 4,
42 18 => 4 ]
43 )
44 );
45 }
46
47 /**
48 * Generate single test cases by combining the dimensions and tests contents
49 *
50 * It creates:
51 * [$width, $height, $max, $expected],
52 * [$width, $height, $max2, $expected2], ...
53 * out of parameters:
54 * $width, $height, { $max => $expected, $max2 => $expected2, ... }
55 *
56 * @param int $width
57 * @param int $height
58 * @param array $tests associative array of $max => $expected values
59 * @return array
60 */
61 private static function generateTestFitBoxWidthData( $width, $height, $tests ) {
62 $result = [];
63 foreach ( $tests as $max => $expected ) {
64 $result[] = [ $width, $height, $max, $expected ];
65 }
66 return $result;
67 }
68
69 /**
70 * @covers MediaHandler::getPageRangesByDimensions
71 *
72 * @dataProvider provideTestGetPageRangesByDimensions
73 */
74 public function testGetPageRangesByDimensions( $pagesByDimensions, $expected ) {
75 $this->assertEquals( $expected, MediaHandler::getPageRangesByDimensions( $pagesByDimensions ) );
76 }
77
78 public static function provideTestGetPageRangesByDimensions() {
79 return [
80 [ [ '123x456' => [ 1 ] ], '123x456:1' ],
81 [ [ '123x456' => [ 1, 2 ] ], '123x456:1-2' ],
82 [ [ '123x456' => [ 1, 2, 3 ] ], '123x456:1-3' ],
83 [ [ '123x456' => [ 1, 2, 3, 5 ] ], '123x456:1-3,5' ],
84 [ [ '123x456' => [ 1, 3 ] ], '123x456:1,3' ],
85 [ [ '123x456' => [ 1, 2, 3, 5, 6, 7 ] ], '123x456:1-3,5-7' ],
86 [ [ '123x456' => [ 1, 2, 3, 5, 6, 7 ],
87 '789x789' => [ 4, 8, 9 ] ], '123x456:1-3,5-7/789x789:4,8-9'
88 ],
89 ];
90 }
91 }