1 image.test public ImageDimensionsUnitTest::testImageDimensions()

Test styled image dimensions cumulatively.

File

core/modules/image/tests/image.test, line 1289
Tests for image.module.

Class

ImageDimensionsUnitTest
Tests that images have correct dimensions when styled.

Code

public function testImageDimensions() {
  // Create a working copy of the file.
  $files = $this->backdropGetTestFiles('image');
  $file = reset($files);
  $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);

  // Create a style.
  image_style_save(array('name' => 'test', 'label' => 'Test'));
  $generated_uri = 'public://styles/test/public/' . backdrop_basename($original_uri);
  $url = image_style_url('test', $original_uri);

  $variables = array(
    'style_name' => 'test',
    'uri' => $original_uri,
    'width' => 40,
    'height' => 20,
  );

  // Scale an image that is wider than it is high.
  $effect = array(
    'name' => 'image_scale',
    'data' => array(
      'width' => 120,
      'height' => 90,
      'upscale' => TRUE,
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . check_plain($url) . '" width="120" height="60" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 120, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 60, 'Expected height was found.');

  // Rotate 90 degrees anticlockwise.
  $effect = array(
    'name' => 'image_rotate',
    'data' => array(
      'degrees' => -90,
      'random' => FALSE,
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . check_plain($url) . '" width="60" height="120" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 60, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 120, 'Expected height was found.');

  // Scale an image that is higher than it is wide (rotated by previous effect).
  $effect = array(
    'name' => 'image_scale',
    'data' => array(
      'width' => 120,
      'height' => 90,
      'upscale' => TRUE,
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . check_plain($url) . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 90, 'Expected height was found.');

  // Test upscale disabled.
  $effect = array(
    'name' => 'image_scale',
    'data' => array(
      'width' => 400,
      'height' => 200,
      'upscale' => FALSE,
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . check_plain($url) . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 90, 'Expected height was found.');

  // Add a desaturate effect.
  $effect = array(
    'name' => 'image_desaturate',
    'data' => array(),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . $url . '" width="45" height="90" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 45, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 90, 'Expected height was found.');

  // Add a random rotate effect.
  $effect = array(
    'name' => 'image_rotate',
    'data' => array(
      'degrees' => 180,
      'random' => TRUE,
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . check_plain($url) . '" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');


  // Add a crop effect.
  $effect = array(
    'name' => 'image_crop',
    'data' => array(
      'width' => 30,
      'height' => 30,
      'anchor' => 'center-center',
    ),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . $url . '" width="30" height="30" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $image_info = image_get_info($generated_uri);
  $this->assertEqual($image_info['width'], 30, 'Expected width was found.');
  $this->assertEqual($image_info['height'], 30, 'Expected height was found.');

  // Rotate to a non-multiple of 90 degrees.
  $effect = array(
    'name' => 'image_rotate',
    'data' => array(
      'degrees' => 57,
      'random' => FALSE,
    ),
  );

  $effect = image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . $url . '" alt="" />', 'Expected img tag was found.');
  $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
  $this->backdropGet($url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');

  image_effect_delete('test', $effect);

  // Ensure that an effect with no dimensions callback unsets the dimensions.
  // This ensures compatibility with 7.0 contrib modules.
  $effect = array(
    'name' => 'image_module_test_null',
    'data' => array(),
  );

  image_effect_save('test', $effect);
  $img_tag = theme('image_style', $variables);
  $this->assertEqual($img_tag, '<img src="' . $url . '" alt="" />', 'Expected img tag was found.');
}