You are here

function SmartcropTestCase::testScaleAndCrop in Smart Crop 7

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

Test cropping of images with known properties.

File

tests/smartcrop.test, line 64
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/config/media/image-styles/add', array(
    'name' => 'test',
  ), t('Create new style'));
  $this
    ->drupalPost(NULL, array(
    'new' => 'smartcrop_scale_and_crop',
  ), t('Add'));
  $edit = array(
    'data[width]' => 10,
    'data[height]' => 10,
  );
  $this
    ->drupalPost(NULL, $edit, t('Add effect'));

  // Create an image field that uses the new style.
  $instance = $this
    ->createImageField('smartcrop_image', 'page');
  $instance['display']['default']['type'] = 'image';
  $instance['display']['default']['settings']['image_style'] = 'test';
  field_update_instance($instance);

  // 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[smartcrop_image_und_0]' => realpath(dirname(__FILE__) . '/' . $file),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($edit['title']);
    $image_uri = $node->smartcrop_image[LANGUAGE_NONE][0]['uri'];
    $this
      ->drupalGet(image_style_url('test', $image_uri));
    $this
      ->assertResponse(200, t('Retrieved cropped image.'));
    $actual_image = imagecreatefrompng(image_style_url('test', $image_uri));
    $this
      ->assertImageEqual($actual_image, $expected_image, 1, t('@file was cropped correctly.', array(
      '@file' => $file,
    )));
  }

  // Test Upscaling.
  $this
    ->drupalGet('admin/config/media/image-styles/edit/test');
  $this
    ->clickLink('edit');
  $edit = array(
    'data[width]' => 100,
    'data[height]' => 100,
    'data[upscale]' => 1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update effect'));
  $file = 'center.png';
  $edit = array(
    'title' => $this
      ->randomName(),
    'files[smartcrop_image_und_0]' => realpath(dirname(__FILE__) . '/' . $file),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $image_uri = $node->smartcrop_image[LANGUAGE_NONE][0]['uri'];
  $this
    ->drupalGet(image_style_url('test', $image_uri));
  $this
    ->assertResponse(200, t('Retrieved cropped image.'));
  $expected_image = imagecreatefrompng(dirname(__FILE__) . '/center_10X.png');
  $actual_image = imagecreatefrompng(image_style_url('test', $image_uri));
  $this
    ->assertImageEqual($actual_image, $expected_image, 1, t('@file was upscaled correctly.', array(
    '@file' => $file,
  )));

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