diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index a0aaf77..79ab089 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -50,12 +50,13 @@ function testResizeEffect() { 'width' => 1, 'height' => 2, )); - $this->assertToolkitOperationsCalled(array('resize')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); + $this->assertEqual($calls['apply'][0][0], 'resize', "'resize' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['width'], 1, 'Width was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['height'], 2, 'Height was passed correctly'); } /** @@ -67,12 +68,13 @@ function testScaleEffect() { 'width' => 10, 'height' => 10, )); - $this->assertToolkitOperationsCalled(array('scale')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $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'); + $this->assertEqual($calls['apply'][0][0], 'scale', "'scale' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['width'], 10, 'Width was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['height'], 10, 'Height was based off aspect ratio and passed correctly'); } /** @@ -81,18 +83,19 @@ function testScaleEffect() { function testCropEffect() { // @todo should test the keyword offsets. $this->assertImageEffect('image_crop', array( - 'anchor' => 'top-1', + 'anchor' => 'left-1', 'width' => 3, 'height' => 4, )); - $this->assertToolkitOperationsCalled(array('crop')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $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'); + $this->assertEqual($calls['apply'][0][0], 'crop', "'crop' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['x'], 0, 'X was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['y'], 1, 'Y was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['width'], 3, 'Width was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['height'], 4, 'Height was passed correctly'); } /** @@ -103,12 +106,13 @@ function testScaleAndCropEffect() { 'width' => 5, 'height' => 10, )); - $this->assertToolkitOperationsCalled(array('scale_and_crop')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['apply'][0][0], 'scale_and_crop', "'scale_and_crop' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['width'], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['apply'][0][1]['height'], 10, 'Height was computed and passed correctly'); } /** @@ -116,11 +120,12 @@ function testScaleAndCropEffect() { */ function testDesaturateEffect() { $this->assertImageEffect('image_desaturate', array()); - $this->assertToolkitOperationsCalled(array('desaturate')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed'); + $this->assertEqual($calls['apply'][0][0], 'desaturate', "'desaturate' was passed correctly as operation"); + $this->assertEqual(count($calls['apply'][0][1]), 0, 'No parameters were passed'); } /** @@ -132,12 +137,13 @@ function testRotateEffect() { 'degrees' => 90, 'bgcolor' => '#fff', )); - $this->assertToolkitOperationsCalled(array('rotate')); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $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['apply'][0][0], 'rotate', "'rotate' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['degrees'], 90, 'Degrees were passed correctly'); + $this->assertEqual($calls['apply'][0][1]['background'], 0xffffff, 'Background color was passed correctly'); } /** diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php index 6b95e29..ac64512 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php @@ -59,8 +59,8 @@ protected function validateArguments(ImageInterface $image, array $arguments) { * {@inheritdoc} */ protected function execute(ImageInterface $image, array $arguments = array()) { - return $this->getToolkit()->apply('resize', $image, $arguments['resize']) - && $this->getToolkit()->apply('crop', $image, $arguments); + return $this->getToolkit()->apply('resize', $arguments['resize']) + && $this->getToolkit()->apply('crop', $arguments); } } diff --git a/core/modules/system/src/Tests/Image/ToolkitTest.php b/core/modules/system/src/Tests/Image/ToolkitTest.php index 7c19bf6..0bb61cb 100644 --- a/core/modules/system/src/Tests/Image/ToolkitTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitTest.php @@ -50,88 +50,31 @@ function testSave() { } /** - * Test the image_resize() function. + * Test the image_apply() function. */ - function testResize() { - $data = array('width' => 1, 'height' => 2); - $this->assertTrue($this->image->apply('resize', $data), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('resize')); + function testApply() { + $data = array('p1' => 1, 'p2' => TRUE, 'p3' => 'text'); + $this->assertTrue($this->image->apply('my_operation', $data), 'Function returned the expected value.'); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); + $this->assertEqual($calls['apply'][0][0], 'my_operation', "'my_operation' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1]['p1'], 1, 'integer parameter p1 was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['p2'], TRUE, 'boolean parameter p2 was passed correctly'); + $this->assertEqual($calls['apply'][0][1]['p3'], 'text', 'string parameter p3 was passed correctly'); } /** - * Test the image_scale() function. + * Test the image_apply() function. */ - function testScale() { -// TODO: need to test upscaling - $data = array('width' => 10, 'height' => 10); - $this->assertTrue($this->image->apply('scale', $data), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('scale')); + function testApplyNoParameters() { + $this->assertTrue($this->image->apply('my_operation'), 'Function returned the expected value.'); - // Check the parameters. + // Check that apply was called and with the correct parameters. + $this->assertToolkitOperationsCalled(array('apply')); $calls = $this->imageTestGetAllCalls(); - $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'); - } - - /** - * Test the image_scale_and_crop() function. - */ - function testScaleAndCrop() { - $data = array('width' => 5, 'height' => 10); - $this->assertTrue($this->image->apply('scale_and_crop', $data), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('scale_and_crop')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - - $this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly'); - } - - /** - * Test the image_rotate() function. - */ - function testRotate() { - $data = array('degrees' => 90, 'background' => 1); - $this->assertTrue($this->image->apply('rotate', $data), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('rotate')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly'); - $this->assertEqual($calls['rotate'][0][1], 1, 'Background color was passed correctly'); - } - - /** - * Test the image_crop() function. - */ - function testCrop() { - $data = array('x' => 1, 'y' => 2, 'width' => 3, 'height' => 4); - $this->assertTrue($this->image->apply('crop', $data), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('crop')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - $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'); - } - - /** - * Test the image_desaturate() function. - */ - function testDesaturate() { - $this->assertTrue($this->image->apply('desaturate'), 'Function returned the expected value.'); - $this->assertToolkitOperationsCalled(array('desaturate')); - - // Check the parameters. - $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.'); + $this->assertEqual($calls['apply'][0][0], 'my_operation', "'my_operation' was passed correctly as operation"); + $this->assertEqual($calls['apply'][0][1], array(), 'passing no parameters was handled correctly'); } } 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 5b56734..7e9b2fd 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 @@ -7,6 +7,7 @@ namespace Drupal\image_test\Plugin\ImageToolkit; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Component\Utility\Unicode; use Drupal\Core\ImageToolkit\ImageToolkitBase; @@ -45,7 +46,7 @@ class TestToolkit extends ImageToolkitBase { * {@inheritdoc} */ public function settingsForm() { - $this->logCall('settings', array()); + $this->logCall('settings', func_get_args()); $form['test_parameter'] = array( '#type' => 'number', '#title' => $this->t('Test toolkit parameter'), @@ -70,7 +71,7 @@ public function settingsFormSubmit($form, &$form_state) { * {@inheritdoc} */ public function parseFile() { - $this->logCall('parseFile', array()); + $this->logCall('parseFile', func_get_args()); $data = @getimagesize($this->getImage()->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); @@ -85,7 +86,7 @@ public function parseFile() { * {@inheritdoc} */ public function save($destination) { - $this->logCall('save', array($destination)); + $this->logCall('save', func_get_args()); // Return false so that image_save() doesn't try to chmod the destination // file that we didn't bother to create. return FALSE; @@ -95,15 +96,14 @@ public function save($destination) { * Stores the values passed to a toolkit call. * * @param string $op - * One of the image toolkit operations: 'parseFile', 'save', 'settings', - * 'resize', 'rotate', 'crop', 'desaturate'. + * One of the toolkit methods 'parseFile', 'save', 'settings', or 'apply'. * @param array $args * Values passed to hook. * * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset() * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls() */ - public function logCall($op, $args) { + protected function logCall($op, $args) { $results = \Drupal::state()->get('image_test.results') ?: array(); $results[$op][] = $args; \Drupal::state()->set('image_test.results', $results); @@ -153,7 +153,7 @@ public function setType($type) { /** * {@inheritdoc} */ - public function getMimeType(ImageInterface $image) { + public function getMimeType() { return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; } @@ -186,4 +186,12 @@ protected static function supportedTypes() { return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); } + /** + * {@inheritdoc} + */ + public function apply($operation, array $arguments = array()) { + $this->logCall('apply', func_get_args()); + return TRUE; + } + }