You are here

function CDNImageTestCase::testImage in CDN 7.2

File

tests/cdn.test, line 517
Test CDN.

Class

CDNImageTestCase

Code

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 alt="Foo!" src="' . $url . '" title="Bar!"/>';
  };

  // 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 rel="gallery" href="' . $a_url . '" class="gallery-image"><img alt="Foo!" src="' . $img_url . '" title="Bar!" /></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);
  $html = cdn_post_render_html_alter($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);
  $html = cdn_post_render_html_alter($html);
  $this
    ->assertIdentical($template($a_url, $cdn . $img_url), $html, 'Linked image HTML correctly altered (anchor unmodified, even with query strings).');
}