You are here

imagecache_external.test in Imagecache External 7.2

Same filename and directory in other branches
  1. 8 imagecache_external.test

Tests for Imagecache External.

File

imagecache_external.test
View source
<?php

/**
 * @file
 * Tests for Imagecache External.
 */

/**
 * Tests the functions for working with public/private file schemes.
 */
class ImagecacheExternalTestCase extends DrupalWebTestCase {
  protected $external_image;
  protected $admin_user;
  protected $web_user;
  public static function getInfo() {
    return array(
      'name' => 'Imagecache External Tests',
      'description' => 'Test Imagecache External.',
      'group' => 'Imagecache External',
    );
  }
  function setUp() {
    parent::setUp(array(
      'imagecache_external',
      'text',
    ));
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'access content',
      'access administration pages',
      'administer site configuration',
      'administer content types',
      'administer nodes',
      'create article content',
      'edit any article content',
      'delete any article content',
      'administer image styles',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // // Login as a normal user.
    // $user = $this->drupalCreateUser(array('access content'));
    // $this->drupalLogin($user);
    // Set an external image to use for testing. We specifically pick one from
    // Simpletest that is larger than the 'thumbnail' style tested.
    foreach ($this
      ->drupalGetTestFiles('image') as $file) {
      $info = image_get_info($file->uri);
      if ($info['width'] > 100 || $info['height'] > 100) {
        $this->external_image = array(
          'uri' => $file->uri,
          'url' => file_create_url($file->uri),
          'height' => $info['height'],
          'width' => $info['width'],
        );
        break;
      }
    }
  }

  /**
   * Create a new text field to store test URL's.
   *
   * @param $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param $type_name
   *   The node type that this field will be added to.
   * @param $field_settings
   *   A list of field settings that will be added to the defaults.
   * @param $instance_settings
   *   A list of instance settings that will be added to the instance defaults.
   * @param $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   */
  function createTextField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
    $field = array(
      'field_name' => $name,
      'type' => 'text',
      'settings' => array(),
      'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
    );
    $field['settings'] = array_merge($field['settings'], $field_settings);
    field_create_field($field);
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'node',
      'label' => $name,
      'bundle' => $type_name,
      'required' => !empty($instance_settings['required']),
      'settings' => array(),
      'widget' => array(
        'type' => 'text_textfield',
        'settings' => array(),
      ),
    );
    $instance['settings'] = array_merge($instance['settings'], $instance_settings);
    $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
    return field_create_instance($instance);
  }

  /**
   * Set a to a node.
   *
   * @param $image
   *   A file object representing the image to upload.
   * @param $field_name
   *   Name of the image field the image should be attached to.
   * @param $type
   *   The type of node to create.
   */
  function addNodeImage($image_uri, $field_name, $type) {
    $edit = array(
      'title' => $this
        ->randomName(),
      $field_name . '[' . LANGUAGE_NONE . '][0][value]' => $image_uri,
    );
    $this
      ->drupalPost('node/add/' . $type, $edit, t('Save'));

    // Retrieve ID of the newly created node from the current URL.
    $matches = array();
    preg_match('/node\\/([0-9]+)/', $this
      ->getUrl(), $matches);
    return isset($matches[1]) ? $matches[1] : FALSE;
  }

  /**
   * Test caching an external image.
   */
  function testCachingExternalImage() {

    // Create a text field that uses the new style for rendering the image.
    $field_name = strtolower($this
      ->randomName(10));
    $this
      ->createTextField($field_name, 'article');
    $instance = field_info_instance('node', $field_name, 'article');
    $instance['display']['default']['type'] = 'imagecache_external_image';
    $instance['display']['default']['settings']['imagecache_external_style'] = 'thumbnail';
    field_update_instance($instance);

    // Create a new node with an image attached.
    $nid = $this
      ->addNodeImage($this->external_image['url'], $field_name, 'article');
    if ($this->external_image['width'] > $this->external_image['height']) {
      $width = 100;
      $height = ceil(100 * ($this->external_image['height'] / $this->external_image['width']));
    }
    else {
      $height = 100;
      $width = ceil(100 * ($this->external_image['width'] / $this->external_image['height']));
    }

    // Test that image is displayed using newly created style.
    $this
      ->drupalGet('node/' . $nid);
    $img_tag = theme('imagecache_external', array(
      'style_name' => 'thumbnail',
      'path' => $this->external_image['url'],
      'width' => $width,
      'height' => $height,
    ));
    $this
      ->assertRaw($img_tag, t('Image was successfully cached as %path.', array(
      '%path' => $img_tag,
    )));
  }

  /**
   * Test caching an external image with style using the public:// scheme.
   */
  function testCachingExternalImageWithImageStylePublic() {
    $this
      ->_testCachingExternalImageWithImageStyle('public');
  }

  /**
   * Test caching an external image with style using the private:// scheme.
   */
  function testCachingExternalImageWithImageStylePrivate() {
    $this
      ->_testCachingExternalImageWithImageStyle('private');
  }

  /**
   * Test caching an external image with an image style.
   */
  function _testCachingExternalImageWithImageStyle($scheme) {
    $style = image_style_save(array(
      'name' => rand(),
    ));
    variable_set('file_default_scheme', $scheme);
    $local_image_uri = imagecache_external_generate_path($this->external_image['url']);
    $local_image_url = image_style_url($style['name'], $local_image_uri);

    // Check if we can access the generated image.
    $this
      ->drupalGet($local_image_url);
    $this
      ->assertResponse(200, t('Image can be reached on %url', array(
      '%url' => $local_image_url,
    )));

    // // Check if the theme function works as expected.
    // $img_tag = theme_imagecache_external(array(
    //   'style_name' => $style['name'],
    //   'path' => $this->external_image['url'],
    //   'alt' => 'Drupal logo',
    //   'width' => 20,
    //   'height' => 20,
    // ));
    // $expected_tag = '<img typeof="foaf:Image" src="' . check_plain($local_image_url) . '" width="20" height="20" alt="Drupal logo" />';
    // $this->assertEqual($img_tag, $expected_tag, t('Expected img tag %tag was found on the %scheme filesystem.', array(
    //   '%tag' => $expected_tag,
    //   '%scheme' => $scheme,
    // )));
  }

  /**
   * Test caching an external image using the whitelist.
   */
  function testCachingExternalImageUsingWhitelist() {
    global $base_url;
    variable_set('imagecache_external_option', 'white');
    variable_set('imagecache_external_hosts', parse_url($base_url, PHP_URL_HOST));
    $this
      ->testCachingExternalImage();
  }

  /**
   * Test caching an external image using the managed file system.
   */
  function testCachingExternalImageUsingManagedFileSystem() {
    variable_set('imagecache_external_management', 'managed');
    if ($path = imagecache_external_generate_path($this->external_image['url'])) {
      $query = db_select('file_managed', 'f')
        ->fields('f', array(
        'fid',
      ))
        ->condition('uri', $path)
        ->execute()
        ->fetchCol();
      $fid = array_shift($query);
      $this
        ->assertTrue(!empty($fid), t('Image %original was cached in the managed filesystem with file ID %fid.', array(
        '%fid' => $fid,
        '%original' => $this->external_image['url'],
      )));
    }
  }

  /**
   * Test the behaviour of imagecache_external_validate_host().
   */
  public function testCachingExternalWhitelistValidation() {
    variable_set('imagecache_external_option', 'white');

    // Ensure that special regex characters are properly escaped.
    variable_set('imagecache_external_hosts', 'http://drupal.org');

    // This fails because the host has a different hostname.
    $failed_validation = imagecache_external_validate_host('http://example.org/drupal');
    $this
      ->assertFalse($failed_validation, t('Whitelist validation failed for http://example.org/drupal.'));

    // This fails because the whitelist host has a protocol.
    $successful_validation = imagecache_external_validate_host('http://drupal.org/drupal');
    $this
      ->assertFalse($successful_validation, t('Whitelist validation failed for http://drupal.org/drupal.'));

    // Check that hosts without protocols work for both http and https.
    variable_set('imagecache_external_hosts', 'drupal.org');
    foreach (array(
      'http://drupal.org',
      'https://drupal.org',
    ) as $host) {
      $validation = imagecache_external_validate_host($host);
      $this
        ->assertTrue($validation, t('Whitelist validation passed for @host.', array(
        '@host' => $host,
      )));
    }

    // Check that the matching doesn't exhibit regex-like behaviour.
    $regex_validation = imagecache_external_validate_host('http://drupallorg.org');
    $this
      ->assertFalse($regex_validation, t('Whitelist validation failed for http://drupallorg.org.'));
  }

  /**
   * Test the cron cleanup job.
   */
  public function testCachingCronCleanup() {
    $external_image_files = array();
    $source_image_files = array_slice($this
      ->drupalGetTestFiles('image'), 0, 2);
    $externals_directory = file_build_uri(variable_get('imagecache_directory', 'externals'));

    // Ensure the externals directory exists.
    file_prepare_directory($externals_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

    // Move two images to the externals directory.
    foreach ($source_image_files as $source_image_file) {
      $destination_uri = $externals_directory . DIRECTORY_SEPARATOR . drupal_basename($source_image_file->uri);
      file_unmanaged_copy($source_image_file->uri, $destination_uri);
      $external_image_files[] = $destination_uri;
    }
    $this
      ->assertExternalsCacheContains(2);
    drupal_cron_run();

    // Assert that there are still two images (default should be no change).
    $this
      ->assertExternalsCacheContains(2);

    // Set the cron threshold to be seven days.
    variable_set('imagecache_external_cron_flush_threshold', 7);

    // Set one image to be two weeks old (over the threshold).
    touch(drupal_realpath($external_image_files[0]), time() - 3600 * 24 * 14);

    // Run cron again without resetting the last run time.
    drupal_cron_run();

    // Assert that there are still two images (i.e. cron hasn't run).
    $this
      ->assertExternalsCacheContains(2);

    // Now reset the last run time and run cron again.
    variable_set('imagecache_external_cron_last_run', 0);
    drupal_cron_run();
    $this
      ->assertExternalsCacheContains(1);
  }

  /**
   * Assert that the cache contains the specified number of files.
   *
   * @param int $num_files
   *   The number of files that should be in the cache.
   */
  public function assertExternalsCacheContains($num_files) {
    $externals_directory = file_build_uri(variable_get('imagecache_directory', 'externals'));
    $num_external_files = count(file_scan_directory($externals_directory, '/.+\\.(jpe?g|gif|png)$/i'));
    $params = array(
      '@expected' => $num_files,
      '@found' => $num_external_files,
    );
    $this
      ->assertEqual($num_external_files, $num_files, t('Expected @expected images to be left in cache. Found @found.', $params));
  }

}

Classes

Namesort descending Description
ImagecacheExternalTestCase Tests the functions for working with public/private file schemes.