summaryrefslogtreecommitdiff
path: root/modules/image
diff options
context:
space:
mode:
Diffstat (limited to 'modules/image')
-rw-r--r--modules/image/image.field.inc13
-rw-r--r--modules/image/image.module6
-rw-r--r--modules/image/image.test106
-rw-r--r--modules/image/tests/image_module_test.module11
4 files changed, 121 insertions, 15 deletions
diff --git a/modules/image/image.field.inc b/modules/image/image.field.inc
index 1be15839c..60c0f5ac0 100644
--- a/modules/image/image.field.inc
+++ b/modules/image/image.field.inc
@@ -600,9 +600,12 @@ function theme_image_formatter($variables) {
$item = $variables['item'];
$image = array(
'path' => $item['uri'],
- 'alt' => $item['alt'],
);
+ if (array_key_exists('alt', $item)) {
+ $image['alt'] = $item['alt'];
+ }
+
if (isset($item['attributes'])) {
$image['attributes'] = $item['attributes'];
}
@@ -613,7 +616,7 @@ function theme_image_formatter($variables) {
}
// Do not output an empty 'title' attribute.
- if (drupal_strlen($item['title']) > 0) {
+ if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
$image['title'] = $item['title'];
}
@@ -625,9 +628,11 @@ function theme_image_formatter($variables) {
$output = theme('image', $image);
}
- if (!empty($variables['path']['path'])) {
+ // The link path and link options are both optional, but for the options to be
+ // processed, the link path must at least be an empty string.
+ if (isset($variables['path']['path'])) {
$path = $variables['path']['path'];
- $options = $variables['path']['options'];
+ $options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
// When displaying an image inside a link, the html option must be TRUE.
$options['html'] = TRUE;
$output = l($output, $path, $options);
diff --git a/modules/image/image.module b/modules/image/image.module
index a9c4d027c..ff50452d5 100644
--- a/modules/image/image.module
+++ b/modules/image/image.module
@@ -34,7 +34,7 @@ define('IMAGE_STORAGE_MODULE', IMAGE_STORAGE_OVERRIDE | IMAGE_STORAGE_DEFAULT);
require_once DRUPAL_ROOT . '/modules/image/image.field.inc';
/**
- * Implement of hook_help().
+ * Implements hook_help().
*/
function image_help($path, $arg) {
switch ($path) {
@@ -1054,7 +1054,7 @@ function image_effect_definitions() {
$effects = &drupal_static(__FUNCTION__);
if (!isset($effects)) {
- if ($cache = cache_get("image_effects:$langcode") && !empty($cache->data)) {
+ if ($cache = cache_get("image_effects:$langcode")) {
$effects = $cache->data;
}
else {
@@ -1262,7 +1262,7 @@ function theme_image_style($variables) {
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
- // Determine the url for the styled image.
+ // Determine the URL for the styled image.
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
return theme('image', $variables);
}
diff --git a/modules/image/image.test b/modules/image/image.test
index e4b6d373a..1ca846506 100644
--- a/modules/image/image.test
+++ b/modules/image/image.test
@@ -183,7 +183,7 @@ class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase {
// Create a working copy of the file.
$files = $this->drupalGetTestFiles('image');
- $file = reset($files);
+ $file = array_shift($files);
$image_info = image_get_info($file->uri);
$original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
// Let the image_module_test module know about this file, so it can claim
@@ -212,18 +212,30 @@ class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase {
$this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', t('Cache-Control header was set to prevent caching.'));
$this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', t('Expected custom header has been added.'));
- // Verify access is denied to private image styles.
- $this->drupalLogout();
+
+ // Make sure that a second request to the already existing derivate works
+ // too.
$this->drupalGet($generate_url);
+ $this->assertResponse(200, t('Image was generated at the URL.'));
+
+ // Repeat this with a different file that we do not have access to and
+ // make sure that access is denied.
+ $file_noaccess = array_shift($files);
+ $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
+ $generated_uri_noaccess = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri_noaccess);
+ $this->assertFalse(file_exists($generated_uri_noaccess), t('Generated file does not exist.'));
+ $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);
+
+ $this->drupalGet($generate_url_noaccess);
$this->assertResponse(403, t('Confirmed that access is denied for the private image style.') );
// Verify that images are not appended to the response. Currently this test only uses PNG images.
if (strpos($generate_url, '.png') === FALSE ) {
- $this->fail( t('Confirming that private image styles are not appended require PNG file.') );
+ $this->fail('Confirming that private image styles are not appended require PNG file.');
}
else {
// Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the
// response body.
- $this->assertNoRaw( chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), t('No PNG signature found in the response body.') );
+ $this->assertNoRaw( chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
}
}
}
@@ -243,7 +255,7 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
}
function setUp() {
- parent::setUp('image_test');
+ parent::setUp('image_module_test');
module_load_include('inc', 'image', 'image.effects');
}
@@ -330,6 +342,25 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
$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'));
}
+
+ /**
+ * Test image effect caching.
+ */
+ function testImageEffectsCaching() {
+ $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
+
+ // First call should grab a fresh copy of the data.
+ $effects = image_effect_definitions();
+ $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
+
+ // Second call should come from cache.
+ drupal_static_reset('image_effect_definitions');
+ drupal_static_reset('image_module_test_image_effect_info_alter');
+ $cached_effects = image_effect_definitions();
+ $this->assertTrue(is_null($image_effect_definitions_called), 'image_effect_definitions() returned data from cache.');
+
+ $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
+ }
}
/**
@@ -717,7 +748,7 @@ class ImageFieldDisplayTestCase extends ImageFieldTestCase {
if ($scheme == 'private') {
// Only verify HTTP headers when using private scheme and the headers are
// sent by Drupal.
- $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png; name="' . $test_image->filename . '"', t('Content-Type header was sent.'));
+ $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', t('Content-Type header was sent.'));
$this->assertEqual($this->drupalGetHeader('Content-Disposition'), 'inline; filename="' . $test_image->filename . '"', t('Content-Disposition header was sent.'));
$this->assertEqual($this->drupalGetHeader('Cache-Control'), 'private', t('Cache-Control header was sent.'));
@@ -1566,3 +1597,64 @@ class ImageFieldDefaultImagesTestCase extends ImageFieldTestCase {
}
}
+
+/**
+ * Tests image theme functions.
+ */
+class ImageThemeFunctionWebTestCase extends DrupalWebTestCase {
+
+ public static function getInfo() {
+ return array(
+ 'name' => 'Image theme functions',
+ 'description' => 'Test that the image theme functions work correctly.',
+ 'group' => 'Image',
+ );
+ }
+
+ function setUp() {
+ parent::setUp(array('image'));
+ }
+
+ /**
+ * Tests usage of the image field formatters.
+ */
+ function testImageFormatterTheme() {
+ // Create an image.
+ $files = $this->drupalGetTestFiles('image');
+ $file = reset($files);
+ $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
+
+ // Create a style.
+ image_style_save(array('name' => 'test'));
+ $url = image_style_url('test', $original_uri);
+
+ // Test using theme_image_formatter() without an image title, alt text, or
+ // link options.
+ $path = $this->randomName();
+ $element = array(
+ '#theme' => 'image_formatter',
+ '#image_style' => 'test',
+ '#item' => array(
+ 'uri' => $original_uri,
+ ),
+ '#path' => array(
+ 'path' => $path,
+ ),
+ );
+ $rendered_element = render($element);
+ $expected_result = '<a href="' . url($path) . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
+ $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
+
+ // Link the image to a fragment on the page, and not a full URL.
+ $fragment = $this->randomName();
+ $element['#path']['path'] = '';
+ $element['#path']['options'] = array(
+ 'external' => TRUE,
+ 'fragment' => $fragment,
+ );
+ $rendered_element = render($element);
+ $expected_result = '<a href="#' . $fragment . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
+ $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
+ }
+
+}
diff --git a/modules/image/tests/image_module_test.module b/modules/image/tests/image_module_test.module
index 766a9d957..8a322fb97 100644
--- a/modules/image/tests/image_module_test.module
+++ b/modules/image/tests/image_module_test.module
@@ -9,7 +9,6 @@ function image_module_test_file_download($uri) {
if (variable_get('image_module_test_file_download', FALSE) == $uri) {
return array('X-Image-Owned-By' => 'image_module_test');
}
- return -1;
}
/**
@@ -39,3 +38,13 @@ function image_module_test_image_effect_info() {
function image_module_test_null_effect(array &$image, array $data) {
return TRUE;
}
+
+/**
+ * Implements hook_image_effect_info_alter().
+ *
+ * Used to keep a count of cache misses in image_effect_definitions().
+ */
+function image_module_test_image_effect_info_alter(&$effects) {
+ $image_effects_definition_called = &drupal_static(__FUNCTION__, 0);
+ $image_effects_definition_called++;
+}