WordReplacement - Use generic writeRecords/deleteRecords which call hooks
authorColeman Watts <coleman@civicrm.org>
Thu, 2 Dec 2021 20:34:08 +0000 (15:34 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 4 Dec 2021 13:49:50 +0000 (08:49 -0500)
CRM/Core/BAO/WordReplacement.php
Civi/Api4/Generic/Traits/DAOActionTrait.php
Civi/Api4/Service/Spec/Provider/FieldDomainIdSpecProvider.php
tests/phpunit/api/v4/Entity/WordReplacementTest.php [new file with mode: 0644]

index 818e0b2ec8cd9c77f97fec2bcd635dbe4a5051b8..feb3d85393eca5961918e9795c7a479a64db0555 100644 (file)
@@ -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;
   }
 
   /**
index 01b7856052d46aab91d34407db7d1eeab8219c33..46120cd40beb57534d678c5cbaba7d038bd8bc10 100644 (file)
@@ -116,6 +116,7 @@ trait DAOActionTrait {
       'EntityTag' => 'add',
       'GroupContact' => 'add',
       'Navigation' => 'writeRecords',
+      'WordReplacement' => 'writeRecords',
     ];
     $method = $functionNames[$this->getEntityName()] ?? NULL;
     if (!isset($method)) {
index c833277c4eeba911f2b67da512a48cdb8bb7658f..bb3533cd023afdd8e1d7d845617880ec49a93af2 100644 (file)
@@ -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 (file)
index 0000000..5f493c9
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+
+namespace api\v4\Entity;
+
+use api\v4\UnitTestCase;
+
+/**
+ * @group headless
+ */
+class WordReplacementTest extends UnitTestCase {
+
+  public function testDefaults() {
+    $create = \Civi\Api4\WordReplacement::create(FALSE)
+      ->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']);
+  }
+
+}