Skip to content

Commit

Permalink
Merge pull request #14828 from seamuslee001/deprecation_notices
Browse files Browse the repository at this point in the history
Add in Deprecation warnings on Cache functons
  • Loading branch information
seamuslee001 authored Jul 15, 2019
2 parents 4d21998 + fd5f6b0 commit 9c68f97
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CRM/Core/BAO/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
* @deprecated
*/
public static function &getItem($group, $path, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::getItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
$value = $adapter::getItem($group, $path, $componentID);
return $value;
Expand Down Expand Up @@ -116,6 +119,9 @@ public static function &getItem($group, $path, $componentID = NULL) {
* @deprecated
*/
public static function &getItems($group, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::getItems is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::getItems($group, $componentID);
}
Expand Down Expand Up @@ -161,6 +167,9 @@ public static function &getItems($group, $componentID = NULL) {
* @deprecated
*/
public static function setItem(&$data, $group, $path, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::setItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::setItem($data, $group, $path, $componentID);
}
Expand Down Expand Up @@ -232,6 +241,9 @@ public static function setItem(&$data, $group, $path, $componentID = NULL) {
* @deprecated
*/
public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::deleteGroup is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::deleteGroup($group, $path);
}
Expand Down
22 changes: 19 additions & 3 deletions tests/phpunit/CRM/Core/BAO/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
*/
class CRM_Core_BAO_CacheTest extends CiviUnitTestCase {

/**
* @var CRM_Utils_Cache_Interface
*/
protected $a;

public function createSimpleCache() {
return new CRM_Utils_Cache_FastArrayDecorator(
$this->a = CRM_Utils_Cache::create([
'name' => 'CRM_Core_BAO_CacheTest',
'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
])
);
}

public function testMultiVersionDecode() {
$encoders = ['serialize', ['CRM_Core_BAO_Cache', 'encode']];
$values = [NULL, 0, 1, TRUE, FALSE, [], ['abcd'], 'ab;cd', new stdClass()];
Expand Down Expand Up @@ -68,17 +82,19 @@ public function exampleValues() {
* @dataProvider exampleValues
*/
public function testSetGetItem($originalValue) {
CRM_Core_BAO_Cache::setItem($originalValue, __CLASS__, 'testSetGetItem');
$this->createSimpleCache();
$this->a->set('testSetGetItem', $originalValue);

$return_1 = CRM_Core_BAO_Cache::getItem(__CLASS__, 'testSetGetItem');
$return_1 = $this->a->get('testSetGetItem');
$this->assertEquals($originalValue, $return_1);

// Wipe out any in-memory copies of the cache. Check to see if the SQL
// read is correct.

CRM_Core_BAO_Cache::$_cache = NULL;
CRM_Utils_Cache::$_singleton = NULL;
$return_2 = CRM_Core_BAO_Cache::getItem(__CLASS__, 'testSetGetItem');
$this->a->values = [];
$return_2 = $this->a->get('testSetGetItem');
$this->assertEquals($originalValue, $return_2);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/api/v3/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public function testFlush() {
// check all of them -- just enough to make sure that the API is doing
// something

$this->assertTrue(NULL === CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH));
$this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));

$data = 'abc';
CRM_Core_BAO_Cache::setItem($data, self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH);
Civi::cache()->set(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH), $data);

$this->assertEquals('abc', CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH));
$this->assertEquals('abc', Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));

$params = array();
$result = $this->callAPIAndDocument('system', 'flush', $params, __FUNCTION__, __FILE__, "Flush all system caches", 'Flush');

$this->assertTrue(NULL === CRM_Core_BAO_Cache::getItem(self::TEST_CACHE_GROUP, self::TEST_CACHE_PATH));
$this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));
}

/**
Expand Down

0 comments on commit 9c68f97

Please sign in to comment.