Add unit test
authorSeamus Lee <seamuslee001@gmail.com>
Mon, 13 Nov 2023 23:43:57 +0000 (10:43 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Mon, 13 Nov 2023 23:47:10 +0000 (10:47 +1100)
CRM/Core/Form.php
CRM/Event/Form/SelfSvcTransfer.php
tests/phpunit/CRM/Event/Form/SelfSvcTransferTest.php

index 3a605bfa7d38238ea7e2197e8754b3e2dd482276..da2a22ae6d1238ab6e188ce8e60a4a99bc66c0d2 100644 (file)
@@ -2550,8 +2550,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   protected function validateAuthenticatedCheckSumContactID(?int $contactID): int {
     $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
-    if ($userChecksum && CRM_Contact_BAO_Contact_Utils::validChecksum($requestedContactID, $userChecksum)) {
-      return $requestedContactID;
+    if ($userChecksum && CRM_Contact_BAO_Contact_Utils::validChecksum($contactID, $userChecksum)) {
+      return $contactID;
     }
     return 0;
   }
index f658ae38798aec5b367c75dbab58f899993ed9f0..eafb8fd202ef0cd5ed2ee8399f0947c06a50f72f 100644 (file)
@@ -125,7 +125,7 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form {
     $this->_event_id = $this->_part_values['event_id'];
     $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
     $this->define('Contact', 'ContactFrom', ['id' => (int) $this->_part_values['participant_contact_id']]);
-    if (!$this->validateAuthenticatedCheckSumContactID($this->getDefinition('ContactFrom')['id']) && !CRM_Core_Permission::check('edit all events')) {
+    if (!$this->validateAuthenticatedCheckSumContactID($this->lookup('ContactFrom', 'id')) && !CRM_Core_Permission::check('edit all events')) {
       CRM_Core_Error::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url);
     }
     $this->assign('action', $this->_action);
@@ -154,9 +154,14 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form {
     }
     // for front-end user show and use the basic three fields used to create a contact
     else {
-      $this->add('text', 'email', ts('To Email'), $this->lookup('ContactFrom', 'email_primary.email'), TRUE);
-      $this->add('text', 'last_name', ts('To Last Name'), $this->_to_contact_last_name, TRUE);
-      $this->add('text', 'first_name', ts('To First Name'), $this->_to_contact_first_name, TRUE);
+      $this->add('text', 'email', ts('To Email'), NULL, TRUE);
+      $this->add('text', 'last_name', ts('To Last Name'), NULL, TRUE);
+      $this->add('text', 'first_name', ts('To First Name'), NULL, TRUE);
+      $this->setDefaults([
+        'email' => $this->lookup('ContactFrom', 'email_primary.email'),
+        'last_name' => $this->_to_contact_last_name,
+        'first_name' => $this->_to_contact_first_name,
+      ]);
     }
 
     $this->addButtons([
index aef377b5105a7d05e05d56484d2b991190f11b70..a0c2a09eb18b7e71478d79bfd3d679a6e2a92d4e 100644 (file)
@@ -33,4 +33,31 @@ class CRM_Event_Form_SelfSvcTransferTest extends CiviUnitTestCase {
     $this->assertStringContainsString('fixme.domainemail@example.org', $emails[1]);
   }
 
+  /**
+   * Test Transfer as anonymous
+   */
+  public function testTransferAnonymous(): void {
+    CRM_Core_Session::singleton()->getStatus(TRUE);
+    $event = $this->eventCreateUnpaid(['start_date' => date('Ymd', strtotime('+2 month')), 'end_date' => date('Ymd', strtotime('+2 month')), 'registration_end_date' => date('Ymd', strtotime('+1 month')), 'allow_selfcancelxfer' => 1]);
+    $_REQUEST['pid'] = $this->participantCreate(['status_id' => 'Registered', 'event_id' => $event['id']]);
+    $_REQUEST['cs'] = CRM_Contact_BAO_Contact_Utils::generateChecksum($this->callAPISuccess('Participant', 'getsingle', ['id' => $_REQUEST['pid']])['contact_id']);
+    $_REQUEST['is_backoffice'] = 0;
+    $this->addLocationBlockToDomain();
+    $this->individualCreate(['email' => 'new2@example.org']);
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
+    /** @var CRM_Event_Form_SelfSvcTransfer $form*/
+    $form = $this->getFormObject('CRM_Event_Form_SelfSvcTransfer', [
+      'first_name' => 'test',
+      'last_name' => 'selftransfer',
+      'email' => 'new2@example.org',
+    ]);
+    $form->buildForm();
+    try {
+      $form->postProcess();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $this->assertEquals('Registration Transferred', CRM_Core_Session::singleton()->getStatus()[1]['title']);
+    }
+  }
+
 }