From d0b58293b2804ca4e6b899e85989d1913ac73590 Mon Sep 17 00:00:00 2001
From: Eileen McNaughton <emcnaughton@wikimedia.org>
Date: Sat, 4 Sep 2021 08:54:24 +1200
Subject: [PATCH] dev/core#2814 Fix profile edit to use messagetemplate:render

If the api were merged I would have used that - but this adds the test & when can test converting
to the api when it is merged. It gets the hard lifting of this conversion out of the way
---
 CRM/Profile/Form/Edit.php                   | 22 ++++-----
 tests/phpunit/CRM/Profile/Form/EditTest.php | 49 +++++++++++++++++++++
 2 files changed, 57 insertions(+), 14 deletions(-)
 create mode 100644 tests/phpunit/CRM/Profile/Form/EditTest.php

diff --git a/CRM/Profile/Form/Edit.php b/CRM/Profile/Form/Edit.php
index a466c3b2f7c..c0f7ec2076a 100644
--- a/CRM/Profile/Form/Edit.php
+++ b/CRM/Profile/Form/Edit.php
@@ -243,6 +243,9 @@ SELECT module,is_reserved
   /**
    * Process the user submitted custom data values.
    *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function postProcess() {
     parent::postProcess();
@@ -287,20 +290,11 @@ SELECT module,is_reserved
       $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams);
     }
     else {
-      // Replace tokens from post URL
-      $contactParams = [
-        'contact_id' => $this->_id,
-        'version' => 3,
-      ];
-
-      $contact = civicrm_api('contact', 'get', $contactParams);
-      $contact = reset($contact['values']);
-
-      $dummyMail = new CRM_Mailing_BAO_Mailing();
-      $dummyMail->body_text = $this->_postURL;
-      $tokens = $dummyMail->getTokens();
-
-      $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens));
+      $url = CRM_Core_BAO_MessageTemplate::renderTemplate([
+        'messageTemplate' => ['msg_text' => $this->_postURL],
+        'contactId' => $this->_id,
+        'disableSmarty' => TRUE,
+      ])['text'];
     }
 
     $session->replaceUserContext($url);
diff --git a/tests/phpunit/CRM/Profile/Form/EditTest.php b/tests/phpunit/CRM/Profile/Form/EditTest.php
new file mode 100644
index 00000000000..753ce74f27d
--- /dev/null
+++ b/tests/phpunit/CRM/Profile/Form/EditTest.php
@@ -0,0 +1,49 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+use Civi\Api4\UFJoin;
+
+/**
+ * Test class for CRM_Price_BAO_PriceSet.
+ * @group headless
+ */
+class CRM_Profile_Form_EditTest extends CiviUnitTestCase {
+
+  public function tearDown(): void {
+    $this->quickCleanup(['civicrm_uf_field', 'civicrm_uf_group']);
+    parent::tearDown();
+  }
+
+  /**
+   * Test the url on the profile edit form renders tokens
+   *
+   * @throws \API_Exception
+   */
+  public function testProfileUrl(): void {
+    $profileID = Civi\Api4\UFGroup::create(FALSE)->setValues([
+      'post_URL' => 'civicrm/{contact.display_name}',
+      'title' => 'title',
+    ])->execute()->first()['id'];
+    UFJoin::create(FALSE)->setValues([
+      'module' => 'Profile',
+      'uf_group_id' => $profileID,
+    ])->execute();
+    $this->uFFieldCreate(['uf_group_id' => $profileID]);
+    $id = $this->individualCreate();
+    $form = $this->getFormObject('CRM_Profile_Form_Edit');
+    $form->set('gid', $profileID);
+    $form->set('id', $id);
+    $form->buildForm();
+    $form->postProcess();
+    $this->assertEquals('civicrm/Mr. Anthony Anderson II', CRM_Core_Session::singleton()->popUserContext());
+  }
+
+}
-- 
GitLab