You are here

function FileValidatorTest::testFileValidateImageResolution in SimpleTest 7

This ensures the resolution of a specific file is within bounds. The image will be resized if it's too large.

File

tests/file.test, line 389
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileValidatorTest
This will run tests against the file validation functions (file_validate_*).

Code

function testFileValidateImageResolution() {

  // Non-images.
  $errors = file_validate_image_resolution($this->non_image);
  $this
    ->assertEqual(count($errors), 0, t("Shouldn't get any errors for a non-image file."), 'File');
  $errors = file_validate_image_resolution($this->non_image, '50x50', '100x100');
  $this
    ->assertEqual(count($errors), 0, t("Don't check the resolution on non files."), 'File');

  // Minimum size.
  $errors = file_validate_image_resolution($this->image);
  $this
    ->assertEqual(count($errors), 0, t('No errors for an image when there is no minimum or maximum resolution.'), 'File');
  $errors = file_validate_image_resolution($this->image, 0, '200x1');
  $this
    ->assertEqual(count($errors), 1, t("Got an error for an image that wasn't wide enough."), 'File');
  $errors = file_validate_image_resolution($this->image, 0, '1x200');
  $this
    ->assertEqual(count($errors), 1, t("Got an error for an image that wasn't tall enough."), 'File');
  $errors = file_validate_image_resolution($this->image, 0, '200x200');
  $this
    ->assertEqual(count($errors), 1, t('Small images report an error.'), 'File');

  // Maximum size.
  if (image_get_toolkit()) {

    // Copy the image so that the original doesn't get resized.
    $temp_dir = file_directory_path('temporary');
    copy(drupal_realpath('misc/druplicon.png'), drupal_realpath($temp_dir) . '/druplicon.png');
    $this->image->uri = $temp_dir . '/druplicon.png';
    $errors = file_validate_image_resolution($this->image, '10x5');
    $this
      ->assertEqual(count($errors), 0, t('No errors should be reported when an oversized image can be scaled down.'), 'File');
    $info = image_get_info($this->image->uri);
    $this
      ->assertTrue($info['width'] <= 10, t('Image scaled to correct width.'), 'File');
    $this
      ->assertTrue($info['height'] <= 5, t('Image scaled to correct height.'), 'File');
    unlink(drupal_realpath($temp_dir . '/druplicon.png'));
  }
  else {

    // TODO: should check that the error is returned if no toolkit is available.
    $errors = file_validate_image_resolution($this->image, '5x10');
    $this
      ->assertEqual(count($errors), 1, t("Oversize images that can't be scaled get an error."), 'File');
  }
}