1 | <?php |
||
26 | class EmailDataFactoryTest extends TestCase |
||
27 | { |
||
28 | /** |
||
29 | * @var \Netgen\Bundle\InformationCollectionBundle\Factory\EmailDataFactory |
||
30 | */ |
||
31 | protected $factory; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $config; |
||
37 | |||
38 | /** |
||
39 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
40 | */ |
||
41 | protected $translationHelper; |
||
42 | |||
43 | /** |
||
44 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
45 | */ |
||
46 | protected $fieldHelper; |
||
47 | |||
48 | /** |
||
49 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
50 | */ |
||
51 | protected $contentService; |
||
52 | |||
53 | /** |
||
54 | * @var \eZ\Publish\Core\Repository\Values\ContentType\ContentType |
||
55 | */ |
||
56 | protected $contentType; |
||
57 | |||
58 | /** |
||
59 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
60 | */ |
||
61 | protected $twig; |
||
62 | |||
63 | /** |
||
64 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
65 | */ |
||
66 | protected $templateWrapper; |
||
67 | |||
68 | /** |
||
69 | * @var \eZ\Publish\Core\Repository\Values\ContentType\ContentType |
||
70 | */ |
||
71 | protected $contentType2; |
||
72 | |||
73 | /** |
||
74 | * @var \eZ\Publish\Core\Repository\Values\Content\VersionInfo |
||
75 | */ |
||
76 | protected $versionInfo; |
||
77 | |||
78 | public function setUp() |
||
79 | { |
||
80 | $this->config = array( |
||
81 | 'templates' => array( |
||
82 | 'default' => 'AcmeBundle::email.html.twig', |
||
83 | 'content_types' => array( |
||
84 | 'test_content_type' => 'AcmeBundle::test_content_type.html.twig', |
||
85 | ), |
||
86 | ), |
||
87 | 'default_variables' => array( |
||
88 | 'sender' => '[email protected]', |
||
89 | 'recipient' => '[email protected]', |
||
90 | 'subject' => 'subject', |
||
91 | ), |
||
92 | ); |
||
93 | |||
94 | $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) |
||
95 | ->disableOriginalConstructor() |
||
96 | ->setMethods(array('getTranslatedField')) |
||
97 | ->getMock(); |
||
98 | |||
99 | $this->fieldHelper = $this->getMockBuilder(FieldHelper::class) |
||
100 | ->disableOriginalConstructor() |
||
101 | ->setMethods(array('isFieldEmpty')) |
||
102 | ->getMock(); |
||
103 | |||
104 | $this->contentService = $this->getMockBuilder(ContentService::class) |
||
105 | ->disableOriginalConstructor() |
||
106 | ->setMethods(array('loadContent')) |
||
107 | ->getMock(); |
||
108 | |||
109 | $this->twig = $this->getMockBuilder(\Twig_Environment::class) |
||
110 | ->disableOriginalConstructor() |
||
111 | ->setMethods(array('load')) |
||
112 | ->getMock(); |
||
113 | |||
114 | $this->contentType = new ContentType(array( |
||
115 | 'identifier' => 'test_content_type', |
||
116 | 'fieldDefinitions' => array(), |
||
117 | )); |
||
118 | |||
119 | $this->contentType2 = new ContentType(array( |
||
120 | 'identifier' => 'test_content_type2', |
||
121 | 'fieldDefinitions' => array(), |
||
122 | )); |
||
123 | |||
124 | $this->versionInfo = new VersionInfo(array( |
||
125 | 'contentInfo' => new ContentInfo(array( |
||
126 | 'contentTypeId' => 123, |
||
127 | )), |
||
128 | )); |
||
129 | |||
130 | $this->factory = new EmailDataFactory( |
||
131 | $this->config, |
||
132 | $this->translationHelper, |
||
133 | $this->fieldHelper, |
||
134 | $this->contentService, |
||
135 | $this->twig |
||
136 | ); |
||
137 | parent::setUp(); |
||
138 | } |
||
139 | |||
140 | public function testBuildingWithSenderRecipientAndSubjectFromContent() |
||
141 | { |
||
142 | $twig = new Twig_Environment( |
||
143 | new Twig_Loader_Array( |
||
144 | array( |
||
145 | 'index' => '{% block email %}{{ "email body" }}{% endblock %}', |
||
146 | ) |
||
147 | ) |
||
148 | ); |
||
149 | |||
150 | $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); |
||
151 | |||
152 | $this->factory = new EmailDataFactory( |
||
153 | $this->config, |
||
154 | $this->translationHelper, |
||
155 | $this->fieldHelper, |
||
156 | $this->contentService, |
||
157 | $this->twig |
||
158 | ); |
||
159 | |||
160 | $recipientField = new Field(array( |
||
161 | 'value' => new EmailValue('[email protected]'), |
||
162 | 'languageCode' => 'eng_GB', |
||
163 | 'fieldDefIdentifier' => 'recipient', |
||
164 | )); |
||
165 | |||
166 | $senderField = new Field(array( |
||
167 | 'value' => new EmailValue('[email protected]'), |
||
168 | 'languageCode' => 'eng_GB', |
||
169 | 'fieldDefIdentifier' => 'sender', |
||
170 | )); |
||
171 | |||
172 | $subjectField = new Field(array( |
||
173 | 'value' => new TextLineValue('subject test'), |
||
174 | 'languageCode' => 'eng_GB', |
||
175 | 'fieldDefIdentifier' => 'subject', |
||
176 | )); |
||
177 | |||
178 | $content = new Content(array( |
||
179 | 'internalFields' => array( |
||
180 | $recipientField, $senderField, $subjectField, |
||
181 | ), |
||
182 | 'versionInfo' => $this->versionInfo, |
||
183 | )); |
||
184 | |||
185 | $this->fieldHelper->expects($this->exactly(3)) |
||
186 | ->method('isFieldEmpty') |
||
187 | ->withAnyParameters() |
||
188 | ->willReturn(false); |
||
189 | |||
190 | $this->translationHelper->expects($this->at(0)) |
||
191 | ->method('getTranslatedField') |
||
192 | ->with($content, 'recipient') |
||
193 | ->willReturn($recipientField); |
||
194 | |||
195 | $this->translationHelper->expects($this->at(1)) |
||
196 | ->method('getTranslatedField') |
||
197 | ->with($content, 'sender') |
||
198 | ->willReturn($senderField); |
||
199 | |||
200 | $this->translationHelper->expects($this->at(2)) |
||
201 | ->method('getTranslatedField') |
||
202 | ->with($content, 'subject') |
||
203 | ->willReturn($subjectField); |
||
204 | |||
205 | $this->contentService->expects($this->once()) |
||
206 | ->method('loadContent') |
||
207 | ->with(123) |
||
208 | ->willReturn($content); |
||
209 | |||
210 | $location = new Location( |
||
211 | array( |
||
212 | 'id' => 12345, |
||
213 | 'contentInfo' => new ContentInfo(array('id' => 123)), |
||
214 | ) |
||
215 | ); |
||
216 | |||
217 | $contentType = new ContentType(array( |
||
218 | 'identifier' => 'test', |
||
219 | 'fieldDefinitions' => array(), |
||
220 | )); |
||
221 | |||
222 | $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location)); |
||
223 | |||
224 | $this->twig->expects($this->once()) |
||
225 | ->method('load') |
||
226 | ->willReturn($templateWrapper); |
||
227 | |||
228 | /** @var EmailData $value */ |
||
229 | $value = $this->factory->build($event); |
||
230 | |||
231 | $this->assertInstanceOf(EmailData::class, $value); |
||
232 | $this->assertEquals('[email protected]', $value->recipient); |
||
233 | $this->assertEquals('[email protected]', $value->sender); |
||
234 | $this->assertEquals('subject test', $value->subject); |
||
235 | $this->assertEquals('email body', $value->body); |
||
236 | } |
||
237 | |||
238 | public function testBuildingWithSenderRecipientAndSubjectFromTemplate() |
||
239 | { |
||
240 | $template = <<<TEMPLATE |
||
241 | {% block email %}{{ 'My email body' }}{% endblock %} |
||
242 | {% block subject %}{{ 'My custom subject' }}{% endblock %} |
||
243 | {% block recipient %}{{ '[email protected]' }}{% endblock %} |
||
244 | {% block sender %}{{ '[email protected]' }}{% endblock %} |
||
245 | TEMPLATE; |
||
246 | |||
247 | $twig = new Twig_Environment( |
||
248 | new Twig_Loader_Array( |
||
249 | array( |
||
250 | 'index' => $template, |
||
251 | ) |
||
252 | ) |
||
253 | ); |
||
254 | |||
255 | $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); |
||
256 | |||
257 | $this->factory = new EmailDataFactory( |
||
258 | $this->config, |
||
259 | $this->translationHelper, |
||
260 | $this->fieldHelper, |
||
261 | $this->contentService, |
||
262 | $this->twig |
||
263 | ); |
||
264 | |||
265 | $recipientField = new Field(array( |
||
266 | 'value' => new EmailValue('[email protected]'), |
||
267 | 'languageCode' => 'eng_GB', |
||
268 | 'fieldDefIdentifier' => 'recipient', |
||
269 | )); |
||
270 | |||
271 | $senderField = new Field(array( |
||
272 | 'value' => new EmailValue('[email protected]'), |
||
273 | 'languageCode' => 'eng_GB', |
||
274 | 'fieldDefIdentifier' => 'sender', |
||
275 | )); |
||
276 | |||
277 | $subjectField = new Field(array( |
||
278 | 'value' => new TextLineValue('subject test'), |
||
279 | 'languageCode' => 'eng_GB', |
||
280 | 'fieldDefIdentifier' => 'subject', |
||
281 | )); |
||
282 | |||
283 | $content = new Content(array( |
||
284 | 'internalFields' => array( |
||
285 | $recipientField, $senderField, $subjectField, |
||
286 | ), |
||
287 | 'versionInfo' => $this->versionInfo, |
||
288 | )); |
||
289 | |||
290 | $this->fieldHelper->expects($this->never()) |
||
291 | ->method('isFieldEmpty'); |
||
292 | |||
293 | $this->translationHelper->expects($this->never()) |
||
294 | ->method('getTranslatedField'); |
||
295 | |||
296 | $this->contentService->expects($this->once()) |
||
297 | ->method('loadContent') |
||
298 | ->with(123) |
||
299 | ->willReturn($content); |
||
300 | |||
301 | $location = new Location( |
||
302 | array( |
||
303 | 'id' => 12345, |
||
304 | 'contentInfo' => new ContentInfo(array('id' => 123)), |
||
305 | ) |
||
306 | ); |
||
307 | |||
308 | $contentType = new ContentType(array( |
||
309 | 'identifier' => 'test_content_type', |
||
310 | 'fieldDefinitions' => array(), |
||
311 | )); |
||
312 | |||
313 | $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location)); |
||
314 | |||
315 | $this->twig->expects($this->once()) |
||
316 | ->method('load') |
||
317 | ->willReturn($templateWrapper); |
||
318 | |||
319 | /** @var EmailData $value */ |
||
320 | $value = $this->factory->build($event); |
||
321 | |||
322 | $this->assertInstanceOf(EmailData::class, $value); |
||
323 | $this->assertEquals('[email protected]', $value->recipient); |
||
324 | $this->assertEquals('[email protected]', $value->sender); |
||
325 | $this->assertEquals('My custom subject', $value->subject); |
||
326 | $this->assertEquals('My email body', $value->body); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\MissingEmailBlockException |
||
331 | * @expectedExceptionMessage Missing email block in index template, currently there is foo available. |
||
332 | */ |
||
333 | public function testBuildingWithNoEmailBlockInTemplate() |
||
334 | { |
||
335 | $twig = new Twig_Environment( |
||
336 | new Twig_Loader_Array( |
||
337 | array( |
||
338 | 'index' => '{% block foo %}{% endblock %}', |
||
339 | ) |
||
340 | ) |
||
341 | ); |
||
342 | |||
343 | $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); |
||
344 | |||
345 | $recipientField = new Field(array( |
||
346 | 'value' => new EmailValue('[email protected]'), |
||
347 | 'languageCode' => 'eng_GB', |
||
348 | 'fieldDefIdentifier' => 'recipient', |
||
349 | )); |
||
350 | |||
351 | $senderField = new Field(array( |
||
352 | 'value' => new EmailValue('[email protected]'), |
||
353 | 'languageCode' => 'eng_GB', |
||
354 | 'fieldDefIdentifier' => 'sender', |
||
355 | )); |
||
356 | |||
357 | $subjectField = new Field(array( |
||
358 | 'value' => new TextLineValue('subject test'), |
||
359 | 'languageCode' => 'eng_GB', |
||
360 | 'fieldDefIdentifier' => 'subject', |
||
361 | )); |
||
362 | |||
363 | $content = new Content(array( |
||
364 | 'internalFields' => array( |
||
365 | $recipientField, $senderField, $subjectField, |
||
366 | ), |
||
367 | 'versionInfo' => $this->versionInfo, |
||
368 | )); |
||
369 | |||
370 | $this->fieldHelper->expects($this->never()) |
||
371 | ->method('isFieldEmpty'); |
||
372 | |||
373 | $this->translationHelper->expects($this->never()) |
||
374 | ->method('getTranslatedField'); |
||
375 | |||
376 | $this->contentService->expects($this->once()) |
||
377 | ->method('loadContent') |
||
378 | ->with(123) |
||
379 | ->willReturn($content); |
||
380 | |||
381 | $location = new Location( |
||
382 | array( |
||
383 | 'id' => 12345, |
||
384 | 'contentInfo' => new ContentInfo(array('id' => 123)), |
||
385 | ) |
||
386 | ); |
||
387 | |||
388 | $contentType = new ContentType(array( |
||
389 | 'identifier' => 'test', |
||
390 | 'fieldDefinitions' => array(), |
||
391 | )); |
||
392 | |||
393 | $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location)); |
||
394 | |||
395 | $this->twig->expects($this->once()) |
||
396 | ->method('load') |
||
397 | ->willReturn($templateWrapper); |
||
398 | |||
399 | /** @var EmailData $value */ |
||
400 | $value = $this->factory->build($event); |
||
401 | |||
402 | $this->assertInstanceOf(EmailData::class, $value); |
||
403 | $this->assertEquals('[email protected]', $value->recipient); |
||
404 | $this->assertEquals('[email protected]', $value->sender); |
||
405 | $this->assertEquals('subject test', $value->subject); |
||
406 | $this->assertEquals('body test', $value->body); |
||
407 | } |
||
408 | |||
409 | public function testBuildingWithSenderRecipientAndSubjectFromConfiguration() |
||
410 | { |
||
411 | $twig = new Twig_Environment( |
||
412 | new Twig_Loader_Array( |
||
413 | array( |
||
414 | 'index' => '{% block email %}{% endblock %}', |
||
415 | ) |
||
416 | ) |
||
417 | ); |
||
418 | |||
419 | $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index')); |
||
420 | |||
421 | $recipientField = new Field(array( |
||
422 | 'value' => new EmailValue('[email protected]'), |
||
423 | 'languageCode' => 'eng_GB', |
||
424 | 'fieldDefIdentifier' => 'recipient', |
||
425 | )); |
||
426 | |||
427 | $senderField = new Field(array( |
||
428 | 'value' => new EmailValue('[email protected]'), |
||
429 | 'languageCode' => 'eng_GB', |
||
430 | 'fieldDefIdentifier' => 'sender', |
||
431 | )); |
||
432 | |||
433 | $subjectField = new Field(array( |
||
434 | 'value' => new TextLineValue('subject test'), |
||
435 | 'languageCode' => 'eng_GB', |
||
436 | 'fieldDefIdentifier' => 'subject', |
||
437 | )); |
||
438 | |||
439 | $content = new Content(array( |
||
440 | 'internalFields' => array( |
||
441 | $recipientField, $senderField, $subjectField, |
||
442 | ), |
||
443 | 'versionInfo' => $this->versionInfo, |
||
444 | )); |
||
445 | |||
446 | $this->fieldHelper->expects($this->exactly(3)) |
||
447 | ->method('isFieldEmpty') |
||
448 | ->withAnyParameters() |
||
449 | ->willReturn(true); |
||
450 | |||
451 | $this->translationHelper->expects($this->never()) |
||
452 | ->method('getTranslatedField') |
||
453 | ->with($content, 'recipient'); |
||
454 | |||
455 | $this->translationHelper->expects($this->never()) |
||
456 | ->method('getTranslatedField') |
||
457 | ->with($content, 'sender'); |
||
458 | |||
459 | $this->translationHelper->expects($this->never()) |
||
460 | ->method('getTranslatedField') |
||
461 | ->with($content, 'subject'); |
||
462 | |||
463 | $this->contentService->expects($this->once()) |
||
464 | ->method('loadContent') |
||
465 | ->with(123) |
||
466 | ->willReturn($content); |
||
467 | |||
468 | $location = new Location( |
||
469 | array( |
||
470 | 'id' => 12345, |
||
471 | 'contentInfo' => new ContentInfo(array('id' => 123)), |
||
472 | ) |
||
473 | ); |
||
474 | |||
475 | $contentType = new ContentType(array( |
||
476 | 'identifier' => 'test', |
||
477 | 'fieldDefinitions' => array(), |
||
478 | )); |
||
479 | |||
480 | $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location)); |
||
481 | |||
482 | $this->twig->expects($this->once()) |
||
483 | ->method('load') |
||
484 | ->willReturn($templateWrapper); |
||
485 | |||
486 | /** @var EmailData $value */ |
||
487 | $value = $this->factory->build($event); |
||
488 | |||
489 | $this->assertInstanceOf(EmailData::class, $value); |
||
490 | $this->assertEquals($this->config['default_variables']['recipient'], $value->recipient); |
||
491 | $this->assertEquals($this->config['default_variables']['sender'], $value->sender); |
||
492 | $this->assertEquals($this->config['default_variables']['subject'], $value->subject); |
||
493 | } |
||
494 | } |
||
495 |