image.test 19 KB

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