1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for core image handling API.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Base class for image manipulation testing.
|
10
|
*/
|
11
|
class ImageToolkitTestCase extends DrupalWebTestCase {
|
12
|
protected $toolkit;
|
13
|
protected $file;
|
14
|
protected $image;
|
15
|
|
16
|
function setUp() {
|
17
|
$modules = func_get_args();
|
18
|
if (isset($modules[0]) && is_array($modules[0])) {
|
19
|
$modules = $modules[0];
|
20
|
}
|
21
|
$modules[] = 'image_test';
|
22
|
|
23
|
parent::setUp($modules);
|
24
|
|
25
|
// Use the image_test.module's test toolkit.
|
26
|
$this->toolkit = 'test';
|
27
|
|
28
|
// Pick a file for testing.
|
29
|
$file = current($this->drupalGetTestFiles('image'));
|
30
|
$this->file = $file->uri;
|
31
|
|
32
|
// Setup a dummy image to work with, this replicate image_load() so we
|
33
|
// can avoid calling it.
|
34
|
$this->image = new stdClass();
|
35
|
$this->image->source = $this->file;
|
36
|
$this->image->info = image_get_info($this->file);
|
37
|
$this->image->toolkit = $this->toolkit;
|
38
|
|
39
|
// Clear out any hook calls.
|
40
|
image_test_reset();
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Assert that all of the specified image toolkit operations were called
|
45
|
* exactly once once, other values result in failure.
|
46
|
*
|
47
|
* @param $expected
|
48
|
* Array with string containing with the operation name, e.g. 'load',
|
49
|
* 'save', 'crop', etc.
|
50
|
*/
|
51
|
function assertToolkitOperationsCalled(array $expected) {
|
52
|
// Determine which operations were called.
|
53
|
$actual = array_keys(array_filter(image_test_get_all_calls()));
|
54
|
|
55
|
// Determine if there were any expected that were not called.
|
56
|
$uncalled = array_diff($expected, $actual);
|
57
|
if (count($uncalled)) {
|
58
|
$this->assertTrue(FALSE, format_string('Expected operations %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled))));
|
59
|
}
|
60
|
else {
|
61
|
$this->assertTrue(TRUE, format_string('All the expected operations were called: %expected', array('%expected' => implode(', ', $expected))));
|
62
|
}
|
63
|
|
64
|
// Determine if there were any unexpected calls.
|
65
|
$unexpected = array_diff($actual, $expected);
|
66
|
if (count($unexpected)) {
|
67
|
$this->assertTrue(FALSE, format_string('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected))));
|
68
|
}
|
69
|
else {
|
70
|
$this->assertTrue(TRUE, 'No unexpected operations were called.');
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Test that the functions in image.inc correctly pass data to the toolkit.
|
77
|
*/
|
78
|
class ImageToolkitUnitTest extends ImageToolkitTestCase {
|
79
|
public static function getInfo() {
|
80
|
return array(
|
81
|
'name' => 'Image toolkit tests',
|
82
|
'description' => 'Check image toolkit functions.',
|
83
|
'group' => 'Image',
|
84
|
);
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Check that hook_image_toolkits() is called and only available toolkits are
|
89
|
* returned.
|
90
|
*/
|
91
|
function testGetAvailableToolkits() {
|
92
|
$toolkits = image_get_available_toolkits();
|
93
|
$this->assertTrue(isset($toolkits['test']), 'The working toolkit was returned.');
|
94
|
$this->assertFalse(isset($toolkits['broken']), 'The toolkit marked unavailable was not returned');
|
95
|
$this->assertToolkitOperationsCalled(array());
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Test the image_load() function.
|
100
|
*/
|
101
|
function testLoad() {
|
102
|
$image = image_load($this->file, $this->toolkit);
|
103
|
$this->assertTrue(is_object($image), 'Returned an object.');
|
104
|
$this->assertEqual($this->toolkit, $image->toolkit, 'Image had toolkit set.');
|
105
|
$this->assertToolkitOperationsCalled(array('load', 'get_info'));
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Test the image_save() function.
|
110
|
*/
|
111
|
function testSave() {
|
112
|
$this->assertFalse(image_save($this->image), 'Function returned the expected value.');
|
113
|
$this->assertToolkitOperationsCalled(array('save'));
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Test the image_resize() function.
|
118
|
*/
|
119
|
function testResize() {
|
120
|
$this->assertTrue(image_resize($this->image, 1, 2), 'Function returned the expected value.');
|
121
|
$this->assertToolkitOperationsCalled(array('resize'));
|
122
|
|
123
|
// Check the parameters.
|
124
|
$calls = image_test_get_all_calls();
|
125
|
$this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly');
|
126
|
$this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly');
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Test the image_scale() function.
|
131
|
*/
|
132
|
function testScale() {
|
133
|
// TODO: need to test upscaling
|
134
|
$this->assertTrue(image_scale($this->image, 10, 10), 'Function returned the expected value.');
|
135
|
$this->assertToolkitOperationsCalled(array('resize'));
|
136
|
|
137
|
// Check the parameters.
|
138
|
$calls = image_test_get_all_calls();
|
139
|
$this->assertEqual($calls['resize'][0][1], 10, 'Width was passed correctly');
|
140
|
$this->assertEqual($calls['resize'][0][2], 5, 'Height was based off aspect ratio and passed correctly');
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Test the image_scale_and_crop() function.
|
145
|
*/
|
146
|
function testScaleAndCrop() {
|
147
|
$this->assertTrue(image_scale_and_crop($this->image, 5, 10), 'Function returned the expected value.');
|
148
|
$this->assertToolkitOperationsCalled(array('resize', 'crop'));
|
149
|
|
150
|
// Check the parameters.
|
151
|
$calls = image_test_get_all_calls();
|
152
|
|
153
|
$this->assertEqual($calls['crop'][0][1], 7.5, 'X was computed and passed correctly');
|
154
|
$this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly');
|
155
|
$this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly');
|
156
|
$this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly');
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Test the image_rotate() function.
|
161
|
*/
|
162
|
function testRotate() {
|
163
|
$this->assertTrue(image_rotate($this->image, 90, 1), 'Function returned the expected value.');
|
164
|
$this->assertToolkitOperationsCalled(array('rotate'));
|
165
|
|
166
|
// Check the parameters.
|
167
|
$calls = image_test_get_all_calls();
|
168
|
$this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly');
|
169
|
$this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly');
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Test the image_crop() function.
|
174
|
*/
|
175
|
function testCrop() {
|
176
|
$this->assertTrue(image_crop($this->image, 1, 2, 3, 4), 'Function returned the expected value.');
|
177
|
$this->assertToolkitOperationsCalled(array('crop'));
|
178
|
|
179
|
// Check the parameters.
|
180
|
$calls = image_test_get_all_calls();
|
181
|
$this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly');
|
182
|
$this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly');
|
183
|
$this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly');
|
184
|
$this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly');
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Test the image_desaturate() function.
|
189
|
*/
|
190
|
function testDesaturate() {
|
191
|
$this->assertTrue(image_desaturate($this->image), 'Function returned the expected value.');
|
192
|
$this->assertToolkitOperationsCalled(array('desaturate'));
|
193
|
|
194
|
// Check the parameters.
|
195
|
$calls = image_test_get_all_calls();
|
196
|
$this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.');
|
197
|
}
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Test the core GD image manipulation functions.
|
202
|
*/
|
203
|
class ImageToolkitGdTestCase extends DrupalWebTestCase {
|
204
|
// Colors that are used in testing.
|
205
|
protected $black = array(0, 0, 0, 0);
|
206
|
protected $red = array(255, 0, 0, 0);
|
207
|
protected $green = array(0, 255, 0, 0);
|
208
|
protected $blue = array(0, 0, 255, 0);
|
209
|
protected $yellow = array(255, 255, 0, 0);
|
210
|
protected $fuchsia = array(255, 0, 255, 0); // Used as background colors.
|
211
|
protected $transparent = array(0, 0, 0, 127);
|
212
|
protected $white = array(255, 255, 255, 0);
|
213
|
|
214
|
protected $width = 40;
|
215
|
protected $height = 20;
|
216
|
|
217
|
public static function getInfo() {
|
218
|
return array(
|
219
|
'name' => 'Image GD manipulation tests',
|
220
|
'description' => 'Check that core image manipulations work properly: scale, resize, rotate, crop, scale and crop, and desaturate.',
|
221
|
'group' => 'Image',
|
222
|
);
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* Function to compare two colors by RGBa.
|
227
|
*/
|
228
|
function colorsAreEqual($color_a, $color_b) {
|
229
|
// Fully transparent pixels are equal, regardless of RGB.
|
230
|
if ($color_a[3] == 127 && $color_b[3] == 127) {
|
231
|
return TRUE;
|
232
|
}
|
233
|
|
234
|
foreach ($color_a as $key => $value) {
|
235
|
if ($color_b[$key] != $value) {
|
236
|
return FALSE;
|
237
|
}
|
238
|
}
|
239
|
|
240
|
return TRUE;
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Function for finding a pixel's RGBa values.
|
245
|
*/
|
246
|
function getPixelColor($image, $x, $y) {
|
247
|
$color_index = imagecolorat($image->resource, $x, $y);
|
248
|
|
249
|
$transparent_index = imagecolortransparent($image->resource);
|
250
|
if ($color_index == $transparent_index) {
|
251
|
return array(0, 0, 0, 127);
|
252
|
}
|
253
|
|
254
|
return array_values(imagecolorsforindex($image->resource, $color_index));
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* Since PHP can't visually check that our images have been manipulated
|
259
|
* properly, build a list of expected color values for each of the corners and
|
260
|
* the expected height and widths for the final images.
|
261
|
*/
|
262
|
function testManipulations() {
|
263
|
// If GD isn't available don't bother testing this.
|
264
|
module_load_include('inc', 'system', 'image.gd');
|
265
|
if (!function_exists('image_gd_check_settings') || !image_gd_check_settings()) {
|
266
|
$this->pass(t('Image manipulations for the GD toolkit were skipped because the GD toolkit is not available.'));
|
267
|
return;
|
268
|
}
|
269
|
|
270
|
// Typically the corner colors will be unchanged. These colors are in the
|
271
|
// order of top-left, top-right, bottom-right, bottom-left.
|
272
|
$default_corners = array($this->red, $this->green, $this->blue, $this->transparent);
|
273
|
|
274
|
// A list of files that will be tested.
|
275
|
$files = array(
|
276
|
'image-test.png',
|
277
|
'image-test.gif',
|
278
|
'image-test.jpg',
|
279
|
);
|
280
|
|
281
|
// Setup a list of tests to perform on each type.
|
282
|
$operations = array(
|
283
|
'resize' => array(
|
284
|
'function' => 'resize',
|
285
|
'arguments' => array(20, 10),
|
286
|
'width' => 20,
|
287
|
'height' => 10,
|
288
|
'corners' => $default_corners,
|
289
|
),
|
290
|
'scale_x' => array(
|
291
|
'function' => 'scale',
|
292
|
'arguments' => array(20, NULL),
|
293
|
'width' => 20,
|
294
|
'height' => 10,
|
295
|
'corners' => $default_corners,
|
296
|
),
|
297
|
'scale_y' => array(
|
298
|
'function' => 'scale',
|
299
|
'arguments' => array(NULL, 10),
|
300
|
'width' => 20,
|
301
|
'height' => 10,
|
302
|
'corners' => $default_corners,
|
303
|
),
|
304
|
'upscale_x' => array(
|
305
|
'function' => 'scale',
|
306
|
'arguments' => array(80, NULL, TRUE),
|
307
|
'width' => 80,
|
308
|
'height' => 40,
|
309
|
'corners' => $default_corners,
|
310
|
),
|
311
|
'upscale_y' => array(
|
312
|
'function' => 'scale',
|
313
|
'arguments' => array(NULL, 40, TRUE),
|
314
|
'width' => 80,
|
315
|
'height' => 40,
|
316
|
'corners' => $default_corners,
|
317
|
),
|
318
|
'crop' => array(
|
319
|
'function' => 'crop',
|
320
|
'arguments' => array(12, 4, 16, 12),
|
321
|
'width' => 16,
|
322
|
'height' => 12,
|
323
|
'corners' => array_fill(0, 4, $this->white),
|
324
|
),
|
325
|
'scale_and_crop' => array(
|
326
|
'function' => 'scale_and_crop',
|
327
|
'arguments' => array(10, 8),
|
328
|
'width' => 10,
|
329
|
'height' => 8,
|
330
|
'corners' => array_fill(0, 4, $this->black),
|
331
|
),
|
332
|
);
|
333
|
|
334
|
// Systems using non-bundled GD2 don't have imagerotate. Test if available.
|
335
|
if (function_exists('imagerotate')) {
|
336
|
$operations += array(
|
337
|
'rotate_5' => array(
|
338
|
'function' => 'rotate',
|
339
|
'arguments' => array(5, 0xFF00FF), // Fuchsia background.
|
340
|
'width' => 42,
|
341
|
'height' => 24,
|
342
|
'corners' => array_fill(0, 4, $this->fuchsia),
|
343
|
),
|
344
|
'rotate_90' => array(
|
345
|
'function' => 'rotate',
|
346
|
'arguments' => array(90, 0xFF00FF), // Fuchsia background.
|
347
|
'width' => 20,
|
348
|
'height' => 40,
|
349
|
'corners' => array($this->fuchsia, $this->red, $this->green, $this->blue),
|
350
|
),
|
351
|
'rotate_transparent_5' => array(
|
352
|
'function' => 'rotate',
|
353
|
'arguments' => array(5),
|
354
|
'width' => 42,
|
355
|
'height' => 24,
|
356
|
'corners' => array_fill(0, 4, $this->transparent),
|
357
|
),
|
358
|
'rotate_transparent_90' => array(
|
359
|
'function' => 'rotate',
|
360
|
'arguments' => array(90),
|
361
|
'width' => 20,
|
362
|
'height' => 40,
|
363
|
'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
|
364
|
),
|
365
|
);
|
366
|
}
|
367
|
|
368
|
// Systems using non-bundled GD2 don't have imagefilter. Test if available.
|
369
|
if (function_exists('imagefilter')) {
|
370
|
$operations += array(
|
371
|
'desaturate' => array(
|
372
|
'function' => 'desaturate',
|
373
|
'arguments' => array(),
|
374
|
'height' => 20,
|
375
|
'width' => 40,
|
376
|
// Grayscale corners are a bit funky. Each of the corners are a shade of
|
377
|
// gray. The values of these were determined simply by looking at the
|
378
|
// final image to see what desaturated colors end up being.
|
379
|
'corners' => array(
|
380
|
array_fill(0, 3, 76) + array(3 => 0),
|
381
|
array_fill(0, 3, 149) + array(3 => 0),
|
382
|
array_fill(0, 3, 29) + array(3 => 0),
|
383
|
array_fill(0, 3, 225) + array(3 => 127)
|
384
|
),
|
385
|
),
|
386
|
);
|
387
|
}
|
388
|
|
389
|
foreach ($files as $file) {
|
390
|
foreach ($operations as $op => $values) {
|
391
|
// Load up a fresh image.
|
392
|
$image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file, 'gd');
|
393
|
if (!$image) {
|
394
|
$this->fail(t('Could not load image %file.', array('%file' => $file)));
|
395
|
continue 2;
|
396
|
}
|
397
|
|
398
|
// All images should be converted to truecolor when loaded.
|
399
|
$image_truecolor = imageistruecolor($image->resource);
|
400
|
$this->assertTrue($image_truecolor, format_string('Image %file after load is a truecolor image.', array('%file' => $file)));
|
401
|
|
402
|
if ($image->info['extension'] == 'gif') {
|
403
|
if ($op == 'desaturate') {
|
404
|
// Transparent GIFs and the imagefilter function don't work together.
|
405
|
$values['corners'][3][3] = 0;
|
406
|
}
|
407
|
}
|
408
|
|
409
|
// Perform our operation.
|
410
|
$function = 'image_' . $values['function'];
|
411
|
$arguments = array();
|
412
|
$arguments[] = &$image;
|
413
|
$arguments = array_merge($arguments, $values['arguments']);
|
414
|
call_user_func_array($function, $arguments);
|
415
|
|
416
|
// To keep from flooding the test with assert values, make a general
|
417
|
// value for whether each group of values fail.
|
418
|
$correct_dimensions_real = TRUE;
|
419
|
$correct_dimensions_object = TRUE;
|
420
|
$correct_colors = TRUE;
|
421
|
|
422
|
// Check the real dimensions of the image first.
|
423
|
if (imagesy($image->resource) != $values['height'] || imagesx($image->resource) != $values['width']) {
|
424
|
$correct_dimensions_real = FALSE;
|
425
|
}
|
426
|
|
427
|
// Check that the image object has an accurate record of the dimensions.
|
428
|
if ($image->info['width'] != $values['width'] || $image->info['height'] != $values['height']) {
|
429
|
$correct_dimensions_object = FALSE;
|
430
|
}
|
431
|
// Now check each of the corners to ensure color correctness.
|
432
|
foreach ($values['corners'] as $key => $corner) {
|
433
|
// Get the location of the corner.
|
434
|
switch ($key) {
|
435
|
case 0:
|
436
|
$x = 0;
|
437
|
$y = 0;
|
438
|
break;
|
439
|
case 1:
|
440
|
$x = $values['width'] - 1;
|
441
|
$y = 0;
|
442
|
break;
|
443
|
case 2:
|
444
|
$x = $values['width'] - 1;
|
445
|
$y = $values['height'] - 1;
|
446
|
break;
|
447
|
case 3:
|
448
|
$x = 0;
|
449
|
$y = $values['height'] - 1;
|
450
|
break;
|
451
|
}
|
452
|
$color = $this->getPixelColor($image, $x, $y);
|
453
|
$correct_colors = $this->colorsAreEqual($color, $corner);
|
454
|
}
|
455
|
|
456
|
$directory = file_default_scheme() . '://imagetests';
|
457
|
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
|
458
|
$file_path = $directory . '/' . $op . '.' . $image->info['extension'];
|
459
|
image_save($image, $file_path);
|
460
|
|
461
|
$this->assertTrue($correct_dimensions_real, format_string('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
|
462
|
$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)));
|
463
|
// JPEG colors will always be messed up due to compression.
|
464
|
if ($image->info['extension'] != 'jpg') {
|
465
|
$this->assertTrue($correct_colors, format_string('Image %file object after %action action has the correct color placement.', array('%file' => $file, '%action' => $op)));
|
466
|
}
|
467
|
}
|
468
|
|
469
|
// Check that saved image reloads without raising PHP errors.
|
470
|
$image_reloaded = image_load($file_path);
|
471
|
}
|
472
|
}
|
473
|
|
474
|
/**
|
475
|
* Tests loading an image whose transparent color index is out of range.
|
476
|
*/
|
477
|
function testTransparentColorOutOfRange() {
|
478
|
// This image was generated by taking an initial image with a palette size
|
479
|
// of 6 colors, and setting the transparent color index to 6 (one higher
|
480
|
// than the largest allowed index), as follows:
|
481
|
// @code
|
482
|
// $image = imagecreatefromgif('modules/simpletest/files/image-test.gif');
|
483
|
// imagecolortransparent($image, 6);
|
484
|
// imagegif($image, 'modules/simpletest/files/image-test-transparent-out-of-range.gif');
|
485
|
// @endcode
|
486
|
// This allows us to test that an image with an out-of-range color index
|
487
|
// can be loaded correctly.
|
488
|
$file = 'image-test-transparent-out-of-range.gif';
|
489
|
$image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file);
|
490
|
|
491
|
if (!$image) {
|
492
|
$this->fail(format_string('Could not load image %file.', array('%file' => $file)));
|
493
|
}
|
494
|
else {
|
495
|
// All images should be converted to truecolor when loaded.
|
496
|
$image_truecolor = imageistruecolor($image->resource);
|
497
|
$this->assertTrue($image_truecolor, format_string('Image %file after load is a truecolor image.', array('%file' => $file)));
|
498
|
}
|
499
|
}
|
500
|
}
|
501
|
|
502
|
/**
|
503
|
* Tests the file move function for managed files.
|
504
|
*/
|
505
|
class ImageFileMoveTest extends ImageToolkitTestCase {
|
506
|
public static function getInfo() {
|
507
|
return array(
|
508
|
'name' => 'Image moving',
|
509
|
'description' => 'Tests the file move function for managed files.',
|
510
|
'group' => 'Image',
|
511
|
);
|
512
|
}
|
513
|
|
514
|
/**
|
515
|
* Tests moving a randomly generated image.
|
516
|
*/
|
517
|
function testNormal() {
|
518
|
// Pick a file for testing.
|
519
|
$file = current($this->drupalGetTestFiles('image'));
|
520
|
|
521
|
// Create derivative image.
|
522
|
$style = image_style_load(key(image_styles()));
|
523
|
$derivative_uri = image_style_path($style['name'], $file->uri);
|
524
|
image_style_create_derivative($style, $file->uri, $derivative_uri);
|
525
|
|
526
|
// Check if derivative image exists.
|
527
|
$this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
|
528
|
|
529
|
// Clone the object so we don't have to worry about the function changing
|
530
|
// our reference copy.
|
531
|
$desired_filepath = 'public://' . $this->randomName();
|
532
|
$result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR);
|
533
|
|
534
|
// Check if image has been moved.
|
535
|
$this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.');
|
536
|
|
537
|
// Check if derivative image has been flushed.
|
538
|
$this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
|
539
|
}
|
540
|
}
|
541
|
|