Convert event badges to use token processor
[civicrm-core.git] / tests / phpunit / CRM / Event / Form / Task / BadgeTest.php
1 <?php
2
3 use Civi\Api4\PrintLabel;
4
5 /**
6 * Test CRM_Event_Form_Registration functions.
7 *
8 * @package CiviCRM
9 * @group headless
10 */
11 class CRM_Event_Form_Task_BadgeTest extends CiviUnitTestCase {
12
13 use CRMTraits_Custom_CustomDataTrait;
14
15 public function tearDown(): void {
16 $this->quickCleanup(['civicrm_participant', 'civicrm_print_label'], TRUE);
17 parent::tearDown();
18 }
19
20 /**
21 * Test the the submit function on the event participant submit function.
22 */
23 public function testSubmit(): void {
24 $this->createCustomGroupWithFieldOfType(['extends' => 'Participant']);
25 $contactID = $this->individualCreate(['employer_id' => 1]);
26 $participantID = $this->participantCreate([
27 'contact_id' => $contactID,
28 'fee_level' => 'low',
29 ]);
30
31 $badgeLayout = PrintLabel::get()->addSelect('data')->execute()->first();
32 $values = [
33 'data' => array_merge($badgeLayout['data'], ['token' => [], 'font_name' => [''], 'font_size' => [], 'text_alignment' => []]),
34 ];
35 foreach (array_keys($this->getAvailableTokens()) as $id => $token) {
36 $index = $id + 1;
37 $values['data']['token'][$index] = $token;
38 $values['data']['font_name'][$index] = 'dejavusans';
39 $values['data']['font_size'][$index] = '20';
40 $values['data']['font_style'][$index] = '';
41 $values['data']['text_alignment'][$index] = 'C';
42 }
43 PrintLabel::update()->addWhere('id', '=', 1)->setValues($values)->execute();
44
45 $_REQUEST['context'] = 'view';
46 $_REQUEST['id'] = $participantID;
47 $_REQUEST['cid'] = $contactID;
48 /* @var CRM_Event_Form_Task_Badge $form */
49 $form = $this->getFormObject(
50 'CRM_Event_Form_Task_Badge',
51 ['badge_id' => 1],
52 NULL,
53 [
54 'task' => CRM_Core_Task::BATCH_UPDATE,
55 'radio_ts' => 'ts_sel',
56 'mark_x_' . $participantID => 1,
57 ]
58 );
59 $form->buildForm();
60 try {
61 $form->postProcess();
62 }
63 catch (CRM_Core_Exception_PrematureExitException $e) {
64 $tokens = $e->errorData['formattedRow']['token'];
65 $this->assertEquals([
66 'value' => 'Annual CiviCRM meet',
67 'font_name' => 'dejavusans',
68 'font_size' => '20',
69 'font_style' => '',
70 'text_alignment' => 'C',
71 'token' => '{event.title}',
72 ], $tokens[1]);
73 $index = 1;
74 foreach ($this->getAvailableTokens() as $token => $expected) {
75 $this->assertEquals($expected, $tokens[$index]['value'], 'failure in token ' . $token);
76 $index++;
77 }
78 return;
79 }
80 $this->fail('Should not be reached');
81 }
82
83 /**
84 * @return string[]
85 */
86 protected function getAvailableTokens(): array {
87 return [
88 '{event.title}' => 'Annual CiviCRM meet',
89 '{contact.display_name}' => 'Mr. Anthony Anderson II',
90 '{contact.current_employer}' => 'Default Organization',
91 '{event.start_date}' => 'October 21st',
92 '{participant.status_id}' => 2,
93 '{participant.role_id}' => 1,
94 '{participant.register_date}' => 'February 19th, 2007 12:00 AM',
95 '{participant.source}' => 'Wimbeldon',
96 '{participant.fee_level}' => 'low',
97 '{participant.fee_amount}' => NULL,
98 '{participant.registered_by_id}' => NULL,
99 '{participant.transferred_to_contact_id}' => NULL,
100 '{participant.role_id:label}' => 'Attendee',
101 '{participant.fee_label}' => NULL,
102 '{event.end_date}' => 'October 23rd',
103 '{event.id}' => 1,
104 ];
105 }
106
107 }