Merge pull request #12669 from eileenmcnaughton/dedupe_zero
[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 /**
11 * @var EventDispatcher
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
116 /**
117 * Perform a full mail-merge, substituting multiple tokens for multiple
118 * contacts in multiple messages.
119 */
120 public function testFull() {
121 $p = new TokenProcessor($this->dispatcher, array(
122 'controller' => __CLASS__,
123 ));
124 $p->addMessage('greeting_html', 'Good morning, <p>{contact.display_name}</p>. {custom.foobar} Bye!', 'text/html');
125 $p->addMessage('greeting_text', 'Good morning, {contact.display_name}. {custom.foobar} Bye!', 'text/plain');
126 $p->addRow()
127 ->context(array('contact_id' => 123))
128 ->format('text/plain')->tokens(array(
129 'contact' => array('display_name' => 'What'),
130 ));
131 $p->addRow()
132 ->context(array('contact_id' => 4))
133 ->format('text/plain')->tokens(array(
134 'contact' => array('display_name' => 'Who'),
135 ));
136 $p->addRow()
137 ->context(array('contact_id' => 10))
138 ->format('text/plain')->tokens(array(
139 'contact' => array('display_name' => 'Darth Vader'),
140 ));
141
142 $expectHtml = array(
143 0 => 'Good morning, <p>What</p>. #0123 is a good number. Trickster {contact.display_name}. Bye!',
144 1 => 'Good morning, <p>Who</p>. #0004 is a good number. Trickster {contact.display_name}. Bye!',
145 2 => 'Good morning, <p>Darth Vader</p>. #0010 is a good number. Trickster {contact.display_name}. Bye!',
146 );
147
148 $expectText = array(
149 0 => 'Good morning, What. #0123 is a good number. Trickster {contact.display_name}. Bye!' ,
150 1 => 'Good morning, Who. #0004 is a good number. Trickster {contact.display_name}. Bye!',
151 2 => 'Good morning, Darth Vader. #0010 is a good number. Trickster {contact.display_name}. Bye!',
152 );
153
154 $rowCount = 0;
155 foreach ($p->evaluate()->getRows() as $key => $row) {
156 /** @var TokenRow */
157 $this->assertTrue($row instanceof TokenRow);
158 $this->assertEquals($expectHtml[$key], $row->render('greeting_html'));
159 $this->assertEquals($expectText[$key], $row->render('greeting_text'));
160 $rowCount++;
161 }
162 $this->assertEquals(3, $rowCount);
163 $this->assertEquals(0, $this->counts['onListTokens']); // This may change in the future.
164 $this->assertEquals(1, $this->counts['onEvalTokens']);
165 }
166
167 public function onListTokens(TokenRegisterEvent $e) {
168 $this->counts[__FUNCTION__]++;
169 $e->register('custom', array(
170 'foobar' => 'A special message about foobar',
171 ));
172 }
173
174 public function onEvalTokens(TokenValueEvent $e) {
175 $this->counts[__FUNCTION__]++;
176 foreach ($e->getRows() as $row) {
177 /** @var TokenRow $row */
178 $row->format('text/html');
179 $row->tokens['custom']['foobar'] = sprintf("#%04d is a good number. Trickster {contact.display_name}.", $row->context['contact_id']);
180 }
181 }
182
183}