‪TYPO3CMS  11.5
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;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Prophecy\Prophecy\ObjectProphecy;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 class ‪EmailLoginNotificationTest extends UnitTestCase
31 {
32  use ProphecyTrait;
33 
38  {
39  $_SERVER['HTTP_HOST'] = 'localhost';
40  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
41  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
42  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $backendUser->uc['emailMeAtLogin'] = 1;
46 
47  $userData = [
48  'email' => 'test@acme.com',
49  ];
50 
51  $mailMessage = $this->‪setUpMailMessageProphecy();
52  $mailerProphecy = $this->prophesize(Mailer::class);
53  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
54  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
55 
56  $subject = new ‪EmailLoginNotification();
57  $subject->emailAtLogin(['user' => $userData], $backendUser);
58  }
59 
64  {
65  $_SERVER['HTTP_HOST'] = 'localhost';
66  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
67  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
68  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $backendUser->uc['emailMeAtLogin'] = 0;
72 
73  $userData = [
74  'username' => 'karl',
75  'email' => 'test@acme.com',
76  ];
77 
78  $subject = new ‪EmailLoginNotification();
79  $subject->emailAtLogin(['user' => $userData], $backendUser);
80 
81  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
82  }
83 
88  {
89  $_SERVER['HTTP_HOST'] = 'localhost';
90  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
91  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
92  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $backendUser->uc['emailMeAtLogin'] = 1;
96 
97  $userData = [
98  'username' => 'karl',
99  'email' => 'dot.com',
100  ];
101 
102  $subject = new ‪EmailLoginNotification();
103  $subject->emailAtLogin(['user' => $userData], $backendUser);
104 
105  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
106  }
107 
112  {
113  $_SERVER['HTTP_HOST'] = 'localhost';
114  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
115  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
116  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
117  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 2;
118  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $backendUser->method('isAdmin')->willReturn(true);
122 
123  $userData = [
124  'username' => 'karl',
125  ];
126 
127  $mailMessage = $this->‪setUpMailMessageProphecy();
128  $mailerProphecy = $this->prophesize(Mailer::class);
129  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
130  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
131 
132  $subject = new ‪EmailLoginNotification();
133  $subject->emailAtLogin(['user' => $userData], $backendUser);
134 
135  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
136  }
137 
142  {
143  $_SERVER['HTTP_HOST'] = 'localhost';
144  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
145  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
146  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
147  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
148  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
149  ->disableOriginalConstructor()
150  ->getMock();
151  $backendUser->method('isAdmin')->willReturn(true);
152 
153  $userData = [
154  'username' => 'karl',
155  ];
156 
157  $mailMessage = $this->‪setUpMailMessageProphecy();
158  $mailerProphecy = $this->prophesize(Mailer::class);
159  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
160  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
161 
162  $subject = new ‪EmailLoginNotification();
163  $subject->emailAtLogin(['user' => $userData], $backendUser);
164 
165  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
166  }
167 
172  {
173  $_SERVER['HTTP_HOST'] = 'localhost';
174  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
175  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
176  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
177  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
178  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
179  ->disableOriginalConstructor()
180  ->getMock();
181  $backendUser->method('isAdmin')->willReturn(false);
182 
183  $userData = [
184  'username' => 'karl',
185  ];
186 
187  $mailMessage = $this->‪setUpMailMessageProphecy();
188  $mailerProphecy = $this->prophesize(Mailer::class);
189  $mailerProphecy->send($mailMessage)->shouldBeCalledOnce();
190  GeneralUtility::addInstance(Mailer::class, $mailerProphecy->reveal());
191 
192  $subject = new ‪EmailLoginNotification();
193  $subject->emailAtLogin(['user' => $userData], $backendUser);
194 
195  $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
196  }
197 
202  {
203  $_SERVER['HTTP_HOST'] = 'localhost';
204  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
205  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
206  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
207  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 2;
208  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
209  ->disableOriginalConstructor()
210  ->getMock();
211  $backendUser->method('isAdmin')->willReturn(false);
212 
213  $userData = [
214  'username' => 'karl',
215  ];
216 
217  $subject = new ‪EmailLoginNotification();
218  $subject->emailAtLogin(['user' => $userData], $backendUser);
219 
220  // no additional assertion here as the test would fail due to not mocking the email API
221  }
222 
226  protected function ‪setUpMailMessageProphecy(): ObjectProphecy
227  {
228  $mailMessage = $this->prophesize(FluidEmail::class);
229  $mailMessage->to(Argument::any())->willReturn($mailMessage->reveal());
230  $mailMessage->setTemplate(Argument::any())->willReturn($mailMessage->reveal());
231  $mailMessage->from(Argument::any())->willReturn($mailMessage->reveal());
232  $mailMessage->setRequest(Argument::any())->willReturn($mailMessage->reveal());
233  $mailMessage->assignMultiple(Argument::cetera())->willReturn($mailMessage->reveal());
234  GeneralUtility::addInstance(FluidEmail::class, $mailMessage->reveal());
235  return $mailMessage;
236  }
237 }
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:140
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin
‪emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin()
Definition: EmailLoginNotificationTest.php:62
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:110
‪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:200
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\setUpMailMessageProphecy
‪ObjectProphecy< FluidEmail > setUpMailMessageProphecy()
Definition: EmailLoginNotificationTest.php:225
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn()
Definition: EmailLoginNotificationTest.php:170
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin
‪emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin()
Definition: EmailLoginNotificationTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail
‪emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail()
Definition: EmailLoginNotificationTest.php:86
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest
Definition: EmailLoginNotificationTest.php:31