From 3b1de011c76ab44aa0f1593d33c7e35da9cab993 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 2 Dec 2021 16:26:28 -0800 Subject: [PATCH] ConformanceTest::checkDeletionAllowed - Coverage for `hook_pre`. Fix `Contact`. 1. Expand assertions used for `hook_post` to check `hook_pre`. 2. Recognize the quirks in how `Contact` records are treated in the hooks. --- .../phpunit/api/v4/Entity/ConformanceTest.php | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index 68d419b669..7194f288ec 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -31,6 +31,8 @@ use Civi\Api4\Event\ValidateValuesEvent; use Civi\Api4\Service\Spec\CustomFieldSpec; use Civi\Api4\Service\Spec\FieldSpec; use Civi\Api4\Utils\CoreUtil; +use Civi\Core\Event\PostEvent; +use Civi\Core\Event\PreEvent; use Civi\Test\HookInterface; /** @@ -432,16 +434,14 @@ class ConformanceTest extends UnitTestCase implements HookInterface { $deleteAction->setUseTrash(FALSE); } - $log = []; - $listen = function($e) use (&$log) { - $log[] = $e->entity . '.' . $e->action; - }; - \Civi::dispatcher()->addListener('hook_civicrm_post', $listen); - $deleteResult = $deleteAction->execute(); - \Civi::dispatcher()->removeListener('hook_civicrm_post', $listen); + $log = $this->withPrePostLogging(function() use (&$deleteAction, &$deleteResult) { + $deleteResult = $deleteAction->execute(); + }); // We should have emitted an event. - $this->assertTrue(in_array("$entity.delete", $log), "$entity should emit hook_civicrm_post() for deletions"); + $hookEntity = ($entity === 'Contact') ? 'Individual' : $entity; /* ooph */ + $this->assertContains("pre.{$hookEntity}.delete", $log, "$entity should emit hook_civicrm_pre() for deletions"); + $this->assertContains("post.{$hookEntity}.delete", $log, "$entity should emit hook_civicrm_post() for deletions"); // should get back an array of deleted id $this->assertEquals([['id' => $id]], (array) $deleteResult); @@ -518,4 +518,38 @@ class ConformanceTest extends UnitTestCase implements HookInterface { return in_array('ReadOnly', $entityClass::getInfo()['type'], TRUE); } + /** + * Temporarily enable logging for `hook_civicrm_pre` and `hook_civicrm_post`. + * + * @param callable $callable + * Run this function. Create a log while running this function. + * @return array + * Log; list of times the hooks were called. + * Ex: ['pre.Event.delete', 'post.Event.delete'] + */ + protected function withPrePostLogging($callable): array { + $log = []; + + $listen = function ($e) use (&$log) { + if ($e instanceof PreEvent) { + $log[] = "pre.{$e->entity}.{$e->action}"; + } + elseif ($e instanceof PostEvent) { + $log[] = "post.{$e->entity}.{$e->action}"; + } + }; + + try { + \Civi::dispatcher()->addListener('hook_civicrm_pre', $listen); + \Civi::dispatcher()->addListener('hook_civicrm_post', $listen); + $callable(); + } + finally { + \Civi::dispatcher()->removeListener('hook_civicrm_pre', $listen); + \Civi::dispatcher()->removeListener('hook_civicrm_post', $listen); + } + + return $log; + } + } -- 2.25.1