You are here

function ImageFieldValidateTestCase::testResolution in ImageField 6.3

Test resolution settings are checked on upload.

File

tests/imagefield.test, line 165

Class

ImageFieldValidateTestCase
Test class to check for various validations.

Code

function testResolution() {
  $type = $this->node_type;
  $field = $this->field;

  // Get our test file (PNG, 360x240).
  $test_file = $this
    ->getTestFile('image', 64027);
  $test_image_info = image_get_info($test_file->filepath);
  $width = $test_image_info['width'];
  $height = $test_image_info['height'];

  // Set the resolution to a minimum to check on x axis.
  $widget_options = array(
    'min_resolution' => $width + 1 . 'x1',
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw(t('The image is too small; the minimum dimensions are'), t('Image could not be saved when too small horizontally.'));

  // Set the resolution to a minimum to check on y axis.
  $widget_options = array(
    'min_resolution' => '1x' . ($height + 1),
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw('The image is too small; the minimum dimensions are', t('Image could not be saved when too small vertically.'));

  // Remove the minimum dimension add a max dimension on the x axis.
  $widget_options = array(
    'min_resolution' => '',
    'max_resolution' => $width / 4 . 'x40000',
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $nid = $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image resized when too large horizontally.'));

  // Validate the image size.
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $image_info = image_get_info($node_file['filepath']);
  $this
    ->assertTrue($image_info['height'] == $height / 4 && $image_info['width'] == $width / 4, t('Resized image has proper dimensions.'));

  // Add a max dimension on the y axis.
  $widget_options = array(
    'max_resolution' => '40000x' . $height / 2,
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $nid = $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image resized when too large vertically.'));

  // Validate the image size.
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $image_info = image_get_info($node_file['filepath']);
  $this
    ->assertTrue($image_info['height'] == $height / 2 && $image_info['width'] == $width / 2, t('Resized image has proper dimensions.'));

  // Set exact dimensions on the field and make sure the image doesn't get
  // resized and the upload is allowed.
  $widget_options = array(
    'min_resolution' => '360x240',
    'max_resolution' => '360x240',
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $nid = $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertNoRaw('The image was resized to fit within the maximum allowed dimensions', t('Image was not resized when uploaded with exact dimensions.'));

  // Validate the image size.
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $image_info = image_get_info($node_file['filepath']);
  $this
    ->assertTrue($image_info['height'] == $height && $image_info['width'] == $width, t('Resized image has proper dimensions.'));

  // Check that a scaled image will fit between the resolutions.
  $widget_options = array(
    'min_resolution' => '300x200',
    'max_resolution' => '340x220',
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $nid = $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image was resized when fitting between maximum and minimum dimensions.'));

  // Check that an image not fitting between dimensions will not upload.
  $widget_options = array(
    'min_resolution' => '220x360',
    'max_resolution' => '240x360',
  );
  $this
    ->updateFileField($this->field['field_name'], $type->name, array(), $widget_options);
  $nid = $this
    ->uploadNodeFile($test_file, $this->field, $type->name);
  $this
    ->assertRaw('The image will not fit between the dimensions of', t('Image was not uploaded when not fitting between maximum and minimum dimensions.'));
}