PortableInfobox/tests/phpunit/helpers/PortableInfoboxImagesHelperTest.php

193 lines
5.3 KiB
PHP
Raw Normal View History

<?php
use PortableInfobox\Helpers\PortableInfoboxImagesHelper;
2018-08-08 09:42:22 +00:00
/**
* @group PortableInfobox
* @covers PortableInfobox\Helpers\PortableInfoboxImagesHelper
2018-08-08 09:42:22 +00:00
*/
class PortableInfoboxImagesHelperTest extends MediaWikiTestCase {
private $helper;
2021-09-11 23:07:07 +00:00
protected function setUp(): void {
parent::setUp();
2016-12-29 10:58:27 +00:00
$this->helper = new PortableInfoboxImagesHelper();
}
2021-09-11 23:07:07 +00:00
protected function tearDown(): void {
2018-08-08 09:42:22 +00:00
unset( $this->helper );
parent::tearDown();
}
2015-09-09 12:54:08 +00:00
/**
* @param $width
* @param $max
* @param $imageWidth
* @param $imageHeight
* @param $expected
* @dataProvider thumbnailSizesDataProvider
2015-09-09 12:54:08 +00:00
*/
public function testGetThumbnailSizes( $width, $max, $imageWidth, $imageHeight, $expected ) {
2016-12-29 10:58:27 +00:00
$helper = new PortableInfoboxImagesHelper();
$result = $helper->getThumbnailSizes( $width, $max, $imageWidth, $imageHeight );
2015-12-11 17:56:49 +00:00
$this->assertEquals( $expected, $result );
2015-12-11 17:56:49 +00:00
}
public function thumbnailSizesDataProvider() {
2015-12-11 17:56:49 +00:00
return [
[
'preferredWidth' => 270,
'maxHeight' => 500,
'originalWidth' => 270,
'originalHeight' => 250,
'expected' => [ 'width' => 270, 'height' => 250 ]
2015-12-11 17:56:49 +00:00
],
[
'preferredWidth' => 300,
'maxHeight' => 500,
'originalWidth' => 350,
'originalHeight' => 250,
'expected' => [ 'width' => 300, 'height' => 214 ]
2015-12-11 17:56:49 +00:00
],
[
'preferredWidth' => 300,
'maxHeight' => 500,
'originalWidth' => 300,
'originalHeight' => 550,
'expected' => [ 'width' => 273, 'height' => 500 ]
2015-12-11 17:56:49 +00:00
],
[
'preferredWidth' => 200,
'maxHeight' => 500,
'originalWidth' => 300,
'originalHeight' => 400,
'expected' => [ 'width' => 200, 'height' => 267 ]
2015-12-11 17:56:49 +00:00
],
[
'preferredWidth' => 270,
'maxHeight' => 500,
'originalWidth' => 100,
'originalHeight' => 300,
'expected' => [ 'width' => 100, 'height' => 300 ]
2015-12-11 17:56:49 +00:00
],
[
'preferredWidth' => 270,
'maxHeight' => 500,
'originalWidth' => 800,
'originalHeight' => 600,
'expected' => [ 'width' => 270, 'height' => 203 ]
],
2015-12-11 17:56:49 +00:00
];
}
2016-03-17 11:12:36 +00:00
/**
* @param $customWidth
* @param $preferredWidth
* @param $resultDimensions
* @param $thumbnailDimensions
* @param $thumbnail2xDimensions
2016-03-17 11:12:36 +00:00
* @param $originalDimension
* @dataProvider customWidthProvider
*/
2018-10-02 07:41:19 +00:00
public function testCustomWidthLogic(
$customWidth, $preferredWidth, $resultDimensions, $thumbnailDimensions, $thumbnail2xDimensions,
$originalDimension
) {
2016-03-17 11:12:36 +00:00
$expected = [
'thumbnail' => null,
'thumbnail2x' => null,
2018-08-16 09:25:53 +00:00
'width' => $resultDimensions['width'],
2018-08-22 14:22:30 +00:00
'height' => $resultDimensions['height']
2016-03-17 11:12:36 +00:00
];
$thumb = $this->getMockBuilder( 'ThumbnailImage' )
->disableOriginalConstructor()
->setMethods( [ 'isError', 'getUrl' ] )
->getMock();
$file = $this->getMockBuilder( 'File' )
->disableOriginalConstructor()
->setMethods( [ 'exists', 'transform', 'getWidth', 'getHeight', 'getMediaType' ] )
2016-03-17 11:12:36 +00:00
->getMock();
2018-10-02 07:41:19 +00:00
$file->expects( $this->once() )
->method( 'exists' )
->willReturn( true );
$file->expects( $this->once() )
->method( 'getWidth' )
->willReturn( $originalDimension['width'] );
$file->expects( $this->once() )
->method( 'getHeight' )
->willReturn( $originalDimension['height'] );
$file->expects( $this->once() )
->method( 'getMediaType' )
->willReturn( MEDIATYPE_BITMAP );
2016-03-17 11:12:36 +00:00
$file->expects( $this->any() )
->method( 'transform' )
2018-10-02 07:41:19 +00:00
->with( $this->logicalOr(
$this->equalTo( $thumbnailDimensions ),
$this->equalTo( $thumbnail2xDimensions )
) )
->willReturn( $thumb );
2018-08-16 09:25:53 +00:00
2018-08-08 09:42:22 +00:00
global $wgPortableInfoboxCustomImageWidth;
$wgPortableInfoboxCustomImageWidth = $customWidth;
2016-03-17 11:12:36 +00:00
2018-08-22 14:22:30 +00:00
$result = $this->helper->extendImageData( $file, $preferredWidth );
2016-03-17 11:12:36 +00:00
$this->assertEquals( $expected, $result );
}
public function customWidthProvider() {
return [
[
'custom' => false,
'preferred' => 300,
'result' => [ 'width' => 300, 'height' => 200 ],
'thumbnail' => [ 'width' => 300, 'height' => 200 ],
'thumbnail2x' => [ 'width' => 600, 'height' => 400 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 300, 'height' => 200 ]
],
[
'custom' => 400,
'preferred' => 300,
'result' => [ 'width' => 300, 'height' => 200 ],
'thumbnail' => [ 'width' => 300, 'height' => 200 ],
'thumbnail2x' => [ 'width' => 600, 'height' => 400 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 300, 'height' => 200 ]
],
[
'custom' => 400,
'preferred' => 300,
'result' => [ 'width' => 300, 'height' => 180 ],
'thumbnail' => [ 'width' => 400, 'height' => 240 ],
'thumbnail2x' => [ 'width' => 800, 'height' => 480 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 500, 'height' => 300 ]
],
[
'custom' => 600,
'preferred' => 300,
'result' => [ 'width' => 300, 'height' => 500 ],
'thumbnail' => [ 'width' => 300, 'height' => 500 ],
'thumbnail2x' => [ 'width' => 600, 'height' => 1000 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 300, 'height' => 500 ]
],
[
'custom' => 600,
'preferred' => 300,
'result' => [ 'width' => 188, 'height' => 500 ],
'thumbnail' => [ 'width' => 188, 'height' => 500 ],
'thumbnail2x' => [ 'width' => 376, 'height' => 1000 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 300, 'height' => 800 ]
],
[
'custom' => 600,
'preferred' => 300,
'result' => [ 'width' => 300, 'height' => 375 ],
'thumbnail' => [ 'width' => 600, 'height' => 750 ],
'thumbnail2x' => [ 'width' => 1200, 'height' => 1500 ],
2016-03-17 11:12:36 +00:00
'original' => [ 'width' => 1200, 'height' => 1500 ]
],
];
}
}