class CDNImageTestCase

Hierarchy

Expanded class hierarchy of CDNImageTestCase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
CDNImageTestCase::getInfo public static function
CDNImageTestCase::setUp function Overrides CDNTestCase::setUp
CDNImageTestCase::testImage function
CDNTestCase::configureHTTPS function Configure HTTPS-related settings.
CDNTestCase::getExpandedFilePath function Given a file URI, get the expanded file path.
CDNTestCase::loadFile function
CDNTestCase::setRequestProtocol function Set the protocol of the current "request".
CDNTestCase::tearDown function
CDNTestCase::touchFile function Given a file URI, get its path, create the file and ensure it exists.
CDNTestCase::variableSet function Mock function for variable_set().
CDNTestCase::variableSetDefaults function Set the default variable values for the CDN module.

File

modules/contrib/cdn/cdn.test, line 471
Test CDN.

View source
class CDNImageTestCase extends CDNTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Image HTML altering',
      'description' => 'Verify that image URLs inside HTML are rewritten correctly.',
      'group' => 'CDN',
    );
  }

  function setUp() {
    parent::setUp();
    $this->loadFile('cdn.fallback.inc');
  }

  function testImage() {
    $cdn = 'http://cdn-a.com';
    $this->variableSet(CDN_BASIC_MAPPING_VARIABLE, $cdn);
    $this->variableSet(CDN_MODE_VARIABLE, CDN_MODE_BASIC);

    // Image altering type 1: "just image", i.e. "<img />".
    $template = function($url) {
      return '<img src="' . $url . '" />';
    };
    // Simplest case possible.
    $img_url = base_path() . 'foo/bar/image.png';
    $html = $template($img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $img_url), $html, 'Image HTML correctly altered.');
    // Query strings should not be stripped
    $img_url = base_path() . 'foo/bar/image.png?foobar';
    $html = $template($img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $img_url), $html, 'Image HTML correctly altered (query string not stripped).');
    // In particular: not the query string used to generate image styles.
    $img_url = base_path() . 'foo/bar/image.png?itok=1234abcd';
    $html = $template($img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $img_url), $html, 'Image HTML correctly altered (image style query string not stripped).');
    // Edge case: a script generating an image is not (yet) supported.
    $img_url = base_path() . 'foo/bar/showimage?formula=12345.png';
    $html = $template($img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $img_url), $html, 'Image HTML correctly altered (query string not stripped).');

    // Image altering type 2: "linked image", i.e. "<a><img /></a>"..
    $template = function($a_url, $img_url) {
      return '<a href="' . $a_url . '"><img src="' . $img_url . '" /></a>';
    };
    // Simplest case possible: a linked image linking to the same image.
    $img_base_url = base_path() . 'foo/bar/image';
    $a_url = $img_url = $img_base_url . '.png';
    $html = $template($a_url, $img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered.');
    // Slightly more complex: a linked image linking to a derivative image.
    $img_url = $img_base_url . '-thumbnail.png?itok=5678wxyz';
    $html = $template($a_url, $img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered.');
    // Slightly more complex: a linked derivative image linking to another
    // derivative image.
    $a_url = $img_base_url . '-large.png?itok=9012klmn';
    $img_url = $img_base_url . '-thumbnail.png?itok=5678wxyz';
    $html = $template($a_url, $img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($cdn . $a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered.');
    // Ensure that images linking to content (i.e. not a bigger version of the
    // image) don't get their anchors modified
    $a_url = base_path() . 'node';
    $html = $template($a_url, $img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered (anchor unmodified).');
    // Same, but now for a link with a query string.
    $a_url = base_path() . 'node?foobar';
    $html = $template($a_url, $img_url);
    cdn_html_alter_image_urls($html);
    $this->assertIdentical($template($a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered (anchor unmodified, even with query strings).');
  }
}