/**
* 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.
}
/**
- * 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;
}
/**
'EntityTag' => 'add',
'GroupContact' => 'add',
'Navigation' => 'writeRecords',
+ 'WordReplacement' => 'writeRecords',
];
$method = $functionNames[$this->getEntityName()] ?? NULL;
if (!isset($method)) {
*/
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');;
}
}
--- /dev/null
+<?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']);
+ }
+
+}