CRM-19130 - Add test for regression in CRM_Core_BAO_Cache
authorTim Otten <totten@civicrm.org>
Thu, 11 Aug 2016 20:20:56 +0000 (13:20 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 11 Aug 2016 20:20:56 +0000 (13:20 -0700)
The earlier patch for CRM-19130 broke support for reading individual items
from the SQL cache.  Although the test-suite hits the cache layer a lot
(e.g.  CRM_Utils_Cache_SqlGroupTest), the bug wasn't exposed by other tests
because (a) caches are replicated in-memory and in SQL (b) caches are often
read in batch mode.

This explicitly flushes the in-memory cache and reads the SQL cache.

CRM/Utils/Cache.php
tests/phpunit/CRM/Core/BAO/CacheTest.php [new file with mode: 0644]

index 8c04a9e81ae655871b89ca112ccb4f4a79b854ef..9b093a88f717c13b75d5def0ccf30c033e51e20b 100644 (file)
  */
 class CRM_Utils_Cache {
   /**
+   * (Quasi-Private) Treat this as private. It is marked public to facilitate testing.
+   *
    * We only need one instance of this object. So we use the singleton
    * pattern and cache the instance in this variable
    *
    * @var object
    */
-  static private $_singleton = NULL;
+  public static $_singleton = NULL;
 
   /**
    * Constructor.
diff --git a/tests/phpunit/CRM/Core/BAO/CacheTest.php b/tests/phpunit/CRM/Core/BAO/CacheTest.php
new file mode 100644 (file)
index 0000000..dee3f60
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Core_BAO_CacheTest
+ * @group headless
+ */
+class CRM_Core_BAO_CacheTest extends CiviUnitTestCase {
+
+  public function testSetGetItem() {
+    $originalValue = array('abc' => 'def');
+    CRM_Core_BAO_Cache::setItem($originalValue, __CLASS__, 'testSetGetItem');
+
+    $return_1 = CRM_Core_BAO_Cache::getItem(__CLASS__, '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->assertEquals($originalValue, $return_2);
+  }
+
+}