-
-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18995 from lucky091588/2199_ufgroup_pre_post_hook
Add pre() and post() hooks for ufgroup entity
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . '"'); | ||
} | ||
|
||
} |