From e0ff173228bb3cd653915b5953c5e4ce4433b069 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 2 Jun 2023 18:44:28 +1200 Subject: [PATCH] Fix UFGroupTest to cleanup it's UFGroups --- tests/phpunit/CRM/Core/BAO/UFGroupTest.php | 122 +++++++++++++-------- 1 file changed, 74 insertions(+), 48 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/UFGroupTest.php b/tests/phpunit/CRM/Core/BAO/UFGroupTest.php index d320f9f051..e16106a314 100644 --- a/tests/phpunit/CRM/Core/BAO/UFGroupTest.php +++ b/tests/phpunit/CRM/Core/BAO/UFGroupTest.php @@ -1,18 +1,21 @@ callAPISuccess('SystemLog', 'create', [ + elseif ($op === 'delete') { + $this->callAPISuccess('SystemLog', 'create', [ 'message' => "CRM_Core_BAO_UFGroupTest::implementHookPre $id", 'level' => 'info', ]); @@ -20,13 +23,13 @@ class CRM_Core_BAO_UFGroupTest extends CiviUnitTestCase { } } - public function implementHookPost($op, $objectName, $objectId, &$objectRef) { - if ($objectName == 'UFGroup') { - if ($op == 'create') { + public function implementHookPost($op, $objectName, $objectId, $objectRef): void { + if ($objectName === 'UFGroup') { + if ($op === 'create') { $objectRef->is_active = 0; } - elseif ($op == 'delete') { - $systemLog = $this->callAPISuccess('SystemLog', 'create', [ + elseif ($op === 'delete') { + $this->callAPISuccess('SystemLog', 'create', [ 'message' => "CRM_Core_BAO_UFGroupTest::implementHookPost $objectId", 'level' => 'info', ]); @@ -34,36 +37,40 @@ class CRM_Core_BAO_UFGroupTest extends CiviUnitTestCase { } } - public function testPreHookIsCalledForCreate() { + /** + * Test that when creating a UFGroup the registered pre-hook is called. + * + * @throws \CRM_Core_Exception + */ + public function testPreHookIsCalledForCreate(): void { // Specify pre hook implementation. - $this->hookClass->setHook('civicrm_pre', array($this, 'implementHookPre')); + $this->hookClass->setHook('civicrm_pre', [$this, 'implementHookPre']); - // Create a ufgroup with BAO. - $params = [ + $this->createUFGroup([ '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'); + ]); + // Assert that pre hook implementation was called. + $ufGroup = UFGroup::get()->addWhere('title', '=', 'testPreHookIsCalledForCreate')->execute()->first(); + $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')); + /** + * Test the hook is called during delete. + * + * @throws \CRM_Core_Exception + */ + public function testPreHookIsCalledForDelete(): void { + $this->hookClass->setHook('civicrm_pre', [$this, 'implementHookPre']); - // Create a ufgroup with BAO. - $params = [ + $ufGroupID = $this->createUFGroup([ 'title' => 'testPreHookIsCalledForDelete', 'is_active' => 1, - ]; - $ufGroup = CRM_Core_BAO_UFGroup::add($params); - $ufGroupID = $ufGroup->id; - $ufGroup = CRM_Core_BAO_UFGroup::deleteRecord(['id' => $ufGroupID]); + ])['id']; + + UFGroup::delete()->addWhere('id', '=', $ufGroupID)->execute(); - // Assert that pre hook implemntation was called for delete op. + // Assert that pre hook implementation was called for delete op. $systemLogCount = $this->callAPISuccess('SystemLog', 'getcount', [ 'message' => "CRM_Core_BAO_UFGroupTest::implementHookPre $ufGroupID", 'level' => 'info', @@ -72,32 +79,39 @@ class CRM_Core_BAO_UFGroupTest extends CiviUnitTestCase { $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 = [ + /** + * Test the hook is called when created a UF Group. + * + * @throws \CRM_Core_Exception + */ + public function testPostHookIsCalledForCreate(): void { + $this->hookClass->setHook('civicrm_post', [$this, 'implementHookPost']); + $ufGroup = $this->createUFGroup([ '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'); + // Assert that pre hook implementation 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')); + /** + * Test that the hook fires during UFGroup (profile) delete. + * + * @throws \CRM_Core_Exception + */ + public function testPostHookIsCalledForDelete(): void { + $this->hookClass->setHook('civicrm_post', [$this, 'implementHookPost']); - $params = [ + $ufGroupID = UFGroup::create()->setValues([ 'title' => 'testPostHookIsCalledForDelete', 'is_active' => 1, - ]; - $ufGroup = CRM_Core_BAO_UFGroup::add($params); - $ufGroupID = $ufGroup->id; - $ufGroup = CRM_Core_BAO_UFGroup::deleteRecord(['id' => $ufGroupID]); + ])->execute()->first()['id']; + + UFGroup::delete()->addWhere('id', '=', $ufGroupID)->execute(); - // Assert that pre hook implemntation was called for delete op. + // Assert that pre hook implementation was called for delete op. $systemLogCount = $this->callAPISuccess('SystemLog', 'getcount', [ 'message' => "CRM_Core_BAO_UFGroupTest::implementHookPost $ufGroupID", 'level' => 'info', @@ -106,4 +120,16 @@ class CRM_Core_BAO_UFGroupTest extends CiviUnitTestCase { $this->assertEquals(1, $systemLogCount, 'There should be one system log entry with message "CRM_Core_BAO_UFGroupTest::implementHookPost ' . $ufGroupID . '"'); } + /** + * Create a UF Group. + * + * @return array + * @throws \CRM_Core_Exception + */ + protected function createUFGroup($values): ?array { + $ufGroup = UFGroup::create()->setValues($values)->execute()->first(); + $this->ids['UFGroup'][] = $ufGroup['id']; + return $ufGroup; + } + } -- 2.25.1