Skip to content

Commit

Permalink
Merge pull request #26434 from eileenmcnaughton/uf_group_cleanup
Browse files Browse the repository at this point in the history
[TEST] Further minor fixes on UFGroup Cleanup
  • Loading branch information
colemanw authored Jun 5, 2023
2 parents 5739d5c + 82e6c17 commit 5438e5c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 105 deletions.
12 changes: 6 additions & 6 deletions tests/phpunit/CRM/Contribute/Form/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ public function testSubmitWithNoteCreditCard(): void {
] + $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']);
}

/**
Expand Down Expand Up @@ -1517,8 +1517,8 @@ public function testCustomFieldsOnMembershipGetUpdated(): void {
'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',
Expand Down Expand Up @@ -1730,7 +1730,7 @@ public function testDonationOnMembershipPagePriceSet(): void {
/**
* 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',
Expand Down Expand Up @@ -2109,8 +2109,8 @@ public function testContributionFormRule() {
* 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,
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Profile/Form/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function tearDown(): void {
* @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,
Expand Down
17 changes: 15 additions & 2 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ protected function tearDown(): void {
if (!empty($this->ids['UFGroup'])) {
UFGroup::delete(FALSE)->addWhere('id', 'IN', $this->ids['UFGroup'])->execute();
}
unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
parent::tearDown();
}

Expand Down Expand Up @@ -557,8 +558,20 @@ public function createExternalAPI() {
* @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']);
}

/**
Expand Down
145 changes: 80 additions & 65 deletions tests/phpunit/api/v3/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -57,18 +49,21 @@ public function testCreate() {
* 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']);
}

/**
* 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'],
]);
Expand All @@ -77,102 +72,122 @@ public function testDelete() {

/**
* 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', [
'msg_title' => __FUNCTION__,
'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', [
'msg_title' => __FUNCTION__,
'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]);
}

}
27 changes: 13 additions & 14 deletions tests/phpunit/api/v3/PcpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -45,7 +45,7 @@ public function setUp(): void {
/**
* 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);
Expand All @@ -54,9 +54,9 @@ public function testCreatePcp() {
/**
* 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);
}

Expand All @@ -67,8 +67,8 @@ public function testDisablePcp() {
* 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']);
Expand All @@ -78,8 +78,8 @@ public function testGetPcp() {
/**
* 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']);
Expand All @@ -97,13 +97,12 @@ public function testDeletePcp() {
* 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', []));
}

Expand Down
Loading

0 comments on commit 5438e5c

Please sign in to comment.