Commit | Line | Data |
---|---|---|
8adcd073 TO |
1 | <?php |
2 | namespace Civi\Token; | |
3 | ||
8adcd073 TO |
4 | use Civi\Token\Event\TokenRegisterEvent; |
5 | use Civi\Token\Event\TokenValueEvent; | |
6 | use Symfony\Component\EventDispatcher\EventDispatcher; | |
7 | ||
8 | class 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 | ||
61 | /** | |
62 | * Check that the TokenRow helper can correctly read/update token | |
63 | * values. | |
64 | */ | |
65 | public function testRowTokens() { | |
66 | $p = new TokenProcessor($this->dispatcher, array( | |
67 | 'controller' => __CLASS__, | |
68 | )); | |
69 | $createdRow = $p->addRow() | |
70 | ->tokens('one', 1) | |
71 | ->tokens('two', array(2 => 3)) | |
72 | ->tokens(array( | |
73 | 'two' => array(4 => 5), | |
74 | 'three' => array(6 => 7), | |
75 | )) | |
76 | ->tokens('four', 8, 9); | |
77 | $gotRow = $p->getRow(0); | |
78 | foreach (array($createdRow, $gotRow) as $row) { | |
79 | $this->assertEquals(1, $row->tokens['one']); | |
80 | $this->assertEquals(3, $row->tokens['two'][2]); | |
81 | $this->assertEquals(5, $row->tokens['two'][4]); | |
82 | $this->assertEquals(7, $row->tokens['three'][6]); | |
83 | $this->assertEquals(9, $row->tokens['four'][8]); | |
84 | } | |
85 | } | |
86 | ||
87 | public function testGetMessageTokens() { | |
88 | $p = new TokenProcessor($this->dispatcher, array( | |
89 | 'controller' => __CLASS__, | |
90 | )); | |
91 | $p->addMessage('greeting_html', 'Good morning, <p>{contact.display_name}</p>. {custom.foobar}!', 'text/html'); | |
92 | $p->addMessage('greeting_text', 'Good morning, {contact.display_name}. {custom.whizbang}, {contact.first_name}!', 'text/plain'); | |
93 | $expected = array( | |
94 | 'contact' => array('display_name', 'first_name'), | |
95 | 'custom' => array('foobar', 'whizbang'), | |
96 | ); | |
97 | $this->assertEquals($expected, $p->getMessageTokens()); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Perform a full mail-merge, substituting multiple tokens for multiple | |
102 | * contacts in multiple messages. | |
103 | */ | |
104 | public function testFull() { | |
105 | $p = new TokenProcessor($this->dispatcher, array( | |
106 | 'controller' => __CLASS__, | |
107 | )); | |
108 | $p->addMessage('greeting_html', 'Good morning, <p>{contact.display_name}</p>. {custom.foobar} Bye!', 'text/html'); | |
109 | $p->addMessage('greeting_text', 'Good morning, {contact.display_name}. {custom.foobar} Bye!', 'text/plain'); | |
110 | $p->addRow() | |
111 | ->context(array('contact_id' => 123)) | |
112 | ->format('text/plain')->tokens(array( | |
113 | 'contact' => array('display_name' => 'What'), | |
114 | )); | |
115 | $p->addRow() | |
116 | ->context(array('contact_id' => 4)) | |
117 | ->format('text/plain')->tokens(array( | |
118 | 'contact' => array('display_name' => 'Who'), | |
119 | )); | |
120 | $p->addRow() | |
121 | ->context(array('contact_id' => 10)) | |
122 | ->format('text/plain')->tokens(array( | |
123 | 'contact' => array('display_name' => 'Darth Vader'), | |
124 | )); | |
125 | ||
126 | $expectHtml = array( | |
127 | 0 => 'Good morning, <p>What</p>. #0123 is a good number. Trickster {contact.display_name}. Bye!', | |
128 | 1 => 'Good morning, <p>Who</p>. #0004 is a good number. Trickster {contact.display_name}. Bye!', | |
129 | 2 => 'Good morning, <p>Darth Vader</p>. #0010 is a good number. Trickster {contact.display_name}. Bye!', | |
130 | ); | |
131 | ||
132 | $expectText = array( | |
133 | 0 => 'Good morning, What. #0123 is a good number. Trickster {contact.display_name}. Bye!' , | |
134 | 1 => 'Good morning, Who. #0004 is a good number. Trickster {contact.display_name}. Bye!', | |
135 | 2 => 'Good morning, Darth Vader. #0010 is a good number. Trickster {contact.display_name}. Bye!', | |
136 | ); | |
137 | ||
138 | $rowCount = 0; | |
139 | foreach ($p->evaluate()->getRows() as $key => $row) { | |
140 | /** @var TokenRow */ | |
141 | $this->assertTrue($row instanceof TokenRow); | |
142 | $this->assertEquals($expectHtml[$key], $row->render('greeting_html')); | |
143 | $this->assertEquals($expectText[$key], $row->render('greeting_text')); | |
144 | $rowCount++; | |
145 | } | |
146 | $this->assertEquals(3, $rowCount); | |
147 | $this->assertEquals(0, $this->counts['onListTokens']); // This may change in the future. | |
148 | $this->assertEquals(1, $this->counts['onEvalTokens']); | |
149 | } | |
150 | ||
151 | public function onListTokens(TokenRegisterEvent $e) { | |
152 | $this->counts[__FUNCTION__]++; | |
153 | $e->register('custom', array( | |
154 | 'foobar' => 'A special message about foobar', | |
155 | )); | |
156 | } | |
157 | ||
158 | public function onEvalTokens(TokenValueEvent $e) { | |
159 | $this->counts[__FUNCTION__]++; | |
160 | foreach ($e->getRows() as $row) { | |
161 | /** @var TokenRow $row */ | |
162 | $row->format('text/html'); | |
163 | $row->tokens['custom']['foobar'] = sprintf("#%04d is a good number. Trickster {contact.display_name}.", $row->context['contact_id']); | |
164 | } | |
165 | } | |
166 | ||
167 | } |