You are here

class CDNImageTestCase in CDN 7.2

Hierarchy

Expanded class hierarchy of CDNImageTestCase

File

tests/cdn.test, line 503
Test CDN.

View source
class CDNImageTestCase extends CDNUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Image and Download Link HTML altering',
      'description' => 'Verify that image and anchor link 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 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).');
  }
  function testOtherContentLink() {
    $cdn = 'http://cdn-a.com';
    $this
      ->variableSet(CDN_BASIC_MAPPING_VARIABLE, "{$cdn}|.png .pdf");
    $this
      ->variableSet(CDN_MODE_VARIABLE, CDN_MODE_BASIC);

    // Image altering type 1: "just image", i.e. "<img />".
    $template = function ($url) {
      return '<a href="' . $url . '">Download this!</a>';
    };

    // Simplest case possible.
    $a_url = base_path() . 'foo/bar/image.png';
    $html = $template($a_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($cdn . $a_url), $html, 'Other content link correctly altered.');

    // Query strings should not be stripped.
    $a_url = base_path() . 'foo/bar/image.pdf?foobar';
    $html = $template($a_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($cdn . $a_url), $html, 'Other content link correctly altered (query string not stripped).');

    // Edge case: a script generating an image is not (yet) supported.
    $a_url = base_path() . 'foo/bar/image.gif';
    $html = $template($a_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($a_url), $html, 'Other content link correctly NOT altered (extension does not map).');

    // Edge case: a script generating an image is not (yet) supported.
    $a_url = base_path() . 'foo/bar/showimage?formula=12345.png';
    $html = $template($a_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($a_url), $html, 'Other content link correctly NOT altered (extension not pulled from query string).');
  }
  function testOtherContentLinkWrappingImage() {
    $cdn = 'http://cdn-a.com';
    $this
      ->variableSet(CDN_BASIC_MAPPING_VARIABLE, "{$cdn}|.png .jpeg");
    $this
      ->variableSet(CDN_MODE_VARIABLE, CDN_MODE_BASIC);

    // 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>';
    };

    // Test that images linking to a URL with an extension that matches a
    // mapped extension, even if the extension is different than the image,
    // are modified.
    $a_url = base_path() . 'node.jpeg';
    $img_url = base_path() . 'foo/bar/image.png';
    $html = $template($a_url, $img_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($cdn . $a_url, $cdn . $img_url), $html, 'Other content link wrapping image correctly altered.');
    $a_url = base_path() . 'node.gif';
    $html = $template($a_url, $img_url);
    $html = cdn_post_render_html_alter($html);
    $this
      ->assertIdentical($template($a_url, $cdn . $img_url), $html, 'Other content link wrapping correctly NOT altered (extension does not map).');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CDNImageTestCase::getInfo public static function
CDNImageTestCase::setUp function Sets up unit test environment. Overrides CDNUnitTestCase::setUp
CDNImageTestCase::testImage function
CDNImageTestCase::testOtherContentLink function
CDNImageTestCase::testOtherContentLinkWrappingImage function
CDNUnitTestCase::configureHTTPS function Configure HTTPS-related settings.
CDNUnitTestCase::getExpandedFilePath function Given a file URI, get the expanded file path.
CDNUnitTestCase::loadFile function
CDNUnitTestCase::setRequestProtocol function Set the protocol of the current "request".
CDNUnitTestCase::setUserAgent function Set the User-Agent of the current "request".
CDNUnitTestCase::tearDown function Overrides DrupalUnitTestCase::tearDown
CDNUnitTestCase::touchFile function Given a file URI, get its path, create the file and ensure it exists.
CDNUnitTestCase::variableSet function Mock function for variable_set().
CDNUnitTestCase::variableSetDefaults function Set the default variable values for the CDN module.
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct