[TEST] Further minor fixes on UFGroup Cleanup
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 4 Jun 2023 21:20:50 +0000 (09:20 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 5 Jun 2023 05:51:39 +0000 (17:51 +1200)
tests/phpunit/CRM/Contribute/Form/ContributionTest.php
tests/phpunit/CRM/Profile/Form/EditTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/MessageTemplateTest.php
tests/phpunit/api/v3/PcpTest.php
tests/phpunit/api/v3/SurveyTest.php

index b828a9db22c2ce77c7ebfec05c67d9140d4c95d9..39f0f2f2455597a89c456f10482c812a76a6b1db 100644 (file)
@@ -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,
index 500fde0ea70259311c3bc992bcb329691367abf9..191cec0db3b8fc6d85779fe6fb0a5b2f9c5214d5 100644 (file)
@@ -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,
index 2804ff2256d057e06ef54e6e6ceafc1cac7e461e..dcd280d78c9acf93c328a35a1216d436176d70d6 100644 (file)
@@ -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']);
   }
 
   /**
index 5272a1cc63c179b26053c4ed2ba8acaff0d1f5f7..15da965948a89eb43ce94c8837b1deb177b04a41 100644 (file)
@@ -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]);
   }
 
 }
index 262a00d82370916b1bac96d975f6ccbd607cff71..0b24ad1358d4eaa5068493130ccbeb6fa1a17c78 100644 (file)
@@ -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', []));
   }
 
index e9bedd70612a0401ba16cb4e673937d4381027e6..bfe0430b3092af1387e00ff3f69b34d1e5b2b2b4 100644 (file)
@@ -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', []));
   }