From 8c7d3b889dd048cc336957c944f9c0b1127485fc Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 5 Apr 2023 08:19:55 -0400 Subject: [PATCH] REF - Update ContactType BAO to use writeRecord, deprecated add function --- CRM/Admin/Form/ContactType.php | 2 +- CRM/Contact/BAO/ContactType.php | 105 +++++++----------- Civi/Core/Event/PostEvent.php | 2 +- .../BAO/ContactType/ContactSearchTest.php | 6 +- .../Contact/BAO/ContactType/ContactTest.php | 8 +- .../BAO/ContactType/ContactTypeTest.php | 46 +------- .../BAO/ContactType/RelationshipTest.php | 6 +- tests/phpunit/api/v3/ContactTypeTest.php | 10 +- .../phpunit/api/v4/Entity/ContactTypeTest.php | 9 ++ 9 files changed, 70 insertions(+), 124 deletions(-) diff --git a/CRM/Admin/Form/ContactType.php b/CRM/Admin/Form/ContactType.php index 77b92cb89a..05ed078408 100644 --- a/CRM/Admin/Form/ContactType.php +++ b/CRM/Admin/Form/ContactType.php @@ -142,7 +142,7 @@ class CRM_Admin_Form_ContactType extends CRM_Admin_Form { $params['image_URL'] = ''; } - $contactType = CRM_Contact_BAO_ContactType::add($params); + $contactType = CRM_Contact_BAO_ContactType::writeRecord($params); CRM_Core_Session::setStatus(ts("The Contact Type '%1' has been saved.", [1 => $contactType->label] ), ts('Saved'), 'success'); diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 77eaa89407..c02fcf63fe 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -495,9 +495,43 @@ WHERE subtype.name IN ('" . implode("','", $subType) . "' )"; * @param \Civi\Core\Event\PostEvent $event */ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + /** @var CRM_Contact_DAO_ContactType $contactType */ + $contactType = $event->object; + if ($event->action === 'edit' && $contactType->find(TRUE)) { + // Update navigation menu for contact type + $navigation = [ + 'label' => ts("New %1", [1 => $contactType->label]), + 'name' => "New {$contactType->name}", + 'is_active' => $contactType->is_active, + ]; + civicrm_api4('Navigation', 'save', [ + 'checkPermissions' => FALSE, + 'records' => [$navigation], + 'match' => ['name'], + ]); + } + if ($event->action === 'create' && $contactType->find(TRUE)) { + $name = self::getBasicType($contactType->name); + if (!$name) { + return; + } + $navigation = [ + 'label' => ts("New %1", [1 => $contactType->label]), + 'name' => "New {$contactType->name}", + 'url' => "civicrm/contact/add?ct=$name&cst={$contactType->name}&reset=1", + 'permission' => 'add contacts', + 'parent_id:name' => "New $name", + 'is_active' => $contactType->is_active, + ]; + civicrm_api4('Navigation', 'save', [ + 'checkPermissions' => FALSE, + 'records' => [$navigation], + 'match' => ['name'], + ]); + } if ($event->action === 'delete') { $sep = CRM_Core_DAO::VALUE_SEPARATOR; - $subType = "$sep{$event->object->name}$sep"; + $subType = "$sep{$contactType->name}$sep"; // For contacts with just the one sub-type, set to null $sql = " UPDATE civicrm_contact SET contact_sub_type = NULL @@ -511,80 +545,23 @@ WHERE contact_sub_type LIKE '%{$subType}%'"; // remove navigation entry which was auto-created when this sub-type was added \Civi\Api4\Navigation::delete(FALSE) - ->addWhere('name', '=', "New {$event->object->name}") + ->addWhere('name', '=', "New {$contactType->name}") ->addWhere('url', 'LIKE', 'civicrm/contact/add%') // Overide the default which limits to a single domain ->addWhere('domain_id', '>', 0) ->execute(); - - Civi::cache('contactTypes')->clear(); } + Civi::cache('contactTypes')->clear(); } /** - * Add or update Contact SubTypes. - * - * @param array $params - * An assoc array of name/value pairs. - * - * @return CRM_Contact_DAO_ContactType|NULL + * @deprecated + * @return CRM_Contact_DAO_ContactType * @throws \CRM_Core_Exception */ public static function add($params) { - - // label or name - if (empty($params['id']) && empty($params['label'])) { - // @todo consider throwing exception instead. - return NULL; - } - if (empty($params['id']) && empty($params['name'])) { - $params['name'] = ucfirst(CRM_Utils_String::munge($params['label'])); - } - if (!empty($params['parent_id']) && - !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id']) - ) { - return NULL; - } - - $contactType = new CRM_Contact_DAO_ContactType(); - $contactType->copyValues($params); - $contactType->id = $params['id'] ?? NULL; - - $contactType->save(); - if ($contactType->find(TRUE)) { - $contactName = $contactType->name; - $contact = ucfirst($contactType->label); - $active = $contactType->is_active; - } - - if (!empty($params['id'])) { - $newParams = [ - 'label' => ts("New %1", [1 => $contact]), - 'is_active' => $contactType->is_active, - ]; - CRM_Core_BAO_Navigation::processUpdate(['name' => "New $contactName"], $newParams); - } - else { - $name = self::getBasicType($contactName); - if (!$name) { - return NULL; - } - $value = ['name' => "New $name"]; - CRM_Core_BAO_Navigation::retrieve($value, $navinfo); - $navigation = [ - 'label' => ts("New %1", [1 => $contact]), - 'name' => "New $contactName", - 'url' => "civicrm/contact/add?ct=$name&cst=$contactName&reset=1", - 'permission' => 'add contacts', - 'parent_id' => $navinfo['id'], - 'is_active' => $active, - ]; - CRM_Core_BAO_Navigation::add($navigation); - } - CRM_Core_BAO_Navigation::resetNavigation(); - Civi::cache('contactTypes')->clear(); - - return $contactType; + CRM_Core_Error::deprecatedFunctionWarning('writeRecord'); + return self::writeRecord($params); } /** diff --git a/Civi/Core/Event/PostEvent.php b/Civi/Core/Event/PostEvent.php index d620ded7dc..bbf7223907 100644 --- a/Civi/Core/Event/PostEvent.php +++ b/Civi/Core/Event/PostEvent.php @@ -35,7 +35,7 @@ class PostEvent extends GenericHookEvent { public $id; /** - * @var CRM_Core_DAO + * @var \CRM_Core_DAO */ public $object; diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactSearchTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactSearchTest.php index 8e1604887e..a68928fcda 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactSearchTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactSearchTest.php @@ -91,7 +91,7 @@ class CRM_Contact_BAO_ContactType_ContactSearchTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->student = $params['name']; $parents = 'indivi_parent' . substr(sha1(rand()), 0, 7); @@ -102,7 +102,7 @@ class CRM_Contact_BAO_ContactType_ContactSearchTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->parent = $params['name']; $organizations = 'org_sponsor' . substr(sha1(rand()), 0, 7); @@ -113,7 +113,7 @@ class CRM_Contact_BAO_ContactType_ContactSearchTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->sponsor = $params['name']; $this->individualParams = [ diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php index 637fdd88b2..4fcbc02c64 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTest.php @@ -36,7 +36,7 @@ class CRM_Contact_BAO_ContactType_ContactTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->student = $params['name']; $params = [ @@ -46,7 +46,7 @@ class CRM_Contact_BAO_ContactType_ContactTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->parent = $params['name']; $params = [ @@ -56,7 +56,7 @@ class CRM_Contact_BAO_ContactType_ContactTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->sponsor = $params['name']; $params = [ @@ -66,7 +66,7 @@ class CRM_Contact_BAO_ContactType_ContactTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->team = $params['name']; } diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php index 59642a2122..f7079061ce 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php @@ -348,7 +348,7 @@ class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - $result = CRM_Contact_BAO_ContactType::add($params); + $result = CRM_Contact_BAO_ContactType::writeRecord($params); $this->assertEquals($result->label, $params['label']); $this->assertEquals($result->name, $params['name']); $this->assertEquals($result->parent_id, $params['parent_id']); @@ -361,7 +361,7 @@ class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 2, 'is_active' => 0, ]; - $result = CRM_Contact_BAO_ContactType::add($params); + $result = CRM_Contact_BAO_ContactType::writeRecord($params); $this->assertEquals($result->label, $params['label']); $this->assertEquals($result->name, $params['name']); $this->assertEquals($result->parent_id, $params['parent_id']); @@ -369,46 +369,6 @@ class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase { CRM_Contact_BAO_ContactType::deleteRecord(['id' => $result->id]); } - /** - * Test add() with invalid data - */ - public function testAddInvalid1() { - - // parent id does not exist in db - $params = [ - 'label' => 'subType', - 'name' => 'subType', - // non existent - 'parent_id' => 100, - 'is_active' => 1, - ]; - $result = CRM_Contact_BAO_ContactType::add($params); - $this->assertEquals($result, NULL); - } - - public function testAddInvalid2() { - - // params does not have name and label keys - $params = [ - 'parent_id' => 1, - 'is_active' => 1, - ]; - $result = CRM_Contact_BAO_ContactType::add($params); - $this->assertEquals($result, NULL); - } - - public function testAddInvalid3() { - - // params does not have parent_id - $params = [ - 'label' => 'subType', - 'name' => 'subType', - 'is_active' => 1, - ]; - $result = CRM_Contact_BAO_ContactType::add($params); - $this->assertEquals($result, NULL); - } - /** * Test del() with valid data. */ @@ -420,7 +380,7 @@ class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - $subtype = CRM_Contact_BAO_ContactType::add($params); + $subtype = CRM_Contact_BAO_ContactType::writeRecord($params); $result = CRM_Contact_BAO_ContactType::subTypes(); $this->assertEquals(TRUE, in_array($subtype->name, $result, TRUE)); $this->callAPISuccess('ContactType', 'delete', ['id' => $subtype->id]); diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php index 525ea7a8fa..29680baad6 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php @@ -35,7 +35,7 @@ class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->student = $params['name']; $params = [ @@ -45,7 +45,7 @@ class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->parent = $params['name']; $params = [ @@ -55,7 +55,7 @@ class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->sponsor = $params['name']; //create contacts diff --git a/tests/phpunit/api/v3/ContactTypeTest.php b/tests/phpunit/api/v3/ContactTypeTest.php index 6406f58172..608c0acd42 100644 --- a/tests/phpunit/api/v3/ContactTypeTest.php +++ b/tests/phpunit/api/v3/ContactTypeTest.php @@ -41,7 +41,7 @@ class api_v3_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->subTypeIndividual = $params['name']; $params = [ @@ -51,7 +51,7 @@ class api_v3_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->subTypeOrganization = $params['name']; $params = [ @@ -61,7 +61,7 @@ class api_v3_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 2, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $this->subTypeHousehold = $params['name']; } @@ -271,7 +271,7 @@ class api_v3_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 1, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $subtype = $params['name']; // check for Type:Individual subype:sub_individual @@ -314,7 +314,7 @@ class api_v3_ContactTypeTest extends CiviUnitTestCase { 'parent_id' => 3, 'is_active' => 1, ]; - CRM_Contact_BAO_ContactType::add($params); + CRM_Contact_BAO_ContactType::writeRecord($params); $subtype = $params['name']; // check for Type:Organization subype:sub_organization diff --git a/tests/phpunit/api/v4/Entity/ContactTypeTest.php b/tests/phpunit/api/v4/Entity/ContactTypeTest.php index a339b25b05..11718ac18a 100644 --- a/tests/phpunit/api/v4/Entity/ContactTypeTest.php +++ b/tests/phpunit/api/v4/Entity/ContactTypeTest.php @@ -40,6 +40,15 @@ class ContactTypeTest extends Api4TestBase implements TransactionalInterface { // Menu item should have been auto-created $this->assertCount(1, Navigation::get(FALSE)->addWhere('name', '=', 'New Tester')->execute()); + ContactType::update(FALSE) + ->addWhere('name', '=', 'Tester') + ->addValue('label', 'Tested') + ->execute(); + + // Menu item should have been updated + $nav = Navigation::get(FALSE)->addWhere('name', '=', 'New Tester')->execute()->single(); + $this->assertEquals('New Tested', $nav['label']); + ContactType::delete(FALSE) ->addWhere('name', '=', 'Tester') ->execute(); -- 2.25.1