Skip to content

Commit

Permalink
Merge pull request #9980 from mickadoo/CRM-20254-cache-busting-custom…
Browse files Browse the repository at this point in the history
…-css

CRM-20254: add cache buster string for custom CSS
  • Loading branch information
totten authored Mar 16, 2017
2 parents 43e3832 + 33603e1 commit d90703e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
16 changes: 14 additions & 2 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
$file = '';
}
if ($addCacheCode) {
$file .= '?r=' . $this->getCacheCode();
$file = $this->addCacheCode($file);
}
// TODO consider caching results
$base = $this->paths->hasVariable($ext)
Expand Down Expand Up @@ -643,7 +643,8 @@ public function addCoreStyles($region = 'html-header') {
// Load custom or core css
$config = CRM_Core_Config::singleton();
if (!empty($config->customCSSURL)) {
$this->addStyleUrl($config->customCSSURL, 99, $region);
$customCSSURL = $this->addCacheCode($config->customCSSURL);
$this->addStyleUrl($customCSSURL, 99, $region);
}
if (!Civi::settings()->get('disable_core_css')) {
$this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
Expand Down Expand Up @@ -881,4 +882,15 @@ private function resolveFileName(&$fileName, $extName) {
}
}

/**
* @param string $url
* @return string
*/
public function addCacheCode($url) {
$hasQuery = strpos($url, '?') !== FALSE;
$operator = $hasQuery ? '&' : '?';

return $url . $operator . 'r=' . $this->cacheCode;
}

}
37 changes: 37 additions & 0 deletions tests/phpunit/CRM/Core/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase {
*/
protected $mapper;

/**
* @var string for testing cache buster generation
*/
protected $cacheBusterString = 'xBkdk3';

protected $originalRequest;
protected $originalGet;

Expand Down Expand Up @@ -348,4 +353,36 @@ public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey
return array($basedir, $c, $mapper);
}

/**
* @param string $url
* @param string $expected
*
* @dataProvider urlForCacheCodeProvider
*/
public function testAddingCacheCode($url, $expected) {
$resources = CRM_Core_Resources::singleton();
$resources->setCacheCode($this->cacheBusterString);
$this->assertEquals($expected, $resources->addCacheCode($url));
}

/**
* @return array
*/
public function urlForCacheCodeProvider() {
return array(
array(
'http://www.civicrm.org',
'http://www.civicrm.org?r=' . $this->cacheBusterString,
),
array(
'www.civicrm.org/custom.css?foo=bar',
'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString,
),
array(
'civicrm.org/custom.css?car=blue&foo=bar',
'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString,
),
);
}

}

0 comments on commit d90703e

Please sign in to comment.