-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathExampleWorkflowMessageTest.php
346 lines (297 loc) · 12.1 KB
/
ExampleWorkflowMessageTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\WorkflowMessage;
use Civi\Test\Invasive;
/**
* Test the WorkflowMessage class
*
* @group headless
*/
class ExampleWorkflowMessageTest extends \CiviUnitTestCase {
protected function setUp(): void {
$this->useTransaction();
parent::setUp();
}
/**
* @return \Civi\WorkflowMessage\WorkflowMessageInterface
*/
protected static function createExample() {
return new class() extends GenericWorkflowMessage {
const WORKFLOW = 'my_example_wf';
/**
* Use this to provide interop with old-style `groupName`.
* @deprecated
*/
const GROUP = 'msg_old_style_grp';
/**
* @var string
* @scope tplParams as my_public_string
*/
public $myPublicString;
/**
* @var int
* @scope tplParams as my_int
*/
protected $myProtectedInt;
/**
* @var string[]
*/
protected $implicitStringArray;
/**
* @var string[]
* @dataType Text
* @serialize COMMA
*/
protected $explicitStringArray;
/**
* @var int
* @scope tplParams as some.deep.thing
* @required
*/
protected $deepValue;
protected function exportExtraTplParams(array &$export): void {
$export['some_extra_tpl_stuff'] = 100;
}
};
}
public function testValidateFail() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->import('modelProps', [
'myPublicString' => 'ok',
'implicitStringArray' => 'single',
'myProtectedInt' => 'two',
'deepValue' => NULL,
]);
$errors = $ex->validate();
$expected = [];
$expected[] = ['severity' => 'error', 'fields' => ['contactId', 'contact'], 'name' => 'missingContact', 'message' => 'Message template requires one of these fields (contactId, contact)'];
$expected[] = ['severity' => 'error', 'fields' => ['implicitStringArray'], 'name' => 'wrong_type', 'message' => 'Field should have type string[].'];
$expected[] = ['severity' => 'error', 'fields' => ['myProtectedInt'], 'name' => 'wrong_type', 'message' => 'Field should have type int.'];
$expected[] = ['severity' => 'error', 'fields' => ['deepValue'], 'name' => 'wrong_type', 'message' => 'Field should have type int.'];
$expected[] = ['severity' => 'error', 'fields' => ['deepValue'], 'name' => 'required', 'message' => 'Missing required field deepValue.'];
$cmp = function($a, $b) {
if ($v = strnatcmp($a['message'], $b['message'])) {
return $v;
}
return strnatcmp(implode(',', $a['fields']), implode(',', $b['fields']));
};
usort($errors, $cmp);
usort($expected, $cmp);
$this->assertEquals($expected, $errors);
}
public function testValidatePass() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->import('modelProps', [
'contactId' => $this->individualCreate(),
'myPublicString' => 'ok',
'implicitStringArray' => ['single'],
'myProtectedInt' => 2,
'deepValue' => 34,
]);
$errors = $ex->validate();
$expected = [];
$this->assertEquals($expected, $errors);
}
/**
* Assert that "getFields()" provides metadata from properties/docblocks.
*/
public function testGetFields() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$fields = $ex->getFields();
/** @var \Civi\WorkflowMessage\FieldSpec $field */
$field = $fields['myPublicString'];
$this->assertEquals(['string'], $field->getType());
$this->assertEquals('String', $field->getDataType());
$this->assertEquals(NULL, $field->getSerialize());
$field = $fields['implicitStringArray'];
$this->assertEquals(['string[]'], $field->getType());
$this->assertEquals('Blob', $field->getDataType());
$this->assertEquals(\CRM_Core_DAO::SERIALIZE_JSON, $field->getSerialize());
$field = $fields['explicitStringArray'];
$this->assertEquals(['string[]'], $field->getType());
$this->assertEquals('Text', $field->getDataType());
$this->assertEquals(\CRM_Core_DAO::SERIALIZE_COMMA, $field->getSerialize());
$field = $fields['myProtectedInt'];
$this->assertEquals(['int'], $field->getType());
$this->assertEquals('Integer', $field->getDataType());
$this->assertEquals(NULL, $field->getSerialize());
}
/**
* Assert that getters/setters work on class fields.
*/
public function testGetSetClassFields() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->setmyProtectedInt(5);
$this->assertEquals(5, $ex->getmyProtectedInt());
$this->assertEquals(5, Invasive::get([$ex, 'myProtectedInt']));
$ex->setMyPublicString('hello');
$this->assertEquals('hello', $ex->getMyPublicString());
$this->assertEquals('hello', $ex->myPublicString);
}
/**
* Assert that import()/export() work on standard fields.
*/
public function testImportExportStandardField() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->import('tplParams', [
'my_public_string' => 'hello world',
'my_int' => 10,
'some' => ['deep' => ['thing' => 20]],
]);
$this->assertEquals('hello world', $ex->getMyPublicString());
$this->assertEquals(10, $ex->getMyProtectedInt());
$this->assertEquals(20, $ex->getDeepValue());
$ex->myPublicString .= ' and stuff';
$ex->setDeepValue(22);
$tpl = $ex->export('tplParams');
$this->assertEquals('hello world and stuff', $tpl['my_public_string']);
$this->assertEquals(10, $tpl['my_int']);
$this->assertEquals(22, $tpl['some']['deep']['thing']);
$this->assertEquals(100, $tpl['some_extra_tpl_stuff']);
$envelope = $ex->export('envelope');
$this->assertEquals('my_example_wf', $envelope['workflow']);
$this->assertEquals('msg_old_style_grp', $envelope['groupName']);
}
/**
* Assert that unrecognized fields are preserved in the round-trip from import=>export.
*/
public function testImportExportExtraField() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->import('tplParams', [
'my.st!er_y' => ['is not mentioned anywhere'],
]);
$tpl = $ex->export('tplParams');
$this->assertEquals(['is not mentioned anywhere'], $tpl['my.st!er_y']);
}
/**
* Assert that
*/
public function testImportExportUnmappedField() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = static::createExample();
$ex->import('tplParams', [
'implicitStringArray' => ['is not mapped between class and tpl'],
]);
$this->assertEquals(NULL, $ex->getimplicitStringArray());
$ex->setimplicitStringArray(['this is the real class field']);
$tpl = $ex->export('tplParams');
$this->assertEquals(['is not mapped between class and tpl'], $tpl['implicitStringArray']);
$classData = $ex->export('modelProps');
$this->assertEquals(['this is the real class field'], $classData['implicitStringArray']);
}
/**
* Create an impromptu instance of `WorkflowMessage` for a new/unknown workflow.
*/
public function testImpromptuImportExport() {
/** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */
$ex = WorkflowMessage::create('some_impromptu_wf', [
'envelope' => ['from' => 'foo@example.com'],
'tokenContext' => ['contactId' => 123],
'tplParams' => [
'myImpromputInt' => 456,
'impromptu_smarty_data' => ['is not mentioned anywhere'],
],
]);
$this->assertTrue($ex instanceof GenericWorkflowMessage);
$tpl = $ex->export('tplParams');
$this->assertEquals(456, $tpl['myImpromputInt']);
$this->assertEquals(['is not mentioned anywhere'], $tpl['impromptu_smarty_data']);
$this->assertTrue(!isset($tpl['workflow']));
$envelope = $ex->export('envelope');
$this->assertEquals('some_impromptu_wf', $envelope['workflow']);
$this->assertEquals('foo@example.com', $envelope['from']);
$this->assertTrue(!isset($envelope['myProtectedInt']));
$tokenCtx = $ex->export('tokenContext');
$this->assertEquals(123, $tokenCtx['contactId']);
$this->assertTrue(!isset($envelope['myProtectedInt']));
}
public function testExampleRender() {
$hookCount = 0;
$rand = rand(0, 1000);
$cid = $this->individualCreate(['first_name' => 'Foo', 'last_name' => 'Bar' . $rand, 'prefix_id' => NULL, 'suffix_id' => NULL]);
/** @var \Civi\WorkflowMessage\GenericWorkflowMessage $ex */
$ex = $this->createExample()->setContactId($cid);
\Civi::dispatcher()->addListener('hook_civicrm_alterMailParams', function($e) use (&$hookCount) {
$hookCount++;
$this->assertEquals('my_example_wf', $e->params['workflow'], 'ExampleWorkflow::WORKFLOW should propagate to params[workflow]');
});
$this->assertEquals(0, $hookCount);
$rendered = $ex->renderTemplate([
'messageTemplate' => [
'msg_subject' => 'Hello {contact.display_name}',
],
]);
$this->assertEquals(1, $hookCount);
$this->assertEquals('Hello Foo Bar' . $rand, $rendered['subject']);
}
public function testImpromptuRender() {
$hookCount = 0;
$rand = rand(0, 1000);
$cid = $this->individualCreate(['first_name' => 'Foo', 'last_name' => 'Bar' . $rand, 'prefix_id' => NULL, 'suffix_id' => NULL]);
/** @var \Civi\WorkflowMessage\GenericWorkflowMessage $ex */
$ex = WorkflowMessage::create('some_impromptu_wf', [
'tokenContext' => ['contactId' => $cid],
]);
\Civi::dispatcher()->addListener('hook_civicrm_alterMailParams', function($e) use (&$hookCount) {
$hookCount++;
$this->assertEquals('some_impromptu_wf', $e->params['workflow'], 'Adhoc name should propagate to params[workflow]');
});
$this->assertEquals(0, $hookCount);
$rendered = $ex->renderTemplate([
'messageTemplate' => [
'msg_subject' => 'Hello {contact.display_name}',
],
]);
$this->assertEquals(1, $hookCount);
$this->assertEquals('Hello Foo Bar' . $rand, $rendered['subject']);
}
public function testRenderStoredTemplate() {
$hookCount = 0;
$rand = rand(0, 1000);
$cid = $this->individualCreate(['first_name' => 'Foo', 'last_name' => 'Bar' . $rand, 'prefix_id' => NULL, 'suffix_id' => NULL]);
/** @var \Civi\WorkflowMessage\GenericWorkflowMessage $ex */
$ex = WorkflowMessage::create('petition_sign', [
'tokenContext' => ['contactId' => $cid],
'tplParams' => [
'greeting' => 'Greetings yo',
'petition' => ['title' => 'The Fake Petition'],
'petitionTitle' => 'The Fake Petition',
'survey_id' => NULL,
],
]);
\Civi::dispatcher()->addListener('hook_civicrm_alterMailParams', function($e) use (&$hookCount) {
$hookCount++;
$this->assertEquals('petition_sign', $e->params['workflow']);
});
$this->assertEquals(0, $hookCount);
$rendered = $ex->renderTemplate();
$this->assertEquals(1, $hookCount);
$this->assertStringContainsString('Foo Bar' . $rand, $rendered['subject']);
$this->assertStringContainsString('Thank you for signing The Fake Petition', $rendered['html']);
$this->assertStringContainsString('Thank you for signing The Fake Petition', $rendered['text']);
}
//public function testImpromptuTokens() {
// /** @var \Civi\WorkflowMessage\GenericWorkflowMessage $ex */
// $ex = WorkflowMessage::create('some_impromptu_wf', [
// 'envelope' => [
// 'contactId' => 123,
// ],
// ]);
// $tokens = $ex->getTokens();
// $this->assertEquals('First ZZName', $tokens['contact.first_name']['label']);
//}
}