1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace EthanYehuda \CronjobManager \Test \Integration ;
5
+
6
+ use EthanYehuda \CronjobManager \Model \ErrorNotificationEmail ;
7
+ use Magento \Cron \Model \Schedule ;
8
+ use Magento \Framework \Mail \Message ;
9
+ use Magento \Framework \Mail \Template \TransportBuilder ;
10
+ use Magento \TestFramework \ObjectManager ;
11
+ use Magento \TestFramework \Helper \Bootstrap ;
12
+ use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
13
+ use PHPUnit \Framework \TestCase ;
14
+
15
+ /**
16
+ * @magentoAppArea crontab
17
+ * @magentoAppIsolation enabled
18
+ */
19
+ class ErrorNotificationEmailTest extends TestCase
20
+ {
21
+ /**
22
+ * @var ObjectManager
23
+ */
24
+ private $ objectManager ;
25
+ /**
26
+ * @var ErrorNotificationEmail
27
+ */
28
+ private $ errorNotificationEmail ;
29
+ /**
30
+ * @var \Magento\TestFramework\Mail\Template\TransportBuilderMock
31
+ */
32
+ private $ transportBuilder ;
33
+
34
+ protected function setUp ()
35
+ {
36
+ $ this ->objectManager = Bootstrap::getObjectManager ();
37
+ $ this ->transportBuilder = $ this ->objectManager ->get (TransportBuilderMock::class);
38
+ $ this ->objectManager ->addSharedInstance (
39
+ $ this ->transportBuilder ,
40
+ TransportBuilder::class
41
+ );
42
+ $ this ->errorNotificationEmail = $ this ->objectManager ->get (ErrorNotificationEmail::class);
43
+ }
44
+
45
+ /**
46
+ * @magentoConfigFixture default_store system/cron_job_manager/email_notification 0
47
+ * @magentoAdminConfigFixture system/cron_job_manager/email_recipients errors@example.com,other@example.com
48
+ * @magentoAdminConfigFixture system/cron_job_manager/email_identity general
49
+ * @magentoConfigFixture current_store trans_email/ident_general/name No-Reply
50
+ * @magentoConfigFixture current_store trans_email/ident_general/email noreply@example.com
51
+ */
52
+ public function testDoNotSendIfConfigurationDisabled ()
53
+ {
54
+ $ this ->givenScheduleWithData (
55
+ [
56
+ 'job_code ' => 'dummy_job_code ' ,
57
+ 'executed_at ' => '1999-12-31 23:59:00 ' ,
58
+ 'finished_at ' => '1900-01-01 00:00:00 ' ,
59
+ 'status ' => Schedule::STATUS_ERROR ,
60
+ 'messages ' => "Hello, I am the <Y2K> Bug \n\nHere be stacktrace " ,
61
+ ],
62
+ $ schedule
63
+ );
64
+ $ this ->whenNotificationIsSent ($ schedule , $ sentMessage );
65
+ $ this ->thenEmailShouldNotBeSent ($ sentMessage );
66
+ }
67
+
68
+ /**
69
+ * @magentoAdminConfigFixture system/cron_job_manager/email_notification 1
70
+ * @magentoAdminConfigFixture system/cron_job_manager/email_recipients errors@example.com,other@example.com
71
+ * @magentoAdminConfigFixture system/cron_job_manager/email_identity general
72
+ * @magentoConfigFixture current_store trans_email/ident_general/name No-Reply
73
+ * @magentoConfigFixture current_store trans_email/ident_general/email noreply@example.com
74
+ */
75
+ public function testSentWithTemplateToConfiguredAddresses ()
76
+ {
77
+ $ this ->givenScheduleWithData (
78
+ [
79
+ 'job_code ' => 'dummy_job_code ' ,
80
+ 'executed_at ' => '1999-12-31 23:59:00 ' ,
81
+ 'finished_at ' => '1900-01-01 00:00:00 ' ,
82
+ 'status ' => Schedule::STATUS_ERROR ,
83
+ 'messages ' => "Hello, I am the <Y2K> Bug \n\nHere be stacktrace " ,
84
+ ],
85
+ $ schedule
86
+ );
87
+ $ this ->whenNotificationIsSent ($ schedule , $ sentMessage );
88
+ $ this ->thenEmailShouldBeSent ($ sentMessage , 'noreply@example.com ' , ['errors@example.com ' , 'other@example.com ' ]);
89
+ $ this ->andEmailShouldHaveContents (
90
+ $ sentMessage ,
91
+ [
92
+ 'job_code ' => '<td>dummy_job_code</td> ' ,
93
+ 'messages ' => "<td>Hello, I am the <Y2K> Bug<br /> \n<br /> \nHere be stacktrace</td> " ,
94
+ 'exeuted_at ' => '<td>1999-12-31 23:59:00</td> ' ,
95
+ 'finished_at ' => '<td>1900-01-01 00:00:00</td> ' ,
96
+ ]
97
+ );
98
+ }
99
+
100
+ private function givenScheduleWithData (array $ scheduleData , &$ schedule ): void
101
+ {
102
+ $ schedule = $ this ->objectManager ->create (
103
+ Schedule::class,
104
+ [
105
+ 'data ' => $ scheduleData ,
106
+ ]
107
+ );
108
+ }
109
+
110
+ private function whenNotificationIsSent ($ schedule , &$ sentMessage ): void
111
+ {
112
+ $ this ->errorNotificationEmail ->sendFor ($ schedule );
113
+ $ sentMessage = $ this ->transportBuilder ->getSentMessage ();
114
+ }
115
+
116
+ private function thenEmailShouldBeSent (?Message $ sentMessage , string $ expectedSender , array $ expectedRecipients )
117
+ {
118
+ $ this ->assertNotNull ($ sentMessage , 'A mail should have been sent ' );
119
+ $ messageDetails = \Zend \Mail \Message::fromString ($ sentMessage ->getRawMessage ());
120
+ $ this ->assertEquals ([$ expectedSender ], \array_keys (\iterator_to_array ($ messageDetails ->getFrom ())));
121
+ $ this ->assertEquals ($ expectedRecipients , \array_keys (\iterator_to_array ($ messageDetails ->getTo ())));
122
+ }
123
+
124
+ private function thenEmailShouldNotBeSent (?Message $ sentMessage )
125
+ {
126
+ $ this ->assertNull ($ sentMessage , 'A mail should not have been sent ' );
127
+ }
128
+
129
+ private function andEmailShouldHaveContents (Message $ sentMessage , array $ expectedContents ): void
130
+ {
131
+ $ content = $ sentMessage ->getBody ()->getParts ()[0 ]->getContent ();
132
+ foreach ($ expectedContents as $ expectedKey => $ expectedContent ) {
133
+ $ this ->assertContains ($ expectedContent , $ content , "Content should contain $ expectedKey " );
134
+ }
135
+ }
136
+ }
0 commit comments