diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php index 9e21c03..a67501b 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -190,6 +190,9 @@ protected function validateArguments(array $arguments) { * * @return bool * TRUE if the operation was performed successfully, FALSE otherwise. + * + * @throws \RuntimeException + * If the operation can not be performed. */ abstract protected function execute(array $arguments); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php index 779deef..3ff986b 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php @@ -34,6 +34,8 @@ * * @throws \InvalidArgumentException * If one or more of the arguments are not valid. + * @throws \RuntimeException + * If the operation can not be performed. */ public function apply(array $arguments); diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 1dc3c73..1f5c89b 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -148,7 +148,9 @@ public function setResource($resource) { */ public function getResource() { if (!is_resource($this->resource)) { - $this->load(); + if (!$this->load()) { + return NULL; + }; } return $this->resource; } @@ -156,12 +158,12 @@ public function getResource() { /** * Returns the memory required to perform a GD function. * - * @return int|null - * Memory required to perform a GD operation, in bytes, or NULL if not - * available. + * @return string|null + * A human readable representation of the memory required to perform a GD + * operation (e.g. '15.6 Mb'). */ public function getRequiredMemory() { - return $this->requiredMemory; + return $this->requiredMemory !== NULL ? format_size($this->requiredMemory) : NULL; } /** @@ -203,7 +205,11 @@ protected function load() { // Check if enough memory is available. if (!$this->checkAvailableMemory($this->getWidth(), $this->getHeight())) { - throw new \RuntimeException("Not enough memory (required: " . format_size($this->getRequiredMemory()) . ") to load image '{$path}'"); + // If not, need to mark the image as invalid, to avoid useless attempts + // to manipulate it. + $this->preLoadInfo = NULL; + $this->logger->error("Not enough memory (required: %required_memory) to load image '%file'", ['%required_memory' => $this->getRequiredMemory(), '%file' => $this->getSource()]); + return FALSE; } $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); @@ -485,9 +491,6 @@ protected static function supportedTypes() { * @return bool * TRUE if there is sufficient memory to allow the operation. * - * @throws \RuntimeException - * When the available memory is not sufficient. - * * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623 */ public function checkAvailableMemory($width, $height, $bits_per_pixel = 32, $tweak_factor = 1.7) { diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php index 4baa5d7..8226dc7 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -86,7 +86,7 @@ protected function validateArguments(array $arguments) { protected function execute(array $arguments) { // Check if enough memory is available. if (!$this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) { - throw new \RuntimeException("Not enough memory (required: " . format_size($this->getToolkit()->getRequiredMemory()) . ") to perform the image '{$this->getPluginId()}' operation"); + throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation"); } // Get the image type. diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php index eb38c5f..f5fc048 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -105,7 +105,7 @@ protected function execute(array $arguments) { // by the image. $diagonal = (int) sqrt(pow($this->getToolkit()->getWidth(), 2) + pow($this->getToolkit()->getHeight(), 2)) + 1; if (!$this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) { - throw new \RuntimeException("Not enough memory (required: " . format_size($this->getToolkit()->getRequiredMemory()) . ") to perform the image '{$this->getPluginId()}' operation"); + throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation"); } // Stores the original GD resource. diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php new file mode 100644 index 0000000..23edaee5 --- /dev/null +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -0,0 +1,205 @@ +source = __DIR__ . '/../../../../../../simpletest/files/image-test.jpg'; + } + + /** + * Sets the GD toolkit mock. + * + * @param array $stubs + * (optional) Array containing methods to be replaced with stubs. + */ + protected function setToolkitMock(array $stubs = []) { + $logger = $this->getMock('Psr\Log\LoggerInterface'); + $operation_manager = $this->getMock('Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface'); + $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); + $stream_wrapper_manager = $this->getMock('Drupal\Core\StreamWrapper\StreamWrapperManagerInterface'); + $stubs = array_merge(['getPluginId'], $stubs); + $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit'); + $this->toolkit = $mock_builder + ->setConstructorArgs([[], '', [], $operation_manager, $logger, $config_factory, $stream_wrapper_manager]) + ->setMethods($stubs) + ->getMock(); + $this->toolkit->expects($this->any()) + ->method('getPluginId') + ->will($this->returnValue('gd')); + } + + /** + * Sets the GD toolkit operation mock. + * + * @param string $class_name + * The name of the GD toolkit operation class to be mocked. + * @param ImageToolkitInterface $toolkit + * The image toolkit object. + * @param array $stubs + * (optional) Array containing methods to be replaced with stubs. + */ + protected function setToolkitOperationMock($class_name, ImageToolkitInterface $toolkit, array $stubs = []) { + $logger = $this->getMock('Psr\Log\LoggerInterface'); + $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name); + $this->toolkitOperation = $mock_builder + ->setMethods($stubs) + ->setConstructorArgs([[], '', [], $toolkit, $logger]) + ->getMock(); + } + + /** + * Mock function for GDToolkit::apply(). + */ + public function applyMockedOperation($operation, array $arguments = []) { + return $this->toolkitOperation->apply($arguments); + } + + /** + * Test loading an image. + * + * @covers ::load + */ + public function testLoad() { + $this->setToolkitMock(['checkAvailableMemory']); + $this->toolkit->expects($this->any()) + ->method('checkAvailableMemory') + ->will($this->returnValue(TRUE)); + + $this->image = new Image($this->toolkit, $this->source); + $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); + $this->assertTrue($this->image->isValid()); + $this->assertEquals(40, $this->image->getWidth()); + $this->assertEquals(20, $this->image->getHeight()); + } + + /** + * Simulates not enough memory to load an image. + * + * @covers ::load + */ + public function testNotEnoughMemoryOnLoad() { + $this->setToolkitMock(['checkAvailableMemory']); + $this->toolkit->expects($this->any()) + ->method('checkAvailableMemory') + ->will($this->returnValue(FALSE)); + + // Getting image from file validates the Image object, since GD toolkit is + // lazy loading the resource. + $this->image = new Image($this->toolkit, $this->source); + $this->assertTrue($this->image->isValid()); + // Trying to get GD resource without enough memory yields to NULL resource + // and invalidates Image object. + $this->assertFalse(is_resource($this->image->getToolkit()->getResource())); + $this->assertFalse($this->image->isValid()); + $this->assertEquals(NULL, $this->image->getWidth()); + $this->assertEquals(NULL, $this->image->getHeight()); + } + + /** + * Simulates not enough memory to create a new image. + * + * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute + * + * @expectedException \RuntimeException + */ + public function testNotEnoughMemoryOnCreateNew() { + $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']); + $this->setToolkitOperationMock('CreateNew', $this->toolkit); + $this->toolkit->expects($this->any()) + ->method('checkAvailableMemory') + ->will($this->onConsecutiveCalls(TRUE, FALSE)); + $this->toolkit->expects($this->any()) + ->method('getToolkitOperation') + ->will($this->returnValue($this->toolkitOperation)); + $this->toolkit->expects($this->once()) + ->method('apply') + ->will($this->returnCallback([$this, 'applyMockedOperation'])); + + $this->image = new Image($this->toolkit, $this->source); + // Get the GD resource to force GDToolkit::load, where the memory check is + // mocked to succeed. + $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); + // Try the Image::createNew operation, where the memory check is mocked + // to fail. Expected a runtime exception. + $this->image->createNew(100, 200); + } + + /** + * Simulates not enough memory to rotate an image. + * + * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\Rotate::execute + * + * @expectedException \RuntimeException + */ + public function testNotEnoughMemoryOnRotate() { + $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']); + $this->setToolkitOperationMock('Rotate', $this->toolkit); + $this->toolkit->expects($this->any()) + ->method('checkAvailableMemory') + ->will($this->onConsecutiveCalls(TRUE, FALSE)); + $this->toolkit->expects($this->any()) + ->method('getToolkitOperation') + ->will($this->returnValue($this->toolkitOperation)); + $this->toolkit->expects($this->once()) + ->method('apply') + ->will($this->returnCallback([$this, 'applyMockedOperation'])); + + $this->image = new Image($this->toolkit, $this->source); + // Get the GD resource to force GDToolkit::load, where the memory check is + // mocked to succeed. + $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); + // Try the Image::rotate operation, where the memory check is mocked + // to fail. Expected a runtime exception. + $this->image->rotate(120); + } + +}