From 82e6c17034833c433eb42f686711ef462370d482 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 5 Jun 2023 09:20:50 +1200 Subject: [PATCH] [TEST] Further minor fixes on UFGroup Cleanup --- .../CRM/Contribute/Form/ContributionTest.php | 12 +- tests/phpunit/CRM/Profile/Form/EditTest.php | 4 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 17 +- tests/phpunit/api/v3/MessageTemplateTest.php | 145 ++++++++++-------- tests/phpunit/api/v3/PcpTest.php | 27 ++-- tests/phpunit/api/v3/SurveyTest.php | 31 ++-- 6 files changed, 131 insertions(+), 105 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index b828a9db22..39f0f2f245 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -897,7 +897,7 @@ Paid By: Check', ] + $this->getCreditCardParams()); $this->callAPISuccessGetCount('Contribution', ['contact_id' => $this->_individualId], 1); $note = $this->callAPISuccessGetSingle('note', ['entity_table' => 'civicrm_contribution']); - $this->assertEquals($note['note'], 'Super cool and interesting stuff'); + $this->assertEquals('Super cool and interesting stuff', $note['note']); } /** @@ -1517,8 +1517,8 @@ Paid By: Check', 'text_length' => 255, ]); - // create profile - $membershipCustomFieldsProfile = civicrm_api3('UFGroup', 'create', [ + // Create profile. + $membershipCustomFieldsProfile = $this->createTestEntity('UFGroup', [ 'is_active' => 1, 'group_type' => 'Membership,Individual', 'title' => 'Membership Custom Fields', @@ -1730,7 +1730,7 @@ Paid By: Check', /** * Test no warnings or errors during preProcess when editing. */ - public function testPreProcessContributionEdit() { + public function testPreProcessContributionEdit(): void { // Simulate a contribution in pending status $contribution = $this->callAPISuccess( 'Contribution', @@ -2109,8 +2109,8 @@ Paid By: Check', * Check that formRule validates you can only have one contribution with a * given trxn_id. */ - public function testContributionFormRuleDuplicateTrxn() { - $contribution = $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['trxn_id' => '1234'])); + public function testContributionFormRuleDuplicateTrxn(): void { + $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['trxn_id' => '1234'])); $fields = [ 'contact_id' => $this->_individualId, diff --git a/tests/phpunit/CRM/Profile/Form/EditTest.php b/tests/phpunit/CRM/Profile/Form/EditTest.php index 500fde0ea7..191cec0db3 100644 --- a/tests/phpunit/CRM/Profile/Form/EditTest.php +++ b/tests/phpunit/CRM/Profile/Form/EditTest.php @@ -28,10 +28,10 @@ class CRM_Profile_Form_EditTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testProfileUrl(): void { - $profileID = Civi\Api4\UFGroup::create(FALSE)->setValues([ + $profileID = $this->createTestEntity('UFGroup', [ 'post_URL' => 'civicrm/{contact.display_name}', 'title' => 'title', - ])->execute()->first()['id']; + ])['id']; UFJoin::create(FALSE)->setValues([ 'module' => 'Profile', 'uf_group_id' => $profileID, diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 2804ff2256..dcd280d78c 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -491,6 +491,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { if (!empty($this->ids['UFGroup'])) { UFGroup::delete(FALSE)->addWhere('id', 'IN', $this->ids['UFGroup'])->execute(); } + unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); parent::tearDown(); } @@ -557,8 +558,20 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * @return array * api Result */ - public function createTestEntity() { - return $entity = $this->callAPISuccess($this->entity, 'create', $this->params); + + /** + * Create an entity, recording it's details for tearDown. + * + * @param string $entity + * @param array $params + * @param string $identifier + * + * @return array|int + */ + protected function createTestEntity(string $entity, array $params, string $identifier = 'default') { + $result = $this->callAPISuccess($entity, 'create', $params); + $this->setTestEntityID($entity, $result['id'], $identifier); + return reset($result['values']); } /** diff --git a/tests/phpunit/api/v3/MessageTemplateTest.php b/tests/phpunit/api/v3/MessageTemplateTest.php index 5272a1cc63..15da965948 100644 --- a/tests/phpunit/api/v3/MessageTemplateTest.php +++ b/tests/phpunit/api/v3/MessageTemplateTest.php @@ -23,28 +23,20 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { public function setUp(): void { $this->_apiversion = 3; parent::setUp(); - $this->useTransaction(TRUE); - $template = CRM_Core_DAO::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray(); + $this->useTransaction(); $this->params = [ - 'msg_title' => $template['msg_title'], - 'msg_subject' => $template['msg_subject'], - 'msg_text' => $template['msg_text'], - 'msg_html' => $template['msg_html'], - 'workflow_id' => $template['workflow_id'], - 'is_default' => $template['is_default'], - 'is_reserved' => $template['is_reserved'], + 'msg_title' => 'title', + 'msg_subject' => 'subject', + 'msg_text' => 'text', + 'msg_html' => 'html', + 'workflow_name' => 'friend', ]; } - public function tearDown(): void { - parent::tearDown(); - unset(CRM_Core_Config::singleton()->userPermissionClass->permissions); - } - /** * Test create function succeeds. */ - public function testCreate() { + public function testCreate(): void { $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params, __FUNCTION__, __FILE__); $this->getAndCheck($this->params, $result['id'], $this->entity); } @@ -57,8 +49,11 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { * Add extra checks for any 'special' return values or * behaviours */ - public function testGet() { - $result = $this->callAPIAndDocument('MessageTemplate', 'get', $this->params, __FUNCTION__, __FILE__); + public function testGet(): void { + $result = $this->callAPIAndDocument('MessageTemplate', 'get', [ + 'workflow_name' => 'contribution_invoice_receipt', + 'is_default' => 1, + ], __FUNCTION__, __FILE__); $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); } @@ -66,9 +61,9 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { /** * Check the delete function succeeds. */ - public function testDelete() { - $entity = $this->createTestEntity(); - $result = $this->callAPIAndDocument('MessageTemplate', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__); + public function testDelete(): void { + $entity = $this->createTestEntity('MessageTemplate', $this->params); + $this->callAPIAndDocument('MessageTemplate', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__); $checkDeleted = $this->callAPISuccess($this->entity, 'get', [ 'id' => $entity['id'], ]); @@ -77,11 +72,13 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { /** * If you give workflow_id, then workflow_name should also be set. + * + * @throws \CRM_Core_Exception */ - public function testWorkflowIdToName() { - $wfName = 'uf_notify'; - $wfId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [ - 1 => [$wfName, 'String'], + public function testWorkflowIDToName(): void { + $workflowName = 'uf_notify'; + $workflowID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [ + 1 => [$workflowName, 'String'], ]); $created = $this->callAPISuccess('MessageTemplate', 'create', [ @@ -89,22 +86,24 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { 'msg_subject' => __FUNCTION__, 'msg_text' => __FUNCTION__, 'msg_html' => __FUNCTION__, - 'workflow_id' => $wfId, + 'workflow_id' => $workflowID, ]); - $this->assertEquals($wfName, $created['values'][$created['id']]['workflow_name']); - $this->assertEquals($wfId, $created['values'][$created['id']]['workflow_id']); + $this->assertEquals($workflowName, $created['values'][$created['id']]['workflow_name']); + $this->assertEquals($workflowID, $created['values'][$created['id']]['workflow_id']); $get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]); - $this->assertEquals($wfName, $get['workflow_name']); - $this->assertEquals($wfId, $get['workflow_id']); + $this->assertEquals($workflowName, $get['workflow_name']); + $this->assertEquals($workflowID, $get['workflow_id']); } /** * If you give workflow_name, then workflow_id should also be set. + * + * @throws \CRM_Core_Exception */ - public function testWorkflowNameToId() { - $wfName = 'petition_sign'; - $wfId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [ - 1 => [$wfName, 'String'], + public function testWorkflowNameToID(): void { + $workflowName = 'petition_sign'; + $workflowID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [ + 1 => ['petition_sign', 'String'], ]); $created = $this->callAPISuccess('MessageTemplate', 'create', [ @@ -112,67 +111,83 @@ class api_v3_MessageTemplateTest extends CiviUnitTestCase { 'msg_subject' => __FUNCTION__, 'msg_text' => __FUNCTION__, 'msg_html' => __FUNCTION__, - 'workflow_name' => $wfName, + 'workflow_name' => $workflowName, ]); - $this->assertEquals($wfName, $created['values'][$created['id']]['workflow_name']); - $this->assertEquals($wfId, $created['values'][$created['id']]['workflow_id']); + $this->assertEquals($workflowName, $created['values'][$created['id']]['workflow_name']); + $this->assertEquals($workflowID, $created['values'][$created['id']]['workflow_id']); $get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]); - $this->assertEquals($wfName, $get['workflow_name']); - $this->assertEquals($wfId, $get['workflow_id']); + $this->assertEquals($workflowName, $get['workflow_name']); + $this->assertEquals($workflowID, $get['workflow_id']); } - public function testPermissionChecks() { - $entity = $this->createTestEntity(); + /** + * Test workflow permissions. + * + * edit message templates allows editing all templates, otherwise: + * - edit user-driven message templates is required when workflow_name is not set. + * - edit system workflow message templates is required when workflow_name is set. + */ + public function testPermissionChecks(): void { + $this->createTestEntity('MessageTemplate', [ + 'msg_title' => 'title', + 'msg_subject' => 'subject', + 'msg_html' => 'html', + 'workflow_name' => 'friend', + ], 'workflow'); + + $this->createTestEntity('MessageTemplate', [ + 'msg_title' => 'title', + 'msg_subject' => 'subject', + 'msg_html' => 'html', + ], 'user'); + CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates']; - // Ensure that it cannot create a system message or update a system message tempalte given current permissions. + // Attempting to update the workflow template should fail with only user permissions. $this->callAPIFailure('MessageTemplate', 'create', [ - 'id' => $entity['id'], + 'id' => $this->ids['MessageTemplate']['workflow'], 'msg_subject' => 'test msg permission subject', 'check_permissions' => TRUE, ]); - $testUserEntity = $entity['values'][$entity['id']]; - unset($testUserEntity['id']); - $testUserEntity['msg_subject'] = 'Test user message template'; - unset($testUserEntity['workflow_id']); - unset($testUserEntity['workflow_name']); - $testuserEntity['check_permissions'] = TRUE; - // ensure that it can create user templates; - $userEntity = $this->callAPISuccess('MessageTemplate', 'create', $testUserEntity); + + // The user message should be possible to update. + $this->callAPISuccess('MessageTemplate', 'create', [ + 'id' => $this->ids['MessageTemplate']['user'], + 'msg_subject' => 'Test user message template', + 'check_permissions' => TRUE, + ]); CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates']; - // Now check that when its swapped around permissions that the correct reponses are detected. + // Now check that when its swapped around permissions that the correct responses are detected. $this->callAPIFailure('MessageTemplate', 'create', [ - 'id' => $userEntity['id'], + 'id' => $this->ids['MessageTemplate']['user'], 'msg_subject' => 'User template updated by system message permission', 'check_permissions' => TRUE, ]); $this->callAPISuccess('MessageTemplate', 'create', [ - 'id' => $entity['id'], + 'id' => $this->ids['MessageTemplate']['workflow'], 'msg_subject' => 'test msg permission subject', 'check_permissions' => TRUE, ]); - $newEntityParams = $entity['values'][$entity['id']]; - unset($newEntityParams['id']); - $newEntityParams['check_permissions'] = TRUE; - $this->callAPISuccess('MessageTemplate', 'create', $newEntityParams); - // verify with all 3 permissions someone can do everything. + + // With both permissions the user can update both template types. CRM_Core_Config::singleton()->userPermissionClass->permissions = [ 'edit system workflow message templates', 'edit user-driven message templates', ]; $this->callAPISuccess('MessageTemplate', 'create', [ - 'id' => $userEntity['id'], - 'msg_subject' => 'User template updated by system message permission', + 'id' => $this->ids['MessageTemplate']['workflow'], + 'msg_subject' => 'Workflow template updated', 'check_permissions' => TRUE, ]); $this->callAPISuccess('MessageTemplate', 'create', [ - 'id' => $entity['id'], - 'msg_subject' => 'test msg permission subject', + 'id' => $this->ids['MessageTemplate']['user'], + 'msg_subject' => 'User template updated', 'check_permissions' => TRUE, ]); - // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates + + // Verify that the backwards compatibility still works i.e. having edit message templates allows for editing of both kinds of message templates CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates']; - $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]); - $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject backwards compatabilty', 'check_permissions' => TRUE]); + $this->callAPISuccess('MessageTemplate', 'create', ['id' => $this->ids['MessageTemplate']['workflow'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]); + $this->callAPISuccess('MessageTemplate', 'create', ['id' => $this->ids['MessageTemplate']['user'], 'msg_subject' => 'test msg permission subject backwards compatibility', 'check_permissions' => TRUE]); } } diff --git a/tests/phpunit/api/v3/PcpTest.php b/tests/phpunit/api/v3/PcpTest.php index 262a00d823..0b24ad1358 100644 --- a/tests/phpunit/api/v3/PcpTest.php +++ b/tests/phpunit/api/v3/PcpTest.php @@ -34,7 +34,7 @@ class api_v3_PcpTest extends CiviUnitTestCase { public function setUp(): void { $this->params = [ - 'title' => "Pcp title", + 'title' => 'Pcp title', 'contact_id' => 1, 'page_id' => 1, 'pcp_block_id' => 1, @@ -45,7 +45,7 @@ class api_v3_PcpTest extends CiviUnitTestCase { /** * Test create function succeeds. */ - public function testCreatePcp() { + public function testCreatePcp(): void { $result = $this->callAPIAndDocument('Pcp', 'create', $this->params, __FUNCTION__, __FILE__); $this->getAndCheck($this->params, $result['id'], $this->entity); @@ -54,9 +54,9 @@ class api_v3_PcpTest extends CiviUnitTestCase { /** * Test disable a PCP succeeds. */ - public function testDisablePcp() { - $result = civicrm_api3('Pcp', 'create', $this->params); - civicrm_api3('Pcp', 'create', ['id' => $result['id'], 'is_active' => 0]); + public function testDisablePcp(): void { + $result = $this->callAPISuccess('Pcp', 'create', $this->params); + $this->callAPISuccess('Pcp', 'create', ['id' => $result['id'], 'is_active' => 0]); $this->getAndCheck($this->params + ['is_active' => 0], $result['id'], $this->entity); } @@ -67,8 +67,8 @@ class api_v3_PcpTest extends CiviUnitTestCase { * action on create. Add extra checks for any 'special' return values or * behaviours */ - public function testGetPcp() { - $this->createTestEntity(); + public function testGetPcp(): void { + $this->createTestEntity('PCP', $this->params); $result = $this->callAPIAndDocument('Pcp', 'get', $this->params, __FUNCTION__, __FILE__); $this->assertEquals(1, $result['count']); @@ -78,8 +78,8 @@ class api_v3_PcpTest extends CiviUnitTestCase { /** * Check the delete function succeeds. */ - public function testDeletePcp() { - $entity = $this->createTestEntity(); + public function testDeletePcp(): void { + $entity = $this->createTestEntity('PCP', $this->params); $checkCreated = $this->callAPISuccess($this->entity, 'get', ['id' => $entity['id']]); $this->assertEquals(1, $checkCreated['count']); @@ -97,13 +97,12 @@ class api_v3_PcpTest extends CiviUnitTestCase { * is best put in the $description variable as it will then be displayed in the * test generated examples. (these are to be found in the api/examples folder). */ - public function testGetPcpChainDelete() { - $description = "Demonstrates get + delete in the same call."; - $subfile = 'ChainedGetDelete'; - $params = ['title' => "Pcp title", 'api.Pcp.delete' => 1]; + public function testGetPcpChainDelete(): void { + $description = 'Demonstrates get + delete in the same call.'; + $params = ['title' => 'Pcp title', 'api.Pcp.delete' => 1]; $this->callAPISuccess('Pcp', 'create', $this->params); $this->callAPIAndDocument('Pcp', 'get', $params, __FUNCTION__, - __FILE__, $description, $subfile); + __FILE__, $description, 'ChainedGetDelete'); $this->assertEquals(0, $this->callAPISuccess('Pcp', 'getcount', [])); } diff --git a/tests/phpunit/api/v3/SurveyTest.php b/tests/phpunit/api/v3/SurveyTest.php index e9bedd7061..bfe0430b30 100644 --- a/tests/phpunit/api/v3/SurveyTest.php +++ b/tests/phpunit/api/v3/SurveyTest.php @@ -41,18 +41,18 @@ class api_v3_SurveyTest extends CiviUnitTestCase { $this->useTransaction(); $this->enableCiviCampaign(); $this->params = [ - 'title' => "survey title", + 'title' => 'survey title', 'activity_type_id' => $phoneBankActivityTypeID, 'max_number_of_contacts' => 12, - 'instructions' => "Call people, ask for money", + 'instructions' => 'Call people, ask for money', ]; } /** * Test create function succeeds. */ - public function testCreateSurvey() { - $result = $this->callAPIAndDocument('survey', 'create', $this->params, __FUNCTION__, __FILE__); + public function testCreateSurvey(): void { + $result = $this->callAPIAndDocument('Survey', 'create', $this->params, __FUNCTION__, __FILE__); $this->getAndCheck($this->params, $result['id'], $this->entity); } @@ -63,9 +63,9 @@ class api_v3_SurveyTest extends CiviUnitTestCase { * action on create. Add extra checks for any 'special' return values or * behaviours */ - public function testGetSurvey() { - $this->createTestEntity(); - $result = $this->callAPIAndDocument('survey', 'get', $this->params, __FUNCTION__, __FILE__); + public function testGetSurvey(): void { + $this->createTestEntity('Survey', $this->params); + $result = $this->callAPIAndDocument('Survey', 'get', $this->params, __FUNCTION__, __FILE__); $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); } @@ -73,9 +73,9 @@ class api_v3_SurveyTest extends CiviUnitTestCase { /** * Check the delete function succeeds. */ - public function testDeleteSurvey() { - $entity = $this->createTestEntity(); - $result = $this->callAPIAndDocument('survey', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__); + public function testDeleteSurvey(): void { + $entity = $this->createTestEntity('Survey', $this->params); + $this->callAPIAndDocument('survey', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__); $checkDeleted = $this->callAPISuccess($this->entity, 'get', []); $this->assertEquals(0, $checkDeleted['count']); } @@ -87,15 +87,14 @@ class api_v3_SurveyTest extends CiviUnitTestCase { * is best put in the $description variable as it will then be displayed in the * test generated examples. (these are to be found in the api/examples folder). */ - public function testGetSurveyChainDelete() { - $description = "Demonstrates get + delete in the same call."; - $subfile = 'ChainedGetDelete'; + public function testGetSurveyChainDelete(): void { + $description = 'Demonstrates get + delete in the same call.'; $params = [ - 'title' => "survey title", + 'title' => 'survey title', 'api.survey.delete' => 1, ]; - $result = $this->callAPISuccess('survey', 'create', $this->params); - $result = $this->callAPIAndDocument('survey', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); + $this->callAPISuccess('Survey', 'create', $this->params); + $this->callAPIAndDocument('Survey', 'get', $params, __FUNCTION__, __FILE__, $description, 'ChainedGetDelete'); $this->assertEquals(0, $this->callAPISuccess('survey', 'getcount', [])); } -- 2.25.1