From 294442952b11ed7ea553bf0d37eeb5d6e31992b4 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 6 Apr 2016 10:26:48 +1200 Subject: [PATCH] CRM-18360 Expose logging data through GET api --- CRM/Logging/Differ.php | 3 + api/v3/Logging.php | 43 +++++ api/v3/examples/Logging/Get.php | 241 +++++++++++++++++++++++++++ tests/phpunit/api/v3/LoggingTest.php | 43 +++++ 4 files changed, 330 insertions(+) create mode 100644 api/v3/examples/Logging/Get.php diff --git a/CRM/Logging/Differ.php b/CRM/Logging/Differ.php index 408841313f..ffac073531 100644 --- a/CRM/Logging/Differ.php +++ b/CRM/Logging/Differ.php @@ -242,6 +242,9 @@ WHERE lt.log_conn_id = %1 AND 'field' => $diff, 'from' => CRM_Utils_Array::value($diff, $original), 'to' => CRM_Utils_Array::value($diff, $changed), + 'table' => $table, + 'log_date' => $changed['log_date'], + 'log_conn_id' => $changed['log_conn_id'], ); } } diff --git a/api/v3/Logging.php b/api/v3/Logging.php index fc2890a0f4..f3418a639d 100644 --- a/api/v3/Logging.php +++ b/api/v3/Logging.php @@ -48,3 +48,46 @@ function civicrm_api3_logging_revert($params) { $reverter->revert(); return civicrm_api3_create_success(1); } + +/** + * Get a log change. + * + * @param array $params + * + * @return array + * API Success Array + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ +function civicrm_api3_logging_get($params) { + $schema = new CRM_Logging_Schema(); + $interval = (empty($params['log_date'])) ? NULL : $params['interval']; + $differ = new CRM_Logging_Differ($params['log_conn_id'], CRM_Utils_Array::value('log_date', $params), $interval); + return civicrm_api3_create_success($differ->getAllChangesForConnection($schema->getLogTablesForContact())); +} + +/** + * Get a log change. + * + * @param array $params + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ +function _civicrm_api3_logging_get_spec(&$params) { + $params['log_conn_id'] = array( + 'title' => 'Logging Connection ID', + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => TRUE, + ); + $params['log_date'] = array( + 'title' => 'Logging Timestamp', + 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME, + ); + $params['interval'] = array( + 'title' => ts('Interval (required if date is included)'), + 'type' => CRM_Utils_Type::T_STRING, + 'api.default' => '10 SECOND', + 'description' => ts('Used when log_date is passed in'), + ); +} diff --git a/api/v3/examples/Logging/Get.php b/api/v3/examples/Logging/Get.php new file mode 100644 index 0000000000..3e181ae209 --- /dev/null +++ b/api/v3/examples/Logging/Get.php @@ -0,0 +1,241 @@ + 'wooty woot', + 'log_date' => '2016-04-06 00:08:23', + ); + + try{ + $result = civicrm_api3('Logging', 'get', $params); + } + catch (CiviCRM_API3_Exception $e) { + // Handle error here. + $errorMessage = $e->getMessage(); + $errorCode = $e->getErrorCode(); + $errorData = $e->getExtraParams(); + return array( + 'error' => $errorMessage, + 'error_code' => $errorCode, + 'error_data' => $errorData, + ); + } + + return $result; +} + +/** + * Function returns array of result expected from previous function. + * + * @return array + * API result array + */ +function logging_get_expectedresult() { + + $expectedResult = array( + 'is_error' => 0, + 'version' => 3, + 'count' => 17, + 'values' => array( + '0' => array( + 'action' => 'Update', + 'id' => '3', + 'field' => 'sort_name', + 'from' => 'Anderson, Anthony', + 'to' => 'Dwarf, Dopey', + 'table' => 'civicrm_contact', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '1' => array( + 'action' => 'Update', + 'id' => '3', + 'field' => 'display_name', + 'from' => 'Mr. Anthony Anderson II', + 'to' => 'Mr. Dopey Dwarf II', + 'table' => 'civicrm_contact', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '2' => array( + 'action' => 'Update', + 'id' => '3', + 'field' => 'first_name', + 'from' => 'Anthony', + 'to' => 'Dopey', + 'table' => 'civicrm_contact', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '3' => array( + 'action' => 'Update', + 'id' => '3', + 'field' => 'last_name', + 'from' => 'Anderson', + 'to' => 'Dwarf', + 'table' => 'civicrm_contact', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '4' => array( + 'action' => 'Update', + 'id' => '3', + 'field' => 'modified_date', + 'from' => '2016-04-06 00:08:06', + 'to' => '2016-04-06 00:08:23', + 'table' => 'civicrm_contact', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '5' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'id', + 'from' => '', + 'to' => '4', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '6' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'contact_id', + 'from' => '', + 'to' => '3', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '7' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'location_type_id', + 'from' => '', + 'to' => '', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '8' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'email', + 'from' => '', + 'to' => 'dopey@mail.com', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '9' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'is_primary', + 'from' => '', + 'to' => 0, + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '10' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'is_billing', + 'from' => '', + 'to' => 0, + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '11' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'on_hold', + 'from' => '', + 'to' => 0, + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '12' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'is_bulkmail', + 'from' => '', + 'to' => 0, + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '13' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'hold_date', + 'from' => '', + 'to' => '', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '14' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'reset_date', + 'from' => '', + 'to' => '', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '15' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'signature_text', + 'from' => '', + 'to' => '', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + '16' => array( + 'action' => 'Insert', + 'id' => '4', + 'field' => 'signature_html', + 'from' => '', + 'to' => '', + 'table' => 'civicrm_email', + 'log_date' => '2016-04-06 00:08:23', + 'log_conn_id' => 'wooty woot', + ), + ), + ); + + return $expectedResult; +} + +/* +* This example has been generated from the API test suite. +* The test that created it is called "testGet" +* and can be found at: +* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/LoggingTest.php +* +* You can see the outcome of the API tests at +* https://test.civicrm.org/job/CiviCRM-master-git/ +* +* To Learn about the API read +* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API +* +* Browse the api on your own site with the api explorer +* http://MYSITE.ORG/path/to/civicrm/api +* +* Read more about testing here +* http://wiki.civicrm.org/confluence/display/CRM/Testing +* +* API Standards documentation: +* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards +*/ diff --git a/tests/phpunit/api/v3/LoggingTest.php b/tests/phpunit/api/v3/LoggingTest.php index 6dcfd562d0..8056dc8d88 100644 --- a/tests/phpunit/api/v3/LoggingTest.php +++ b/tests/phpunit/api/v3/LoggingTest.php @@ -46,6 +46,7 @@ class api_v3_LoggingTest extends CiviUnitTestCase { * Clean up log tables. */ protected function tearDown() { + $this->quickCleanup(array('civicrm_email')); parent::tearDown(); $this->callAPISuccess('Setting', 'create', array('logging' => FALSE)); $schema = new CRM_Logging_Schema(); @@ -245,4 +246,46 @@ class api_v3_LoggingTest extends CiviUnitTestCase { $this->callAPISuccessGetCount('Email', array('id' => $email['id']), 0); } + /** + * Test changes can be retrieved. + */ + public function testGet() { + $contactId = $this->individualCreate(); + $this->callAPISuccess('Setting', 'create', array('logging' => TRUE)); + CRM_Core_DAO::executeQuery("SET @uniqueID = 'wooty woot'"); + $timeStamp = date('Y-m-d H:i:s'); + $this->callAPISuccess('Contact', 'create', array( + 'id' => $contactId, + 'first_name' => 'Dopey', + 'last_name' => 'Dwarf', + 'api.email.create' => array('email' => 'dopey@mail.com')) + ); + $this->callAPISuccessGetSingle('email', array('email' => 'dopey@mail.com')); + $diffs = $this->callAPIAndDocument('Logging', 'get', array('log_conn_id' => 'wooty woot', 'log_date' => $timeStamp), __FUNCTION__, __FILE__); + $this->assertLoggingIncludes($diffs['values'], array('to' => 'Dwarf, Dopey')); + $this->assertLoggingIncludes($diffs['values'], array('to' => 'Mr. Dopey Dwarf II', 'table' => 'civicrm_contact', 'action' => 'Update', 'field' => 'display_name')); + $this->assertLoggingIncludes($diffs['values'], array('to' => 'dopey@mail.com', 'table' => 'civicrm_email', 'action' => 'Insert', 'field' => 'email')); + } + + /** + * Assert the values in the $expect array in included in the logging diff. + * + * @param array $diffs + * @param array $expect + * + * @return bool + * @throws \CRM_Core_Exception + */ + public function assertLoggingIncludes($diffs, $expect) { + foreach ($diffs as $diff) { + foreach ($expect as $expectKey => $expectValue) { + if ($diff[$expectKey] != $expectValue) { + continue; + } + return TRUE; + } + } + throw new CRM_Core_Exception("No match found for key : $expectKey with value : $expectValue"); + } + } -- 2.25.1