Merge pull request #13986 from seamuslee001/coder_upgrade_uf_tag_sms
[civicrm-core.git] / tests / phpunit / Civi / Token / TokenProcessorTest.php
CommitLineData
8adcd073
TO
1<?php
2namespace Civi\Token;
3
8adcd073
TO
4use Civi\Token\Event\TokenRegisterEvent;
5use Civi\Token\Event\TokenValueEvent;
6use Symfony\Component\EventDispatcher\EventDispatcher;
7
8class TokenProcessorTest extends \CiviUnitTestCase {
9
10 /**
39b959db 11 * @var \Symfony\Component\EventDispatcher\EventDispatcher
8adcd073
TO
12 */
13 protected $dispatcher;
14
15 /**
16 * @var array
17 * Array(string $funcName => int $invocationCount).
18 */
19 protected $counts;
20
21 protected function setUp() {
22 $this->useTransaction(TRUE);
23 parent::setUp();
24 $this->dispatcher = new EventDispatcher();
25 $this->dispatcher->addListener(Events::TOKEN_REGISTER, array($this, 'onListTokens'));
26 $this->dispatcher->addListener(Events::TOKEN_EVALUATE, array($this, 'onEvalTokens'));
27 $this->counts = array(
28 'onListTokens' => 0,
29 'onEvalTokens' => 0,
30 );
31 }
32
33 /**
34 * Check that the TokenRow helper can correctly read/update context
35 * values.
36 */
37 public function testRowContext() {
38 $p = new TokenProcessor($this->dispatcher, array(
39 'controller' => __CLASS__,
40 'omega' => '99',
41 ));
42 $createdRow = $p->addRow()
43 ->context('one', 1)
44 ->context('two', array(2 => 3))
45 ->context(array(
46 'two' => array(4 => 5),
47 'three' => array(6 => 7),
48 'omega' => '98',
49 ));
50 $gotRow = $p->getRow(0);
51 foreach (array($createdRow, $gotRow) as $row) {
52 $this->assertEquals(1, $row->context['one']);
53 $this->assertEquals(3, $row->context['two'][2]);
54 $this->assertEquals(5, $row->context['two'][4]);
55 $this->assertEquals(7, $row->context['three'][6]);
56 $this->assertEquals(98, $row->context['omega']);
57 $this->assertEquals(__CLASS__, $row->context['controller']);
58 }
59 }
60
3f52ee8b
AS
61 /**
62 * Check that getContextValues() returns the correct data
63 */
64 public function testGetContextValues() {
65 $p = new TokenProcessor($this->dispatcher, array(
66 'controller' => __CLASS__,
67 'omega' => '99',
68 ));
69 $p->addRow()->context('id', 10)->context('omega', '98');
70 $p->addRow()->context('id', 10)->context('contact', (object) ['cid' => 10]);
71 $p->addRow()->context('id', 11)->context('contact', (object) ['cid' => 11]);
72 $this->assertArrayValuesEqual([10, 11], $p->getContextValues('id'));
73 $this->assertArrayValuesEqual(['99', '98'], $p->getContextValues('omega'));
74 $this->assertArrayValuesEqual([10, 11], $p->getContextValues('contact', 'cid'));
75 }
76
8adcd073
TO
77 /**
78 * Check that the TokenRow helper can correctly read/update token
79 * values.
80 */
81 public function testRowTokens() {
82 $p = new TokenProcessor($this->dispatcher, array(
83 'controller' => __CLASS__,
84 ));
85 $createdRow = $p->addRow()
86 ->tokens('one', 1)
87 ->tokens('two', array(2 => 3))
88 ->tokens(array(
89 'two' => array(4 => 5),
90 'three' => array(6 => 7),
91 ))
92 ->tokens('four', 8, 9);
93 $gotRow = $p->getRow(0);
94 foreach (array($createdRow, $gotRow) as $row) {
95 $this->assertEquals(1, $row->tokens['one']);
96 $this->assertEquals(3, $row->tokens['two'][2]);
97 $this->assertEquals(5, $row->tokens['two'][4]);
98 $this->assertEquals(7, $row->tokens['three'][6]);
99 $this->assertEquals(9, $row->tokens['four'][8]);
100 }
101 }
102
103 public function testGetMessageTokens() {
104 $p = new TokenProcessor($this->dispatcher, array(
105 'controller' => __CLASS__,
106 ));
107 $p->addMessage('greeting_html', 'Good morning, <p>{contact.display_name}</p>. {custom.foobar}!', 'text/html');
108 $p->addMessage('greeting_text', 'Good morning, {contact.display_name}. {custom.whizbang}, {contact.first_name}!', 'text/plain');
109 $expected = array(
110 'contact' => array('display_name', 'first_name'),
111 'custom' => array('foobar', 'whizbang'),
112 );
113 $this->assertEquals($expected, $p->getMessageTokens());
114 }
115
1c4a04c9
AS
116 public function testListTokens() {
117 $p = new TokenProcessor($this->dispatcher, array(
118 'controller' => __CLASS__,
119 ));
120 $p->addToken(array('entity' => 'MyEntity', 'field' => 'myField', 'label' => 'My Label'));
121 $this->assertEquals(array('{MyEntity.myField}' => 'My Label'), $p->listTokens());
122 }
123
8adcd073
TO
124 /**
125 * Perform a full mail-merge, substituting multiple tokens for multiple
126 * contacts in multiple messages.
127 */
128 public function testFull() {
129 $p = new TokenProcessor($this->dispatcher, array(
130 'controller' => __CLASS__,
131 ));
132 $p->addMessage('greeting_html', 'Good morning, <p>{contact.display_name}</p>. {custom.foobar} Bye!', 'text/html');
133 $p->addMessage('greeting_text', 'Good morning, {contact.display_name}. {custom.foobar} Bye!', 'text/plain');
134 $p->addRow()
135 ->context(array('contact_id' => 123))
136 ->format('text/plain')->tokens(array(
137 'contact' => array('display_name' => 'What'),
138 ));
139 $p->addRow()
140 ->context(array('contact_id' => 4))
141 ->format('text/plain')->tokens(array(
142 'contact' => array('display_name' => 'Who'),
143 ));
144 $p->addRow()
145 ->context(array('contact_id' => 10))
146 ->format('text/plain')->tokens(array(
147 'contact' => array('display_name' => 'Darth Vader'),
148 ));
149
150 $expectHtml = array(
151 0 => 'Good morning, <p>What</p>. #0123 is a good number. Trickster {contact.display_name}. Bye!',
152 1 => 'Good morning, <p>Who</p>. #0004 is a good number. Trickster {contact.display_name}. Bye!',
153 2 => 'Good morning, <p>Darth Vader</p>. #0010 is a good number. Trickster {contact.display_name}. Bye!',
154 );
155
156 $expectText = array(
39b959db 157 0 => 'Good morning, What. #0123 is a good number. Trickster {contact.display_name}. Bye!',
8adcd073
TO
158 1 => 'Good morning, Who. #0004 is a good number. Trickster {contact.display_name}. Bye!',
159 2 => 'Good morning, Darth Vader. #0010 is a good number. Trickster {contact.display_name}. Bye!',
160 );
161
162 $rowCount = 0;
163 foreach ($p->evaluate()->getRows() as $key => $row) {
164 /** @var TokenRow */
165 $this->assertTrue($row instanceof TokenRow);
166 $this->assertEquals($expectHtml[$key], $row->render('greeting_html'));
167 $this->assertEquals($expectText[$key], $row->render('greeting_text'));
168 $rowCount++;
169 }
170 $this->assertEquals(3, $rowCount);
39b959db
SL
171 // This may change in the future.
172 $this->assertEquals(0, $this->counts['onListTokens']);
8adcd073
TO
173 $this->assertEquals(1, $this->counts['onEvalTokens']);
174 }
175
176 public function onListTokens(TokenRegisterEvent $e) {
177 $this->counts[__FUNCTION__]++;
178 $e->register('custom', array(
179 'foobar' => 'A special message about foobar',
180 ));
181 }
182
183 public function onEvalTokens(TokenValueEvent $e) {
184 $this->counts[__FUNCTION__]++;
185 foreach ($e->getRows() as $row) {
186 /** @var TokenRow $row */
187 $row->format('text/html');
188 $row->tokens['custom']['foobar'] = sprintf("#%04d is a good number. Trickster {contact.display_name}.", $row->context['contact_id']);
189 }
190 }
191
192}