- <?php
- * @file
- * Tests for core image handling API.
- */
-
- * Base class for image manipulation testing.
- */
- class ImageToolkitTestCase extends BackdropWebTestCase {
- protected $toolkit;
- protected $file;
- protected $image;
-
- function setUp() {
- $modules = func_get_args();
- if (isset($modules[0]) && is_array($modules[0])) {
- $modules = $modules[0];
- }
- $modules[] = 'image_test';
-
- parent::setUp($modules);
-
-
- $this->toolkit = 'test';
-
-
- $file = current($this->backdropGetTestFiles('image'));
- $this->file = $file->uri;
-
-
-
- $this->image = new stdClass();
- $this->image->source = $this->file;
- $this->image->info = image_get_info($this->file);
- $this->image->toolkit = $this->toolkit;
-
-
- image_test_reset();
- }
-
-
- * Assert that all of the specified image toolkit operations were called
- * exactly once once, other values result in failure.
- *
- * @param $expected
- * Array with string containing with the operation name, e.g. 'load',
- * 'save', 'crop', etc.
- */
- function assertToolkitOperationsCalled(array $expected) {
-
- $actual = array_keys(array_filter(image_test_get_all_calls()));
-
-
- $uncalled = array_diff($expected, $actual);
- if (count($uncalled)) {
- $this->assertTrue(FALSE, format_string('Expected operations %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled))));
- }
- else {
- $this->assertTrue(TRUE, format_string('All the expected operations were called: %expected', array('%expected' => implode(', ', $expected))));
- }
-
-
- $unexpected = array_diff($actual, $expected);
- if (count($unexpected)) {
- $this->assertTrue(FALSE, format_string('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected))));
- }
- else {
- $this->assertTrue(TRUE, 'No unexpected operations were called.');
- }
- }
- }
-
- * Test that the functions in image.inc correctly pass data to the toolkit.
- */
- class ImageToolkitUnitTest extends ImageToolkitTestCase {
-
- * Check that hook_image_toolkits() is called and only available toolkits are
- * returned.
- */
- function testGetAvailableToolkits() {
- $toolkits = image_get_available_toolkits();
- $this->assertTrue(isset($toolkits['test']), 'The working toolkit was returned.');
- $this->assertFalse(isset($toolkits['broken']), 'The toolkit marked unavailable was not returned');
- $this->assertToolkitOperationsCalled(array());
- }
-
-
- * Test the image_load() function.
- */
- function testLoad() {
- $image = image_load($this->file, $this->toolkit);
- $this->assertTrue(is_object($image), 'Returned an object.');
- $this->assertEqual($this->toolkit, $image->toolkit, 'Image had toolkit set.');
- $this->assertToolkitOperationsCalled(array('load', 'get_info'));
- }
-
-
- * Test the image_save() function.
- */
- function testSave() {
- $this->assertFalse(image_save($this->image), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('save'));
- }
-
-
- * Test the image_resize() function.
- */
- function testResize() {
- $this->assertTrue(image_resize($this->image, 1, 2), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly');
- }
-
-
- * Test the image_scale() function.
- */
- function testScale() {
-
- $this->assertTrue(image_scale($this->image, 10, 10), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize'));
-
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 10, 'Width was passed correctly');
- $this->assertEqual($calls['resize'][0][2], 5, 'Height was based off aspect ratio and passed correctly');
- }
-
-
- * Test the image_scale_and_crop() function.
- */
- function testScaleAndCrop() {
- $this->assertTrue(image_scale_and_crop($this->image, 5, 10, 'center-center'), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('resize', 'crop'));
-
-
- $calls = image_test_get_all_calls();
-
- $this->assertEqual($calls['crop'][0][1], 8, 'X was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly');
- $this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly');
- }
-
-
- * Test the image_rotate() function.
- */
- function testRotate() {
- $this->assertTrue(image_rotate($this->image, 90, 1), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('rotate'));
-
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly');
- $this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly');
- }
-
-
- * Test the image_crop() function.
- */
- function testCrop() {
- $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('crop'));
-
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly');
- $this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly');
- $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly');
- $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly');
- }
-
-
- * Test the image_desaturate() function.
- */
- function testDesaturate() {
- $this->assertTrue(image_desaturate($this->image), 'Function returned the expected value.');
- $this->assertToolkitOperationsCalled(array('desaturate'));
-
-
- $calls = image_test_get_all_calls();
- $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.');
- }
- }
-
- * Test the core GD image manipulation functions.
- */
- class ImageToolkitGdTestCase extends BackdropWebTestCase {
-
- protected $black = array(0, 0, 0, 0);
- protected $red = array(255, 0, 0, 0);
- protected $green = array(0, 255, 0, 0);
- protected $blue = array(0, 0, 255, 0);
- protected $yellow = array(255, 255, 0, 0);
- protected $fuchsia = array(255, 0, 255, 0);
- protected $transparent = array(0, 0, 0, 127);
- protected $white = array(255, 255, 255, 0);
-
- protected $width = 40;
- protected $height = 20;
-
- protected function checkRequirements() {
- $errors = parent::checkRequirements();
-
- image_get_available_toolkits();
- if (!function_exists('image_gd_check_settings') || !image_gd_check_settings()) {
- $errors[] = 'Image manipulations for the GD toolkit cannot run because the GD toolkit is not available.';
- }
- return $errors;
- }
-
-
- * Function to compare two colors by RGBa.
- */
- function colorsAreEqual($color_a, $color_b) {
-
- if ($color_a[3] == 127 && $color_b[3] == 127) {
- return TRUE;
- }
-
- foreach ($color_a as $key => $value) {
- if ($color_b[$key] != $value) {
- return FALSE;
- }
- }
-
- return TRUE;
- }
-
-
- * Function for finding a pixel's RGBa values.
- */
- function getPixelColor($image, $x, $y) {
- $color_index = imagecolorat($image->resource, $x, $y);
-
- $transparent_index = imagecolortransparent($image->resource);
- if ($color_index == $transparent_index) {
- return array(0, 0, 0, 127);
- }
-
- return array_values(imagecolorsforindex($image->resource, $color_index));
- }
-
-
- * Since PHP can't visually check that our images have been manipulated
- * properly, build a list of expected color values for each of the corners and
- * the expected height and widths for the final images.
- */
- function testManipulations() {
-
-
- $default_corners = array($this->red, $this->green, $this->blue, $this->transparent);
-
-
- $files = array(
- 'image-test.png',
- 'image-test.gif',
- 'image-test.jpg',
- );
-
-
- $operations = array(
- 'resize' => array(
- 'function' => 'resize',
- 'arguments' => array(20, 10),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'scale_x' => array(
- 'function' => 'scale',
- 'arguments' => array(20, NULL),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'scale_y' => array(
- 'function' => 'scale',
- 'arguments' => array(NULL, 10),
- 'width' => 20,
- 'height' => 10,
- 'corners' => $default_corners,
- ),
- 'upscale_x' => array(
- 'function' => 'scale',
- 'arguments' => array(80, NULL, TRUE),
- 'width' => 80,
- 'height' => 40,
- 'corners' => $default_corners,
- ),
- 'upscale_y' => array(
- 'function' => 'scale',
- 'arguments' => array(NULL, 40, TRUE),
- 'width' => 80,
- 'height' => 40,
- 'corners' => $default_corners,
- ),
- 'crop' => array(
- 'function' => 'crop',
- 'arguments' => array(12, 4, 16, 12),
- 'width' => 16,
- 'height' => 12,
- 'corners' => array_fill(0, 4, $this->white),
- ),
- 'scale_and_crop' => array(
- 'function' => 'scale_and_crop',
- 'arguments' => array(10, 8),
- 'width' => 10,
- 'height' => 8,
- 'corners' => array_fill(0, 4, $this->black),
- ),
- );
-
-
-
-
- if (function_exists('imagerotate') && (version_compare(PHP_VERSION, '7.0.26', '<') || (version_compare(PHP_VERSION, '7.1', '>=') && version_compare(PHP_VERSION, '7.1.12', '<')))) {
- $operations += array(
- 'rotate_5' => array(
- 'function' => 'rotate',
- 'arguments' => array(5, 'FF00FF'),
- 'width' => 41,
- 'height' => 23,
- 'corners' => array_fill(0, 4, $this->fuchsia),
- ),
- 'rotate_90' => array(
- 'function' => 'rotate',
- 'arguments' => array(90, 0xFF00FF),
- 'width' => 20,
- 'height' => 40,
- 'corners' => array($this->fuchsia, $this->red, $this->green, $this->blue),
- ),
- 'rotate_transparent_5' => array(
- 'function' => 'rotate',
- 'arguments' => array(5),
- 'width' => 41,
- 'height' => 23,
- 'corners' => array_fill(0, 4, $this->transparent),
- ),
- 'rotate_transparent_90' => array(
- 'function' => 'rotate',
- 'arguments' => array(90),
- 'width' => 20,
- 'height' => 40,
- 'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
- ),
- );
- }
-
-
- if (function_exists('imagefilter')) {
- $operations += array(
- 'desaturate' => array(
- 'function' => 'desaturate',
- 'arguments' => array(),
- 'height' => 20,
- 'width' => 40,
-
-
-
- 'corners' => array(
- array_fill(0, 3, 76) + array(3 => 0),
- array_fill(0, 3, 149) + array(3 => 0),
- array_fill(0, 3, 29) + array(3 => 0),
- array_fill(0, 3, 0) + array(3 => 127)
- ),
- ),
- );
- }
-
- foreach ($files as $file) {
- foreach ($operations as $op => $values) {
-
- $image = image_load(backdrop_get_path('module', 'simpletest') . '/files/' . $file, 'gd');
- if (!$image) {
- $this->fail(t('Could not load image %file.', array('%file' => $file)));
- continue 2;
- }
-
-
-
- if ($image->info['extension'] == 'gif') {
- if ($op == 'desaturate') {
- $values['corners'][3] = $this->white;
- }
- }
-
-
- $function = 'image_' . $values['function'];
- $arguments = array();
- $arguments[] = &$image;
- $arguments = array_merge($arguments, $values['arguments']);
- call_user_func_array($function, $arguments);
-
-
-
- $correct_dimensions_real = TRUE;
- $correct_dimensions_object = TRUE;
- $correct_colors = TRUE;
-
-
- $width_actual = imagesx($image->resource);
- $height_actual = imagesy($image->resource);
-
-
-
-
-
- if (version_compare(PHP_VERSION, '7.0', '<') && $values['function'] === 'rotate' && $values['arguments'][0] % 90 != 0) {
- $values['width'] = $width_actual;
- $values['height'] = $height_actual;
- }
-
-
- if ($width_actual != $values['width'] || $height_actual != $values['height']) {
- $correct_dimensions_real = FALSE;
- }
-
-
- if ($image->info['width'] != $values['width'] || $image->info['height'] != $values['height']) {
- $correct_dimensions_object = FALSE;
- }
-
- foreach ($values['corners'] as $key => $corner) {
-
- switch ($key) {
- case 0:
- $x = 0;
- $y = 0;
- break;
- case 1:
- $x = $values['width'] - 1;
- $y = 0;
- break;
- case 2:
- $x = $values['width'] - 1;
- $y = $values['height'] - 1;
- break;
- case 3:
- $x = 0;
- $y = $values['height'] - 1;
- break;
- }
- $color = $this->getPixelColor($image, $x, $y);
- $correct_colors = $this->colorsAreEqual($color, $corner);
- }
-
- $directory = file_default_scheme() . '://image_tests';
- file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
- image_save($image, $directory . '/' . $op . '.' . $image->info['extension']);
-
- $this->assertTrue($correct_dimensions_real, format_string('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
- $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)));
-
- if (!in_array($image->info['extension'], array('jpg', 'jpeg'))) {
- $this->assertTrue($correct_colors, format_string('Image %file object after %action action has the correct color placement.', array('%file' => $file, '%action' => $op)));
- }
- }
- }
- }
- }
-
- * Tests the file move function for managed files.
- */
- class ImageFileMoveTest extends ImageToolkitTestCase {
-
- * Tests moving a randomly generated image.
- */
- function testNormal() {
-
- $file = entity_create('file', (array) current($this->backdropGetTestFiles('image')));
-
-
- $style = image_style_load(key(image_styles()));
- $derivative_uri = image_style_path($style['name'], $file->uri);
- image_style_create_derivative($style, $file->uri, $derivative_uri);
-
-
- $this->assertTrue(file_exists($derivative_uri), 'Make sure derivative image is generated successfully.');
-
-
-
- $desired_filepath = 'public://' . $this->randomName();
- $result = file_move(clone $file, $desired_filepath, FILE_EXISTS_ERROR);
-
-
- $this->assertTrue(file_exists($result->uri), 'Make sure image is moved successfully.');
-
-
- $this->assertFalse(file_exists($derivative_uri), 'Make sure derivative image has been flushed.');
- }
- }
-