From 8d6fe79b536308d3765a8e40e638a71a0b2c3eba Mon Sep 17 00:00:00 2001 From: webchick Date: Thu, 14 Jun 2012 11:01:02 +0200 Subject: [PATCH] Issue #1598582 by aspilicious: Convert image.test to PSR-0. --- .../Drupal/image/Tests/ImageEffectsTest.php | 4 +- .../system/Tests/Image/FileMoveTest.php | 52 ++++ .../system/Tests/Image/ToolkitGdTest.php} | 237 +----------------- .../Drupal/system/Tests/Image/ToolkitTest.php | 133 ++++++++++ .../system/Tests/Image/ToolkitTestBase.php | 72 ++++++ 5 files changed, 263 insertions(+), 235 deletions(-) create mode 100644 core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php rename core/modules/system/{tests/image.test => lib/Drupal/system/Tests/Image/ToolkitGdTest.php} (53%) create mode 100644 core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php create mode 100644 core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php index 5b9df0446a4..0456162fe1d 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php @@ -7,13 +7,13 @@ namespace Drupal\image\Tests; -use ImageToolkitTestCase; +use Drupal\system\Tests\Image\ToolkitTestBase; /** * Use the image_test.module's mock toolkit to ensure that the effects are * properly passing parameters to the image toolkit. */ -class ImageEffectsTest extends ImageToolkitTestCase { +class ImageEffectsTest extends ToolkitTestBase { public static function getInfo() { return array( 'name' => 'Image effects', diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php new file mode 100644 index 00000000000..ebd2a5d899a --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php @@ -0,0 +1,52 @@ + 'Image moving', + 'description' => 'Tests the file move function for managed files.', + 'group' => 'Image', + ); + } + + /** + * Tests moving a randomly generated image. + */ + function testNormal() { + // Pick a file for testing. + $file = entity_create('file', (array) current($this->drupalGetTestFiles('image'))); + + // Create derivative image. + $style = image_style_load(key(image_styles())); + $derivative_uri = image_style_path($style['name'], $file->uri); + image_style_create_derivative($style, $file->uri, $derivative_uri); + + // Check if derivative image exists. + $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.'); + + // Clone the object so we don't have to worry about the function changing + // our reference copy. + $desired_filepath = 'public://' . $this->randomName(); + $result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR); + + // Check if image has been moved. + $this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.'); + + // Check if derivative image has been flushed. + $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.'); + } +} diff --git a/core/modules/system/tests/image.test b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php similarity index 53% rename from core/modules/system/tests/image.test rename to core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php index 9dcce4908fe..611126d2ba0 100644 --- a/core/modules/system/tests/image.test +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php @@ -2,201 +2,17 @@ /** * @file - * Tests for core image handling API. + * Definition of Drupal\system\Tests\Image\ToolkitGdTest. */ +namespace Drupal\system\Tests\Image; + use Drupal\simpletest\WebTestBase; -/** - * Base class for image manipulation testing. - */ -class ImageToolkitTestCase extends WebTestBase { - protected $toolkit; - protected $file; - protected $image; - - function setUp() { - parent::setUp('image_test'); - - // Use the image_test.module's test toolkit. - $this->toolkit = 'test'; - - // Pick a file for testing. - $file = current($this->drupalGetTestFiles('image')); - $this->file = $file->uri; - - // Setup a dummy image to work with, this replicate image_load() so we - // can avoid calling it. - $this->image = new stdClass(); - $this->image->source = $this->file; - $this->image->info = image_get_info($this->file); - $this->image->toolkit = $this->toolkit; - - // Clear out any hook calls. - image_test_reset(); - } - - /** - * Assert that all of the specified image toolkit operations were called - * exactly once once, other values result in failure. - * - * @param $expected - * Array with string containing with the operation name, e.g. 'load', - * 'save', 'crop', etc. - */ - function assertToolkitOperationsCalled(array $expected) { - // Determine which operations were called. - $actual = array_keys(array_filter(image_test_get_all_calls())); - - // Determine if there were any expected that were not called. - $uncalled = array_diff($expected, $actual); - if (count($uncalled)) { - $this->assertTrue(FALSE, t('Expected operations %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)))); - } - else { - $this->assertTrue(TRUE, t('All the expected operations were called: %expected', array('%expected' => implode(', ', $expected)))); - } - - // Determine if there were any unexpected calls. - $unexpected = array_diff($actual, $expected); - if (count($unexpected)) { - $this->assertTrue(FALSE, t('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected)))); - } - else { - $this->assertTrue(TRUE, t('No unexpected operations were called.')); - } - } -} - -/** - * Test that the functions in image.inc correctly pass data to the toolkit. - */ -class ImageToolkitUnitTest extends ImageToolkitTestCase { - public static function getInfo() { - return array( - 'name' => 'Image toolkit tests', - 'description' => 'Check image toolkit functions.', - 'group' => 'Image', - ); - } - - /** - * Check that hook_image_toolkits() is called and only available toolkits are - * returned. - */ - function testGetAvailableToolkits() { - $toolkits = image_get_available_toolkits(); - $this->assertTrue(isset($toolkits['test']), t('The working toolkit was returned.')); - $this->assertFalse(isset($toolkits['broken']), t('The toolkit marked unavailable was not returned')); - $this->assertToolkitOperationsCalled(array()); - } - - /** - * Test the image_load() function. - */ - function testLoad() { - $image = image_load($this->file, $this->toolkit); - $this->assertTrue(is_object($image), t('Returned an object.')); - $this->assertEqual($this->toolkit, $image->toolkit, t('Image had toolkit set.')); - $this->assertToolkitOperationsCalled(array('load', 'get_info')); - } - - /** - * Test the image_save() function. - */ - function testSave() { - $this->assertFalse(image_save($this->image), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('save')); - } - - /** - * Test the image_resize() function. - */ - function testResize() { - $this->assertTrue(image_resize($this->image, 1, 2), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('resize')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - $this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly')); - $this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly')); - } - - /** - * Test the image_scale() function. - */ - function testScale() { -// TODO: need to test upscaling - $this->assertTrue(image_scale($this->image, 10, 10), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('resize')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - $this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly')); - $this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly')); - } - - /** - * Test the image_scale_and_crop() function. - */ - function testScaleAndCrop() { - $this->assertTrue(image_scale_and_crop($this->image, 5, 10), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('resize', 'crop')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - - $this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly')); - $this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly')); - $this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly')); - $this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly')); - } - - /** - * Test the image_rotate() function. - */ - function testRotate() { - $this->assertTrue(image_rotate($this->image, 90, 1), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('rotate')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - $this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly')); - $this->assertEqual($calls['rotate'][0][2], 1, t('Background color was passed correctly')); - } - - /** - * Test the image_crop() function. - */ - function testCrop() { - $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('crop')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - $this->assertEqual($calls['crop'][0][1], 1, t('X was passed correctly')); - $this->assertEqual($calls['crop'][0][2], 2, t('Y was passed correctly')); - $this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly')); - $this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly')); - } - - /** - * Test the image_desaturate() function. - */ - function testDesaturate() { - $this->assertTrue(image_desaturate($this->image), t('Function returned the expected value.')); - $this->assertToolkitOperationsCalled(array('desaturate')); - - // Check the parameters. - $calls = image_test_get_all_calls(); - $this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.')); - } -} - /** * Test the core GD image manipulation functions. */ -class ImageToolkitGdTestCase extends WebTestBase { +class ToolkitGdTest extends WebTestBase { // Colors that are used in testing. protected $black = array(0, 0, 0, 0); protected $red = array(255, 0, 0, 0); @@ -468,48 +284,3 @@ class ImageToolkitGdTestCase extends WebTestBase { } } - -/** - * Tests the file move function for managed files. - * - * @todo This test belongs to File module. - */ -class ImageFileMoveTest extends ImageToolkitTestCase { - protected $profile = 'standard'; - - public static function getInfo() { - return array( - 'name' => 'Image moving', - 'description' => 'Tests the file move function for managed files.', - 'group' => 'Image', - ); - } - - /** - * Tests moving a randomly generated image. - */ - function testNormal() { - // Pick a file for testing. - $file = entity_create('file', (array) current($this->drupalGetTestFiles('image'))); - - // Create derivative image. - $style = image_style_load(key(image_styles())); - $derivative_uri = image_style_path($style['name'], $file->uri); - image_style_create_derivative($style, $file->uri, $derivative_uri); - - // Check if derivative image exists. - $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.'); - - // Clone the object so we don't have to worry about the function changing - // our reference copy. - $desired_filepath = 'public://' . $this->randomName(); - $result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR); - - // Check if image has been moved. - $this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.'); - - // Check if derivative image has been flushed. - $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.'); - } -} - diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php new file mode 100644 index 00000000000..091fc5a4a4a --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php @@ -0,0 +1,133 @@ + 'Image toolkit tests', + 'description' => 'Check image toolkit functions.', + 'group' => 'Image', + ); + } + + /** + * Check that hook_image_toolkits() is called and only available toolkits are + * returned. + */ + function testGetAvailableToolkits() { + $toolkits = image_get_available_toolkits(); + $this->assertTrue(isset($toolkits['test']), t('The working toolkit was returned.')); + $this->assertFalse(isset($toolkits['broken']), t('The toolkit marked unavailable was not returned')); + $this->assertToolkitOperationsCalled(array()); + } + + /** + * Test the image_load() function. + */ + function testLoad() { + $image = image_load($this->file, $this->toolkit); + $this->assertTrue(is_object($image), t('Returned an object.')); + $this->assertEqual($this->toolkit, $image->toolkit, t('Image had toolkit set.')); + $this->assertToolkitOperationsCalled(array('load', 'get_info')); + } + + /** + * Test the image_save() function. + */ + function testSave() { + $this->assertFalse(image_save($this->image), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('save')); + } + + /** + * Test the image_resize() function. + */ + function testResize() { + $this->assertTrue(image_resize($this->image, 1, 2), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('resize')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + $this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly')); + $this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly')); + } + + /** + * Test the image_scale() function. + */ + function testScale() { +// TODO: need to test upscaling + $this->assertTrue(image_scale($this->image, 10, 10), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('resize')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + $this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly')); + $this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly')); + } + + /** + * Test the image_scale_and_crop() function. + */ + function testScaleAndCrop() { + $this->assertTrue(image_scale_and_crop($this->image, 5, 10), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('resize', 'crop')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + + $this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly')); + $this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly')); + $this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly')); + $this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly')); + } + + /** + * Test the image_rotate() function. + */ + function testRotate() { + $this->assertTrue(image_rotate($this->image, 90, 1), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('rotate')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + $this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly')); + $this->assertEqual($calls['rotate'][0][2], 1, t('Background color was passed correctly')); + } + + /** + * Test the image_crop() function. + */ + function testCrop() { + $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('crop')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + $this->assertEqual($calls['crop'][0][1], 1, t('X was passed correctly')); + $this->assertEqual($calls['crop'][0][2], 2, t('Y was passed correctly')); + $this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly')); + $this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly')); + } + + /** + * Test the image_desaturate() function. + */ + function testDesaturate() { + $this->assertTrue(image_desaturate($this->image), t('Function returned the expected value.')); + $this->assertToolkitOperationsCalled(array('desaturate')); + + // Check the parameters. + $calls = image_test_get_all_calls(); + $this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.')); + } +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php new file mode 100644 index 00000000000..53d723cbac8 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php @@ -0,0 +1,72 @@ +toolkit = 'test'; + + // Pick a file for testing. + $file = current($this->drupalGetTestFiles('image')); + $this->file = $file->uri; + + // Setup a dummy image to work with, this replicate image_load() so we + // can avoid calling it. + $this->image = new stdClass(); + $this->image->source = $this->file; + $this->image->info = image_get_info($this->file); + $this->image->toolkit = $this->toolkit; + + // Clear out any hook calls. + image_test_reset(); + } + + /** + * Assert that all of the specified image toolkit operations were called + * exactly once once, other values result in failure. + * + * @param $expected + * Array with string containing with the operation name, e.g. 'load', + * 'save', 'crop', etc. + */ + function assertToolkitOperationsCalled(array $expected) { + // Determine which operations were called. + $actual = array_keys(array_filter(image_test_get_all_calls())); + + // Determine if there were any expected that were not called. + $uncalled = array_diff($expected, $actual); + if (count($uncalled)) { + $this->assertTrue(FALSE, t('Expected operations %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)))); + } + else { + $this->assertTrue(TRUE, t('All the expected operations were called: %expected', array('%expected' => implode(', ', $expected)))); + } + + // Determine if there were any unexpected calls. + $unexpected = array_diff($actual, $expected); + if (count($unexpected)) { + $this->assertTrue(FALSE, t('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected)))); + } + else { + $this->assertTrue(TRUE, t('No unexpected operations were called.')); + } + } +}