You are here

function FileEntityUnitTestCase::testImageDimensions in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.test \FileEntityUnitTestCase::testImageDimensions()

Tests storing image height and width as file metadata.

File

./file_entity.test, line 251
Test integration for the file_entity module.

Class

FileEntityUnitTestCase
Tests basic file entity functionality.

Code

function testImageDimensions() {

  // Test hook_file_insert().
  $file = current($this
    ->drupalGetTestFiles('image'));
  $image_file = file_save($file);
  $this
    ->assertTrue(isset($image_file->metadata['height']), 'Image height retrieved on file_save() for an image file.');
  $this
    ->assertTrue(isset($image_file->metadata['width']), 'Image width retrieved on file_save() for an image file.');
  $file = current($this
    ->drupalGetTestFiles('text'));
  $text_file = file_save($file);
  $this
    ->assertFalse(isset($text_file->metadata['height']), 'No image height retrieved on file_save() for an text file.');
  $this
    ->assertFalse(isset($text_file->metadata['width']), 'No image width retrieved on file_save() for an text file.');

  // Test hook_file_load().
  // Clear the cache and load fresh files objects to test file_load behavior.
  entity_get_controller('file')
    ->resetCache();
  $file = file_load($image_file->fid);
  $this
    ->assertTrue(isset($file->metadata['height']), 'Image dimensions retrieved on file_load() for an image file.');
  $this
    ->assertTrue(isset($file->metadata['width']), 'Image dimensions retrieved on file_load() for an image file.');
  $this
    ->assertEqual($file->metadata['height'], $image_file->metadata['height'], 'Loaded image height is equal to saved image height.');
  $this
    ->assertEqual($file->metadata['width'], $image_file->metadata['width'], 'Loaded image width is equal to saved image width.');
  $file = file_load($text_file->fid);
  $this
    ->assertFalse(isset($file->metadata['height']), 'No image height retrieved on file_load() for an text file.');
  $this
    ->assertFalse(isset($file->metadata['width']), 'No image width retrieved on file_load() for an text file.');

  // Test hook_file_update().
  // Load the first image file and resize it.
  $height = $image_file->metadata['width'] / 2;
  $width = $image_file->metadata['height'] / 2;
  $image = image_load($image_file->uri);
  image_resize($image, $width, $height);
  image_save($image);
  file_save($image_file);
  $this
    ->assertEqual($image_file->metadata['height'], $height, 'Image file height updated by file_save().');
  $this
    ->assertEqual($image_file->metadata['width'], $width, 'Image file width updated by file_save().');

  // Clear the cache and reload the file.
  entity_get_controller('file')
    ->resetCache();
  $file = file_load($image_file->fid);
  $this
    ->assertEqual($file->metadata['height'], $height, 'Updated image height retrieved by file_load().');
  $this
    ->assertEqual($file->metadata['width'], $width, 'Updated image width retrieved by file_load().');

  // Verify that the image dimension metadata is removed on file deletion.
  file_delete($file, TRUE);
  $this
    ->assertFalse(db_query('SELECT COUNT(*) FROM {file_metadata} WHERE fid = :fid', array(
    ':fid' => 'fid',
  ))
    ->fetchField(), 'Row deleted in {file_metadata} on file_delete().');
}