class CDNOriginPullTestCase

Hierarchy

Expanded class hierarchy of CDNOriginPullTestCase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
CDNOriginPullTestCase::assertMapping function Assert a CDN mapping (and optionally set a mapping).
CDNOriginPullTestCase::getInfo public static function
CDNOriginPullTestCase::setUp function Overrides CDNTestCase::setUp
CDNOriginPullTestCase::testFileUrlAlterHook function
CDNOriginPullTestCase::testMapping 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 184
Test CDN.

View source
class CDNOriginPullTestCase extends CDNTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Origin Pull mode',
      'description' => 'Verify Origin Pull mode-related functionality.',
      'group' => 'CDN',
    );
  }

  function setUp() {
    parent::setUp();
    $this->loadFile('cdn.basic.inc');
    $this->loadFile('cdn.basic.farfuture.inc');
    $this->variableSet(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
  }

  /**
   * Assert a CDN mapping (and optionally set a mapping).
   *
   * @param $mapping
   *   The mapping to set; if FALSE, no new mapping will be set.
   * @param $parsed_reference
   *   The reference the parsed mapping will be compared to.
   * @param $domains_reference
   *   The reference the domains (as returned by cdn_get_domains()) will be
   *   compared to.
   */
  function assertMapping($mapping, $parsed_reference, $domains_reference) {
    if ($mapping !== FALSE) {
      $this->variableSet(CDN_BASIC_MAPPING_VARIABLE, $mapping);
    }
    $this->assertEqual($parsed_reference, _cdn_basic_parse_raw_mapping(cdn_basic_get_mapping()), 'CDN mapping parsed correctly.');
    $domains = cdn_get_domains();
    sort($domains);
    $this->assertEqual($domains_reference, $domains, 'CDN domains parsed correctly.');
  }

  function testMapping() {
    $this->setRequestProtocol('http');

    $this->assertEqual('', cdn_basic_get_mapping(), 'The default CDN mapping is empty.');

    // Ensure the parsing of the raw mapping works correctly.
    $this->assertMapping('', array(), array());
    $this->assertMapping('http://cdn-a.com', array('*' => array('http://cdn-a.com')), array('cdn-a.com'));
    $this->assertMapping('http://cdn-a.com/', array('*' => array('http://cdn-a.com')), array('cdn-a.com'));
    $this->assertMapping('//cdn-a.com', array('*' => array('//cdn-a.com')), array('cdn-a.com'));
    $this->assertMapping('//cdn-a.com/', array('*' => array('//cdn-a.com')), array('cdn-a.com'));
    $parsed_mapping = array(
      'css' => array('http://cdn-a.com'),
      'jpg' => array('http://cdn-a.com'),
      'jpeg' => array('http://cdn-a.com'),
      'png' => array('http://cdn-a.com'),
      'zip' => array('http://cdn-b.com'),
      '*' => array('http://cdn-c.com'),
    );
    $domains = array('cdn-a.com', 'cdn-b.com', 'cdn-c.com');
    $this->assertMapping("http://cdn-a.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.zip\nhttp://cdn-c.com", $parsed_mapping, $domains);
    $parsed_mapping = array(
      'css' => array('http://cdn-a.com', 'http://cdn-d.com'),
      'jpg' => array('http://cdn-a.com', 'http://cdn-d.com'),
      'jpeg' => array('http://cdn-a.com', 'http://cdn-d.com'),
      'png' => array('http://cdn-a.com', 'http://cdn-d.com', 'http://cdn-b.com'),
      '*' => array('http://cdn-c.com'),
    );
    $domains = array('cdn-a.com', 'cdn-b.com', 'cdn-c.com', 'cdn-d.com');
    $this->assertMapping("http://cdn-a.com http://cdn-d.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.png\nhttp://cdn-c.com", $parsed_mapping, $domains);

    // When a HTTPS request is performed and the CDN is not marked to support
    // HTTPS, then it should fall back to the default CDN mapping.
    $this->setRequestProtocol('https');
    $this->assertMapping(FALSE, $parsed_mapping, $domains);

    // When a HTTPS request is performed and the CDN *is* marked to support
    // HTTPS, then it should still fall back to the default CDN mapping. (When
    // file URLs are actually altered, it will then replace `http://` with
    // `https://` -- this will be tested in a different test.)
    $this->configureHTTPS(TRUE);
    $this->assertMapping(FALSE, $parsed_mapping, $domains);

    // When a HTTPS request is performed *and* the CDN is marked to support
    // HTTPS *and* there's a HTTPS-specific CDN mapping, that mapping should
    // be used instead!
    $this->configureHTTPS(TRUE, "https://cdn-a.com|.css .jpg .jpeg .png\nhttps://cdn-b.com|.zip\nhttps://cdn-c.com");
    $this->assertMapping(FALSE, array(
      'css' => array('https://cdn-a.com'),
      'jpg' => array('https://cdn-a.com'),
      'jpeg' => array('https://cdn-a.com'),
      'png' => array('https://cdn-a.com'),
      'zip' => array('https://cdn-b.com'),
      '*' => array('https://cdn-c.com'),
    ), array('cdn-a.com', 'cdn-b.com', 'cdn-c.com'));

    // Ensure the default CDN mapping is used whenever a HTTP request occurs
    // and the CDN is marked to suppport HTTPS and there's a HTTPS-specific
    // CDN mapping.
    $this->configureHTTPS(FALSE);
    $this->assertMapping(FALSE, $parsed_mapping, $domains);
  }

  function testFileUrlAlterHook() {
    // Provide a very basic CDN mapping.
    $this->variableSet(CDN_BASIC_MAPPING_VARIABLE, 'http://cdn-a.com');

    $filename = 'újjáépítésérol — 100% in B&W.jpg';
    $uri = "public://$filename";
    $this->touchFile($uri);

    cdn_file_url_alter($uri);
    $expected = 'http://cdn-a.com' . base_path() . variable_get('file_public_path', conf_path() . '/files') . '/' . drupal_encode_path($filename);
    $this->assertIdentical($uri, $expected, 'cdn_file_url_alter() works correctly.');
  }
}