diff --git a/core/core.services.yml b/core/core.services.yml index b9069fe..40df0dd 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -605,7 +605,7 @@ services: - { name: event_subscriber } image.toolkit.manager: class: Drupal\Core\ImageToolkit\ImageToolkitManager - arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@config.factory', '@module_handler'] + arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@config.factory', '@module_handler', '@image.toolkit.operation.manager'] image.toolkit.operation.manager: class: Drupal\Core\ImageToolkit\ImageToolkitOperationManager parent: default_plugin_manager diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index 8628bb2..a317705 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -8,11 +8,36 @@ namespace Drupal\Core\ImageToolkit; use Drupal\Core\Image\ImageInterface; +use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface; use Drupal\Core\Plugin\PluginBase; abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { /** + * The image toolkit operation manager. + * + * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface + */ + protected $operationManager; + + /** + * Constructs a Drupal\Component\Plugin\ImageToolkitBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager + * The toolkit operation manager. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->operationManager = $operation_manager; + } + + /** * {@inheritdoc} */ public function getRequirements() { @@ -20,12 +45,24 @@ public function getRequirements() { } /** + * Return a toolkit operation plugin instance. + * + * @param string $operation + * The toolkit operation requested. + * + * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface + * An instance of the requested toolkit operation plugin. + */ + protected function getToolkitOperation($operation) { + $plugin_id = $this->operationManager->getToolkitOperationPluginId($this->getPluginId(), $operation); + return $this->operationManager->createInstance($plugin_id, array(), $this); + } + + /** * @inheritdoc */ public function apply($operation, ImageInterface $image, array $arguments = array()) { - $manager = \Drupal::service('image.toolkit.operation.manager'); - $plugin_id = $manager->getToolkitOperationPluginId($this->getPluginId(), $operation); - $plugin = $manager->createInstance($plugin_id, array(), $this); + $plugin = $this->getToolkitOperation($operation); // Validate and prepare arguments before applying the operation. The // operation might be cancelled if prepare() decides that the final image diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php index 04419af..50db433 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php @@ -10,6 +10,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Component\Plugin\Factory\DefaultFactory; @@ -27,6 +28,13 @@ class ImageToolkitManager extends DefaultPluginManager { protected $configFactory; /** + * The image toolkit operation manager. + * + * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface + */ + protected $operationManager; + + /** * Constructs the ImageToolkitManager object. * * @param \Traversable $namespaces @@ -40,12 +48,15 @@ class ImageToolkitManager extends DefaultPluginManager { * The config factory. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager + * The toolkit operation manager. */ - public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) { + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ImageToolkitOperationManagerInterface $operation_manager) { parent::__construct('Plugin/ImageToolkit', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\Annotation\ImageToolkit'); $this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit_plugins'); $this->configFactory = $config_factory; + $this->operationManager = $operation_manager; } /** @@ -102,7 +113,7 @@ public function getAvailableToolkits() { public function createInstance($plugin_id, array $configuration = array()) { $plugin_definition = $this->getDefinition($plugin_id); $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); - return new $plugin_class($configuration, $plugin_id, $plugin_definition); + return new $plugin_class($configuration, $plugin_id, $plugin_definition, $this->operationManager); } } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php index a4c6fce..81b103b 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -39,9 +39,14 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi } /** - * {@inheritdoc} + * Validates and normalizes arguments. + * + * @param array $arguments + * An associative array of arguments to be used by the toolkit operation. + * + * @throws \InvalidArgumentException. */ - public function prepare(ImageInterface $image, array &$arguments) { + protected function validateArguments(array $arguments) { $manager = \Drupal::typedDataManager(); // Get arguments definition. @@ -87,6 +92,15 @@ public function prepare(ImageInterface $image, array &$arguments) { } } } + + return $arguments; + } + + /** + * {@inheritdoc} + */ + public function prepare(ImageInterface $image, array &$arguments) { + $arguments = $this->validateArguments($arguments); } } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php index 2f2769f..dfd2e45 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManager.php @@ -20,7 +20,7 @@ /** * Manages toolkit operation plugins. */ -class ImageToolkitOperationManager extends DefaultPluginManager { +class ImageToolkitOperationManager extends DefaultPluginManager implements ImageToolkitOperationManagerInterface { /** * Constructs the ImageToolkitOperationManager object. @@ -66,17 +66,7 @@ public function processDefinition(&$definition, $plugin_id) { } /** - * Returns the plugin ID for a given toolkit and operation. - * - * @param string $toolkit - * The toolkit ID. - * @param string $operation - * The operation (e.g. "crop"). - * - * @return string - * The plugin ID. - * - * @throws \Drupal\Component\Plugin\Exception\UnknownPluginException + * {@inheritdoc} */ public function getToolkitOperationPluginId($toolkit, $operation) { $definitions = $this->getDefinitions(); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php new file mode 100644 index 0000000..57f5a14 --- /dev/null +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationManagerInterface.php @@ -0,0 +1,30 @@ +getHeight() / $image->getWidth(); + $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height']; + $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width']; + // Assure integers for all arguments. foreach (array('x', 'y', 'width', 'height') as $key) { $arguments[$key] = (int) round($arguments[$key]); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php index a4ea521..cce2e60 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -46,7 +46,7 @@ public function prepare(ImageInterface $image, array &$arguments) { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) {if ($arguments['width'] == 0) dpm($arguments); + public function apply(ImageInterface $image, array $arguments = array()) { $res = $this->toolkit->createTmp($image, $arguments['width'], $arguments['height']); if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $arguments['width'], $arguments['height'], $image->getWidth(), $image->getHeight())) { diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index ec0c26d..5ff9751 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -30,12 +30,22 @@ class ImageTest extends UnitTestCase { protected $image; /** - * Image toolkit. + * Mocked image toolkit. * * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface */ protected $toolkit; + /** + * Mocked image toolkit operation. + * + * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface + */ + protected $toolkitOperation; + + /** + * @inheritdoc + */ public static function getInfo() { return array( 'name' => 'Image class functionality', @@ -44,9 +54,55 @@ public static function getInfo() { ); } + /** + * @inheritdoc + */ protected function setUp() { // Use the Druplicon image. $this->source = __DIR__ . '/../../../../../misc/druplicon.png'; + } + + /** + * Mocks a toolkit. + * + * @param array $stubs + * (optional) Array containing methods to be replaced with stubs. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + protected function getToolkitMock(array $stubs = array()) { + $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit'); + if ($stubs && is_array($stubs)) { + $mock_builder->setMethods($stubs); + } + return $mock_builder + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Mocks a toolkit operation. + * + * @param string $class_name + * The name of the GD toolkit operation class to be mocked. + * + * @return PHPUnit_Framework_MockObject_MockObject + */ + protected function getToolkitOperationMock($class_name) { + $mock_builder = $this->getMockBuilder('Drupal\\system\\Plugin\\ImageToolkit\\Operation\\gd\\' . $class_name); + return $mock_builder + ->setMethods(array('apply', 'validateArguments')) + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Get an image with a mocked toolkit, for testing. + * + * @param array $stubs + * (optional) Array containing toolkit methods to be replaced with stubs. + */ + protected function getTestImage(array $stubs = array()) { $this->toolkit = $this->getToolkitMock(); $this->toolkit->expects($this->any()) @@ -67,90 +123,116 @@ protected function setUp() { } /** - * Mocks a toolkit. - * - * @param array $stubs - * (optional) Array containing methods to be replaced with stubs. + * Get an image with mocked toolkit and operation, for operation testing. * - * @return PHPUnit_Framework_MockObject_MockObject + * @param string $class_name + * The name of the GD toolkit operation class to be mocked. */ - protected function getToolkitMock(array $stubs = array()) { - $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit'); - if ($stubs && is_array($stubs)) { - $mock_builder->setMethods($stubs); - } - return $mock_builder - ->disableOriginalConstructor() - ->getMock(); + protected function getTestImageForOperation($class_name) { + $this->toolkit = $this->getToolkitMock(array('getToolkitOperation', 'getPluginId', 'getInfo')); + $this->toolkitOperation = $this->getToolkitOperationMock($class_name); + + $this->toolkit->expects($this->any()) + ->method('getPluginId') + ->will($this->returnValue('gd')); + + $this->toolkit->expects($this->any()) + ->method('getInfo') + ->will($this->returnValue(array( + 'width' => 88, + 'height' => 100, + 'extension' => 'png', + 'type' => IMAGETYPE_PNG, + 'mime_type' => 'image/png', + ))); + + $this->toolkit->expects($this->any()) + ->method('getToolkitOperation') + ->will($this->returnValue($this->toolkitOperation)); + + $this->toolkitOperation->expects($this->any()) + ->method('validateArguments') + ->will($this->returnArgument(0)); + + $this->image = new Image($this->source, $this->toolkit); } /** * Tests \Drupal\Core\Image\Image::getExtension(). */ public function testGetExtension() { - $this->assertEquals($this->image->getExtension(), 'png'); + $this->getTestImage(); + $this->assertEquals('png', $this->image->getExtension()); } /** * Tests \Drupal\Core\Image\Image::getHeight(). */ public function testGetHeight() { - $this->assertEquals($this->image->getHeight(), 100); + $this->getTestImage(); + $this->assertEquals(100, $this->image->getHeight()); } /** * Tests \Drupal\Core\Image\Image::setHeight(). */ public function testSetHeight() { + $this->getTestImage(); $this->image->getHeight(); $this->image->setHeight(400); - $this->assertEquals($this->image->getHeight(), 400); + $this->assertEquals(400, $this->image->getHeight()); } /** * Tests \Drupal\Core\Image\Image::getWidth(). */ public function testGetWidth() { - $this->assertEquals($this->image->getWidth(), 88); + $this->getTestImage(); + $this->assertEquals(88, $this->image->getWidth()); } /** * Tests \Drupal\Core\Image\Image::setWidth(). */ public function testSetWidth() { + $this->getTestImage(); $this->image->getHeight(); $this->image->setWidth(337); - $this->assertEquals($this->image->getWidth(), 337); + $this->assertEquals(337, $this->image->getWidth()); } /** * Tests \Drupal\Core\Image\Image::getFileSize */ public function testGetFileSize() { - $this->assertEquals($this->image->getFileSize(), 3905); + $this->getTestImage(); + $this->assertEquals(3905, $this->image->getFileSize()); } /** * Tests \Drupal\Core\Image\Image::getType(). */ public function testGetType() { - $this->assertEquals($this->image->getType(), IMAGETYPE_PNG); + $this->getTestImage(); + $this->assertEquals(IMAGETYPE_PNG, $this->image->getType()); } /** * Tests \Drupal\Core\Image\Image::getMimeType(). */ public function testGetMimeType() { - $this->assertEquals($this->image->getMimeType(), 'image/png'); + $this->getTestImage(); + $this->assertEquals('image/png', $this->image->getMimeType()); } /** * Tests \Drupal\Core\Image\Image::setResource(). */ public function testSetResource() { + $this->getTestImage(); $resource = fopen($this->image->getSource(), 'r'); $this->image->setResource($resource); - $this->assertEquals($this->image->getResource(), $resource); + $this->assertEquals($resource, $this->image->getResource()); // Force \Drupal\Core\Image\Image::hasResource() to return FALSE. $this->image->setResource(FALSE); @@ -161,6 +243,7 @@ public function testSetResource() { * Tests \Drupal\Core\Image\Image::hasResource(). */ public function testHasResource() { + $this->getTestImage(); $this->assertFalse($this->image->hasResource()); $resource = fopen($this->image->getSource(), 'r'); $this->image->setResource($resource); @@ -171,22 +254,25 @@ public function testHasResource() { * Tests \Drupal\Core\Image\Image::setSource(). */ public function testSetSource() { + $this->getTestImage(); $source = __DIR__ . '/../../../../../misc/grippie.png'; $this->image->setSource($source); - $this->assertEquals($this->image->getSource(), $source); + $this->assertEquals($source, $this->image->getSource()); } /** * Tests \Drupal\Core\Image\Image::getToolkitId(). */ public function testGetToolkitId() { - $this->assertEquals($this->image->getToolkitId(), 'gd'); + $this->getTestImage(); + $this->assertEquals('gd', $this->image->getToolkitId()); } /** * Tests \Drupal\Core\Image\Image::save(). */ public function testSave() { + $this->getTestImage(); // This will fail if save() method isn't called on the toolkit. $this->toolkit->expects($this->once()) ->method('save') @@ -204,6 +290,7 @@ public function testSave() { * Tests \Drupal\Core\Image\Image::save(). */ public function testSaveFails() { + $this->getTestImage(); // This will fail if save() method isn't called on the toolkit. $this->toolkit->expects($this->once()) ->method('save') @@ -216,6 +303,7 @@ public function testSaveFails() { * Tests \Drupal\Core\Image\Image::save(). */ public function testChmodFails() { + $this->getTestImage(); // This will fail if save() method isn't called on the toolkit. $this->toolkit->expects($this->once()) ->method('save') @@ -233,6 +321,7 @@ public function testChmodFails() { * Tests \Drupal\Core\Image\Image::save(). */ public function testProcessInfoFails() { + $this->getTestImage(); $this->image->setSource('magic-foobars.png'); $this->assertFalse((bool) $this->image->getWidth()); } @@ -241,182 +330,162 @@ public function testProcessInfoFails() { * Tests \Drupal\Core\Image\Image::scale(). */ public function testScaleWidth() { - $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->any()) - ->method('resize') + $this->getTestImageForOperation('Scale'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); - $ret = $image->apply('scale', array('width' => 44)); - $height = $ret['height']; - $this->assertEquals($height, 50); + + $ret = $this->image->apply('scale', array('width' => 44)); + $this->assertEquals(50, $ret['height']); } /** * Tests \Drupal\Core\Image\Image::scale(). */ public function testScaleHeight() { - $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->any()) - ->method('resize') + $this->getTestImageForOperation('Scale'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); - $ret = $image->apply('scale', array('height' => 50)); - $width = $ret['width']; - $this->assertEquals($width, 44); + + $ret = $this->image->apply('scale', array('height' => 50)); + $this->assertEquals(44, $ret['width']); } /** * Tests \Drupal\Core\Image\Image::scale(). */ public function testScaleSame() { - $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); - + $this->getTestImageForOperation('Scale'); // Dimensions are the same, resize should not be called. - $toolkit->expects($this->never()) - ->method('resize'); + $this->toolkitOperation->expects($this->never()) + ->method('apply'); - $image->apply('scale', array('width' => 88, 'height' => 100)); + $ret = $this->image->apply('scale', array ('width' => 88, 'height' => 100)); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropWidth() { - $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->once()) - ->method('resize') - ->will($this->returnValue(TRUE)); - - $toolkit->expects($this->once()) - ->method('crop') + $this->getTestImageForOperation('ScaleAndCrop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); - $ret = $image->apply('scaleAndCrop', array ('width' => 34, 'height' => 50)); - $x = $ret['x']; - $this->assertEquals($x, 5); + $ret = $this->image->apply('scale_and_crop', array ('width' => 34, 'height' => 50)); + $this->assertEquals(5, $ret['x']); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropHeight() { - $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->once()) - ->method('resize') - ->will($this->returnValue(TRUE)); - - $toolkit->expects($this->once()) - ->method('crop') + $this->getTestImageForOperation('ScaleAndCrop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); - $ret = $image->apply('scaleAndCrop', array ('width' => 44, 'height' => 40)); - $y = $ret['y']; - $this->assertEquals($y, 5); + $ret = $this->image->apply('scale_and_crop', array ('width' => 44, 'height' => 40)); + $this->assertEquals(5, $ret['y']); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropFails() { - $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->any()) - ->method('resize') - ->will($this->returnValue(FALSE)); + $this->getTestImageForOperation('ScaleAndCrop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); - $toolkit->expects($this->never()) - ->method('crop'); - $image->apply('scaleAndCrop', array('width' => 44, 'height' => 40)); + $ret = $this->image->apply('scale_and_crop', array('width' => 44, 'height' => 40)); + $this->assertEquals(0, $ret['x']); + $this->assertEquals(5, $ret['y']); + $this->assertEquals(44, $ret['resize']['width']); + $this->assertEquals(50, $ret['resize']['height']); } /** * Tests \Drupal\Core\Image\Image::crop(). */ public function testCropWidth() { - $toolkit = $this->getToolkitMock(array('getCroppedResource')); - $image = new Image($this->source, $toolkit); + $this->getTestImageForOperation('Crop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); - $toolkit->expects($this->once()) - ->method('getCroppedResource') - ->will($this->returnArgument(4)); // Cropping with width only should preserve the aspect ratio. - $height = $image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44)); - $this->assertEquals($height, 50); + $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44)); + $this->assertEquals(50, $ret['height']); } /** * Tests \Drupal\Core\Image\Image::crop(). */ public function testCropHeight() { - $toolkit = $this->getToolkitMock(array('getCroppedResource')); - $image = new Image($this->source, $toolkit); + $this->getTestImageForOperation('Crop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); - $toolkit->expects($this->once()) - ->method('getCroppedResource') - ->will($this->returnArgument(3)); // Cropping with height only should preserve the aspect ratio. - $width = $image->apply('crop', array ('x' => 0, 'y' => 0, 'height' => 50)); - $this->assertEquals($width, 44); + $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'height' => 50)); + $this->assertEquals(44, $ret['width']); } /** * Tests \Drupal\Core\Image\Image::crop(). */ public function testCrop() { - $toolkit = $this->getToolkitMock(array('getCroppedResource')); - $image = new Image($this->source, $toolkit); + $this->getTestImageForOperation('Crop'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); - $toolkit->expects($this->once()) - ->method('getCroppedResource') - ->will($this->returnArgument(3)); - $width = $image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44, 'height' => 50)); - $this->assertEquals($width, 44); + $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44, 'height' => 50)); + $this->assertEquals(44, $ret['width']); } /** * Tests \Drupal\Core\Image\Image::resize(). */ public function testResize() { - $toolkit = $this->getToolkitMock(array('getResizedResource')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->once()) - ->method('getResizedResource') + $this->getTestImageForOperation('Resize'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); + // Resize with integer for width and height. - $image->apply('resize', array('width' => 30, 'height' => 40)); + $ret = $this->image->apply('resize', array('width' => 30, 'height' => 40)); + $this->assertEquals(30, $ret['width']); + $this->assertEquals(40, $ret['height']); } /** * Tests \Drupal\Core\Image\Image::resize(). */ public function testFloatResize() { - $toolkit = $this->getToolkitMock(array('getResizedResource')); - $image = new Image($this->source, $toolkit); - - $toolkit->expects($this->once()) - ->method('getResizedResource') + $this->getTestImageForOperation('Resize'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') ->will($this->returnArgument(1)); + // Pass a float for width. - $width = $image->apply('resize', array('width' => 30.4, 'height' => 40)); + $ret = $this->image->apply('resize', array('width' => 30.4, 'height' => 40)); // Ensure that the float was rounded to an integer first. - $this->assertEquals($width, 30); + $this->assertEquals(30, $ret['width']); } /** * Tests \Drupal\Core\Image\Image::desaturate(). */ public function testDesaturate() { - $this->toolkit->expects($this->once()) - ->method('desaturate'); + $this->getTestImageForOperation('Desaturate'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); + $this->image->apply('desaturate'); } @@ -424,9 +493,13 @@ public function testDesaturate() { * Tests \Drupal\Core\Image\Image::rotate(). */ public function testRotate() { - $this->toolkit->expects($this->once()) - ->method('rotate'); - $this->image->apply('rotate', array('degrees' => 90)); + $this->getTestImageForOperation('Rotate'); + $this->toolkitOperation->expects($this->once()) + ->method('apply') + ->will($this->returnArgument(1)); + + $ret = $this->image->apply('rotate', array('degrees' => 90)); + $this->assertEquals(90, $ret['degrees']); } }