Unverified Commit d4f75a5d authored by MikeyMJCO's avatar MikeyMJCO Committed by GitHub
Browse files

Merge pull request #19 from kurund/delete-message-template

implement deleting of message template when corresponding mosaico template is deleted 
parents 596a5374 a0435575
<?php
require_once 'mosaicomsgtpl.civix.php';
use CRM_Mosaicomsgtpl_ExtensionUtil as E;
use CRM_Mosaicomsgtpl_ExtensionUtil as E;
/**
* Implements hook_civicrm_post()
*
* This function is used to keep mosaico template and message template in sync
*/
function mosaicomsgtpl_civicrm_post($op, $objectName, $objectId, &$objectRef = NULL) {
if (($op === 'create' || $op === 'edit') && $objectName === 'MosaicoTemplate') {
if (Civi::settings()->get('mosaicomsgtpl_suspend')) {
......@@ -16,6 +21,33 @@ function mosaicomsgtpl_civicrm_post($op, $objectName, $objectId, &$objectRef = N
}
}
/**
* Implements hook_civicrm_pre()
*
* This function is used to delete message template when a corresponding mosico template in deleted
*/
function mosaicomsgtpl_civicrm_pre($op, $objectName, $objectId, &$objectRef = NULL) {
if ($objectName === 'MosaicoTemplate' && $op === 'delete') {
if (Civi::settings()->get('mosaicomsgtpl_suspend')) {
return;
}
// get the message_template_id
$result = civicrm_api3('MosaicoTemplate', 'get', [
'sequential' => 1,
'return' => ["msg_tpl_id"],
'id' => $objectId,
]);
// delete associated message template only if it exists
if (!empty($result['values'][0]['msg_tpl_id'])) {
civicrm_api3('MessageTemplate', 'delete', [
'id' => $result['values'][0]['msg_tpl_id'],
]);
}
}
}
/**
* Implements hook_civicrm_config().
*
......@@ -163,7 +195,6 @@ function mosaicomsgtpl_civicrm_alterMailContent(&$content) {
$content['html'] = strtr($content['html'], $replacements);
$content['text'] = strtr($content['text'], $replacements);
}
}
// --- Functions below this ship commented out. Uncomment as required. ---
......
......@@ -100,7 +100,7 @@ class api_v3_Job_MosaicoMsgSyncTest extends \PHPUnit\Framework\TestCase implemen
$oldCount = CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_msg_template');
// Create the clone and run the sync on it passing is_new (which is what the post hook does).
$second = civicrm_api3('MosaicoTemplate', 'clone', ['id'=>$first['id'], 'title' => 'clone']);
$second = civicrm_api3('MosaicoTemplate', 'clone', ['id' => $first['id'], 'title' => 'clone']);
$result = civicrm_api3('Job', 'mosaico_msg_sync', ['id' => $second['id'], 'is_new' => TRUE]);
// Count templates - should be one more.
......@@ -108,6 +108,34 @@ class api_v3_Job_MosaicoMsgSyncTest extends \PHPUnit\Framework\TestCase implemen
$this->assertEquals(1 + $oldCount, $newCount);
}
/**
* Test deletion of message template corresponding to masaico template
*/
public function testDelete() {
$this->assertEquals('MosaicoTemplate', CRM_Core_DAO_AllCoreTables::getBriefName('CRM_Mosaico_DAO_MosaicoTemplate'));
// Create the first template and run the sync.
$first = $this->createMosaicoTemplate(array('title' => 'First example'));
civicrm_api3('Job', 'mosaico_msg_sync', ['id' => $first['id']]);
// Delete mosaico template
civicrm_api3('MosaicoTemplate', 'delete', array('id' => $first['id']));
// make sure message template is deleted
$message = NULL;
try {
// A significant number of times this will fail if there are no message templates matching the criteria with a `CiviCRM_API3_EXCEPTION`.
$result = civicrm_api3('MessageTemplate', 'getsingle', ['id' => $first['msg_tpl_id']]);
$this->assertEquals($result['count'], 0);
}
catch (CiviCRM_API3_Exception $e) {
$message = $e->getMessage();
}
// If this exception is returned it means Civi cannot find a MessageTemplate matching the criteria - this is what we want/expect.
$this->assertContains('Expected one MessageTemplate but found 0', $message);
}
/**
* Test title/subject parsing.
*
......@@ -150,5 +178,4 @@ class api_v3_Job_MosaicoMsgSyncTest extends \PHPUnit\Framework\TestCase implemen
$msgTpl = civicrm_api3('MosaicoTemplate', 'create', array_merge($defaults, $params));
return $msgTpl['values'][$msgTpl['id']];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment