You are here

function SmartcropTestCase::testScaleAndCrop in Smart Crop 6

Same name and namespace in other branches
  1. 7 tests/smartcrop.test \SmartcropTestCase::testScaleAndCrop()

Test cropping of images with known properties.

File

tests/smartcrop.test, line 67
Tests for the smartcrop module.

Class

SmartcropTestCase
@file Tests for the smartcrop module.

Code

function testScaleAndCrop() {

  // Create a preset with entropy crop.
  $this
    ->drupalPost('admin/build/imagecache/add', array(
    'presetname' => 'test',
  ), t('Save Preset'));
  $this
    ->clickLink(t('Add !action', array(
    '!action' => 'Scale and Smart Crop',
  )));
  $edit = array(
    'data[width]' => 10,
    'data[height]' => 10,
  );
  $this
    ->drupalPost(NULL, $edit, t('Create Action'));

  // Test cropping with synthetic images.
  $test_files = array(
    'bottom.png',
    'left.png',
    'right.png',
    'top.png',
  );
  $expected_image = imagecreatefrompng(dirname(__FILE__) . '/center.png');
  foreach ($test_files as $file) {

    // Upload a file
    $edit = array(
      'title' => $this
        ->randomName(),
      'files[upload]' => realpath(dirname(__FILE__) . '/' . $file),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $this
      ->drupalGet(file_create_url('imagecache/test/' . $file));
    $this
      ->assertResponse(200, t('Retrieved cropped image.'));
    $actual_image = imagecreatefrompng(file_create_url('imagecache/test/' . $file));
    $this
      ->assertImageEqual($actual_image, $expected_image, 1, t('@file was cropped correctly.', array(
      '@file' => $file,
    )));
  }

  // Test Upscaling.
  $this
    ->drupalGet('admin/build/imagecache');
  $this
    ->clickLink('test');
  $this
    ->clickLink('Configure');
  $edit = array(
    'data[width]' => 100,
    'data[height]' => 100,
    'data[upscale]' => 1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update Action'));
  $file = 'center.png';
  $edit = array(
    'title' => $this
      ->randomName(),
    'files[upload]' => realpath(dirname(__FILE__) . '/' . $file),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->drupalGet(file_create_url('imagecache/test/' . $file));
  $this
    ->assertResponse(200, t('Retrieved cropped image.'));
  $expected_image = imagecreatefrompng(dirname(__FILE__) . '/center_10X.png');
  $actual_image = imagecreatefrompng(file_create_url('imagecache/test/' . $file));
  $this
    ->assertImageEqual($actual_image, $expected_image, 1, t('@file was upscaled correctly.', array(
    '@file' => $file,
  )));

  // Test percentage units.
  $this
    ->drupalGet('admin/build/imagecache');
  $this
    ->clickLink('test');
  $this
    ->clickLink('Configure');
  $edit = array(
    'data[width]' => '200%',
    'data[height]' => '200%',
    'data[upscale]' => 1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update Action'));
  $file = 'center.png';
  $edit = array(
    'title' => $this
      ->randomName(),
    'files[upload]' => realpath(dirname(__FILE__) . '/' . $file),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->drupalGet(file_create_url('imagecache/test/center_0.png'));
  $this
    ->assertResponse(200, t('Retrieved cropped image.'));
  $actual_image = imagecreatefrompng(file_create_url('imagecache/test/center_0.png'));
  $expected_image = imagecreatefrompng(dirname(__FILE__) . '/center_2X.png');
  $this
    ->assertImageEqual($actual_image, $expected_image, 1, t('@file was upscaled correctly.', array(
    '@file' => $file,
  )));

  // Clean up.
  imagedestroy($expected_image);
  imagedestroy($actual_image);
}