image.test 17 KB

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