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
|
if (!function_exists('image_gd_check_settings') || !image_gd_check_settings()) {
|
265
|
$this->pass(t('Image manipulations for the GD toolkit were skipped because the GD toolkit is not available.'));
|
266
|
return;
|
267
|
}
|
268
|
|
269
|
// Typically the corner colors will be unchanged. These colors are in the
|
270
|
// order of top-left, top-right, bottom-right, bottom-left.
|
271
|
$default_corners = array($this->red, $this->green, $this->blue, $this->transparent);
|
272
|
|
273
|
// A list of files that will be tested.
|
274
|
$files = array(
|
275
|
'image-test.png',
|
276
|
'image-test.gif',
|
277
|
'image-test.jpg',
|
278
|
);
|
279
|
|
280
|
// Setup a list of tests to perform on each type.
|
281
|
$operations = array(
|
282
|
'resize' => array(
|
283
|
'function' => 'resize',
|
284
|
'arguments' => array(20, 10),
|
285
|
'width' => 20,
|
286
|
'height' => 10,
|
287
|
'corners' => $default_corners,
|
288
|
),
|
289
|
'scale_x' => array(
|
290
|
'function' => 'scale',
|
291
|
'arguments' => array(20, NULL),
|
292
|
'width' => 20,
|
293
|
'height' => 10,
|
294
|
'corners' => $default_corners,
|
295
|
),
|
296
|
'scale_y' => array(
|
297
|
'function' => 'scale',
|
298
|
'arguments' => array(NULL, 10),
|
299
|
'width' => 20,
|
300
|
'height' => 10,
|
301
|
'corners' => $default_corners,
|
302
|
),
|
303
|
'upscale_x' => array(
|
304
|
'function' => 'scale',
|
305
|
'arguments' => array(80, NULL, TRUE),
|
306
|
'width' => 80,
|
307
|
'height' => 40,
|
308
|
'corners' => $default_corners,
|
309
|
),
|
310
|
'upscale_y' => array(
|
311
|
'function' => 'scale',
|
312
|
'arguments' => array(NULL, 40, TRUE),
|
313
|
'width' => 80,
|
314
|
'height' => 40,
|
315
|
'corners' => $default_corners,
|
316
|
),
|
317
|
'crop' => array(
|
318
|
'function' => 'crop',
|
319
|
'arguments' => array(12, 4, 16, 12),
|
320
|
'width' => 16,
|
321
|
'height' => 12,
|
322
|
'corners' => array_fill(0, 4, $this->white),
|
323
|
),
|
324
|
'scale_and_crop' => array(
|
325
|
'function' => 'scale_and_crop',
|
326
|
'arguments' => array(10, 8),
|
327
|
'width' => 10,
|
328
|
'height' => 8,
|
329
|
'corners' => array_fill(0, 4, $this->black),
|
330
|
),
|
331
|
);
|
332
|
|
333
|
// Systems using non-bundled GD2 don't have imagerotate. Test if available.
|
334
|
if (function_exists('imagerotate')) {
|
335
|
$operations += array(
|
336
|
'rotate_5' => array(
|
337
|
'function' => 'rotate',
|
338
|
'arguments' => array(5, 0xFF00FF), // Fuchsia background.
|
339
|
'width' => 42,
|
340
|
'height' => 24,
|
341
|
'corners' => array_fill(0, 4, $this->fuchsia),
|
342
|
),
|
343
|
'rotate_90' => array(
|
344
|
'function' => 'rotate',
|
345
|
'arguments' => array(90, 0xFF00FF), // Fuchsia background.
|
346
|
'width' => 20,
|
347
|
'height' => 40,
|
348
|
'corners' => array($this->fuchsia, $this->red, $this->green, $this->blue),
|
349
|
),
|
350
|
'rotate_transparent_5' => array(
|
351
|
'function' => 'rotate',
|
352
|
'arguments' => array(5),
|
353
|
'width' => 42,
|
354
|
'height' => 24,
|
355
|
'corners' => array_fill(0, 4, $this->transparent),
|
356
|
),
|
357
|
'rotate_transparent_90' => array(
|
358
|
'function' => 'rotate',
|
359
|
'arguments' => array(90),
|
360
|
'width' => 20,
|
361
|
'height' => 40,
|
362
|
'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
|
363
|
),
|
364
|
);
|
365
|
}
|
366
|
|
367
|
// Systems using non-bundled GD2 don't have imagefilter. Test if available.
|
368
|
if (function_exists('imagefilter')) {
|
369
|
$operations += array(
|
370
|
'desaturate' => array(
|
371
|
'function' => 'desaturate',
|
372
|
'arguments' => array(),
|
373
|
'height' => 20,
|
374
|
'width' => 40,
|
375
|
// Grayscale corners are a bit funky. Each of the corners are a shade of
|
376
|
// gray. The values of these were determined simply by looking at the
|
377
|
// final image to see what desaturated colors end up being.
|
378
|
'corners' => array(
|
379
|
array_fill(0, 3, 76) + array(3 => 0),
|
380
|
array_fill(0, 3, 149) + array(3 => 0),
|
381
|
array_fill(0, 3, 29) + array(3 => 0),
|
382
|
array_fill(0, 3, 0) + array(3 => 127)
|
383
|
),
|
384
|
),
|
385
|
);
|
386
|
}
|
387
|
|
388
|
foreach ($files as $file) {
|
389
|
foreach ($operations as $op => $values) {
|
390
|
// Load up a fresh image.
|
391
|
$image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file, 'gd');
|
392
|
if (!$image) {
|
393
|
$this->fail(t('Could not load image %file.', array('%file' => $file)));
|
394
|
continue 2;
|
395
|
}
|
396
|
|
397
|
// Transparent GIFs and the imagefilter function don't work together.
|
398
|
// There is a todo in image.gd.inc to correct this.
|
399
|
if ($image->info['extension'] == 'gif') {
|
400
|
if ($op == 'desaturate') {
|
401
|
$values['corners'][3] = $this->white;
|
402
|
}
|
403
|
}
|
404
|
|
405
|
// Perform our operation.
|
406
|
$function = 'image_' . $values['function'];
|
407
|
$arguments = array();
|
408
|
$arguments[] = &$image;
|
409
|
$arguments = array_merge($arguments, $values['arguments']);
|
410
|
call_user_func_array($function, $arguments);
|
411
|
|
412
|
// To keep from flooding the test with assert values, make a general
|
413
|
// value for whether each group of values fail.
|
414
|
$correct_dimensions_real = TRUE;
|
415
|
$correct_dimensions_object = TRUE;
|
416
|
$correct_colors = TRUE;
|
417
|
|
418
|
// Check the real dimensions of the image first.
|
419
|
if (imagesy($image->resource) != $values['height'] || imagesx($image->resource) != $values['width']) {
|
420
|
$correct_dimensions_real = FALSE;
|
421
|
}
|
422
|
|
423
|
// Check that the image object has an accurate record of the dimensions.
|
424
|
if ($image->info['width'] != $values['width'] || $image->info['height'] != $values['height']) {
|
425
|
$correct_dimensions_object = FALSE;
|
426
|
}
|
427
|
// Now check each of the corners to ensure color correctness.
|
428
|
foreach ($values['corners'] as $key => $corner) {
|
429
|
// Get the location of the corner.
|
430
|
switch ($key) {
|
431
|
case 0:
|
432
|
$x = 0;
|
433
|
$y = 0;
|
434
|
break;
|
435
|
case 1:
|
436
|
$x = $values['width'] - 1;
|
437
|
$y = 0;
|
438
|
break;
|
439
|
case 2:
|
440
|
$x = $values['width'] - 1;
|
441
|
$y = $values['height'] - 1;
|
442
|
break;
|
443
|
case 3:
|
444
|
$x = 0;
|
445
|
$y = $values['height'] - 1;
|
446
|
break;
|
447
|
}
|
448
|
$color = $this->getPixelColor($image, $x, $y);
|
449
|
$correct_colors = $this->colorsAreEqual($color, $corner);
|
450
|
}
|
451
|
|
452
|
$directory = file_default_scheme() . '://imagetests';
|
453
|
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
|
454
|
image_save($image, $directory . '/' . $op . '.' . $image->info['extension']);
|
455
|
|
456
|
$this->assertTrue($correct_dimensions_real, format_string('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
|
457
|
$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)));
|
458
|
// JPEG colors will always be messed up due to compression.
|
459
|
if ($image->info['extension'] != 'jpg') {
|
460
|
$this->assertTrue($correct_colors, format_string('Image %file object after %action action has the correct color placement.', array('%file' => $file, '%action' => $op)));
|
461
|
}
|
462
|
}
|
463
|
}
|
464
|
|
465
|
}
|
466
|
}
|
467
|
|
468
|
/**
|
469
|
* Tests the file move function for managed files.
|
470
|
*/
|
471
|
class ImageFileMoveTest extends ImageToolkitTestCase {
|
472
|
public static function getInfo() {
|
473
|
return array(
|
474
|
'name' => 'Image moving',
|
475
|
'description' => 'Tests the file move function for managed files.',
|
476
|
'group' => 'Image',
|
477
|
);
|
478
|
}
|
479
|
|
480
|
/**
|
481
|
* Tests moving a randomly generated image.
|
482
|
*/
|
483
|
function testNormal() {
|
484
|
// Pick a file for testing.
|
485
|
$file = current($this->drupalGetTestFiles('image'));
|
486
|
|
487
|
// Create derivative image.
|
488
|
$style = image_style_load(key(image_styles()));
|
489
|
$derivative_uri = image_style_path($style['name'], $file->uri);
|
490
|
image_style_create_derivative($style, $file->uri, $derivative_uri);
|
491
|
|
492
|
// Check if derivative image exists.
|
493
|
$this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
|
494
|
|
495
|
// Clone the object so we don't have to worry about the function changing
|
496
|
// our reference copy.
|
497
|
$desired_filepath = 'public://' . $this->randomName();
|
498
|
$result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR);
|
499
|
|
500
|
// Check if image has been moved.
|
501
|
$this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.');
|
502
|
|
503
|
// Check if derivative image has been flushed.
|
504
|
$this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
|
505
|
}
|
506
|
}
|
507
|
|