From de6cd5156c615672d179f0dbe51df31a4abb0fdf Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 Dec 2021 15:34:08 -0500 Subject: [PATCH] WordReplacement - Use generic writeRecords/deleteRecords which call hooks --- CRM/Core/BAO/WordReplacement.php | 36 ++++++++++----- Civi/Api4/Generic/Traits/DAOActionTrait.php | 1 + .../Provider/FieldDomainIdSpecProvider.php | 3 +- .../api/v4/Entity/WordReplacementTest.php | 44 +++++++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 tests/phpunit/api/v4/Entity/WordReplacementTest.php diff --git a/CRM/Core/BAO/WordReplacement.php b/CRM/Core/BAO/WordReplacement.php index 818e0b2ec8..feb3d85393 100644 --- a/CRM/Core/BAO/WordReplacement.php +++ b/CRM/Core/BAO/WordReplacement.php @@ -18,7 +18,7 @@ /** * Class CRM_Core_BAO_WordReplacement. */ -class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { +class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement implements \Civi\Test\HookInterface { /** * Class constructor. @@ -105,21 +105,37 @@ class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { } /** - * Delete website. + * Deprecated delete function * + * @deprecated * @param int $id - * WordReplacement id. - * - * @return object + * @return CRM_Core_DAO_WordReplacement */ public static function del($id) { - $dao = new CRM_Core_DAO_WordReplacement(); - $dao->id = $id; - $dao->delete(); - if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { + return static::deleteRecord(['id' => $id]); + } + + /** + * Callback for hook_civicrm_post(). + * @param \Civi\Core\Event\PostEvent $event + */ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + if ($event->action === 'delete') { self::rebuild(); } - return $dao; + } + + /** + * Efficient function to write multiple records then rebuild at the end + * + * @param array[] $records + * @return CRM_Core_DAO_WordReplacement[] + * @throws CRM_Core_Exception + */ + public static function writeRecords(array $records): array { + $records = parent::writeRecords($records); + self::rebuild(); + return $records; } /** diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index 01b7856052..46120cd40b 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -116,6 +116,7 @@ trait DAOActionTrait { 'EntityTag' => 'add', 'GroupContact' => 'add', 'Navigation' => 'writeRecords', + 'WordReplacement' => 'writeRecords', ]; $method = $functionNames[$this->getEntityName()] ?? NULL; if (!isset($method)) { diff --git a/Civi/Api4/Service/Spec/Provider/FieldDomainIdSpecProvider.php b/Civi/Api4/Service/Spec/Provider/FieldDomainIdSpecProvider.php index c833277c4e..bb3533cd02 100644 --- a/Civi/Api4/Service/Spec/Provider/FieldDomainIdSpecProvider.php +++ b/Civi/Api4/Service/Spec/Provider/FieldDomainIdSpecProvider.php @@ -21,7 +21,8 @@ class FieldDomainIdSpecProvider implements Generic\SpecProviderInterface { */ public function modifySpec(RequestSpec $spec) { $domainIdField = $spec->getFieldByName('domain_id'); - if ($domainIdField && $domainIdField->isRequired()) { + // TODO: The WordReplacement entity should have domain_id required so this OR condition can be removed + if ($domainIdField && ($domainIdField->isRequired() || $domainIdField->getEntity() === 'WordReplacement')) { $domainIdField->setRequired(FALSE)->setDefaultValue('current_domain');; } } diff --git a/tests/phpunit/api/v4/Entity/WordReplacementTest.php b/tests/phpunit/api/v4/Entity/WordReplacementTest.php new file mode 100644 index 0000000000..5f493c9f4f --- /dev/null +++ b/tests/phpunit/api/v4/Entity/WordReplacementTest.php @@ -0,0 +1,44 @@ +addValue('find_word', 'Foo') + ->addValue('replace_word', 'Bar') + ->execute() + ->first(); + + $result = \Civi\Api4\WordReplacement::get(FALSE) + ->addWhere('id', '=', $create['id']) + ->execute()->first(); + $this->assertTrue($result['is_active']); + $this->assertEquals('wildcardMatch', $result['match_type']); + $this->assertEquals(\CRM_Core_Config::domainID(), $result['domain_id']); + } + +} -- 2.25.1