Commit 66e279cb authored by catch's avatar catch

Issue #1591434 by aspilicious: Convert image tests to PSR-0.

parent 0ed0e58c
...@@ -4,5 +4,4 @@ package = Core ...@@ -4,5 +4,4 @@ package = Core
version = VERSION version = VERSION
core = 8.x core = 8.x
dependencies[] = file dependencies[] = file
files[] = image.test
configure = admin/config/media/image-styles configure = admin/config/media/image-styles
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\image\Tests\ImageDimensionsScaleUnitTest.
*/
namespace Drupal\image\Tests;
use Drupal\simpletest\UnitTestBase;
/**
* Tests image_dimensions_scale().
*/
class ImageDimensionsScaleUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'image_dimensions_scale()',
'description' => 'Tests all control flow branches in image_dimensions_scale().',
'group' => 'Image',
);
}
/**
* Tests all control flow branches in image_dimensions_scale().
*/
function testImageDimensionsScale() {
// Define input / output datasets to test different branch conditions.
$test = array();
// Test branch conditions:
// - No height.
// - Upscale, don't need to upscale.
$tests[] = array(
'input' => array(
'dimensions' => array(
'width' => 1000,
'height' => 2000,
),
'width' => 200,
'height' => NULL,
'upscale' => TRUE,
),
'output' => array(
'dimensions' => array(
'width' => 200,
'height' => 400,
),
'return_value' => TRUE,
),
);
// Test branch conditions:
// - No width.
// - Don't upscale, don't need to upscale.
$tests[] = array(
'input' => array(
'dimensions' => array(
'width' => 1000,
'height' => 800,
),
'width' => NULL,
'height' => 140,
'upscale' => FALSE,
),
'output' => array(
'dimensions' => array(
'width' => 175,
'height' => 140,
),
'return_value' => TRUE,
),
);
// Test branch conditions:
// - Source aspect ratio greater than target.
// - Upscale, need to upscale.
$tests[] = array(
'input' => array(
'dimensions' => array(
'width' => 8,
'height' => 20,
),
'width' => 200,
'height' => 140,
'upscale' => TRUE,
),
'output' => array(
'dimensions' => array(
'width' => 56,
'height' => 140,
),
'return_value' => TRUE,
),
);
// Test branch condition: target aspect ratio greater than source.
$tests[] = array(
'input' => array(
'dimensions' => array(
'width' => 2000,
'height' => 800,
),
'width' => 200,
'height' => 140,
'upscale' => FALSE,
),
'output' => array(
'dimensions' => array(
'width' => 200,
'height' => 80,
),
'return_value' => TRUE,
),
);
// Test branch condition: don't upscale, need to upscale.
$tests[] = array(
'input' => array(
'dimensions' => array(
'width' => 100,
'height' => 50,
),
'width' => 200,
'height' => 140,
'upscale' => FALSE,
),
'output' => array(
'dimensions' => array(
'width' => 100,
'height' => 50,
),
'return_value' => FALSE,
),
);
foreach ($tests as $test) {
// Process the test dataset.
$return_value = image_dimensions_scale($test['input']['dimensions'], $test['input']['width'], $test['input']['height'], $test['input']['upscale']);
// Check the width.
$this->assertEqual($test['output']['dimensions']['width'], $test['input']['dimensions']['width'], t('Computed width (@computed_width) equals expected width (@expected_width)', array('@computed_width' => $test['output']['dimensions']['width'], '@expected_width' => $test['input']['dimensions']['width'])));
// Check the height.
$this->assertEqual($test['output']['dimensions']['height'], $test['input']['dimensions']['height'], t('Computed height (@computed_height) equals expected height (@expected_height)', array('@computed_height' => $test['output']['dimensions']['height'], '@expected_height' => $test['input']['dimensions']['height'])));
// Check the return value.
$this->assertEqual($test['output']['return_value'], $return_value, t('Correct return value.'));
}
}
}
<?php
/**
* @file
* Definition of Drupal\image\Tests\ImageDimensionsTest.
*/
namespace Drupal\image\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests that images have correct dimensions when styled.
*/
class ImageDimensionsTest extends WebTestBase {
protected $profile = 'testing';
public static function getInfo() {
return array(
'name' => 'Image dimensions',
'description' => 'Tests that images have correct dimensions when styled.',
'group' => 'Image',
);
}
function setUp() {
parent::setUp(array('image', 'image_module_test'));
}
/**
* Test styled image dimensions cumulatively.
*/
function testImageDimensions() {
// Create a working copy of the file.
$files = $this->drupalGetTestFiles('image');
$file = reset($files);
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
$style = image_style_save(array('name' => 'test'));
$generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
$url = image_style_url('test', $original_uri);
$variables = array(
'style_name' => 'test',
'uri' => $original_uri,
'width' => 40,
'height' => 20,
);
// Verify that the original image matches the hard-coded values.
$image_info = image_get_info($original_uri);
$this->assertEqual($image_info['width'], $variables['width']);
$this->assertEqual($image_info['height'], $variables['height']);
// Scale an image that is wider than it is high.
$effect = array(
'name' => 'image_scale',
'data' => array(
'width' => 120,
'height' => 90,
'upscale' => TRUE,
),
'weight' => 0,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="120" height="60" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 120);
$this->assertEqual($image_info['height'], 60);
// Rotate 90 degrees anticlockwise.
$effect = array(
'name' => 'image_rotate',
'data' => array(
'degrees' => -90,
'random' => FALSE,
),
'weight' => 1,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="60" height="120" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 60);
$this->assertEqual($image_info['height'], 120);
// Scale an image that is higher than it is wide (rotated by previous effect).
$effect = array(
'name' => 'image_scale',
'data' => array(
'width' => 120,
'height' => 90,
'upscale' => TRUE,
),
'weight' => 2,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 45);
$this->assertEqual($image_info['height'], 90);
// Test upscale disabled.
$effect = array(
'name' => 'image_scale',
'data' => array(
'width' => 400,
'height' => 200,
'upscale' => FALSE,
),
'weight' => 3,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 45);
$this->assertEqual($image_info['height'], 90);
// Add a desaturate effect.
$effect = array(
'name' => 'image_desaturate',
'data' => array(),
'weight' => 4,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 45);
$this->assertEqual($image_info['height'], 90);
// Add a random rotate effect.
$effect = array(
'name' => 'image_rotate',
'data' => array(
'degrees' => 180,
'random' => TRUE,
),
'weight' => 5,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
// Add a crop effect.
$effect = array(
'name' => 'image_crop',
'data' => array(
'width' => 30,
'height' => 30,
'anchor' => 'center-center',
),
'weight' => 6,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" width="30" height="30" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
$image_info = image_get_info($generated_uri);
$this->assertEqual($image_info['width'], 30);
$this->assertEqual($image_info['height'], 30);
// Rotate to a non-multiple of 90 degrees.
$effect = array(
'name' => 'image_rotate',
'data' => array(
'degrees' => 57,
'random' => FALSE,
),
'weight' => 7,
);
$effect = image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" alt="" />');
$this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
$this->drupalGet($url);
$this->assertResponse(200, t('Image was generated at the URL.'));
$this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
image_effect_delete('test', $effect);
// Ensure that an effect with no dimensions callback unsets the dimensions.
// This ensures compatibility with 7.0 contrib modules.
$effect = array(
'name' => 'image_module_test_null',
'data' => array(),
'weight' => 8,
);
image_effect_save('test', $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img src="' . $url . '" alt="" />');
}
}
<?php
/**
* @file
* Definition of Drupal\image\Tests\ImageEffectsTest.
*/
namespace Drupal\image\Tests;
use ImageToolkitTestCase;
/**
* Use the image_test.module's mock toolkit to ensure that the effects are
* properly passing parameters to the image toolkit.
*/
class ImageEffectsTest extends ImageToolkitTestCase {
public static function getInfo() {
return array(
'name' => 'Image effects',
'description' => 'Test that the image effects pass parameters to the toolkit correctly.',
'group' => 'Image',
);
}
function setUp() {
parent::setUp('image_test');
module_load_include('inc', 'image', 'image.effects');
}
/**
* Test the image_resize_effect() function.
*/
function testResizeEffect() {
$this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly'));
$this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly'));
}
/**
* Test the image_scale_effect() function.
*/
function testScaleEffect() {
// @todo: need to test upscaling.
$this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly'));
$this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly'));
}
/**
* Test the image_crop_effect() function.
*/
function testCropEffect() {
// @todo should test the keyword offsets.
$this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('crop'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['crop'][0][1], 0, t('X was passed correctly'));
$this->assertEqual($calls['crop'][0][2], 1, t('Y was passed correctly'));
$this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly'));
$this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly'));
}
/**
* Test the image_scale_and_crop_effect() function.
*/
function testScaleAndCropEffect() {
$this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('resize', 'crop'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly'));
$this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly'));
}
/**
* Test the image_desaturate_effect() function.
*/
function testDesaturateEffect() {
$this->assertTrue(image_desaturate_effect($this->image, array()), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('desaturate'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.'));
}
/**
* Test the image_rotate_effect() function.
*/
function testRotateEffect() {
// @todo: need to test with 'random' => TRUE
$this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), t('Function returned the expected value.'));
$this->assertToolkitOperationsCalled(array('rotate'));
// Check the parameters.
$calls = image_test_get_all_calls();
$this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly'));
$this->assertEqual($calls['rotate'][0][2], 0xffffff, t('Background color was passed correctly'));
}
}
<?php
/**
* @file
* Definition of Drupal\image\Tests\ImageFieldDefaultImagesTest.
*/
namespace Drupal\image\Tests;
/**
* Tests default image settings.
*/
class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
public static function getInfo() {
return array(
'name' => 'Image field default images tests',
'description' => 'Tests setting up default images both to the field and field instance.',
'group' => 'Image',
);
}
function setUp() {
parent::setUp(array('field_ui'));
}
/**
* Tests CRUD for fields and fields instances with default images.
*/
function testDefaultImages() {
// Create files to use as the default images.
$files = $this->drupalGetTestFiles('image');
$default_images = array();
foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
$file = entity_create('file', (array) array_pop($files));
$file->save();
$default_images[$image_target] = $file;
}
// Create an image field and add an instance to the article content type.
$field_name = strtolower($this->randomName());
$field_settings = array(
'default_image' => $default_images['field']->fid,
);
$instance_settings = array(
'default_image' => $default_images['instance']->fid,
);
$widget_settings = array(
'preview_image_style' => 'medium',
);
$this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, 'article');
// Add another instance with another default image to the page content type.
$instance2 = array_merge($instance, array(
'bundle' => 'page',
'settings' => array(
'default_image' => $default_images['instance2']->fid,
),
));
field_create_instance($instance2);
$instance2 = field_info_instance('node', $field_name, 'page');
// Confirm the defaults are present on the article field admin form.
$this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
$this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid]"]',
$default_images['field']->fid,
format_string(
'Article image field default equals expected file ID of @fid.',
array('@fid' => $default_images['field']->fid)
)
);
$this->assertFieldByXpath(
'//input[@name="instance[settings][default_image][fid]"]',
$default_images['instance']->fid,
format_string(
'Article image field instance default equals expected file ID of @fid.',
array('@fid' => $default_images['instance']->fid)
)
);
// Confirm the defaults are present on the page field admin form.
$this->drupalGet("admin/structure/types/manage/page/fields/$field_name");
$this->assertFieldByXpath(
'//input[@name="field[settings][default_image][fid]"]',
$default_images['field']->fid,
format_string(
'Page image field default equals expected file ID of @fid.',
array('@fid' => $default_images['field']->fid)
)
);
$this->assertFieldByXpath(
'//input[@name="instance[settings][default_image][fid]"]',
$default_images['instance2']->fid,
format_string(
'Page image field instance default equals expected file ID of @fid.',
array('@fid' => $default_images['instance2']->fid)