diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 30b8a39..4d0a167 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Image; use Drupal\system\Plugin\ImageToolkitInterface; -use \Drupal\Component\Utility\Image as ImageUtility; +use Drupal\Component\Utility\Image as ImageUtility; /** * Defines an image object to represent an image file. diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php index 611e19c..df63bb6 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -24,7 +24,7 @@ class GDToolkit extends PluginBase implements ImageToolkitInterface { /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm() { $form['image_jpeg_quality'] = array( @@ -40,7 +40,7 @@ public function settingsForm() { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsFormSubmit(). + * {@inheritdoc} */ public function settingsFormSubmit($form, &$form_state) { config('system.image.gd') @@ -49,7 +49,7 @@ public function settingsFormSubmit($form, &$form_state) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::resize(). + * {@inheritdoc} */ public function resize(ImageInterface $image, $width, $height) { $res = $this->createTmp($image, $width, $height); @@ -68,7 +68,7 @@ public function resize(ImageInterface $image, $width, $height) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::rotate(). + * {@inheritdoc} */ public function rotate(ImageInterface $image, $degrees, $background = NULL) { // PHP installations using non-bundled GD do not have imagerotate. @@ -123,7 +123,7 @@ public function rotate(ImageInterface $image, $degrees, $background = NULL) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::crop(). + * {@inheritdoc} */ public function crop(ImageInterface $image, $x, $y, $width, $height) { $res = $this->createTmp($image, $width, $height); @@ -142,7 +142,7 @@ public function crop(ImageInterface $image, $x, $y, $width, $height) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::desaturate(). + * {@inheritdoc} */ public function desaturate(ImageInterface $image) { // PHP installations using non-bundled GD do not have imagefilter. @@ -155,7 +155,7 @@ public function desaturate(ImageInterface $image) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::load(). + * {@inheritdoc} */ public function load(ImageInterface $image) { $extension = str_replace('jpg', 'jpeg', $image->getExtension()); @@ -177,7 +177,7 @@ public function load(ImageInterface $image) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::save(). + * {@inheritdoc} */ public function save(ImageInterface $image, $destination) { $scheme = file_uri_scheme($destination); @@ -217,7 +217,7 @@ public function save(ImageInterface $image, $destination) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::getInfo(). + * {@inheritdoc} */ public function getInfo(ImageInterface $image) { $details = FALSE; @@ -282,7 +282,7 @@ public function createTmp(ImageInterface $image, $width, $height) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::isAvailable(). + * {@inheritdoc} */ public static function isAvailable() { if ($check = get_extension_funcs('gd')) { diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php index 64aad2d..7f1492f 100644 --- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php @@ -24,7 +24,7 @@ class TestToolkit extends PluginBase implements ImageToolkitInterface { /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsForm(). + * {@inheritdoc} */ public function settingsForm() { $this->logCall('settings', array()); @@ -32,12 +32,12 @@ public function settingsForm() { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::settingsFormSubmit(). + * {@inheritdoc} */ public function settingsFormSubmit($form, &$form_state) {} /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::getInfo(). + * {@inheritdoc} */ public function getInfo(ImageInterface $image) { $this->logCall('get_info', array($image)); @@ -60,7 +60,7 @@ public function getInfo(ImageInterface $image) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::load(). + * {@inheritdoc} */ public function load(ImageInterface $image) { $this->logCall('load', array($image)); @@ -68,7 +68,7 @@ public function load(ImageInterface $image) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::save(). + * {@inheritdoc} */ public function save(ImageInterface $image, $destination) { $this->logCall('save', array($image, $destination)); @@ -78,7 +78,7 @@ public function save(ImageInterface $image, $destination) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::crop(). + * {@inheritdoc} */ public function crop(ImageInterface $image, $x, $y, $width, $height) { $this->logCall('crop', array($image, $x, $y, $width, $height)); @@ -86,7 +86,7 @@ public function crop(ImageInterface $image, $x, $y, $width, $height) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::resize(). + * {@inheritdoc} */ public function resize(ImageInterface $image, $width, $height) { $this->logCall('resize', array($image, $width, $height)); @@ -94,7 +94,7 @@ public function resize(ImageInterface $image, $width, $height) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::rotate(). + * {@inheritdoc} */ public function rotate(ImageInterface $image, $degrees, $background = NULL) { $this->logCall('rotate', array($image, $degrees, $background)); @@ -102,7 +102,7 @@ public function rotate(ImageInterface $image, $degrees, $background = NULL) { } /** - * Implements \Drupal\system\Plugin\ImageToolkitInterface::desaturate(). + * {@inheritdoc} */ public function desaturate(ImageInterface $image) { $this->logCall('desaturate', array($image)); @@ -128,7 +128,7 @@ protected function logCall($op, $args) { } /** - * Implements Drupal\system\Plugin\ImageToolkitInterface::isAvailable(). + * {@inheritdoc} */ public static function isAvailable() { return TRUE; diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 727490d..92e4804 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -120,6 +120,8 @@ public function testSetResource() { $resource = fopen($this->image->getSource(), 'r'); $this->image->setResource($resource); $this->assertEquals($this->image->getResource(), $resource); + $this->image->setResource(FALSE); + $this->assertNotNull($this->image->getResource()); } /** @@ -152,14 +154,12 @@ public function testGetToolkitId() { * Tests Drupal\Core\Image\Image::save(). */ public function testSave() { - $toolkit = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit') - ->disableOriginalConstructor() - ->getMock(); // This will fail if save() method isn't called on the toolkit. - $toolkit->expects($this->once()) - ->method('save'); + $this->toolkit->expects($this->once()) + ->method('save') + ->will($this->returnValue(TRUE)); - $image = $this->getMock('Drupal\Core\Image\Image', NULL, array($this->image->getSource(), $toolkit)); + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->image->getSource(), $this->toolkit)); $image->expects($this->any()) ->method('chmod') ->will($this->returnValue(TRUE)); @@ -168,6 +168,43 @@ public function testSave() { } /** + * Tests Drupal\Core\Image\Image::save(). + */ + public function testSaveFails() { + // This will fail if save() method isn't called on the toolkit. + $this->toolkit->expects($this->once()) + ->method('save') + ->will($this->returnValue(FALSE)); + + $this->assertFalse($this->image->save()); + } + + /** + * Tests Drupal\Core\Image\Image::save(). + */ + public function testChmodFails() { + // This will fail if save() method isn't called on the toolkit. + $this->toolkit->expects($this->once()) + ->method('save') + ->will($this->returnValue(TRUE)); + + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->image->getSource(), $this->toolkit)); + $image->expects($this->any()) + ->method('chmod') + ->will($this->returnValue(FALSE)); + + $this->assertFalse($image->save()); + } + + /** + * Tests Drupal\Core\Image\Image::save(). + */ + public function testProcessInfoFails() { + $this->image->setSource('magic-foobars.png'); + $this->assertFalse((bool) $this->image->getWidth()); + } + + /** * Tests Drupal\Core\Image\Image::scale(). */ public function testScaleWidth() { @@ -191,6 +228,19 @@ public function testScaleHeight() { } /** + * Tests Drupal\Core\Image\Image::scale(). + */ + public function testScaleSame() { + // Dimensions are the same, resize should not be called. + $this->toolkit->expects($this->never()) + ->method('resize') + ->will($this->returnArgument(1)); + + $width = $this->image->scale(88, 100); + $this->assertEquals($width, 88); + } + + /** * Tests Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropWidth() { @@ -223,6 +273,19 @@ public function testScaleAndCropHeight() { } /** + * Tests Drupal\Core\Image\Image::scaleAndCrop(). + */ + public function testScaleAndCropFails() { + $this->toolkit->expects($this->once()) + ->method('resize') + ->will($this->returnValue(FALSE)); + + $this->toolkit->expects($this->never()) + ->method('crop'); + $this->image->scaleAndCrop(44, 40); + } + + /** * Tests Drupal\Core\Image\Image::crop(). */ public function testCropWidth() {