From 5125fd3133f4fd2a55999eac2376528bb6272834 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 08:31:55 +1300 Subject: [PATCH 1/7] Standardise setUp for CiviCase shared code This lets the function deal properly with the custom fields & does not truncate relationship types - we might still need to fix some places to delete created relationships - but we should do that where we disable civi-case --- tests/fixtures/case_relationships.json | 48 ----------------- tests/phpunit/CRM/Case/BAO/CaseTest.php | 38 +------------- tests/phpunit/CiviTest/CiviCaseTestCase.php | 57 +++++++++++++-------- tests/phpunit/api/v3/ActivityCaseTest.php | 19 ++----- 4 files changed, 42 insertions(+), 120 deletions(-) delete mode 100644 tests/fixtures/case_relationships.json diff --git a/tests/fixtures/case_relationships.json b/tests/fixtures/case_relationships.json deleted file mode 100644 index b53a8597de4f..000000000000 --- a/tests/fixtures/case_relationships.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "civicrm_relationship_type": [ - { - "name_a_b": "Homeless Services Coordinator is", - "label_a_b": "Homeless Services Coordinator is", - "name_b_a": "Homeless Services Coordinator", - "label_b_a": "Homeless Services Coordinator", - "description": "Homeless Services Coordinator", - "contact_type_a": "Individual", - "contact_type_b": "Individual", - "is_reserved": 0, - "is_active": 1 - }, - { - "name_a_b": "Health Services Coordinator is", - "label_a_b": "Health Services Coordinator is", - "name_b_a": "Health Services Coordinator", - "label_b_a": "Health Services Coordinator", - "description": "Health Services Coordinator", - "contact_type_a": "Individual", - "contact_type_b": "Individual", - "is_reserved": 0, - "is_active": 1 - }, - { - "name_a_b": "Senior Services Coordinator is", - "label_a_b": "Senior Services Coordinator is", - "name_b_a": "Senior Services Coordinator", - "label_b_a": "Senior Services Coordinator", - "description": "Senior Services Coordinator", - "contact_type_a": "Individual", - "contact_type_b": "Individual", - "is_reserved": 0, - "is_active": 1 - }, - { - "name_a_b": "Benefits Specialist is", - "label_a_b": "Benefits Specialist is", - "name_b_a": "Benefits Specialist", - "label_b_a": "Benefits Specialist", - "description": "Benefits Specialist", - "contact_type_a": "Individual", - "contact_type_b": "Individual", - "is_reserved": 0, - "is_active": 1 - } - ] -} diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php index 6b7bbf56f82c..e9029add643d 100644 --- a/tests/phpunit/CRM/Case/BAO/CaseTest.php +++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php @@ -4,38 +4,7 @@ * Class CRM_Case_BAO_CaseTest * @group headless */ -class CRM_Case_BAO_CaseTest extends CiviUnitTestCase { - - const TABLES_TO_TRUNCATE = [ - 'civicrm_activity', - 'civicrm_group_contact', - 'civicrm_contact', - 'civicrm_custom_group', - 'civicrm_custom_field', - 'civicrm_case', - 'civicrm_case_contact', - 'civicrm_case_activity', - 'civicrm_case_type', - 'civicrm_file', - 'civicrm_entity_file', - 'civicrm_activity_contact', - 'civicrm_managed', - 'civicrm_relationship', - 'civicrm_relationship_type', - ]; - - public function setUp(): void { - parent::setUp(); - - $this->quickCleanup(self::TABLES_TO_TRUNCATE); - - $this->loadAllFixtures(); - - // I don't understand why need to disable but if don't then only one - // case type is defined on 2nd and subsequent dataprovider runs. - CRM_Core_BAO_ConfigSetting::disableComponent('CiviCase'); - CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); - } +class CRM_Case_BAO_CaseTest extends CiviCaseTestCase { /** * Make sure that the latest case activity works accurately. @@ -57,11 +26,6 @@ public function testCaseActivity(): void { } } - protected function tearDown(): void { - $this->quickCleanup(self::TABLES_TO_TRUNCATE, TRUE); - parent::tearDown(); - } - public function testAddCaseToContact(): void { $this->createLoggedInUser(); $params = [ diff --git a/tests/phpunit/CiviTest/CiviCaseTestCase.php b/tests/phpunit/CiviTest/CiviCaseTestCase.php index 5b35bd62025d..64d509660c2a 100644 --- a/tests/phpunit/CiviTest/CiviCaseTestCase.php +++ b/tests/phpunit/CiviTest/CiviCaseTestCase.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\RelationshipType; + /** * Class CiviReportTestCase */ @@ -67,33 +69,12 @@ public function setUp(): void { // Now, the rule is simply: use the "name" from "civicrm_case_type.name". $this->caseType = 'housing_support'; $this->caseTypeId = 1; - $this->tablesToTruncate = [ - 'civicrm_activity', - 'civicrm_contact', - 'civicrm_custom_group', - 'civicrm_custom_field', - 'civicrm_case', - 'civicrm_case_contact', - 'civicrm_case_activity', - 'civicrm_case_type', - 'civicrm_activity_contact', - 'civicrm_managed', - 'civicrm_relationship', - 'civicrm_relationship_type', - 'civicrm_uf_match', - ]; - - $this->quickCleanup($this->tablesToTruncate); $this->loadAllFixtures(); // enable the default custom templates for the case type xml files $this->customDirectories(['template_path' => TRUE]); - // case is not enabled by default - $enableResult = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); - $this->assertTrue($enableResult, 'Cannot enable CiviCase in line ' . __LINE__); - /** @var \CRM_Utils_Hook_UnitTests $hooks */ $hooks = \CRM_Utils_Hook::singleton(); $hooks->setHook('civicrm_caseTypes', [$this, 'hook_caseTypes']); @@ -114,11 +95,45 @@ public function setUp(): void { */ public function tearDown(): void { $this->customDirectories(['template_path' => FALSE]); + $this->tablesToTruncate = [ + 'civicrm_activity', + 'civicrm_contact', + 'civicrm_case', + 'civicrm_case_contact', + 'civicrm_case_activity', + 'civicrm_case_type', + 'civicrm_activity_contact', + 'civicrm_managed', + 'civicrm_relationship', + 'civicrm_uf_match', + 'civicrm_activity', + 'civicrm_group_contact', + 'civicrm_file', + 'civicrm_entity_file', + ]; $this->quickCleanup($this->tablesToTruncate, TRUE); CRM_Case_XMLRepository::singleton(TRUE); parent::tearDown(); } + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); + } + + public static function tearDownAfterClass(): void { + CRM_Core_BAO_ConfigSetting::disableComponent('CiviCase'); + RelationshipType::delete(FALSE) + ->addWhere('name_b_a', 'IN', [ + 'Homeless Services Coordinator', + 'Health Services Coordinator', + 'Senior Services Coordinator', + 'Benefits Specialist', + ]) + ->execute(); + parent::tearDownAfterClass(); + } + /** * Subclasses may override this if they want to be explicit about the case-type definition. * diff --git a/tests/phpunit/api/v3/ActivityCaseTest.php b/tests/phpunit/api/v3/ActivityCaseTest.php index 8690af852850..3cf7d7b7ecd1 100644 --- a/tests/phpunit/api/v3/ActivityCaseTest.php +++ b/tests/phpunit/api/v3/ActivityCaseTest.php @@ -7,10 +7,6 @@ * @group headless */ class api_v3_ActivityCaseTest extends CiviCaseTestCase { - protected $_params; - protected $_entity; - protected $_cid; - /** * @var array * APIv3 Result (Case.create) @@ -30,20 +26,15 @@ class api_v3_ActivityCaseTest extends CiviCaseTestCase { * and redirect stdin to a temporary file. */ public function setUp(): void { - $this->_entity = 'case'; - parent::setUp(); - - $this->_cid = $this->individualCreate(); - $this->_case = $this->callAPISuccess('case', 'create', [ 'case_type_id' => $this->caseTypeId, 'subject' => __CLASS__, - 'contact_id' => $this->_cid, + 'contact_id' => $this->individualCreate(), ]); $this->_otherActivity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->_cid, + 'source_contact_id' => $this->ids['Contact']['individual_0'], 'activity_type_id' => 'Phone Call', 'subject' => 'Ask not what your API can do for you, but what you can do for your API.', ]); @@ -61,7 +52,7 @@ public function testActivityCreateOnCase(): void { ]; foreach ($subjectArr as $subject) { $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->_cid, + 'source_contact_id' => $this->ids['Contact']['individual_0'], 'activity_type_id' => 'Phone Call', 'subject' => $subject, ]); @@ -76,7 +67,7 @@ public function testActivityCreateOnCase(): void { */ public function testActivityEditAddingCaseIdInSubject(): void { $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->_cid, + 'source_contact_id' => $this->ids['Contact']['individual_0'], 'activity_type_id' => 'Meeting', 'subject' => 'Starting as non-case activity 1', ]); @@ -92,7 +83,7 @@ public function testActivityEditAddingCaseIdInSubject(): void { // Now same thing but just with the id not the hash $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->_cid, + 'source_contact_id' => $this->ids['Contact']['individual_0'], 'activity_type_id' => 'Meeting', 'subject' => 'Starting as non-case activity 2', ]); From 6beb7f70aaf0b403f9039638bb577d6bd4562327 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 09:54:55 +1300 Subject: [PATCH 2/7] Remove case from pseudoconstant test The value of this test was mostly transitional. With it now transitioned and lots of test cover on case elsewhere the overhead of managing case data from this test doesn't seem warranted (given the code needs a lot of re-work) --- tests/phpunit/CRM/Core/PseudoConstantTest.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index 890b4f56aa16..d3af69482ed3 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -17,10 +17,6 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { public function setUp(): void { parent::setUp(); - - $this->loadAllFixtures(); - - CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); } @@ -927,16 +923,6 @@ public function testOptionValues(): void { 'max' => 200, ], ], - 'CRM_Case_DAO_Case' => [ - [ - 'fieldName' => 'status_id', - 'sample' => 'Ongoing', - ], - [ - 'fieldName' => 'case_type_id', - 'sample' => 'Housing Support', - ], - ], 'CRM_Report_DAO_ReportInstance' => [ [ 'fieldName' => 'domain_id', From 24ca45d29c55fc37b9976a25a19021bb72adbe6b Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 10:05:48 +1300 Subject: [PATCH 3/7] Further minor cleanups --- tests/phpunit/CRM/Activity/Page/AJAXTest.php | 51 ++++++-------------- tests/phpunit/CiviTest/CiviCaseTestCase.php | 3 ++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 9 ++-- 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/tests/phpunit/CRM/Activity/Page/AJAXTest.php b/tests/phpunit/CRM/Activity/Page/AJAXTest.php index 7f6b3dee305a..885d0126a56f 100644 --- a/tests/phpunit/CRM/Activity/Page/AJAXTest.php +++ b/tests/phpunit/CRM/Activity/Page/AJAXTest.php @@ -3,48 +3,27 @@ /** * @group headless */ -class CRM_Activity_Page_AJAXTest extends CiviUnitTestCase { - - /** - * @var int - */ - private $loggedInUser; - - /** - * @var int - */ - private $target; - - public function setUp(): void { - parent::setUp(); - - $this->loadAllFixtures(); - - CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); - - $this->loggedInUser = $this->createLoggedInUser(); - $this->target = $this->individualCreate(); - } +class CRM_Activity_Page_AJAXTest extends CiviCaseTestCase { /** * Test the underlying function that implements file-on-case. * - * The UI is a quickform but it's only realized as a popup ajax form that + * The UI is a quick form but it's only realized as a popup ajax form that * doesn't have its own postProcess. Instead the values are ultimately * passed to the function this test is testing. So there's no form or ajax * being tested here, just the final act of filing the activity. */ public function testConvertToCaseActivity(): void { $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->loggedInUser, + 'source_contact_id' => $this->createLoggedInUser(), 'activity_type_id' => 'Meeting', 'subject' => 'test file on case', 'status_id' => 'Completed', - 'target_id' => $this->target, + 'target_id' => $this->individualCreate([], 'target'), ]); $case = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => $this->target, + 'contact_id' => $this->ids['Contact']['target'], 'case_type_id' => 'housing_support', 'subject' => 'Needs housing', ]); @@ -53,7 +32,7 @@ public function testConvertToCaseActivity(): void { 'activityID' => $activity['id'], 'caseID' => $case['id'], 'mode' => 'file', - 'targetContactIds' => $this->target, + 'targetContactIds' => $this->ids['Contact']['target'], ]; $result = CRM_Activity_Page_AJAX::_convertToCaseActivity($params); @@ -72,9 +51,9 @@ public function testConvertToCaseActivity(): void { * Similar to testConvertToCaseActivity above but for copy-to-case. */ public function testCopyToCase(): void { - $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => $this->target, - 'case_type_id' => 'housing_support', + $case1 = $this->createTestEntity('Case', [ + 'contact_id' => $this->individualCreate([], 'target'), + 'case_type_id:name' => 'housing_support', 'subject' => 'Needs housing', ]); $contact2 = $this->individualCreate([], 1, TRUE); @@ -85,11 +64,11 @@ public function testCopyToCase(): void { ]); $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->loggedInUser, + 'source_contact_id' => $this->createLoggedInUser(), 'activity_type_id' => 'Meeting', 'subject' => 'To be copied to case', 'status_id' => 'Completed', - 'target_id' => $this->target, + 'target_id' => $this->ids['Contact']['target'], 'case_id' => $case1['id'], ]); @@ -127,7 +106,7 @@ public function testCopyToCase(): void { */ public function testMoveToCase(): void { $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => $this->target, + 'contact_id' => $this->individualCreate([], 'target'), 'case_type_id' => 'housing_support', 'subject' => 'Needs housing', ]); @@ -139,11 +118,11 @@ public function testMoveToCase(): void { ]); $activity = $this->callAPISuccess('Activity', 'create', [ - 'source_contact_id' => $this->loggedInUser, + 'source_contact_id' => $this->createLoggedInUser(), 'activity_type_id' => 'Meeting', 'subject' => 'To be moved to case', 'status_id' => 'Completed', - 'target_id' => $this->target, + 'target_id' => $this->ids['Contact']['target'], 'case_id' => $case1['id'], ]); @@ -189,7 +168,7 @@ public function testPreserveFilters(): void { // Need a logged in user since the filter is per-contact, but this // cid is the visited contact so could be anything, but might as well // use this one. - 'cid' => $this->loggedInUser, + 'cid' => $this->createLoggedInUser(), 'draw' => '5', 'columns' => [ 0 => [ diff --git a/tests/phpunit/CiviTest/CiviCaseTestCase.php b/tests/phpunit/CiviTest/CiviCaseTestCase.php index 64d509660c2a..52a4bdd1dec3 100644 --- a/tests/phpunit/CiviTest/CiviCaseTestCase.php +++ b/tests/phpunit/CiviTest/CiviCaseTestCase.php @@ -121,6 +121,9 @@ public static function setUpBeforeClass(): void { CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); } + /** + * @throws \CRM_Core_Exception + */ public static function tearDownAfterClass(): void { CRM_Core_BAO_ConfigSetting::disableComponent('CiviCase'); RelationshipType::delete(FALSE) diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 4c6d323676c8..4e104f4b839d 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -542,6 +542,9 @@ protected function tearDown(): void { DedupeRule::delete(FALSE)->addWhere('dedupe_rule_group_id', 'IN', $this->ids['DedupeRuleGroup'])->execute(); DedupeRuleGroup::delete(FALSE)->addWhere('id', 'IN', $this->ids['DedupeRuleGroup'])->execute(); } + if (!empty($this->ids['RelationshipType'])) { + RelationshipType::delete(FALSE)->addWhere('id', 'IN', $this->ids['RelationshipType'])->execute(); + } unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); parent::tearDown(); } @@ -753,10 +756,8 @@ public function membershipRenewalDate($durationUnit, $membershipEndDate) { * @param array $params * * @return int - * - * @throws \CRM_Core_Exception */ - public function relationshipTypeCreate($params = []) { + public function relationshipTypeCreate(array $params = []): int { $params = array_merge([ 'name_a_b' => 'Relation 1 for relationship type create', 'name_b_a' => 'Relation 2 for relationship type create', @@ -766,7 +767,7 @@ public function relationshipTypeCreate($params = []) { 'is_active' => 1, ], $params); - $result = $this->callAPISuccess('relationship_type', 'create', $params); + $result = $this->createTestEntity('RelationshipType', $params); CRM_Core_PseudoConstant::flush('relationshipType'); return $result['id']; From 7caec8b2f4d6283e272b55f05f8d970c127a5621 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 10:41:21 +1300 Subject: [PATCH 4/7] Further minor clean ups --- CRM/Case/XMLProcessor/Process.php | 4 +- tests/fixtures/contacts.json | 52 ------- .../CRM/Case/XMLProcessor/ProcessTest.php | 47 ++---- tests/phpunit/CRM/Case/XMLProcessorTest.php | 22 +-- tests/phpunit/CiviTest/CiviCaseTestCase.php | 1 + tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- tests/phpunit/api/v3/CaseTest.php | 137 +++++++----------- 7 files changed, 69 insertions(+), 196 deletions(-) delete mode 100644 tests/fixtures/contacts.json diff --git a/CRM/Case/XMLProcessor/Process.php b/CRM/Case/XMLProcessor/Process.php index 1cbc49dc6499..d73aced504e6 100644 --- a/CRM/Case/XMLProcessor/Process.php +++ b/CRM/Case/XMLProcessor/Process.php @@ -401,10 +401,8 @@ public function isActivityPresent(&$params) { * @param array $params * * @return bool - * @throws CRM_Core_Exception - * @throws Exception */ - public function createActivity($activityTypeXML, &$params) { + public function createActivity($activityTypeXML, &$params): bool { $activityTypeName = (string) $activityTypeXML->name; $activityTypes = CRM_Case_PseudoConstant::caseActivityType(TRUE, TRUE); $activityTypeInfo = $activityTypes[$activityTypeName] ?? NULL; diff --git a/tests/fixtures/contacts.json b/tests/fixtures/contacts.json deleted file mode 100644 index ba1ddfdd5327..000000000000 --- a/tests/fixtures/contacts.json +++ /dev/null @@ -1,52 +0,0 @@ -{ - "civicrm_contact": [ - { - "id": 3, - "contact_type": "Individual", - "contact_sub_type": null, - "do_not_email": 0, - "do_not_phone": 0, - "do_not_mail": 0, - "do_not_sms": 0, - "do_not_trade": 0, - "is_opt_out": 0, - "legal_identifier": null, - "sort_name": "Site Administrator", - "display_name": "Site Administrator", - "nick_name": null, - "legal_name": null, - "image_URL": null, - "preferred_communication_method": null, - "hash": null, - "api_key": null, - "source": null, - "first_name": "Site", - "middle_name": null, - "last_name": "Administrator" - }, - { - "id": 17, - "contact_type": "Individual", - "contact_sub_type": null, - "do_not_email": 0, - "do_not_phone": 0, - "do_not_mail": 0, - "do_not_sms": 0, - "do_not_trade": 0, - "is_opt_out": 0, - "legal_identifier": null, - "sort_name": null, - "display_name": "Test Contact", - "nick_name": null, - "legal_name": null, - "image_URL": null, - "preferred_communication_method": null, - "hash": null, - "api_key": null, - "source": null, - "first_name": "Test", - "middle_name": null, - "last_name": "Contact" - } - ] -} diff --git a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php index 567a3f15bb2a..94277460f280 100644 --- a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php +++ b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php @@ -54,12 +54,6 @@ public function setUp(): void { $this->process = new CRM_Case_XMLProcessor_Process(); } - public function tearDown(): void { - $this->deleteMoreRelationshipTypes(); - - parent::tearDown(); - } - /** * Creates sample contacts. */ @@ -120,25 +114,14 @@ protected function setupRelationships() { 'name_b_a' => 'Employer', ], ]; - - foreach ($this->relationships as $name => &$relationship) { - $relationship['type_id'] = $this->relationshipTypeCreate([ - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'name_a_b' => $relationship['name_a_b'], - 'label_a_b' => $relationship['name_a_b'], - 'name_b_a' => $relationship['name_b_a'], - 'label_b_a' => $relationship['name_b_a'], - ]); - - if (isset($relationship['contact_id_a'])) { - $this->callAPISuccess('Relationship', 'create', [ - 'contact_id_a' => $relationship['contact_id_a'], - 'contact_id_b' => $relationship['contact_id_b'], - 'relationship_type_id' => $relationship['type_id'], - ]); - } - } + $this->relationshipTypeCreate([ + 'contact_type_a' => 'Individual', + 'contact_type_b' => 'Individual', + 'name_a_b' => 'Pupil of', + 'label_a_b' => 'Pupil of', + 'name_b_a' => 'Instructor', + 'label_b_a' => 'Instructor', + ]); } /** @@ -193,15 +176,6 @@ protected function setupMoreRelationshipTypes() { } } - /** - * Clean up additional relationship types (tearDown). - */ - protected function deleteMoreRelationshipTypes() { - foreach ($this->moreRelationshipTypes as $relationship) { - $this->callAPISuccess('relationship_type', 'delete', ['id' => $relationship['type_id']]); - } - } - /** * Defines the the activity parameters and XML definitions. These can be used * to create the activity. @@ -235,6 +209,8 @@ public function testCreateActivityWithDefaultContactByRelationship(): void { * Test the creation of activities where the default assignee should not * end up being a contact from another case where it has the same client * and relationship. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): void { /* @@ -469,6 +445,7 @@ public function testCaseRoles($key, $xmlString, $expected, $dontcare) { * we don't care about those expected values. * @param array $expected * + * @throws \Exception * @dataProvider xmlCaseRoleDataProvider */ public function testLocateNameOrLabel($key, $xmlString, $dontcare, $expected) { @@ -489,7 +466,7 @@ public function testLocateNameOrLabel($key, $xmlString, $dontcare, $expected) { * Data provider for testCaseRoles and testLocateNameOrLabel * @return array */ - public function xmlCaseRoleDataProvider() { + public function xmlCaseRoleDataProvider(): array { return [ // Simulate one that has been converted to the format it should be going // forward, where name is the actual name, i.e. same as machineName. diff --git a/tests/phpunit/CRM/Case/XMLProcessorTest.php b/tests/phpunit/CRM/Case/XMLProcessorTest.php index 54b98060537c..7b0985c87365 100644 --- a/tests/phpunit/CRM/Case/XMLProcessorTest.php +++ b/tests/phpunit/CRM/Case/XMLProcessorTest.php @@ -7,23 +7,12 @@ */ class CRM_Case_XMLProcessorTest extends CiviCaseTestCase { - /** - * @var CRM_Case_XMLProcessor - */ - private $processor; - - public function setUp(): void { - parent::setUp(); - - $this->processor = new CRM_Case_XMLProcessor(); - } - /** * Test that allRelationshipTypes() doesn't have name and label mixed up * and that is has the right directions. */ public function testAllRelationshipTypes(): void { - + $processor = new CRM_Case_XMLProcessor(); // Add a relationship type to test against. $params = [ 'contact_type_a' => 'Individual', @@ -34,23 +23,20 @@ public function testAllRelationshipTypes(): void { 'label_b_a' => 'Food poison tester for', 'description' => 'Food poison tester', ]; - $result = $this->callAPISuccess('relationship_type', 'create', $params); + $result = $this->createTestEntity('RelationshipType', $params); $relationshipTypeID = $result['id']; // All we can test against is label, so just check A and B are right (or // wrong, depending on your point of view). Let's not use the words right // and wrong let's just call it one way and the other way. - $relationshipTypes = $this->processor->allRelationshipTypes(FALSE); + $relationshipTypes = $processor->allRelationshipTypes(FALSE); $this->assertEquals('Food poison tester is', $relationshipTypes["{$relationshipTypeID}_a_b"]); $this->assertEquals('Food poison tester for', $relationshipTypes["{$relationshipTypeID}_b_a"]); // For true, B and A are the other way around here. - $relationshipTypes = $this->processor->allRelationshipTypes(TRUE); + $relationshipTypes = $processor->allRelationshipTypes(TRUE); $this->assertEquals('Food poison tester is', $relationshipTypes["{$relationshipTypeID}_b_a"]); $this->assertEquals('Food poison tester for', $relationshipTypes["{$relationshipTypeID}_a_b"]); - - // cleanup - $this->callAPISuccess('relationship_type', 'delete', ['id' => $relationshipTypeID]); } } diff --git a/tests/phpunit/CiviTest/CiviCaseTestCase.php b/tests/phpunit/CiviTest/CiviCaseTestCase.php index 52a4bdd1dec3..6c6cd3301771 100644 --- a/tests/phpunit/CiviTest/CiviCaseTestCase.php +++ b/tests/phpunit/CiviTest/CiviCaseTestCase.php @@ -70,6 +70,7 @@ public function setUp(): void { $this->caseType = 'housing_support'; $this->caseTypeId = 1; + $this->individualCreate(['first_name' => 'site', 'last_name' => 'admin'], 'site_admin'); $this->loadAllFixtures(); // enable the default custom templates for the case type xml files diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 4e104f4b839d..6c8c367de5b9 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -767,7 +767,7 @@ public function relationshipTypeCreate(array $params = []): int { 'is_active' => 1, ], $params); - $result = $this->createTestEntity('RelationshipType', $params); + $result = $this->createTestEntity('RelationshipType', $params, $params['name_a_b']); CRM_Core_PseudoConstant::flush('relationshipType'); return $result['id']; diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index c75d34b3f322..4fd77fee8cfa 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -37,9 +37,7 @@ */ class api_v3_CaseTest extends CiviCaseTestCase { protected $_params; - protected $_entity; protected $_apiversion = 3; - protected $followup_activity_type_value; /** * Activity ID of created case. * @@ -47,11 +45,6 @@ class api_v3_CaseTest extends CiviCaseTestCase { */ protected $_caseActivityId; - /** - * @var \Civi\Core\SettingsStack - */ - protected $settingsStack; - /** * Test setup for every test. * @@ -59,64 +52,31 @@ class api_v3_CaseTest extends CiviCaseTestCase { * and redirect stdin to a temporary file. */ public function setUp(): void { - $this->_entity = 'case'; - parent::setUp(); - $activityTypes = $this->callAPISuccess('option_value', 'get', [ - 'option_group_id' => 2, - 'name' => 'Follow Up', - 'label' => 'Follow Up', - 'sequential' => 1, - ]); - $this->followup_activity_type_value = $activityTypes['values'][0]['value']; - $this->_params = [ 'case_type_id' => $this->caseTypeId, 'subject' => 'Test case', - 'contact_id' => 17, - ]; - - $this->settingsStack = new \Civi\Core\SettingsStack(); - } - - public function tearDown(): void { - $this->settingsStack->popAll(); - parent::tearDown(); - } - - /** - * Check with empty array. - */ - public function testCaseCreateEmpty(): void { - $this->callAPIFailure('case', 'create', []); - } - - /** - * Check if required fields are not passed. - */ - public function testCaseCreateWithoutRequired(): void { - $params = [ - 'subject' => 'this case should fail', - 'case_type_id' => 1, + 'contact_id' => $this->individualCreate([], 'case_contact'), ]; - - $this->callAPIFailure('case', 'create', $params); } /** * Test Getlist with id and case_id */ public function testCaseGetListById(): void { - $params = $this->_params; - $params['contact_id'] = $this->individualCreate(); + $params = [ + 'contact_id' => $this->individualCreate(), + 'case_type_id' => 'housing_support', + 'subject' => 'Test case', + ]; //Create 3 sample Cases. - $case1 = $this->callAPISuccess('case', 'create', $params); + $case1 = $this->callAPISuccess('Case', 'create', $params); $params['subject'] = 'Test Case 2'; - $case2 = $this->callAPISuccess('case', 'create', $params); + $case2 = $this->callAPISuccess('Case', 'create', $params); $params['subject'] = 'Test Case 3'; - $this->callAPISuccess('case', 'create', $params); + $this->callAPISuccess('Case', 'create', $params); $getParams = [ 'id' => [$case1['id']], @@ -129,10 +89,10 @@ public function testCaseGetListById(): void { 'case_id.end_date' => ['IS NULL' => 1], ], ]; - $result = $this->callAPISuccess('case', 'getlist', $getParams); + $result = $this->callAPISuccess('Case', 'getlist', $getParams); //Only 1 case should be returned. - $this->assertEquals(count($result['values']), 1); + $this->assertCount(1, $result['values']); $this->assertEquals($result['values'][0]['id'], $case1['id']); // For this next part to work we need to make sure we don't have a ton of cases in total. @@ -150,7 +110,7 @@ public function testCaseGetListById(): void { 'case_id.end_date' => ['IS NULL' => 1], ], ]; - $result = $this->callAPISuccess('case', 'getlist', $getParams); + $result = $this->callAPISuccess('Case', 'getlist', $getParams); $this->assertCount(1, $result['values']); $this->assertEquals(2, $result['values'][0]['id']); } @@ -159,15 +119,17 @@ public function testCaseGetListById(): void { * Test create function with valid parameters. */ public function testCaseCreate(): void { - $params = $this->_params; - // Test using name instead of value. - unset($params['case_type_id']); - $params['case_type'] = $this->caseType; - $result = $this->callAPISuccess('case', 'create', $params); + $params = [ + 'contact_id' => $this->individualCreate(), + 'case_type' => 'housing_support', + 'subject' => 'Test case', + ]; + + $result = $this->callAPISuccess('Case', 'create', $params); $id = $result['id']; // Check result - $result = $this->callAPISuccess('case', 'get', ['id' => $id]); + $result = $this->callAPISuccess('Case', 'get', ['id' => $id]); $this->assertEquals($result['values'][$id]['id'], $id); $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId); $this->assertEquals($result['values'][$id]['subject'], $params['subject']); @@ -177,16 +139,17 @@ public function testCaseCreate(): void { * Test create function with resolved status. */ public function testCaseCreateWithResolvedStatus(): void { - $params = $this->_params; - // Test using name instead of value. - unset($params['case_type_id']); - $params['case_type'] = $this->caseType; - $params['status_id'] = 'Closed'; - $result = $this->callAPISuccess('case', 'create', $params); + $params = [ + 'case_type' => 'housing_support', + 'subject' => 'Test case', + 'contact_id' => $this->individualCreate([], 'case_contact'), + 'status_id' => 'Closed', + ]; + $result = $this->callAPISuccess('Case', 'create', $params); $id = $result['id']; // Check result - $result = $this->callAPISuccess('case', 'get', ['id' => $id]); + $result = $this->callAPISuccess('Case', 'get', ['id' => $id]); $this->assertEquals($result['values'][$id]['id'], $id); $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId); $this->assertEquals($result['values'][$id]['subject'], $params['subject']); @@ -217,8 +180,8 @@ public function testCaseCreateCustom(): void { $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); $params = $this->_params; $params['custom_' . $ids['custom_field_id']] = "custom string"; - $result = $this->callAPISuccess($this->_entity, 'create', $params); - $result = $this->callAPISuccess($this->_entity, 'get', [ + $result = $this->callAPISuccess('Case', 'create', $params); + $result = $this->callAPISuccess('Case', 'get', [ 'return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id'], ]); @@ -289,16 +252,16 @@ public function testCaseUpdateCustom(): void { // Create a case with custom data $params['custom_' . $ids['custom_field_id']] = 'custom string'; - $result = $this->callAPISuccess($this->_entity, 'create', $params); + $result = $this->callAPISuccess('Case', 'create', $params); $caseId = $result['id']; - $result = $this->callAPISuccess($this->_entity, 'get', [ + $result = $this->callAPISuccess('Case', 'get', [ 'return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id'], ]); $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]); - $fields = $this->callAPISuccess($this->_entity, 'getfields', ['version' => $this->_apiversion]); + $fields = $this->callAPISuccess('Case', 'getfields', ['version' => $this->_apiversion]); $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']])); // Update the activity with custom data. @@ -307,9 +270,9 @@ public function testCaseUpdateCustom(): void { 'custom_' . $ids['custom_field_id'] => 'Updated my test data', 'version' => $this->_apiversion, ]; - $result = $this->callAPISuccess($this->_entity, 'create', $params); + $result = $this->callAPISuccess('Case', 'create', $params); - $result = $this->callAPISuccess($this->_entity, 'get', [ + $result = $this->callAPISuccess('Case', 'get', [ 'return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id'], @@ -479,17 +442,17 @@ public function testCaseGetNoCriteria(): void { */ public function testCaseActivityCreate(): void { $params = $this->_params; - $case = $this->callAPISuccess('case', 'create', $params); + $case = $this->callAPISuccess('Case', 'create', $params); $params = [ 'case_id' => $case['id'], // follow up - 'activity_type_id' => $this->followup_activity_type_value, + 'activity_type_id' => 'Follow Up', 'subject' => 'Test followup 123', 'source_contact_id' => $this->getLoggedInUser(), 'target_contact_id' => $this->_params['contact_id'], ]; $result = $this->callAPISuccess('Activity', 'create', $params); - $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']); + $this->assertEquals('Follow Up', CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $result['values'][$result['id']]['activity_type_id'])); // might need this for other tests that piggyback on this one $this->_caseActivityId = $result['values'][$result['id']]['id']; @@ -509,7 +472,7 @@ public function testCaseActivityCreate(): void { // Check that fetching an activity by case id works, as well as returning case_id $result = $this->callAPISuccessGetSingle('Activity', [ 'case_id' => $case['id'], - 'activity_type_id' => $this->followup_activity_type_value, + 'activity_type_id' => 'Follow Up', 'subject' => 'Test followup 123', 'return' => ['case_id'], ]); @@ -519,14 +482,14 @@ public function testCaseActivityCreate(): void { public function testCaseGetByStatus(): void { // Create 2 cases with different status ids. $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), 'subject' => "Test case 1", 'case_type_id' => $this->caseTypeId, 'status_id' => "Open", 'sequential' => 1, ]); $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->ids['Contact']['case_subject'], 'subject' => "Test case 2", 'case_type_id' => $this->caseTypeId, 'status_id' => "Urgent", @@ -534,7 +497,7 @@ public function testCaseGetByStatus(): void { ]); $result = $this->callAPISuccessGetSingle('Case', [ 'sequential' => 1, - 'contact_id' => 17, + 'contact_id' => $this->ids['Contact']['case_subject'], 'status_id' => "Open", ]); $this->assertEquals($case1['id'], $result['id']); @@ -542,9 +505,9 @@ public function testCaseGetByStatus(): void { public function testCaseGetWithRoles(): void { $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), 'subject' => "Test case with roles", - 'case_type_id' => $this->caseTypeId, + 'case_type_id' => 'housing_support', 'status_id' => "Open", ]); $result = $this->callAPISuccessGetSingle('Case', [ @@ -556,7 +519,7 @@ public function testCaseGetWithRoles(): void { $foundManager = FALSE; foreach ($result['contacts'] as $contact) { if ($contact['role'] == 'Client') { - $this->assertEquals(17, $contact['contact_id']); + $this->assertEquals($this->ids['Contact']['case_subject'], $contact['contact_id']); } elseif ($contact['role'] == 'Homeless Services Coordinator is') { $this->assertEquals(1, $contact['creator']); @@ -585,7 +548,7 @@ public function testCaseGetWithRolesNoCreator(): void { $newCaseType = $this->callAPISuccess('CaseType', 'create', $newCaseType); $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), 'subject' => "Test case with roles no creator", 'case_type_id' => $newCaseType['id'], 'status_id' => "Open", @@ -611,7 +574,7 @@ public function testCaseGetWithRolesNoCreator(): void { public function testCaseGetWithDefinition(): void { $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), 'subject' => "Test case with definition", 'case_type_id' => $this->caseTypeId, 'status_id' => "Open", @@ -636,7 +599,7 @@ public function testCaseGetWithDefinition(): void { public function testCaseGetTags(): void { $case1 = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), 'subject' => "Test case with tags", 'case_type_id' => $this->caseTypeId, 'status_id' => "Open", @@ -923,7 +886,7 @@ public function testCaseMerge(): void { public function testTimestamps(): void { $params = $this->_params; - $case_created = $this->callAPISuccess('case', 'create', $params); + $case_created = $this->callAPISuccess('Case', 'create', $params); $case_1 = $this->callAPISuccess('Case', 'getsingle', [ 'id' => $case_created['id'], @@ -970,7 +933,7 @@ public function testTimestamps(): void { } public function testGetOptions(): void { - $options = civicrm_api3('Case', 'getoptions', ['field' => 'medium_id']); + $options = $this->callAPISuccess('Case', 'getoptions', ['field' => 'medium_id']); $this->assertContains('Phone', $options['values']); } From ac6a6187f46eb8608b93b755c17f86bdc22a8880 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 10:56:10 +1300 Subject: [PATCH 5/7] Remove hard-coded id --- tests/phpunit/Civi/CCase/SequenceListenerTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/Civi/CCase/SequenceListenerTest.php b/tests/phpunit/Civi/CCase/SequenceListenerTest.php index c5aa8681100b..94262c7ee33e 100644 --- a/tests/phpunit/Civi/CCase/SequenceListenerTest.php +++ b/tests/phpunit/Civi/CCase/SequenceListenerTest.php @@ -14,7 +14,7 @@ public function setUp(): void { $this->_params = [ 'case_type' => $this->caseType, 'subject' => 'Test case', - 'contact_id' => 17, + 'contact_id' => $this->individualCreate([], 'case_subject'), ]; //Add an activity status with Type = Completed $this->callAPISuccess('OptionValue', 'create', [ @@ -31,9 +31,9 @@ public function testSequence(): void { // Create case; schedule first activity \CRM_Utils_Time::setTime('2013-11-30 01:00:00'); - $case = $this->callAPISuccess('case', 'create', $this->_params); + $case = $this->callAPISuccess('Case', 'create', $this->_params); $analyzer = new \Civi\CCase\Analyzer($case['id']); - $this->assertEquals($caseStatuses['Open'], self::ag($analyzer->getCase(), 'status_id')); + $this->assertEquals('Open', \CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', self::ag($analyzer->getCase(), 'status_id'))); $this->assertApproxTime('2013-11-30 01:00:00', self::ag($analyzer->getSingleActivity('Medical evaluation'), 'activity_date_time')); $this->assertEquals($actStatuses['Scheduled'], self::ag($analyzer->getSingleActivity('Medical evaluation'), 'status_id')); $this->assertFalse($analyzer->hasActivity('Mental health evaluation')); @@ -46,7 +46,7 @@ public function testSequence(): void { 'subject' => 'This is the new subject', ]); $analyzer = new \Civi\CCase\Analyzer($case['id']); - $this->assertEquals($caseStatuses['Open'], self::ag($analyzer->getCase(), 'status_id')); + $this->assertEquals('Open', \CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', self::ag($analyzer->getCase(), 'status_id'))); $this->assertApproxTime('2013-11-30 01:00:00', self::ag($analyzer->getSingleActivity('Medical evaluation'), 'activity_date_time')); $this->assertEquals($actStatuses['Scheduled'], self::ag($analyzer->getSingleActivity('Medical evaluation'), 'status_id')); $this->assertFalse($analyzer->hasActivity('Mental health evaluation')); From ce387541d0cace76d0d8247202c058fe8cf0b885 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 11:22:16 +1300 Subject: [PATCH 6/7] More hard-coding fixes --- tests/phpunit/CRM/Case/BAO/CaseTest.php | 4 +- tests/phpunit/CRM/Case/Form/CaseViewTest.php | 46 ++--- .../CRM/Case/XMLProcessor/ProcessTest.php | 162 ++++++++++-------- .../Civi/CCase/SequenceListenerTest.php | 4 +- tests/phpunit/api/v3/CaseTest.php | 2 +- 5 files changed, 116 insertions(+), 102 deletions(-) diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php index e9029add643d..75a8cd645677 100644 --- a/tests/phpunit/CRM/Case/BAO/CaseTest.php +++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php @@ -30,12 +30,12 @@ public function testAddCaseToContact(): void { $this->createLoggedInUser(); $params = [ 'case_id' => 1, - 'contact_id' => 17, + 'contact_id' => $this->individualCreate(), ]; CRM_Case_BAO_CaseContact::writeRecord($params); $recent = CRM_Utils_Recent::get(); - $this->assertEquals('Test Contact - Housing Support', $recent[0]['title']); + $this->assertEquals('Mr. Anthony Anderson II - Housing Support', $recent[0]['title']); } /** diff --git a/tests/phpunit/CRM/Case/Form/CaseViewTest.php b/tests/phpunit/CRM/Case/Form/CaseViewTest.php index bd2eb187a5c4..b88fbaab52eb 100644 --- a/tests/phpunit/CRM/Case/Form/CaseViewTest.php +++ b/tests/phpunit/CRM/Case/Form/CaseViewTest.php @@ -25,18 +25,22 @@ public function testSearchFilterDropdown(): void { // We don't care about the first one, just check it's what we expect $this->assertEquals('- select activity type -', $options[0]['text']); unset($options[0]); + $expected = []; + foreach ([ + 'Follow up', + 'Income and benefits stabilization', + 'Long-term housing plan', + 'Medical evaluation', + 'Mental health evaluation', + 'Open Case', + 'Secure temporary housing', + ] as $expectedValue) { + $expected[] = [CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $expectedValue) => $expectedValue]; + } $mappedOptions = array_map(function($v) { return [$v['attr']['value'] => $v['text']]; }, $options); - $this->assertEquals([ - [14 => 'Follow up'], - [61 => 'Income and benefits stabilization'], - [59 => 'Long-term housing plan'], - [56 => 'Medical evaluation'], - [57 => 'Mental health evaluation'], - [13 => 'Open Case'], - [58 => 'Secure temporary housing'], - ], array_values($mappedOptions)); + $this->assertEquals($expected, array_values($mappedOptions)); // Now add some activities where the type might not even be in the config. $this->callAPISuccess('Activity', 'create', [ @@ -70,20 +74,20 @@ public function testSearchFilterDropdown(): void { $options = $form->getElement('activity_type_filter_id')->_options; unset($options[0]); $mappedOptions = array_map(function($v) { - return [$v['attr']['value'] => $v['text']]; + return $v['text']; }, $options); $this->assertEquals([ - [3 => 'Email'], - [14 => 'Follow up'], - [12 => 'Inbound Email'], - [61 => 'Income and benefits stabilization'], - [59 => 'Long-term housing plan'], - [56 => 'Medical evaluation'], - [1 => 'Meeting'], - [57 => 'Mental health evaluation'], - [13 => 'Open Case'], - [2 => 'Phone Call'], - [58 => 'Secure temporary housing'], + 'Email', + 'Follow up', + 'Inbound Email', + 'Income and benefits stabilization', + 'Long-term housing plan', + 'Medical evaluation', + 'Meeting', + 'Mental health evaluation', + 'Open Case', + 'Phone Call', + 'Secure temporary housing', ], array_values($mappedOptions)); } diff --git a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php index 94277460f280..7c1a1132bab8 100644 --- a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php +++ b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php @@ -10,37 +10,27 @@ class CRM_Case_XMLProcessor_ProcessTest extends CiviCaseTestCase { /** * @var array */ - private $defaultAssigneeOptionsValues = []; + private array $defaultAssigneeOptionsValues = []; /** * @var array */ - private $contacts = []; - - /** - * @var array - */ - private $relationships = []; - - /** - * @var array - */ - private $moreRelationshipTypes = []; + private array $moreRelationshipTypes = []; /** * @var SimpleXMLElement */ - private $activityTypeXml; + private SimpleXMLElement $activityTypeXml; /** * @var array */ - private $activityParams = []; + private array $activityParams = []; /** * @var CRM_Case_XMLProcessor_Process */ - private $process; + private CRM_Case_XMLProcessor_Process $process; public function setUp(): void { parent::setUp(); @@ -58,11 +48,9 @@ public function setUp(): void { * Creates sample contacts. */ protected function setUpContacts() { - $this->contacts = [ - 'ana' => $this->individualCreate(), - 'beto' => $this->individualCreate(), - 'carlos' => $this->individualCreate(), - ]; + $this->individualCreate(['first_name' => 'ana'], 'ana'); + $this->individualCreate(['first_name' => 'beto'], 'beto'); + $this->individualCreate(['first_name' => 'carlos'], 'carlos'); } /** @@ -93,25 +81,20 @@ protected function setupDefaultAssigneeOptions() { * Adds a relationship between the activity's target contact and default assignee. */ protected function setupRelationships() { - $this->relationships = [ + $relationships = [ 'ana_is_pupil_of_beto' => [ 'type_id' => NULL, 'name_a_b' => 'Pupil of', 'name_b_a' => 'Instructor', - 'contact_id_a' => $this->contacts['ana'], - 'contact_id_b' => $this->contacts['beto'], + 'contact_id_a' => $this->ids['Contact']['ana'], + 'contact_id_b' => $this->ids['Contact']['beto'], ], 'ana_is_spouse_of_carlos' => [ 'type_id' => NULL, 'name_a_b' => 'Spouse of', 'name_b_a' => 'Spouse of', - 'contact_id_a' => $this->contacts['ana'], - 'contact_id_b' => $this->contacts['carlos'], - ], - 'unassigned_employee' => [ - 'type_id' => NULL, - 'name_a_b' => 'Employee of', - 'name_b_a' => 'Employer', + 'contact_id_a' => $this->ids['Contact']['ana'], + 'contact_id_b' => $this->ids['Contact']['carlos'], ], ]; $this->relationshipTypeCreate([ @@ -122,6 +105,13 @@ protected function setupRelationships() { 'name_b_a' => 'Instructor', 'label_b_a' => 'Instructor', ]); + foreach ($relationships as $relationship) { + $this->createTestEntity('Relationship', [ + 'contact_id_a' => $relationship['contact_id_a'], + 'contact_id_b' => $relationship['contact_id_b'], + 'relationship_type_id:name' => $relationship['name_a_b'], + ]); + } } /** @@ -181,13 +171,12 @@ protected function setupMoreRelationshipTypes() { * to create the activity. */ protected function setupActivityDefinitions() { - $activityTypeXml = 'Open Case'; - $this->activityTypeXml = new SimpleXMLElement($activityTypeXml); + $this->activityTypeXml = $this->getActivityTypeXMl(); $this->activityParams = [ 'activity_date_time' => date('Ymd'), // @todo This seems wrong, it just happens to work out because both caseId and caseTypeId equal 1 in the stock setup here. 'caseID' => $this->caseTypeId, - 'clientID' => $this->contacts['ana'], + 'clientID' => $this->ids['Contact']['ana'], 'creatorID' => $this->getLoggedInUser(), ]; } @@ -195,14 +184,16 @@ protected function setupActivityDefinitions() { /** * Tests the creation of activities where the default assignee should be the * target contact's instructor. Beto is the instructor for Ana. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithDefaultContactByRelationship(): void { - $relationship = $this->relationships['ana_is_pupil_of_beto']; - $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_b_a"; + $activityTypeXml = $this->getActivityTypeXMl(); + $activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; + $activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Pupil of') . '_b_a'; - $this->process->createActivity($this->activityTypeXml, $this->activityParams); - $this->assertActivityAssignedToContactExists($this->contacts['beto']); + $this->process->createActivity($activityTypeXml, $this->activityParams); + $this->assertActivityAssignedToContactExists($this->ids['Contact']['beto']); } /** @@ -210,7 +201,7 @@ public function testCreateActivityWithDefaultContactByRelationship(): void { * end up being a contact from another case where it has the same client * and relationship. * - * @throws \CRM_Core_Exception + * @throws \Exception */ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): void { /* @@ -236,21 +227,18 @@ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): vo is test 2. */ - $relationship = $this->relationships['ana_is_pupil_of_beto']; - // Make another case and add a case role with the same relationship we // want, but a different person. - $caseObj = $this->createCase($this->contacts['ana'], $this->getLoggedInUser()); + $caseObj = $this->createCase($this->ids['Contact']['ana'], $this->getLoggedInUser()); $this->callAPISuccess('Relationship', 'create', [ - 'contact_id_a' => $this->contacts['ana'], - 'contact_id_b' => $this->contacts['carlos'], - 'relationship_type_id' => $relationship['type_id'], + 'contact_id_a' => $this->ids['Contact']['ana'], + 'contact_id_b' => $this->ids['Contact']['carlos'], + 'relationship_type_id' => $this->ids['RelationshipType']['Pupil of'], 'case_id' => $caseObj->id, ]); $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_b_a"; - + $this->activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Pupil of') . '_b_a'; $this->process->createActivity($this->activityTypeXml, $this->activityParams); // We can't use assertActivityAssignedToContactExists because it assumes @@ -263,19 +251,19 @@ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): vo $this->assertCount(1, $result); foreach ($result as $activity) { // Note the first parameter is turned into an array to match the second. - $this->assertEquals([$this->contacts['beto']], $activity['assignee_contact_id']); + $this->assertEquals([$this->ids['Contact']['beto']], $activity['assignee_contact_id']); } // Now remove the non-case relationship. $result = $this->callAPISuccess('Relationship', 'get', [ 'case_id' => ['IS NULL' => 1], - 'relationship_type_id' => $relationship['type_id'], - 'contact_id_a' => $this->contacts['ana'], - 'contact_id_b' => $this->contacts['beto'], + 'relationship_type_id' => $this->ids['RelationshipType']['Pupil of'], + 'contact_id_a' => $this->ids['Contact']['ana'], + 'contact_id_b' => $this->ids['Contact']['beto'], ])['values']; $this->assertCount(1, $result); foreach ($result as $activity) { - $result = $this->callAPISuccess('Relationship', 'delete', ['id' => $activity['id']]); + $this->callAPISuccess('Relationship', 'delete', ['id' => $activity['id']]); } // Create another activity on the first case. Make it a different activity @@ -283,7 +271,7 @@ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): vo $activityXml = 'Follow up'; $activityXmlElement = new SimpleXMLElement($activityXml); $activityXmlElement->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $activityXmlElement->default_assignee_relationship = "{$relationship['type_id']}_b_a"; + $this->activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Pupil of') . '_b_a'; $this->process->createActivity($activityXmlElement, $this->activityParams); $result = $this->callAPISuccess('Activity', 'get', [ @@ -301,11 +289,12 @@ public function testCreateActivityWithDefaultContactByRelationshipTwoCases(): vo /** * Tests when the default assignee relationship exists, but in the other direction only. * Ana is a pupil, but has no pupils related to her. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithDefaultContactByRelationshipMissing(): void { - $relationship = $this->relationships['ana_is_pupil_of_beto']; $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_a_b"; + $this->activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Pupil of') . '_a_b'; $this->process->createActivity($this->activityTypeXml, $this->activityParams); $this->assertActivityAssignedToContactExists(NULL); @@ -314,25 +303,27 @@ public function testCreateActivityWithDefaultContactByRelationshipMissing(): voi /** * Tests when the the default assignee relationship exists and is a bidirectional * relationship. Ana and Carlos are spouses. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithDefaultContactByRelationshipBidirectional(): void { - $relationship = $this->relationships['ana_is_spouse_of_carlos']; - $this->activityParams['clientID'] = $this->contacts['carlos']; + $this->activityParams['clientID'] = $this->ids['Contact']['carlos']; $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_a_b"; + $this->activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Spouse of') . '_b_a'; $this->process->createActivity($this->activityTypeXml, $this->activityParams); - $this->assertActivityAssignedToContactExists($this->contacts['ana']); + $this->assertActivityAssignedToContactExists($this->ids['Contact']['ana']); } /** * Tests when the default assignee relationship does not exist. Ana is not an * employee for anyone. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithDefaultContactByRelationButTheresNoRelationship(): void { - $relationship = $this->relationships['unassigned_employee']; $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['BY_RELATIONSHIP']; - $this->activityTypeXml->default_assignee_relationship = "{$relationship['type_id']}_b_a"; + $this->activityTypeXml->default_assignee_relationship = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Relationship', 'relationship_type_id', 'Employee of') . '_b_a'; $this->process->createActivity($this->activityTypeXml, $this->activityParams); $this->assertActivityAssignedToContactExists(NULL); @@ -340,20 +331,24 @@ public function testCreateActivityWithDefaultContactByRelationButTheresNoRelatio /** * Tests the creation of activities with default assignee set to a specific contact. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityAssignedToSpecificContact(): void { $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['SPECIFIC_CONTACT']; - $this->activityTypeXml->default_assignee_contact = $this->contacts['carlos']; + $this->activityTypeXml->default_assignee_contact = $this->ids['Contact']['carlos']; $this->process->createActivity($this->activityTypeXml, $this->activityParams); - $this->assertActivityAssignedToContactExists($this->contacts['carlos']); + $this->assertActivityAssignedToContactExists($this->ids['Contact']['carlos']); } /** * Tests the creation of activities with default assignee set to a specific contact, * but the contact does not exist. + * + * @throws \CRM_Core_Exception */ - public function testCreateActivityAssignedToNonExistantSpecificContact(): void { + public function testCreateActivityAssignedToNonExistentSpecificContact(): void { $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['SPECIFIC_CONTACT']; $this->activityTypeXml->default_assignee_contact = 987456321; @@ -364,6 +359,8 @@ public function testCreateActivityAssignedToNonExistantSpecificContact(): void { /** * Tests the creation of activities with the default assignee being the one * creating the case's activity. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityAssignedToUserCreatingTheCase(): void { $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['USER_CREATING_THE_CASE']; @@ -374,6 +371,8 @@ public function testCreateActivityAssignedToUserCreatingTheCase(): void { /** * Tests the creation of activities when the default assignee is set to NONE. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityAssignedNoUser(): void { $this->activityTypeXml->default_assignee_type = $this->defaultAssigneeOptionsValues['NONE']; @@ -384,6 +383,8 @@ public function testCreateActivityAssignedNoUser(): void { /** * Tests the creation of activities when the default assignee is set to NONE. + * + * @throws \CRM_Core_Exception */ public function testCreateActivityWithNoDefaultAssigneeOption(): void { $this->process->createActivity($this->activityTypeXml, $this->activityParams); @@ -396,7 +397,7 @@ public function testCreateActivityWithNoDefaultAssigneeOption(): void { * * @param int|null $assigneeContactId the ID of the expected assigned contact or NULL if expected to be empty. */ - protected function assertActivityAssignedToContactExists($assigneeContactId) { + protected function assertActivityAssignedToContactExists(?int $assigneeContactId) { $expectedContact = $assigneeContactId === NULL ? [] : [$assigneeContactId]; $result = $this->callAPISuccess('Activity', 'get', [ 'target_contact_id' => $this->activityParams['clientID'], @@ -417,13 +418,12 @@ protected function assertActivityAssignedToContactExists($assigneeContactId) { * relationship type against multiple xml strings. It's not a test * identifier, it's an array key to use to look up something. * @param string $xmlString - * @param array $expected - * @param $dontcare array We're re-using the data provider for two tests and - * we don't care about those expected values. + * @param array|null $expected * + * @throws \Exception * @dataProvider xmlCaseRoleDataProvider */ - public function testCaseRoles($key, $xmlString, $expected, $dontcare) { + public function testCaseRoles(string $key, string $xmlString, ?array $expected) { $xmlObj = new SimpleXMLElement($xmlString); // element 0 is direction (a_b), 1 is the text we want @@ -441,14 +441,14 @@ public function testCaseRoles($key, $xmlString, $expected, $dontcare) { * relationship type against multiple xml strings. It's not a test * identifier, it's an array key to use to look up something. * @param string $xmlString - * @param $dontcare array We're re-using the data provider for two tests and + * @param $unused|null array We're re-using the data provider for two tests and * we don't care about those expected values. * @param array $expected * * @throws \Exception * @dataProvider xmlCaseRoleDataProvider */ - public function testLocateNameOrLabel($key, $xmlString, $dontcare, $expected) { + public function testLocateNameOrLabel(string $key, string $xmlString, ?array $unused, array $expected) { $xmlObj = new SimpleXMLElement($xmlString); // element 0 is direction (a_b), 1 is the text we want. @@ -500,9 +500,9 @@ public function xmlCaseRoleDataProvider(): array { // in an xml file. Here we've made a typo on purpose. [ 'unidirectional_name_label_different', - 'Jedi Masterrrr for11', + 'Jedi Master for11', NULL, - [FALSE, 'Jedi Masterrrr for'], + [FALSE, 'Jedi Master for'], ], // Now some similar tests to above but for bidirectional relationships. // Bidirectional relationship, name and label different, using machine name. @@ -586,7 +586,7 @@ public function testXmlProcessorActivityTypes(): void { $xml = $p->retrieve('housing_support'); $activityTypes = $p->activityTypes($xml->ActivityTypes, FALSE, TRUE, FALSE); $this->assertEquals( - [ + array_values([ 13 => 'Open Case', 56 => 'Medical evaluation changed', 57 => 'Mental health evaluation', @@ -598,9 +598,19 @@ public function testXmlProcessorActivityTypes(): void { 16 => 'Change Case Status', 18 => 'Change Case Start Date', 25 => 'Link Cases', - ], - $activityTypes + ]), + array_values($activityTypes) ); } + private function getActivityTypeXMl(): SimpleXMLElement { + try { + $activityTypeXml = 'Open Case'; + return new SimpleXMLElement($activityTypeXml); + } + catch (Exception $e) { + $this->fail('xml not loaded'); + } + } + } diff --git a/tests/phpunit/Civi/CCase/SequenceListenerTest.php b/tests/phpunit/Civi/CCase/SequenceListenerTest.php index 94262c7ee33e..5bb4f5cf5f84 100644 --- a/tests/phpunit/Civi/CCase/SequenceListenerTest.php +++ b/tests/phpunit/Civi/CCase/SequenceListenerTest.php @@ -33,7 +33,7 @@ public function testSequence(): void { \CRM_Utils_Time::setTime('2013-11-30 01:00:00'); $case = $this->callAPISuccess('Case', 'create', $this->_params); $analyzer = new \Civi\CCase\Analyzer($case['id']); - $this->assertEquals('Open', \CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', self::ag($analyzer->getCase(), 'status_id'))); + $this->assertEquals($caseStatuses['Open'], self::ag($analyzer->getCase(), 'status_id')); $this->assertApproxTime('2013-11-30 01:00:00', self::ag($analyzer->getSingleActivity('Medical evaluation'), 'activity_date_time')); $this->assertEquals($actStatuses['Scheduled'], self::ag($analyzer->getSingleActivity('Medical evaluation'), 'status_id')); $this->assertFalse($analyzer->hasActivity('Mental health evaluation')); @@ -46,7 +46,7 @@ public function testSequence(): void { 'subject' => 'This is the new subject', ]); $analyzer = new \Civi\CCase\Analyzer($case['id']); - $this->assertEquals('Open', \CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', self::ag($analyzer->getCase(), 'status_id'))); + $this->assertEquals($caseStatuses['Open'], self::ag($analyzer->getCase(), 'status_id')); $this->assertApproxTime('2013-11-30 01:00:00', self::ag($analyzer->getSingleActivity('Medical evaluation'), 'activity_date_time')); $this->assertEquals($actStatuses['Scheduled'], self::ag($analyzer->getSingleActivity('Medical evaluation'), 'status_id')); $this->assertFalse($analyzer->hasActivity('Mental health evaluation')); diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index 4fd77fee8cfa..c676f4dea221 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -452,7 +452,7 @@ public function testCaseActivityCreate(): void { 'target_contact_id' => $this->_params['contact_id'], ]; $result = $this->callAPISuccess('Activity', 'create', $params); - $this->assertEquals('Follow Up', CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $result['values'][$result['id']]['activity_type_id'])); + $this->assertEquals('Follow up', CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $result['values'][$result['id']]['activity_type_id'])); // might need this for other tests that piggyback on this one $this->_caseActivityId = $result['values'][$result['id']]['id']; From 8855790d19db132472365837a905ba13dd50443a Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 2 Jan 2025 12:33:39 +1300 Subject: [PATCH 7/7] More cleanup improvments I also removed 2 tests that really just duplicate a whole lot of other tests --- .../CRM/Case/XMLProcessor/ProcessTest.php | 28 ++++----- .../BAO/ContactType/ContactTypeTest.php | 61 +------------------ tests/phpunit/CRM/Dedupe/DedupeFinderTest.php | 9 +-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 4 ++ 4 files changed, 23 insertions(+), 79 deletions(-) diff --git a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php index 7c1a1132bab8..20deed81ec5c 100644 --- a/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php +++ b/tests/phpunit/CRM/Case/XMLProcessor/ProcessTest.php @@ -414,7 +414,7 @@ protected function assertActivityAssignedToContactExists(?int $assigneeContactId * * @param $key string The array key in the moreRelationshipTypes array that * is the relationship type we're currently testing. So not necessarily - * unique for each entry in the dataprovider since want to test a given + * unique for each entry in the dataProvider since want to test a given * relationship type against multiple xml strings. It's not a test * identifier, it's an array key to use to look up something. * @param string $xmlString @@ -441,7 +441,7 @@ public function testCaseRoles(string $key, string $xmlString, ?array $expected) * relationship type against multiple xml strings. It's not a test * identifier, it's an array key to use to look up something. * @param string $xmlString - * @param $unused|null array We're re-using the data provider for two tests and + * @param null|array $unused We're re-using the data provider for two tests and * we don't care about those expected values. * @param array $expected * @@ -470,7 +470,7 @@ public function xmlCaseRoleDataProvider(): array { return [ // Simulate one that has been converted to the format it should be going // forward, where name is the actual name, i.e. same as machineName. - [ + 'unidirectional_name_label_different' => [ // this is the array key in the $this->moreRelationshipTypes array 'unidirectional_name_label_different', // some xml @@ -483,14 +483,14 @@ public function xmlCaseRoleDataProvider(): array { // Simulate one that is still in label format, i.e. one that is still in // xml files that haven't been updated, or in the db but upgrade script // not run yet. - [ + 'unidirectional_name_label_different_label_format' => [ 'unidirectional_name_label_different', 'Jedi Master for11', ['a_b', 'Jedi Master is'], ['a_b', 'jm7ba'], ], // Ditto but where we know name and label are the same in the db. - [ + 'unidirectional_name_label_same' => [ 'unidirectional_name_label_same', 'Quilt Maker for11', ['a_b', 'Quilt Maker is'], @@ -498,29 +498,29 @@ public function xmlCaseRoleDataProvider(): array { ], // Simulate one that is messed up and should fail, e.g. like a typo // in an xml file. Here we've made a typo on purpose. - [ + 'unidirectional_name_label_different_wrong' => [ 'unidirectional_name_label_different', - 'Jedi Master for11', + 'Jedi Masterrrr for11', NULL, - [FALSE, 'Jedi Master for'], + [FALSE, 'Jedi Masterrrr for'], ], // Now some similar tests to above but for bidirectional relationships. // Bidirectional relationship, name and label different, using machine name. - [ + 'bidirectional_name_label_different' => [ 'bidirectional_name_label_different', 'f1211', ['b_a', 'Friend of'], ['b_a', 'f12'], ], // Bidirectional relationship, name and label different, using display label. - [ + 'bidirectional_name_label_different_label' => [ 'bidirectional_name_label_different', 'Friend of11', ['b_a', 'Friend of'], ['b_a', 'f12'], ], // Bidirectional relationship, name and label same. - [ + 'bidirectional_name_label_same' => [ 'bidirectional_name_label_same', 'Enemy of11', ['b_a', 'Enemy of'], @@ -549,7 +549,7 @@ public function testXmlProcessorActivityTypes(): void { // Test getting the `name`s $activityTypes = $p->activityTypes($xml->ActivityTypes, FALSE, FALSE, FALSE); - $this->assertEquals( + $this->assertEquals(array_values( [ 13 => 'Open Case', 56 => 'Medical evaluation', @@ -562,8 +562,8 @@ public function testXmlProcessorActivityTypes(): void { 16 => 'Change Case Status', 18 => 'Change Case Start Date', 25 => 'Link Cases', - ], - $activityTypes + ]), + array_values($activityTypes) ); // While we're here and have the `name`s check the editable types in diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php index 0e05132d7b16..dba08c493efc 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php @@ -136,12 +136,10 @@ public function testGetMethods(): void { /** * Test function for getting contact types. - * - * @throws \CRM_Core_Exception */ public function testContactTypeInfo(): void { $blahType = ['is_active' => 0, 'name' => 'blah', 'label' => 'blah blah', 'parent_id:name' => 'Individual', 'icon' => 'fa-random']; - $createdType = ContactType::create()->setValues($blahType)->execute()->first(); + $createdType = $this->createTestEntity('ContactType', $blahType); $activeTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(); $expected = $this->getExpectedContactTypes(); $this->strictArrayCompare($expected, $activeTypes); @@ -168,7 +166,7 @@ public function testContactTypeInfo(): void { * * @return array */ - public function getExpectedContactTypes() { + public function getExpectedContactTypes(): array { return [ 'Individual' => [ 'id' => 1, @@ -334,7 +332,7 @@ public function getExpectedContactTypes() { * * @return array */ - public function getExpectedAllSubtypes() { + public function getExpectedAllSubtypes(): array { return array_merge( $this->getExpectedContactSubTypes('Individual'), $this->getExpectedContactSubTypes('Household'), @@ -359,57 +357,4 @@ public function getExpectedContactSubTypes($parentType) { return $expected; } - /** - * Test add() methods with valid data - * success expected - */ - public function testAdd(): void { - - $params = [ - 'label' => 'indiviSubType', - 'name' => 'indiviSubType', - 'parent_id' => 1, - 'is_active' => 1, - ]; - $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']); - $this->assertEquals($result->is_active, $params['is_active']); - CRM_Contact_BAO_ContactType::deleteRecord(['id' => $result->id]); - - $params = [ - 'label' => 'householdSubType', - 'name' => 'householdSubType', - 'parent_id' => 2, - 'is_active' => 0, - ]; - $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']); - $this->assertEquals($result->is_active, $params['is_active']); - CRM_Contact_BAO_ContactType::deleteRecord(['id' => $result->id]); - } - - /** - * Test del() with valid data. - */ - public function testDel(): void { - - $params = [ - 'label' => 'indiviSubType', - 'name' => 'indiviSubType', - 'parent_id' => 1, - 'is_active' => 1, - ]; - $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]); - - $result = CRM_Contact_BAO_ContactType::subTypes(); - $this->assertEquals(FALSE, in_array($subtype->name, $result, TRUE)); - } - } diff --git a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php index dabfdebe7a7f..a1c2102afb61 100644 --- a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php +++ b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php @@ -18,14 +18,9 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { /** * Clean up after the test. - * - * @throws \Civi\Core\Exception\DBQueryException */ public function tearDown(): void { - $this->quickCleanup(['civicrm_contact'], TRUE); - if (isset($this->groupID)) { - $this->callAPISuccess('group', 'delete', ['id' => $this->groupID]); - } + $this->quickCleanup(['civicrm_contact', 'civicrm_group'], TRUE); parent::tearDown(); } @@ -134,7 +129,7 @@ public function testCustomRuleCustomFieldsSubtypes(): void { $this->setupForGroupDedupe(); // Create custom group with fields of all types to test. - $this->callAPISuccess('ContactType', 'create', ['name' => 'Big Bank', 'label' => 'biggie', 'parent_id' => 'Individual']); + $this->createTestEntity('ContactType', ['name' => 'Big_Bank', 'label' => 'biggie', 'parent_id:name' => 'Individual']); foreach ($this->ids['Contact'] as $contact_id) { $this->callAPISuccess('Contact', 'create', array_merge([ 'id' => $contact_id, diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 6c8c367de5b9..602b0c7cd4e3 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -28,6 +28,7 @@ declare(strict_types = 1); use Civi\Api4\Address; use Civi\Api4\CiviCase; +use Civi\Api4\ContactType; use Civi\Api4\Contribution; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; @@ -545,6 +546,9 @@ protected function tearDown(): void { if (!empty($this->ids['RelationshipType'])) { RelationshipType::delete(FALSE)->addWhere('id', 'IN', $this->ids['RelationshipType'])->execute(); } + if (!empty($this->ids['ContactType'])) { + ContactType::delete(FALSE)->addWhere('id', 'IN', $this->ids['ContactType'])->execute(); + } unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); parent::tearDown(); }