Skip to content

Commit

Permalink
MAE-823: Sets the form submission values in Session
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Sep 1, 2022
1 parent 01ce0a4 commit c00004b
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions tests/phpunit/CRM/Civicase/Hook/ValidateForm/SendBulkEmailTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Civi\Test\Api3TestTrait;
use CRM_Civicase_Hook_ValidateForm_SendBulkEmail as SendBulkEmailHook;

/**
Expand All @@ -11,6 +12,7 @@ class CRM_Civicase_Hook_SendBulkEmailTest extends BaseHeadlessTest {

use CRM_Civicase_Helpers_SessionTrait;
use Helpers_MailHelpersTrait;
use Api3TestTrait;

/**
* Instance of the form sent on the hook.
Expand All @@ -26,14 +28,21 @@ class CRM_Civicase_Hook_SendBulkEmailTest extends BaseHeadlessTest {
*/
private $caseType;

/**
* Currently logged in User.
*
* @var array
*/
private $loggedInUser;

/**
* {@inheritDoc}
*/
public function setUp() {
parent::setUp();

$loggedInUser = CRM_Civicase_Test_Fabricator_Contact::fabricateWithEmail();
$this->registerCurrentLoggedInContactInSession($loggedInUser['id']);
$this->loggedInUser = CRM_Civicase_Test_Fabricator_Contact::fabricateWithEmail();
$this->registerCurrentLoggedInContactInSession($this->loggedInUser['id']);

$this->caseType = CRM_Civicase_Test_Fabricator_CaseType::fabricate()['id'];
}
Expand Down Expand Up @@ -299,9 +308,20 @@ private function createCaseForContact(int $contactId) {
* Return the result of calling the hook.
*/
private function runHook(string $subject, string $body, array $cases, array $contacts) {
$formName = $this->form->getName();
$senderEmail = $this->callAPISuccess('Email', 'getsingle', ['contact_id' => $this->loggedInUser['id']]);

$formValues = [
'to' => [],
'cc_id' => [],
'bcc_id' => [],
'subject' => $subject,
'text_message' => $body,
'html_message' => $body,
'from_email_address' => $senderEmail['id'],
];
$_REQUEST['allCaseIds'] = $_GET['allCaseIds'] = implode(',', $cases);

$formName = 'Test Form';
$fields = $files = $errors = [];

$this->form->_contactIds = [];
Expand All @@ -315,16 +335,34 @@ private function runHook(string $subject, string $body, array $cases, array $con
'contact_id' => $contact['id'],
'preferred_mail_format' => 'HTML',
];
$formValues['to'][] = $contact['id'] . '::' . $contact['email'];
}
$formValues['to'] = implode(',', $formValues['to']);

$this->setFormSubmitValues($formName, $formValues);
return (new SendBulkEmailHook())->run($formName, $fields, $files, $this->form, $errors);
}

/**
* Sets the form submission values in Session and element.
*
* @param string $formName
* The form name.
* @param array $formValues
* The values of the form fields.
*/
private function setFormSubmitValues(string $formName, array $formValues) {
$this->form->controller = new CRM_Core_Controller_Simple(get_class($this->form), $formName);
$_SESSION['_' . $this->form->controller->_name . '_container']['values'][$formName] = $formValues;
$_SESSION['_' . $this->form->controller->_name . '_container']['values']['Search'] = [];

$this->form->addElement('hidden', 'cc_id', []);
$this->form->addElement('hidden', 'bcc_id', []);
$this->form->addElement('hidden', 'subject', $subject);
$this->form->addElement('hidden', 'text_message', $body);
$this->form->addElement('hidden', 'html_message', $body);
$this->form->addElement('hidden', 'from_email_address', '[email protected]');

return (new SendBulkEmailHook())->run($formName, $fields, $files, $this->form, $errors);
$this->form->addElement('hidden', 'to', $formValues['to']);
$this->form->addElement('hidden', 'subject', $formValues['subject']);
$this->form->addElement('hidden', 'text_message', $formValues['text_message']);
$this->form->addElement('hidden', 'html_message', $formValues['html_message']);
$this->form->addElement('hidden', 'from_email_address', $formValues['from_email_address']);
}

/**
Expand Down

0 comments on commit c00004b

Please sign in to comment.