From ec52a65af5b001821e69fe0a29196a13e9e3cddd Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 5 Sep 2017 18:42:57 +1200 Subject: [PATCH] CRM-21109 Add test for controlling cache clearing --- CRM/Contact/BAO/Contact.php | 4 +-- tests/phpunit/api/v3/ContactTest.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 9c829b29c1..42637eb920 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -351,9 +351,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { //add website CRM_Core_BAO_Website::create($params['website'], $contact->id, $skipDelete); - //get userID from session - $session = CRM_Core_Session::singleton(); - $userID = $session->get('userID'); + $userID = CRM_Core_Session::singleton()->get('userID'); // add notes if (!empty($params['note'])) { if (is_array($params['note'])) { diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 3daf6bd58c..ca3a70cb5e 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -115,6 +115,27 @@ class api_v3_ContactTest extends CiviUnitTestCase { ); } + /** + * Test that it is possible to prevent cache clearing via option. + * + * Cache clearing is bypassed if 'options' => array('do_not_reset_cache' => 1 is used. + */ + public function testCreateIndividualNoCacheClear() { + + $contact = $this->callAPISuccess('contact', 'create', $this->_params); + $groupID = $this->groupCreate(); + + $this->putGroupContactCacheInClearableState($groupID, $contact); + + $this->callAPISuccess('contact', 'create', array('id' => $contact['id'])); + $this->assertEquals(0, CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_group_contact_cache")); + + // Rinse & repeat, but with the option. + $this->putGroupContactCacheInClearableState($groupID, $contact); + $this->callAPISuccess('contact', 'create', array('id' => $contact['id'], 'options' => array('do_not_reset_cache' => 1))); + $this->assertEquals(1, CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_group_contact_cache")); + } + /** * Test for international string acceptance (CRM-10210). * @@ -3490,4 +3511,20 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals($cid, $contact['id']); } + /** + * @param $groupID + * @param $contact + */ + protected function putGroupContactCacheInClearableState($groupID, $contact) { +// We need to force the situation where there is invalid data in the cache and it + // is due to be cleared. + CRM_Core_DAO::executeQuery(" + INSERT INTO civicrm_group_contact_cache (group_id, contact_id) + VALUES ({$groupID}, {$contact['id']}) + "); + CRM_Core_DAO::executeQuery("UPDATE civicrm_group SET cache_date = '2017-01-01'"); + // Reset so it does not skip. + Civi::$statics['CRM_Contact_BAO_GroupContactCache']['is_refresh_init'] = FALSE; + } + } -- 2.25.1