Issue #1598582 by aspilicious: Convert image.test to PSR-0.

8.0.x
webchick 2012-06-14 11:01:02 +02:00
parent 41cd3d5c59
commit 8d6fe79b53
5 changed files with 263 additions and 235 deletions

View File

@ -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',

View File

@ -0,0 +1,52 @@
<?php
/**
* @file
* Definition of Drupal\system\Tests\Image\FileMoveTest.
*/
namespace Drupal\system\Tests\Image;
/**
* Tests the file move function for managed files.
*
* @todo This test belongs to File module.
*/
class FileMoveTest extends ToolkitTestBase {
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.');
}
}

View File

@ -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.');
}
}

View File

@ -0,0 +1,133 @@
<?php
/**
* @file
* Definition of Drupal\system\Tests\Image\ToolkitTest.
*/
namespace Drupal\system\Tests\Image;
/**
* Test that the functions in image.inc correctly pass data to the toolkit.
*/
class ToolkitTest extends ToolkitTestBase {
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.'));
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* @file
* Definition of Drupal\system\Tests\Image\ToolkitTestBase.
*/
namespace Drupal\system\Tests\Image;
use Drupal\simpletest\WebTestBase;
use stdClass;
/**
* Base class for image manipulation testing.
*/
class ToolkitTestBase 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.'));
}
}
}