1 file.test FileUnitTestCase::testImageDimensions()

Tests storing image height and width as file metadata.

File

core/modules/file/tests/file.test, line 2198
Tests for file.module.

Class

FileUnitTestCase
Tests basic file entity functionality.

Code

function testImageDimensions() {
  // Test hook_file_insert().
  $file = current($this->backdropGetTestFiles('image'));
  $image_file = new File((array) $file);
  file_save($image_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->backdropGetTestFiles('text'));
  $text_file = new File((array) $file);
  file_save($text_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.
  $this->assertTrue(db_query('SELECT COUNT(*) FROM {file_metadata} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField(), 'Row exists in {file_metadata} before file_delete().');
  file_delete($file->fid);
  $this->assertFalse(db_query('SELECT COUNT(*) FROM {file_metadata} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField(), 'Row deleted in {file_metadata} on file_delete().');
}