‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪EmailLoginNotificationTest extends UnitTestCase
31 {
32  #[Test]
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  $backendUser->user = [
43  'email' => 'test@acme.com',
44  ];
45 
46  $mailMessage = $this->‪setUpMailMessageMock();
47  $mailerMock = $this->createMock(MailerInterface::class);
48  $mailerMock->expects(self::once())->method('send')->with($mailMessage);
49 
50  $subject = new ‪EmailLoginNotification($mailerMock);
51  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
52  }
53 
54  #[Test]
56  {
57  ‪$_SERVER['HTTP_HOST'] = 'localhost';
58  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
59  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
60  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $backendUser->uc['emailMeAtLogin'] = 0;
64  $backendUser->user = [
65  'username' => 'karl',
66  'email' => 'test@acme.com',
67  ];
68  $mailerMock = $this->createMock(MailerInterface::class);
69 
70  $subject = new ‪EmailLoginNotification($mailerMock);
71  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
72 
73  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
74  }
75 
76  #[Test]
78  {
79  ‪$_SERVER['HTTP_HOST'] = 'localhost';
80  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
81  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
82  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $backendUser->uc['emailMeAtLogin'] = 1;
86  $backendUser->user = [
87  'username' => 'karl',
88  'email' => 'dot.com',
89  ];
90  $mailerMock = $this->createMock(MailerInterface::class);
91 
92  $subject = new ‪EmailLoginNotification($mailerMock);
93  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
94 
95  // no additional assertion here, as the test would fail due to missing mail mocking if it actually tried to send an email
96  }
97 
98  #[Test]
100  {
101  ‪$_SERVER['HTTP_HOST'] = 'localhost';
102  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
103  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
104  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
105  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 2;
106  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109  $backendUser->method('isAdmin')->willReturn(true);
110  $backendUser->user = [
111  'username' => 'karl',
112  ];
113 
114  $mailMessage = $this->‪setUpMailMessageMock('typo3-admin@acme.com');
115  $mailerMock = $this->createMock(MailerInterface::class);
116  $mailerMock->expects(self::once())->method('send')->with($mailMessage);
117 
118  $subject = new ‪EmailLoginNotification($mailerMock);
119  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
120  }
121 
122  #[Test]
124  {
125  ‪$_SERVER['HTTP_HOST'] = 'localhost';
126  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
127  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
128  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
129  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
130  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133  $backendUser->method('isAdmin')->willReturn(true);
134  $backendUser->user = [
135  'username' => 'karl',
136  ];
137 
138  $mailMessage = $this->‪setUpMailMessageMock('typo3-admin@acme.com');
139  $mailerMock = $this->createMock(MailerInterface::class);
140  $mailerMock->expects(self::once())->method('send')->with($mailMessage);
141 
142  $subject = new ‪EmailLoginNotification($mailerMock);
143  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
144  }
145 
146  #[Test]
148  {
149  ‪$_SERVER['HTTP_HOST'] = 'localhost';
150  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
151  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = 'My TYPO3 Inc.';
152  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] = 'typo3-admin@acme.com';
153  ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] = 1;
154  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $backendUser->method('isAdmin')->willReturn(false);
158  $backendUser->user = [
159  'username' => 'karl',
160  ];
161 
162  $mailMessage = $this->‪setUpMailMessageMock('typo3-admin@acme.com');
163  $mailerMock = $this->createMock(MailerInterface::class);
164  $mailerMock->expects(self::once())->method('send')->with($mailMessage);
165 
166  $subject = new ‪EmailLoginNotification($mailerMock);
167  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
168  }
169 
170  #[Test]
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'] = 2;
178  $backendUser = $this->getMockBuilder(BackendUserAuthentication::class)
179  ->disableOriginalConstructor()
180  ->getMock();
181  $backendUser->method('isAdmin')->willReturn(false);
182  $backendUser->user = [
183  'username' => 'karl',
184  ];
185  $mailerMock = $this->createMock(MailerInterface::class);
186 
187  $subject = new ‪EmailLoginNotification($mailerMock);
188  $subject->emailAtLogin(new ‪AfterUserLoggedInEvent($backendUser));
189 
190  // no additional assertion here as the test would fail due to not mocking the email API
191  }
192 
193  protected function ‪setUpMailMessageMock(string $recipient = ''): ‪FluidEmail&MockObject
194  {
195  $mailMessage = $this->createMock(FluidEmail::class);
196 
197  if ($recipient === '') {
198  $mailMessage->method('to')->withAnyParameters()->willReturn($mailMessage);
199  } else {
200  $mailMessage->expects(self::atLeastOnce())->method('to')->with($recipient)->willReturn($mailMessage);
201  }
202  $mailMessage->method('setTemplate')->withAnyParameters()->willReturn($mailMessage);
203  $mailMessage->method('from')->withAnyParameters()->willReturn($mailMessage);
204  $mailMessage->method('setRequest')->withAnyParameters()->willReturn($mailMessage);
205  $mailMessage->method('assignMultiple')->withAnyParameters()->willReturn($mailMessage);
206  GeneralUtility::addInstance(FluidEmail::class, $mailMessage);
207  return $mailMessage;
208  }
209 }
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:123
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin
‪emailAtLoginDoesNotSendAnEmailIfUserHasNoOptin()
Definition: EmailLoginNotificationTest.php:55
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled
‪emailAtLoginSendsEmailToCustomEmailIfAdminWarningIsEnabled()
Definition: EmailLoginNotificationTest.php:99
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\setUpMailMessageMock
‪setUpMailMessageMock(string $recipient='')
Definition: EmailLoginNotificationTest.php:193
‪TYPO3\CMS\Backend\Tests\Unit\Security
Definition: EmailLoginNotificationTest.php:18
‪TYPO3\CMS\Core\Mail\MailerInterface
Definition: MailerInterface.php:28
‪TYPO3\CMS\Backend\Security\EmailLoginNotification
Definition: EmailLoginNotification.php:45
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsNoEmailIfAdminWarningIsEnabledAndNoAdminIsLoggingIn
‪emailAtLoginSendsNoEmailIfAdminWarningIsEnabledAndNoAdminIsLoggingIn()
Definition: EmailLoginNotificationTest.php:171
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn
‪emailAtLoginSendsEmailToCustomEmailIfRegularWarningIsEnabledAndNoAdminIsLoggingIn()
Definition: EmailLoginNotificationTest.php:147
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin
‪emailAtLoginSendsAnEmailIfUserHasValidEmailAndOptin()
Definition: EmailLoginNotificationTest.php:33
‪TYPO3\CMS\Core\Authentication\Event\AfterUserLoggedInEvent
Definition: AfterUserLoggedInEvent.php:27
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest\emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail
‪emailAtLoginDoesNotSendAnEmailIfUserHasInvalidEmail()
Definition: EmailLoginNotificationTest.php:77
‪TYPO3\CMS\Backend\Tests\Unit\Security\EmailLoginNotificationTest
Definition: EmailLoginNotificationTest.php:31