dev/core#2834 Preliminary test on badge
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 13 Sep 2021 11:11:22 +0000 (23:11 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 13 Sep 2021 22:08:00 +0000 (10:08 +1200)
CRM/Badge/BAO/Badge.php
tests/phpunit/CRM/Event/Form/Task/BadgeTest.php [new file with mode: 0644]
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php

index fc98961f7a57b213d0e76d9ab81633bb99618648..e501e95ff7515f35933e7b46b94769b61760276c 100644 (file)
@@ -40,7 +40,7 @@ class CRM_Badge_BAO_Badge {
    * @param array $layoutInfo
    *   Associated array which contains meta data about format/layout.
    */
-  public function createLabels(&$participants, &$layoutInfo) {
+  public function createLabels($participants, &$layoutInfo) {
     $this->pdf = new CRM_Utils_PDF_Label($layoutInfo['format'], 'mm');
     $this->pdf->Open();
     $this->pdf->setPrintHeader(FALSE);
@@ -58,6 +58,9 @@ class CRM_Badge_BAO_Badge {
       $this->pdf->AddPdfLabel($formattedRow);
     }
 
+    if (CIVICRM_UF === 'UnitTests') {
+      throw new CRM_Core_Exception_PrematureExitException('pdf output called', ['formattedRow' => $formattedRow]);
+    }
     $this->pdf->Output(CRM_Utils_String::munge($layoutInfo['title'], '_', 64) . '.pdf', 'D');
     CRM_Utils_System::civiExit();
   }
diff --git a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php
new file mode 100644 (file)
index 0000000..6ba914b
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+/**
+ *  Test CRM_Event_Form_Registration functions.
+ *
+ * @package   CiviCRM
+ * @group headless
+ */
+class CRM_Event_Form_Task_BadgeTest extends CiviUnitTestCase {
+
+  use CRMTraits_Custom_CustomDataTrait;
+
+  /**
+   * Test the the submit function on the event participant submit function.
+   */
+  public function testSubmit(): void {
+    $this->createCustomGroupWithFieldOfType(['extends' => 'Participant']);
+    $contactID = $this->individualCreate();
+    $participantID = $this->participantCreate(['contact_id' => $contactID]);
+
+    $_REQUEST['context'] = 'view';
+    $_REQUEST['id'] = $participantID;
+    $_REQUEST['cid'] = $contactID;
+    /* @var CRM_Event_Form_Task_Badge $form */
+    $form = $this->getFormObject(
+      'CRM_Event_Form_Task_Badge',
+      ['badge_id' => 1],
+      NULL,
+      [
+        'task' => CRM_Core_Task::BATCH_UPDATE,
+        'radio_ts' => 'ts_sel',
+        'mark_x_' . $participantID => 1,
+      ]
+    );
+    $form->buildForm();
+    try {
+      $form->postProcess();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $tokens = $e->errorData['formattedRow']['token'];
+      $this->assertEquals([
+        1 => [
+          'value' => 'Annual CiviCRM meet',
+          'font_name' => 'dejavusans',
+          'font_size' => '9',
+          'font_style' => '',
+          'text_alignment' => 'L',
+          'token' => '{event.title}',
+        ],
+        2 =>
+          [
+            'value' => 'Mr. Anthony Anderson II',
+            'font_name' => 'dejavusans',
+            'font_size' => '20',
+            'font_style' => '',
+            'text_alignment' => 'C',
+            'token' => '{contact.display_name}',
+          ],
+        3 =>
+          [
+            'value' => NULL,
+            'font_name' => 'dejavusans',
+            'font_size' => '15',
+            'font_style' => '',
+            'text_alignment' => 'C',
+            'token' => '{contact.current_employer}',
+          ],
+        4 =>
+          [
+            'value' => 'October 21st',
+            'font_name' => 'dejavusans',
+            'font_size' => '9',
+            'font_style' => '',
+            'text_alignment' => 'R',
+            'token' => '{event.start_date}',
+          ],
+      ], $tokens);
+      return;
+    }
+    $this->fail('Should not be reached');
+  }
+
+}
index 5811015977b1fa55501c316797af9b1598e08ae1..df0e4b10241057cd00f4deae261499ddf87b7f55 100644 (file)
@@ -52,7 +52,15 @@ trait CRMTraits_Custom_CustomDataTrait {
       'max_multiple' => 0,
     ], $params);
     $identifier = $params['name'] ?? $params['title'];
-    $this->ids['CustomGroup'][$identifier] = CustomGroup::create(FALSE)->setValues($params)->execute()->first()['id'];
+    try {
+      $this->ids['CustomGroup'][$identifier] = CustomGroup::create(FALSE)
+        ->setValues($params)
+        ->execute()
+        ->first()['id'];
+    }
+    catch (API_Exception $e) {
+      $this->fail('Could not create group ' . $e->getMessage());
+    }
     return $this->ids['CustomGroup'][$identifier];
   }
 
@@ -90,7 +98,6 @@ trait CRMTraits_Custom_CustomDataTrait {
    *
    * @param array $fieldParams
    *
-   * @throws \API_Exception
    */
   public function createCustomGroupWithFieldOfType(array $groupParams = [], string $customFieldType = 'text', ?string $identifier = NULL, array $fieldParams = []): void {
     $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country'];