123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- class ImageToolkitTestCase extends DrupalWebTestCase {
- protected $toolkit;
- protected $file;
- protected $image;
- function setUp() {
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules[] = 'image_test';
- parent::setUp($modules);
-
- $this->toolkit = 'test';
-
- $file = current($this->drupalGetTestFiles('image'));
- $this->file = $file->uri;
-
-
- $this->image = new stdClass();
- $this->image->source = $this->file;
- $this->image->info = image_get_info($this->file);
- $this->image->toolkit = $this->toolkit;
-
- image_test_reset();
- }
-
- function assertToolkitOperationsCalled(array $expected) {
-
- $actual = array_keys(array_filter(image_test_get_all_calls()));
-
- $uncalled = array_diff($expected, $actual);
- if (count($uncalled)) {
- $this->assertTrue(FALSE, format_string('Expected operations %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled))));
- }
- else {
- $this->assertTrue(TRUE, format_string('All the expected operations were called: %expected', array('%expected' => implode(', ', $expected))));
- }
-
- $unexpected = array_diff($actual, $expected);
- if (count($unexpected)) {
- $this->assertTrue(FALSE, format_string('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected))));
- }
- else {
- $this->assertTrue(TRUE, 'No unexpected operations were called.');
- }
- }
- }
- class ImageToolkitUnitTest extends ImageToolkitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image toolkit tests',
- 'description' => 'Check image toolkit functions.',
- 'group' => 'Image',
- );
- }
-
- function testGetAvailableToolkits() {
- $toolkits = image_get_available_toolkits();
- $this->assertTrue(isset($toolkits['test']), 'The working toolkit was returned.');
- $this->assertFalse(isset($toolkits['broken']), 'The toolkit marked unavailable was not returned');
- $this->assertToolkitOperationsCalled(array());
- }
-
- function testLoad() {
- $image = image_load($this->file, $this->toolkit);
- $this->assertTrue(is_object($image), 'Returned an object.');
- $this->assertEqual($this->toolkit, $image->toolkit, 'Image had toolkit set.');
- $this->assertToolkitOperationsCalled(array('load', 'get_info'));
- }
-
- function testSave() {
- $this->assertFalse(image_save($this->image), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('save'));
- }
-
- function testResize() {
- $this->assertTrue(image_resize($this->image, 1, 2), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly');
- }
-
- function testScale() {
- $this->assertTrue(image_scale($this->image, 10, 10), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 10, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 5, 'Height was based off aspect ratio and passed correctly');
- }
-
- function testScaleAndCrop() {
- $this->assertTrue(image_scale_and_crop($this->image, 5, 10), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize', 'crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 7.5, 'X was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly');
- }
-
- function testRotate() {
- $this->assertTrue(image_rotate($this->image, 90, 1), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('rotate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly');
- $this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly');
- }
-
- function testCrop() {
- $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly');
- $this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly');
- $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly');
- $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly');
- }
-
- function testDesaturate() {
- $this->assertTrue(image_desaturate($this->image), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('desaturate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.');
- }
- }
- class ImageToolkitGdTestCase extends DrupalWebTestCase {
-
- protected $black = array(0, 0, 0, 0);
- protected $red = array(255, 0, 0, 0);
- protected $green = array(0, 255, 0, 0);
- protected $blue = array(0, 0, 255, 0);
- protected $yellow = array(255, 255, 0, 0);
- protected $fuchsia = array(255, 0, 255, 0);
- protected $transparent = array(0, 0, 0, 127);
- protected $white = array(255, 255, 255, 0);
- protected $width = 40;
- protected $height = 20;
- public static function getInfo() {
- return array(
- 'name' => 'Image GD manipulation tests',
- 'description' => 'Check that core image manipulations work properly: scale, resize, rotate, crop, scale and crop, and desaturate.',
- 'group' => 'Image',
- );
- }
-
- function colorsAreEqual($color_a, $color_b) {
-
- if ($color_a[3] == 127 && $color_b[3] == 127) {
- return TRUE;
- }
- foreach ($color_a as $key => $value) {
- if ($color_b[$key] != $value) {
- return FALSE;
- }
- }
- return TRUE;
- }
-
- function getPixelColor($image, $x, $y) {
- $color_index = imagecolorat($image->resource, $x, $y);
- $transparent_index = imagecolortransparent($image->resource);
- if ($color_index == $transparent_index) {
- return array(0, 0, 0, 127);
- }
- return array_values(imagecolorsforindex($image->resource, $color_index));
- }
-
- function testManipulations() {
-
- module_load_include('inc', 'system', 'image.gd');
- if (!function_exists('image_gd_check_settings') || !image_gd_check_settings()) {
- $this->pass(t('Image manipulations for the GD toolkit were skipped because the GD toolkit is not available.'));
- return;
- }
-
-
- $default_corners = array($this->red, $this->green, $this->blue, $this->transparent);
-
- $files = array(
- 'image-test.png',
- 'image-test.gif',
- 'image-test.jpg',
- );
-
- $operations = array(
- 'resize' => array(
- 'function' => 'resize',
- 'arguments' => array(20, 10),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'scale_x' => array(
- 'function' => 'scale',
- 'arguments' => array(20, NULL),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'scale_y' => array(
- 'function' => 'scale',
- 'arguments' => array(NULL, 10),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'upscale_x' => array(
- 'function' => 'scale',
- 'arguments' => array(80, NULL, TRUE),
- 'width' => 80,
- 'height' => 40,
- 'corners' => $default_corners,
- ),
- 'upscale_y' => array(
- 'function' => 'scale',
- 'arguments' => array(NULL, 40, TRUE),
- 'width' => 80,
- 'height' => 40,
- 'corners' => $default_corners,
- ),
- 'crop' => array(
- 'function' => 'crop',
- 'arguments' => array(12, 4, 16, 12),
- 'width' => 16,
- 'height' => 12,
- 'corners' => array_fill(0, 4, $this->white),
- ),
- 'scale_and_crop' => array(
- 'function' => 'scale_and_crop',
- 'arguments' => array(10, 8),
- 'width' => 10,
- 'height' => 8,
- 'corners' => array_fill(0, 4, $this->black),
- ),
- );
-
- if (function_exists('imagerotate')) {
- $operations += array(
- 'rotate_5' => array(
- 'function' => 'rotate',
- 'arguments' => array(5, 0xFF00FF),
- 'width' => 42,
- 'height' => 24,
- 'corners' => array_fill(0, 4, $this->fuchsia),
- ),
- 'rotate_90' => array(
- 'function' => 'rotate',
- 'arguments' => array(90, 0xFF00FF),
- 'width' => 20,
- 'height' => 40,
- 'corners' => array($this->fuchsia, $this->red, $this->green, $this->blue),
- ),
- 'rotate_transparent_5' => array(
- 'function' => 'rotate',
- 'arguments' => array(5),
- 'width' => 42,
- 'height' => 24,
- 'corners' => array_fill(0, 4, $this->transparent),
- ),
- 'rotate_transparent_90' => array(
- 'function' => 'rotate',
- 'arguments' => array(90),
- 'width' => 20,
- 'height' => 40,
- 'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
- ),
- );
- }
-
- if (function_exists('imagefilter')) {
- $operations += array(
- 'desaturate' => array(
- 'function' => 'desaturate',
- 'arguments' => array(),
- 'height' => 20,
- 'width' => 40,
-
-
-
- 'corners' => array(
- array_fill(0, 3, 76) + array(3 => 0),
- array_fill(0, 3, 149) + array(3 => 0),
- array_fill(0, 3, 29) + array(3 => 0),
- array_fill(0, 3, 225) + array(3 => 127)
- ),
- ),
- );
- }
- foreach ($files as $file) {
- foreach ($operations as $op => $values) {
-
- $image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file, 'gd');
- if (!$image) {
- $this->fail(t('Could not load image %file.', array('%file' => $file)));
- continue 2;
- }
-
- $image_truecolor = imageistruecolor($image->resource);
- $this->assertTrue($image_truecolor, format_string('Image %file after load is a truecolor image.', array('%file' => $file)));
- if ($image->info['extension'] == 'gif') {
- if ($op == 'desaturate') {
-
- $values['corners'][3][3] = 0;
- }
- }
-
- $function = 'image_' . $values['function'];
- $arguments = array();
- $arguments[] = &$image;
- $arguments = array_merge($arguments, $values['arguments']);
- call_user_func_array($function, $arguments);
-
-
- $correct_dimensions_real = TRUE;
- $correct_dimensions_object = TRUE;
- $correct_colors = TRUE;
-
- if (imagesy($image->resource) != $values['height'] || imagesx($image->resource) != $values['width']) {
- $correct_dimensions_real = FALSE;
- }
-
- if ($image->info['width'] != $values['width'] || $image->info['height'] != $values['height']) {
- $correct_dimensions_object = FALSE;
- }
-
- foreach ($values['corners'] as $key => $corner) {
-
- switch ($key) {
- case 0:
- $x = 0;
- $y = 0;
- break;
- case 1:
- $x = $values['width'] - 1;
- $y = 0;
- break;
- case 2:
- $x = $values['width'] - 1;
- $y = $values['height'] - 1;
- break;
- case 3:
- $x = 0;
- $y = $values['height'] - 1;
- break;
- }
- $color = $this->getPixelColor($image, $x, $y);
- $correct_colors = $this->colorsAreEqual($color, $corner);
- }
- $directory = file_default_scheme() . '://imagetests';
- file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
- $file_path = $directory . '/' . $op . '.' . $image->info['extension'];
- image_save($image, $file_path);
- $this->assertTrue($correct_dimensions_real, format_string('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
- $this->assertTrue($correct_dimensions_object, format_string('Image %file object after %action action is reporting the proper height and width values.', array('%file' => $file, '%action' => $op)));
-
- if ($image->info['extension'] != 'jpg') {
- $this->assertTrue($correct_colors, format_string('Image %file object after %action action has the correct color placement.', array('%file' => $file, '%action' => $op)));
- }
- }
-
- $image_reloaded = image_load($file_path);
- }
- }
-
- function testTransparentColorOutOfRange() {
-
-
-
-
-
-
-
-
-
-
- $file = 'image-test-transparent-out-of-range.gif';
- $image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file);
- if (!$image) {
- $this->fail(format_string('Could not load image %file.', array('%file' => $file)));
- }
- else {
-
- $image_truecolor = imageistruecolor($image->resource);
- $this->assertTrue($image_truecolor, format_string('Image %file after load is a truecolor image.', array('%file' => $file)));
- }
- }
- }
- class ImageFileMoveTest extends ImageToolkitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image moving',
- 'description' => 'Tests the file move function for managed files.',
- 'group' => 'Image',
- );
- }
-
- function testNormal() {
-
- $file = current($this->drupalGetTestFiles('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);
-
- $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
-
-
- $desired_filepath = 'public://' . $this->randomName();
- $result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR);
-
- $this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.');
-
- $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
- }
- }
|