image.test 17 KB

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