‪TYPO3CMS  10.4
EmailLoginNotificationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 class ‪EmailLoginNotificationTest extends UnitTestCase
29 {
34  {
35  $_SERVER['HTTP_HOST'] = 'localhost';
36  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
37  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
38  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41  $backendUser->uc['emailMeAtLogin'] = 1;
42 
43  $userData = [
44  'email' => 'test@acme.com'
45  ];
46 
47  $mailMessage = $this->‪setUpMailMessageProphecy();
48  $mailerProphecy = $this->prophesize(Mailer::class);
49  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
50  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
51 
52  $subject = new ‪EmailLoginNotification();
53  $subject->emailAtLogin(['user' => $userData], $backendUser);
54  }
55 
60  {
61  $_SERVER['HTTP_HOST'] = 'localhost';
62  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
63  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
64  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $backendUser->uc['emailMeAtLogin'] = 0;
68 
69  $userData = [
70  'username' => 'karl',
71  'email' => 'test@acme.com'
72  ];
73 
74  $subject = new ‪EmailLoginNotification();
75  $subject->emailAtLogin(['user' => $userData], $backendUser);
76 
77  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
78  }
79 
84  {
85  $_SERVER['HTTP_HOST'] = 'localhost';
86  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
87  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
88  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
89  ->disableOriginalConstructor()
90  ->getMock();
91  $backendUser->uc['emailMeAtLogin'] = 1;
92 
93  $userData = [
94  'username' => 'karl',
95  'email' => 'dot.com'
96  ];
97 
98  $subject = new ‪EmailLoginNotification();
99  $subject->emailAtLogin(['user' => $userData], $backendUser);
100 
101  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
102  }
103 
108  {
109  $_SERVER['HTTP_HOST'] = 'localhost';
110  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
111  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
112  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
113  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 2;
114  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117  $backendUser->expects(self::any())->method('isAdmin')->willReturn(true);
118 
119  $userData = [
120  'username' => 'karl'
121  ];
122 
123  $mailMessage = $this->‪setUpMailMessageProphecy();
124  $mailerProphecy = $this->prophesize(Mailer::class);
125  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
126  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
127 
128  $subject = new ‪EmailLoginNotification();
129  $subject->emailAtLogin(['user' => $userData], $backendUser);
130 
131  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
132  }
133 
138  {
139  $_SERVER['HTTP_HOST'] = 'localhost';
140  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
141  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
142  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
143  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
144  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
145  ->disableOriginalConstructor()
146  ->getMock();
147  $backendUser->expects(self::any())->method('isAdmin')->willReturn(true);
148 
149  $userData = [
150  'username' => 'karl'
151  ];
152 
153  $mailMessage = $this->‪setUpMailMessageProphecy();
154  $mailerProphecy = $this->prophesize(Mailer::class);
155  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
156  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
157 
158  $subject = new ‪EmailLoginNotification();
159  $subject->emailAtLogin(['user' => $userData], $backendUser);
160 
161  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
162  }
163 
168  {
169  $_SERVER['HTTP_HOST'] = 'localhost';
170  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
171  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
172  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
173  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
174  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
175  ->disableOriginalConstructor()
176  ->getMock();
177  $backendUser->expects(self::any())->method('isAdmin')->willReturn(false);
178 
179  $userData = [
180  'username' => 'karl'
181  ];
182 
183  $mailMessage = $this->‪setUpMailMessageProphecy();
184  $mailerProphecy = $this->prophesize(Mailer::class);
185  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
186  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
187 
188  $subject = new ‪EmailLoginNotification();
189  $subject->emailAtLogin(['user' => $userData], $backendUser);
190 
191  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
192  }
193 
198  {
199  $_SERVER['HTTP_HOST'] = 'localhost';
200  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
201  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
202  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
203  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 2;
204  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
205  ->disableOriginalConstructor()
206  ->getMock();
207  $backendUser->expects(self::any())->method('isAdmin')->willReturn(false);
208 
209  $userData = [
210  'username' => 'karl'
211  ];
212 
213  $subject = new ‪EmailLoginNotification();
214  $subject->emailAtLogin(['user' => $userData], $backendUser);
215 
216  // no additional assertion here as the test would fail due to not mocking the email API
217  }
218 
222  protected function ‪setUpMailMessageProphecy()
223  {
224  $mailMessage = $this->prophesize(FluidEmail::class);
225  $mailMessage->to(Argument::any())->willReturn($mailMessage->reveal());
226  $mailMessage->setTemplate(Argument::any())->willReturn($mailMessage->reveal());
227  $mailMessage->from(Argument::any())->willReturn($mailMessage->reveal());
228  $mailMessage->setRequest(Argument::any())->willReturn($mailMessage->reveal());
229  $mailMessage->assignMultiple(Argument::cetera())->willReturn($mailMessage->reveal());
230  GeneralUtility::addInstance(FluidEmail::class, $mailMessage->reveal());
231  return $mailMessage;
232  }
233 }
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:137
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin
‪emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin()
Definition: EmailLoginNotificationTest.php:59
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:107
‪TYPO3\CMS\Backend\Tests\Unit\Security
Definition: EmailLoginNotificationTest.php:18
‪TYPO3\CMS\Backend\Security\EmailLoginNotification
Definition: EmailLoginNotification.php:43
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsNoEmailIfAdminWarningIsEnabledAndNoAdminIsLoggingIn
‪emailAtLoginSendsNoEmailIfAdminWarningIsEnabledAndNoAdminIsLoggingIn()
Definition: EmailLoginNotificationTest.php:197
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\setUpMailMessageProphecy
‪Prophecy Prophecy ObjectProphecy FluidEmail setUpMailMessageProphecy()
Definition: EmailLoginNotificationTest.php:222
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn()
Definition: EmailLoginNotificationTest.php:167
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin
‪emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin()
Definition: EmailLoginNotificationTest.php:33
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail
‪emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail()
Definition: EmailLoginNotificationTest.php:83
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest
Definition: EmailLoginNotificationTest.php:29