From 6f12c6eb0ec14c971e5b3b84338511b0c56477a9 Mon Sep 17 00:00:00 2001 From: mickadoo Date: Tue, 14 Mar 2017 10:57:56 +0000 Subject: [PATCH] CRM-20254: add cache buster string for custom CSS and improve query string generation (duplicated from original PR which was to the wrong branch) --- CRM/Core/Resources.php | 28 +++++++++++++++-- tests/phpunit/CRM/Core/ResourcesTest.php | 40 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index cb46b981bd..bccbefa845 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -500,7 +500,7 @@ class CRM_Core_Resources { $file = ''; } if ($addCacheCode) { - $file .= '?r=' . $this->getCacheCode(); + $file = $this->addCacheCode($file); } // TODO consider caching results $base = $this->paths->hasVariable($ext) @@ -643,7 +643,8 @@ class CRM_Core_Resources { // 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); @@ -881,4 +882,27 @@ class CRM_Core_Resources { } } + /** + * @param string $url + * @return string + */ + public function addCacheCode($url) { + parse_str(parse_url($url, PHP_URL_QUERY), $queryParts); + $existing = isset($queryParts['r']) ? $queryParts['r'] : NULL; + $latest = $this->cacheCode; + + if ($existing) { + if ($existing === $latest) { + return $url; // no need to update + } + else { + return str_replace('r=' . $existing, 'r=' . $latest, $url); + } + } + + $operator = empty($queryParts) ? '?' : '&'; + + return $url . $operator . 'r=' . $latest; + } + } diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index ea0d74ca01..693ad2039a 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -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; @@ -348,4 +353,39 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase { return array($basedir, $c, $mapper); } + /** + * @param string $url + * @param string $expected + * + * @dataProvider urlForCacheCodeProvider + */ + public function testAddingCacheCode($url, $expected) { + $resources = CRM_Core_Resources::singleton(); + $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?r=old&foo=bar', + 'civicrm.org/custom.css?r=' . $this->cacheBusterString . '&foo=bar', + ), + array( + 'civicrm.org/custom.css?car=blue&foo=bar', + 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString, + ), + ); + } + } -- 2.25.1