Skip to content

Commit

Permalink
dev/core#2199: Add pre and post hooks for ufgroup entity
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky091588 committed Dec 7, 2020
1 parent 465fc97 commit aa72bcb
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
12 changes: 8 additions & 4 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1459,22 +1459,26 @@ public static function add(&$params, $ids = []) {
if (!empty($params['group_type']) && is_array($params['group_type'])) {
$params['group_type'] = implode(',', $params['group_type']);
}

$hook = empty($params['id']) ? 'create' : 'edit';
CRM_Utils_Hook::pre($hook, 'UFGroup', ($params['id'] ?? NULL), $params);

$ufGroup = new CRM_Core_DAO_UFGroup();
$ufGroup->copyValues($params);

$ufGroupID = CRM_Utils_Array::value('ufgroup', $ids, CRM_Utils_Array::value('id', $params));
if (!$ufGroupID && empty($params['name'])) {
if (empty($params['name'])) {
$ufGroup->name = CRM_Utils_String::munge($ufGroup->title, '_', 56);
}
$ufGroup->id = $ufGroupID;

$ufGroup->save();

if (!$ufGroupID && empty($params['name'])) {
if (empty($params['name'])) {
$ufGroup->name = $ufGroup->name . "_{$ufGroup->id}";
$ufGroup->save();
}

CRM_Utils_Hook::post($hook, 'UFGroup', $ufGroup->id, $ufGroup);

return $ufGroup;
}

Expand Down
109 changes: 109 additions & 0 deletions tests/phpunit/CRM/Core/BAO/UFGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/**
* Class CRM_Core_BAO_UFGroupTest
* @group headless
*/
class CRM_Core_BAO_UFGroupTest extends CiviUnitTestCase {

public function implementHookPre($op, $objectName, $id, &$params) {
if ($objectName == 'UFGroup') {
if ($op == 'create') {
$params['is_active'] = 0;
}
elseif ($op == 'delete') {
$systemLog = $this->callAPISuccess('SystemLog', 'create', [
'message' => "CRM_Core_BAO_UFGroupTest::implementHookPre $id",
'level' => 'info',
]);
}
}
}

public function implementHookPost($op, $objectName, $objectId, &$objectRef) {
if ($objectName == 'UFGroup') {
if ($op == 'create') {
$objectRef->is_active = 0;
}
elseif ($op == 'delete') {
$systemLog = $this->callAPISuccess('SystemLog', 'create', [
'message' => "CRM_Core_BAO_UFGroupTest::implementHookPost $objectId",
'level' => 'info',
]);
}
}
}

public function testPreHookIsCalledForCreate() {
// Specify pre hook implementation.
$this->hookClass->setHook('civicrm_pre', array($this, 'implementHookPre'));

// Create a ufgroup with BAO.
$params = [
'title' => 'testPreHookIsCalledForCreate',
'is_active' => 1,
];
$ufGroup = CRM_Core_BAO_UFGroup::add($params);

// Assert that pre hook implemntation was called.
$this->assertEquals('testPreHookIsCalledForCreate', $ufGroup->title);
$this->assertEquals(0, $ufGroup->is_active, 'Is active should be 0');
}

public function testPreHookIsCalledForDelete() {
// Specify pre hook implementation.
$this->hookClass->setHook('civicrm_pre', array($this, 'implementHookPre'));

// Create a ufgroup with BAO.
$params = [
'title' => 'testPreHookIsCalledForDelete',
'is_active' => 1,
];
$ufGroup = CRM_Core_BAO_UFGroup::add($params);
$ufGroupID = $ufGroup->id;
$ufGroup = CRM_Core_BAO_UFGroup::del($ufGroupID);

// Assert that pre hook implemntation was called for delete op.
$systemLogCount = $this->callAPISuccess('SystemLog', 'getcount', [
'message' => "CRM_Core_BAO_UFGroupTest::implementHookPre $ufGroupID",
'level' => 'info',
]);

$this->assertEquals(1, $systemLogCount, 'There should be one system log entry with message "CRM_Core_BAO_UFGroupTest::implementHookPre ' . $ufGroupID . '"');
}

public function testPostHookIsCalledForCreate() {
$this->hookClass->setHook('civicrm_post', array($this, 'implementHookPost'));

$params = [
'title' => 'testPostHookIsCalledForCreate',
'is_active' => 1,
];
$ufGroup = CRM_Core_BAO_UFGroup::add($params);

// Assert that pre hook implemntation was called.
$this->assertEquals('testPostHookIsCalledForCreate', $ufGroup->title);
$this->assertEquals(0, $ufGroup->is_active, 'Is active should be 0');
}

public function testPostHookIsCalledForDelete() {
$this->hookClass->setHook('civicrm_post', array($this, 'implementHookPost'));

$params = [
'title' => 'testPostHookIsCalledForDelete',
'is_active' => 1,
];
$ufGroup = CRM_Core_BAO_UFGroup::add($params);
$ufGroupID = $ufGroup->id;
$ufGroup = CRM_Core_BAO_UFGroup::del($ufGroupID);

// Assert that pre hook implemntation was called for delete op.
$systemLogCount = $this->callAPISuccess('SystemLog', 'getcount', [
'message' => "CRM_Core_BAO_UFGroupTest::implementHookPost $ufGroupID",
'level' => 'info',
]);

$this->assertEquals(1, $systemLogCount, 'There should be one system log entry with message "CRM_Core_BAO_UFGroupTest::implementHookPost ' . $ufGroupID . '"');
}

}

0 comments on commit aa72bcb

Please sign in to comment.