View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Image;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\Image;
use Drupal\Core\ImageToolkit\ImageToolkitInterface;
use Drupal\Tests\UnitTestCase;
class ImageTest extends UnitTestCase {
protected $source;
protected $image;
protected $toolkit;
protected $toolkitOperation;
protected function setUp() : void {
parent::setUp();
$this->source = __DIR__ . '/../../../../../misc/druplicon.png';
}
protected function getToolkitMock(array $stubs = []) {
$mock_builder = $this
->getMockBuilder('Drupal\\system\\Plugin\\ImageToolkit\\GDToolkit');
$stubs = array_merge([
'getPluginId',
'save',
], $stubs);
return $mock_builder
->disableOriginalConstructor()
->onlyMethods($stubs)
->getMock();
}
protected function getToolkitOperationMock($class_name, ImageToolkitInterface $toolkit) {
$mock_builder = $this
->getMockBuilder('Drupal\\system\\Plugin\\ImageToolkit\\Operation\\gd\\' . $class_name);
$logger = $this
->createMock('Psr\\Log\\LoggerInterface');
return $mock_builder
->onlyMethods([
'execute',
])
->setConstructorArgs([
[],
'',
[],
$toolkit,
$logger,
])
->getMock();
}
protected function getTestImage($load_expected = TRUE, array $stubs = []) {
if (!$load_expected && !in_array('load', $stubs)) {
$stubs = array_merge([
'load',
], $stubs);
}
$this->toolkit = $this
->getToolkitMock($stubs);
$this->toolkit
->expects($this
->any())
->method('getPluginId')
->willReturn('gd');
if (!$load_expected) {
$this->toolkit
->expects($this
->never())
->method('load');
}
$this->image = new Image($this->toolkit, $this->source);
return $this->image;
}
protected function getTestImageForOperation($class_name) {
$this->toolkit = $this
->getToolkitMock([
'getToolkitOperation',
]);
$this->toolkitOperation = $this
->getToolkitOperationMock($class_name, $this->toolkit);
$this->toolkit
->expects($this
->any())
->method('getPluginId')
->willReturn('gd');
$this->toolkit
->expects($this
->any())
->method('getToolkitOperation')
->willReturn($this->toolkitOperation);
$this->image = new Image($this->toolkit, $this->source);
return $this->image;
}
public function testGetHeight() {
$this
->getTestImage(FALSE);
$this
->assertEquals(100, $this->image
->getHeight());
}
public function testGetWidth() {
$this
->getTestImage(FALSE);
$this
->assertEquals(88, $this->image
->getWidth());
}
public function testGetFileSize() {
$this
->getTestImage(FALSE);
$this
->assertEquals(3905, $this->image
->getFileSize());
}
public function testGetType() {
$this
->getTestImage(FALSE);
$this
->assertEquals(IMAGETYPE_PNG, $this->image
->getToolkit()
->getType());
}
public function testGetMimeType() {
$this
->getTestImage(FALSE);
$this
->assertEquals('image/png', $this->image
->getMimeType());
}
public function testIsValid() {
$this
->getTestImage(FALSE);
$this
->assertTrue($this->image
->isValid());
$this
->assertFileIsReadable($this->image
->getSource());
}
public function testGetToolkitId() {
$this
->getTestImage(FALSE);
$this
->assertEquals('gd', $this->image
->getToolkitId());
}
public function testSave() {
$this
->getTestImage();
$toolkit = $this
->getToolkitMock();
$toolkit
->expects($this
->once())
->method('save')
->willReturn(TRUE);
$image = new Image($toolkit, $this->image
->getSource());
$file_system = $this
->prophesize(FileSystemInterface::class);
$file_system
->chmod($this->image
->getSource())
->willReturn(TRUE);
$container = $this
->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')
->onlyMethods([
'get',
])
->getMock();
$container
->expects($this
->once())
->method('get')
->with('file_system')
->willReturn($file_system
->reveal());
\Drupal::setContainer($container);
$image
->save();
}
public function testSaveFails() {
$this
->getTestImage();
$this->toolkit
->expects($this
->once())
->method('save')
->willReturn(FALSE);
$this
->assertFalse($this->image
->save());
}
public function testChmodFails() {
$this
->getTestImage();
$toolkit = $this
->getToolkitMock();
$toolkit
->expects($this
->once())
->method('save')
->willReturn(TRUE);
$image = new Image($toolkit, $this->image
->getSource());
$file_system = $this
->prophesize(FileSystemInterface::class);
$file_system
->chmod($this->image
->getSource())
->willReturn(FALSE);
$container = $this
->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')
->onlyMethods([
'get',
])
->getMock();
$container
->expects($this
->once())
->method('get')
->with('file_system')
->willReturn($file_system
->reveal());
\Drupal::setContainer($container);
$this
->assertFalse($image
->save());
}
public function testParseFileFails() {
$toolkit = $this
->getToolkitMock();
$image = new Image($toolkit, 'magic-foobar.png');
$this
->assertFalse($image
->isValid());
$this
->assertFalse($image
->save());
}
public function testScaleWidth() {
$this
->getTestImageForOperation('Scale');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scale(44, NULL, FALSE);
$this
->assertEquals(50, $ret['height']);
}
public function testScaleHeight() {
$this
->getTestImageForOperation('Scale');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scale(NULL, 50, FALSE);
$this
->assertEquals(44, $ret['width']);
}
public function testScaleSame() {
$this
->getTestImageForOperation('Scale');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scale(88, 100, FALSE);
$this
->assertEquals(88, $ret['width']);
$this
->assertEquals(100, $ret['height']);
}
public function testScaleAndCropWidth() {
$this
->getTestImageForOperation('ScaleAndCrop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scaleAndCrop(34, 50);
$this
->assertEquals(5, $ret['x']);
}
public function testScaleAndCropHeight() {
$this
->getTestImageForOperation('ScaleAndCrop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scaleAndCrop(44, 40);
$this
->assertEquals(5, $ret['y']);
}
public function testScaleAndCropFails() {
$this
->getTestImageForOperation('ScaleAndCrop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->scaleAndCrop(44, 40);
$this
->assertEquals(0, $ret['x']);
$this
->assertEquals(5, $ret['y']);
$this
->assertEquals(44, $ret['resize']['width']);
$this
->assertEquals(50, $ret['resize']['height']);
}
public function testCropWidth() {
$this
->getTestImageForOperation('Crop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->crop(0, 0, 44);
$this
->assertEquals(50, $ret['height']);
}
public function testCropHeight() {
$this
->getTestImageForOperation('Crop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->crop(0, 0, NULL, 50);
$this
->assertEquals(44, $ret['width']);
}
public function testCrop() {
$this
->getTestImageForOperation('Crop');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->crop(0, 0, 44, 50);
$this
->assertEquals(44, $ret['width']);
}
public function testConvert() {
$this
->getTestImageForOperation('Convert');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->convert('png');
$this
->assertEquals('png', $ret['extension']);
}
public function testResize() {
$this
->getTestImageForOperation('Resize');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->resize(30, 40);
$this
->assertEquals(30, $ret['width']);
$this
->assertEquals(40, $ret['height']);
}
public function testFloatResize() {
$this
->getTestImageForOperation('Resize');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->resize(30.4, 40);
$this
->assertEquals(30, $ret['width']);
}
public function testDesaturate() {
$this
->getTestImageForOperation('Desaturate');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$this->image
->desaturate();
}
public function testRotate() {
$this
->getTestImageForOperation('Rotate');
$this->toolkitOperation
->expects($this
->once())
->method('execute')
->willReturnArgument(0);
$ret = $this->image
->rotate(90);
$this
->assertEquals(90, $ret['degrees']);
}
}