diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 5b1b5052ddb..8d1ce9b2ae0 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -58,6 +58,7 @@ class Image implements ImageInterface { */ public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { $this->toolkit = $toolkit; + $this->toolkit->setImage($this); if ($source) { $this->source = $source; $this->parseFile(); @@ -75,14 +76,14 @@ class Image implements ImageInterface { * {@inheritdoc} */ public function getHeight() { - return $this->toolkit->getHeight($this); + return $this->toolkit->getHeight(); } /** * {@inheritdoc} */ public function getWidth() { - return $this->toolkit->getWidth($this); + return $this->toolkit->getWidth(); } /** @@ -96,7 +97,7 @@ class Image implements ImageInterface { * {@inheritdoc} */ public function getMimeType() { - return $this->toolkit->getMimeType($this); + return $this->toolkit->getMimeType(); } /** @@ -130,7 +131,7 @@ class Image implements ImageInterface { } $destination = $destination ?: $this->getSource(); - if ($return = $this->toolkit->save($this, $destination)) { + if ($return = $this->toolkit->save($destination)) { // Clear the cached file size and refresh the image information. clearstatcache(TRUE, $destination); $this->fileSize = filesize($destination); @@ -156,7 +157,7 @@ class Image implements ImageInterface { * image information is populated. */ protected function parseFile() { - if ($this->valid = $this->toolkit->parseFile($this)) { + if ($this->valid = $this->toolkit->parseFile()) { $this->fileSize = filesize($this->source); } return $this->valid; @@ -189,13 +190,12 @@ class Image implements ImageInterface { // removed through https://drupal.org/node/2073759, when // call_user_func_array() will be replaced by // $this->toolkit->apply($name, $this, $arguments). - if (in_array($method, array('setResource', 'getResource', 'hasResource', 'setWidth', 'setHeight', 'getType'))) { + if (in_array($method, array('setResource', 'getResource', 'hasResource', 'setWidth', 'setHeight', 'getType', 'setImage'))) { throw new \BadMethodCallException($method); } if (is_callable(array($this->toolkit, $method))) { // @todo In https://drupal.org/node/2073759, call_user_func_array() will - // be replaced by $this->toolkit->apply($name, $this, $arguments). - array_unshift($arguments, $this); + // be replaced by $this->toolkit->apply($name, $arguments). return call_user_func_array(array($this->toolkit, $method), $arguments); } throw new \BadMethodCallException($method); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index 4f28a6eab1d..85dbe02bb8c 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -7,10 +7,35 @@ namespace Drupal\Core\ImageToolkit; +use Drupal\Core\Image\ImageInterface; use Drupal\Core\Plugin\PluginBase; abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { + /** + * Image object this toolkit instance is tied to. + * + * @var \Drupal\Core\Image\ImageInterface + */ + protected $image; + + /** + * {@inheritdoc} + */ + public function setImage(ImageInterface $image) { + if ($this->image) { + throw new \BadMethodCallException(__METHOD__ . '() may only be called once.'); + } + $this->image = $image; + } + + /** + * {@inheritdoc} + */ + public function getImage() { + return $this->image; + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index b7f4c8c22c1..568bcc8c160 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -46,7 +46,7 @@ use Drupal\Core\Image\ImageInterface; interface ImageToolkitInterface extends PluginInspectionInterface { /** - * Retrieves toolkit's settings form. + * Retrieves the toolkit's settings form. * * @see system_image_toolkit_settings() */ @@ -60,25 +60,40 @@ interface ImageToolkitInterface extends PluginInspectionInterface { public function settingsFormSubmit($form, &$form_state); /** - * Scales an image to the specified size. + * Sets the image object that this toolkit instance is tied to. + * + * @throws \BadMethodCallException + * When called twice. * * @param \Drupal\Core\Image\ImageInterface $image - * An image object. + * The image that this toolkit instance will be tied to. + */ + public function setImage(ImageInterface $image); + + /** + * Gets the image object that this toolkit instance is tied to. + * + * @return \Drupal\Core\Image\ImageInterface + * The image object that this toolkit instance is tied to. + */ + public function getImage(); + + /** + * Scales an image to the specified size. + * * @param int $width * The new width of the resized image, in pixels. * @param int $height * The new height of the resized image, in pixels. * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. */ - public function resize(ImageInterface $image, $width, $height); + public function resize($width, $height); /** * Rotates an image the given number of degrees. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param int $degrees * The number of (clockwise) degrees to rotate the image. * @param string $background @@ -89,15 +104,13 @@ interface ImageToolkitInterface extends PluginInspectionInterface { * be white. * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. */ - public function rotate(ImageInterface $image, $degrees, $background = NULL); + public function rotate($degrees, $background = NULL); /** * Crops an image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param int $x * The starting x offset at which to start the crop, in pixels. * @param int $y @@ -108,46 +121,38 @@ interface ImageToolkitInterface extends PluginInspectionInterface { * The height of the cropped area, in pixels. * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. * * @see image_crop() */ - public function crop(ImageInterface $image, $x, $y, $width, $height); + public function crop($x, $y, $width, $height); /** * Converts an image resource to grayscale. * * Note that transparent GIFs loose transparency when desaturated. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource value will be modified by this - * call. - * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. */ - public function desaturate(ImageInterface $image); + public function desaturate(); /** * Writes an image resource to a destination file. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param string $destination * A string file URI or path where the image should be saved. * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. */ - public function save(ImageInterface $image, $destination); + public function save($destination); /** * Scales an image while maintaining aspect ratio. * * The resulting image can be smaller for one or both target dimensions. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param int $width * (optional) The target width, in pixels. This value is omitted then the * scaling will based only on the height value. @@ -161,7 +166,7 @@ interface ImageToolkitInterface extends PluginInspectionInterface { * @return bool * TRUE on success, FALSE on failure. */ - public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE); + public function scale($width = NULL, $height = NULL, $upscale = FALSE); /** * Scales an image to the exact width and height given. @@ -172,8 +177,6 @@ interface ImageToolkitInterface extends PluginInspectionInterface { * * The resulting image always has the exact target dimensions. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param int $width * The target width, in pixels. * @param int $height @@ -182,52 +185,40 @@ interface ImageToolkitInterface extends PluginInspectionInterface { * @return bool * TRUE on success, FALSE on failure. */ - public function scaleAndCrop(ImageInterface $image, $width, $height); + public function scaleAndCrop($width, $height); /** * Determines if a file contains a valid image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return bool * TRUE if the file could be found and is an image, FALSE otherwise. */ - public function parseFile(ImageInterface $image); + public function parseFile(); /** * Returns the height of the image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return int|null * The height of the image, or NULL if the image is invalid. */ - public function getHeight(ImageInterface $image); + public function getHeight(); /** * Returns the width of the image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return int|null * The width of the image, or NULL if the image is invalid. */ - public function getWidth(ImageInterface $image); + public function getWidth(); /** * Returns the MIME type of the image file. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return string * The MIME type of the image file, or an empty string if the image is * invalid. */ - public function getMimeType(ImageInterface $image); + public function getMimeType(); /** * Gets toolkit requirements in a format suitable for hook_requirements(). @@ -244,10 +235,10 @@ interface ImageToolkitInterface extends PluginInspectionInterface { public function getRequirements(); /** - * Verifies Image Toolkit is set up correctly. + * Verifies that the Image Toolkit is set up correctly. * * @return bool - * True if the GD toolkit is available on this machine. + * TRUE if the toolkit is available on this machine, FALSE otherwise. */ public static function isAvailable(); diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index 8afcb22c543..f30f31ae289 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -54,8 +54,8 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); + $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); + $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); } /** @@ -71,8 +71,8 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); - $this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); + $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); + $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); } /** @@ -89,10 +89,10 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['crop'][0][1], 0, 'X was passed correctly'); - $this->assertEqual($calls['crop'][0][2], 1, 'Y was passed correctly'); - $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); - $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); + $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly'); + $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly'); + $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly'); + $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly'); } /** @@ -107,8 +107,8 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['scaleAndCrop'][0][0], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['scaleAndCrop'][0][1], 10, 'Height was computed and passed correctly'); } /** @@ -120,7 +120,7 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); + $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.'); } /** @@ -136,8 +136,8 @@ class ImageEffectsTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly'); - $this->assertEqual($calls['rotate'][0][2], 0xffffff, 'Background color was passed correctly'); + $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly'); + $this->assertEqual($calls['rotate'][0][1], 0xffffff, 'Background color was passed correctly'); } /** diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 2adc45a60a0..969eaa51389 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit; use Drupal\Component\Utility\Unicode; -use Drupal\Core\Image\ImageInterface; use Drupal\Core\ImageToolkit\ImageToolkitBase; use Drupal\Component\Utility\Image as ImageUtility; @@ -87,7 +86,7 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function resize(ImageInterface $image, $width, $height) { + public function resize($width, $height) { // @todo Dimensions computation will be moved into a dedicated functionality // in https://drupal.org/node/2108307. $width = (int) round($width); @@ -99,7 +98,7 @@ class GDToolkit extends ImageToolkitBase { $res = $this->createTmp($this->getType(), $width, $height); - if (!imagecopyresampled($res, $this->getResource(), 0, 0, 0, 0, $width, $height, $this->getWidth($image), $this->getHeight($image))) { + if (!imagecopyresampled($res, $this->getResource(), 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight())) { return FALSE; } @@ -112,10 +111,10 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function rotate(ImageInterface $image, $degrees, $background = NULL) { + public function rotate($degrees, $background = NULL) { // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { - watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->getSource())); + watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $this->getImage()->getSource())); return FALSE; } @@ -161,10 +160,10 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { + public function crop($x, $y, $width, $height) { // @todo Dimensions computation will be moved into a dedicated functionality // in https://drupal.org/node/2108307. - $aspect = $this->getHeight($image) / $this->getWidth($image); + $aspect = $this->getHeight() / $this->getWidth(); $height = empty($height) ? $width * $aspect : $height; $width = empty($width) ? $height / $aspect : $width; $width = (int) round($width); @@ -189,10 +188,10 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function desaturate(ImageInterface $image) { + public function desaturate() { // PHP installations using non-bundled GD do not have imagefilter. if (!function_exists('imagefilter')) { - watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->getSource())); + watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $this->getImage()->getSource())); return FALSE; } @@ -202,12 +201,12 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { + public function scale($width = NULL, $height = NULL, $upscale = FALSE) { // @todo Dimensions computation will be moved into a dedicated functionality // in https://drupal.org/node/2108307. $dimensions = array( - 'width' => $this->getWidth($image), - 'height' => $this->getHeight($image), + 'width' => $this->getWidth(), + 'height' => $this->getHeight(), ); // Scale the dimensions - if they don't change then just return success. @@ -215,21 +214,21 @@ class GDToolkit extends ImageToolkitBase { return TRUE; } - return $this->resize($image, $dimensions['width'], $dimensions['height']); + return $this->resize($dimensions['width'], $dimensions['height']); } /** * {@inheritdoc} */ - public function scaleAndCrop(ImageInterface $image, $width, $height) { + public function scaleAndCrop($width, $height) { // @todo Dimensions computation will be moved into a dedicated functionality // in https://drupal.org/node/2108307. - $scale = max($width / $this->getWidth($image), $height / $this->getHeight($image)); - $x = ($this->getWidth($image) * $scale - $width) / 2; - $y = ($this->getHeight($image) * $scale - $height) / 2; + $scale = max($width / $this->getWidth(), $height / $this->getHeight()); + $x = ($this->getWidth() * $scale - $width) / 2; + $y = ($this->getHeight() * $scale - $height) / 2; - if ($this->resize($image, $this->getWidth($image) * $scale, $this->getHeight($image) * $scale)) { - return $this->crop($image, $x, $y, $width, $height); + if ($this->resize($this->getWidth() * $scale, $this->getHeight() * $scale)) { + return $this->crop($x, $y, $width, $height); } return FALSE; @@ -238,15 +237,12 @@ class GDToolkit extends ImageToolkitBase { /** * Loads a GD resource from a file. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return bool * TRUE or FALSE, based on success. */ - protected function load($image) { + protected function load() { $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); - if (function_exists($function) && $resource = $function($image->getSource())) { + if (function_exists($function) && $resource = $function($this->getImage()->getSource())) { $this->setResource($resource); if (!imageistruecolor($resource)) { // Convert indexed images to true color, so that filters work @@ -264,7 +260,7 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function save(ImageInterface $image, $destination) { + public function save($destination) { $scheme = file_uri_scheme($destination); // Work around lack of stream wrapper support in imagejpeg() and imagepng(). if ($scheme && file_stream_wrapper_valid_scheme($scheme)) { @@ -303,11 +299,11 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function parseFile(ImageInterface $image) { - $data = @getimagesize($image->getSource()); + public function parseFile() { + $data = @getimagesize($this->getImage()->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); - $this->load($image); + $this->load(); return (bool) $this->getResource(); } return FALSE; @@ -370,14 +366,14 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function getWidth(ImageInterface $image) { + public function getWidth() { return $this->getResource() ? imagesx($this->getResource()) : NULL; } /** * {@inheritdoc} */ - public function getHeight(ImageInterface $image) { + public function getHeight() { return $this->getResource() ? imagesy($this->getResource()) : NULL; } @@ -411,7 +407,7 @@ class GDToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function getMimeType(ImageInterface $image) { + public function getMimeType() { return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; } diff --git a/core/modules/system/src/Tests/Image/ToolkitTest.php b/core/modules/system/src/Tests/Image/ToolkitTest.php index dc60eca11e8..9ec978e59d7 100644 --- a/core/modules/system/src/Tests/Image/ToolkitTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitTest.php @@ -58,8 +58,8 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); + $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); + $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); } /** @@ -72,8 +72,8 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); - $this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); + $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); + $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); } /** @@ -86,8 +86,8 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scaleAndCrop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scaleAndCrop'][0][2], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['scaleAndCrop'][0][0], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['scaleAndCrop'][0][1], 10, 'Height was computed and passed correctly'); } /** @@ -99,8 +99,8 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly'); - $this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly'); + $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly'); + $this->assertEqual($calls['rotate'][0][1], 1, 'Background color was passed correctly'); } /** @@ -112,10 +112,10 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly'); - $this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly'); - $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); - $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); + $this->assertEqual($calls['crop'][0][0], 1, 'X was passed correctly'); + $this->assertEqual($calls['crop'][0][1], 2, 'Y was passed correctly'); + $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly'); + $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly'); } /** @@ -127,6 +127,6 @@ class ToolkitTest extends ToolkitTestBase { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); + $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.'); } } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php index 452087ac07d..ecbe0fef836 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php @@ -8,7 +8,6 @@ namespace Drupal\image_test\Plugin\ImageToolkit; use Drupal\Component\Utility\Unicode; -use Drupal\Core\Image\ImageInterface; use Drupal\Core\ImageToolkit\ImageToolkitBase; /** @@ -70,9 +69,9 @@ class TestToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function parseFile(ImageInterface $image) { - $this->logCall('parseFile', array($image)); - $data = @getimagesize($image->getSource()); + public function parseFile() { + $this->logCall('parseFile', array()); + $data = @getimagesize($this->getImage()->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); $this->width = $data[0]; @@ -85,8 +84,8 @@ class TestToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function save(ImageInterface $image, $destination) { - $this->logCall('save', array($image, $destination)); + public function save($destination) { + $this->logCall('save', array($destination)); // Return false so that image_save() doesn't try to chmod the destination // file that we didn't bother to create. return FALSE; @@ -95,48 +94,48 @@ class TestToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { - $this->logCall('crop', array($image, $x, $y, $width, $height)); + public function crop($x, $y, $width, $height) { + $this->logCall('crop', array($x, $y, $width, $height)); return TRUE; } /** * {@inheritdoc} */ - public function resize(ImageInterface $image, $width, $height) { - $this->logCall('resize', array($image, $width, $height)); + public function resize($width, $height) { + $this->logCall('resize', array($width, $height)); return TRUE; } /** * {@inheritdoc} */ - public function rotate(ImageInterface $image, $degrees, $background = NULL) { - $this->logCall('rotate', array($image, $degrees, $background)); + public function rotate($degrees, $background = NULL) { + $this->logCall('rotate', array($degrees, $background)); return TRUE; } /** * {@inheritdoc} */ - public function desaturate(ImageInterface $image) { - $this->logCall('desaturate', array($image)); + public function desaturate() { + $this->logCall('desaturate', array()); return TRUE; } /** * {@inheritdoc} */ - public function scale(ImageInterface $image, $width = NULL, $height = NULL, $upscale = FALSE) { - $this->logCall('scale', array($image, $width, $height, $upscale)); + public function scale($width = NULL, $height = NULL, $upscale = FALSE) { + $this->logCall('scale', array($width, $height, $upscale)); return TRUE; } /** * {@inheritdoc} */ - public function scaleAndCrop(ImageInterface $image, $width, $height) { - $this->logCall('scaleAndCrop', array($image, $width, $height)); + public function scaleAndCrop($width, $height) { + $this->logCall('scaleAndCrop', array($width, $height)); return TRUE; } @@ -161,14 +160,14 @@ class TestToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function getWidth(ImageInterface $image) { + public function getWidth() { return $this->width; } /** * {@inheritdoc} */ - public function getHeight(ImageInterface $image) { + public function getHeight() { return $this->height; } @@ -202,7 +201,7 @@ class TestToolkit extends ImageToolkitBase { /** * {@inheritdoc} */ - public function getMimeType(ImageInterface $image) { + public function getMimeType() { return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; } diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index e2542ed8e3f..8e1fd65593e 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -15,6 +15,13 @@ use Drupal\Tests\UnitTestCase; */ class ImageTest extends UnitTestCase { + /** + * Path to the image file. + * + * @var string + */ + protected $source; + /** * Image object. * @@ -121,11 +128,12 @@ class ImageTest extends UnitTestCase { */ public function testSave() { // This will fail if save() method isn't called on the toolkit. - $this->toolkit->expects($this->once()) + $toolkit = $this->getToolkitMock(); + $toolkit->expects($this->once()) ->method('save') ->will($this->returnValue(TRUE)); - $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource())); $image->expects($this->any()) ->method('chmod') ->will($this->returnValue(TRUE)); @@ -150,11 +158,12 @@ class ImageTest extends UnitTestCase { */ public function testChmodFails() { // This will fail if save() method isn't called on the toolkit. - $this->toolkit->expects($this->once()) + $toolkit = $this->getToolkitMock(); + $toolkit->expects($this->once()) ->method('save') ->will($this->returnValue(TRUE)); - $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource())); $image->expects($this->any()) ->method('chmod') ->will($this->returnValue(FALSE)); @@ -182,7 +191,7 @@ class ImageTest extends UnitTestCase { $toolkit->expects($this->any()) ->method('resize') - ->will($this->returnArgument(2)); + ->will($this->returnArgument(1)); $height = $image->scale(44); $this->assertEquals($height, 50); } @@ -196,7 +205,7 @@ class ImageTest extends UnitTestCase { $toolkit->expects($this->any()) ->method('resize') - ->will($this->returnArgument(1)); + ->will($this->returnArgument(0)); $width = $image->scale(NULL, 50); $this->assertEquals($width, 44); } @@ -211,7 +220,7 @@ class ImageTest extends UnitTestCase { // Dimensions are the same, resize should not be called. $toolkit->expects($this->never()) ->method('resize') - ->will($this->returnArgument(1)); + ->will($this->returnArgument(0)); $width = $image->scale(88, 100); $this->assertEquals($width, 88); @@ -230,7 +239,7 @@ class ImageTest extends UnitTestCase { $toolkit->expects($this->once()) ->method('crop') - ->will($this->returnArgument(1)); + ->will($this->returnArgument(0)); $x = $image->scaleAndCrop(34, 50); $this->assertEquals($x, 5); @@ -249,7 +258,7 @@ class ImageTest extends UnitTestCase { $toolkit->expects($this->once()) ->method('crop') - ->will($this->returnArgument(2)); + ->will($this->returnArgument(1)); $y = $image->scaleAndCrop(44, 40); $this->assertEquals($y, 5); @@ -300,7 +309,7 @@ class ImageTest extends UnitTestCase { $toolkit->expects($this->once()) ->method('crop') - ->will($this->returnArgument(3)); + ->will($this->returnArgument(2)); $width = $image->crop(0, 0, 44, 50); $this->assertEquals($width, 44); }