‪TYPO3CMS  11.5
RecoveryServiceTest.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 Generator;
21 use Prophecy\Argument;
22 use Prophecy\Prophecy\ObjectProphecy;
23 use Psr\EventDispatcher\EventDispatcherInterface;
24 use Symfony\Component\Mime\Address;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
37 class ‪RecoveryServiceTest extends UnitTestCase
38 {
39  use \Prophecy\PhpUnit\ProphecyTrait;
43  protected ‪$resetSingletonInstances = true;
44 
46  protected ObjectProphecy ‪$userRepository;
47 
49  protected ObjectProphecy ‪$recoveryConfiguration;
50 
52  protected ObjectProphecy ‪$templatePathsProphecy;
53 
54  protected function ‪setUp(): void
55  {
56  parent::setUp();
57  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
58  $this->userRepository = $this->prophesize(FrontendUserRepository::class);
59  $this->recoveryConfiguration = $this->prophesize(RecoveryConfiguration::class);
60  $this->templatePathsProphecy = $this->prophesize(TemplatePaths::class);
61  }
62 
71  string $emailAddress,
73  array $userInformation,
74  Address $receiver,
75  array $settings
76  ): void {
78  $emailAddress,
80  $userInformation
81  );
82 
83  $expectedViewVariables = [
84  'receiverName' => $receiver->getName(),
85  'url' => 'some uri',
86  'validUntil' => date($settings['dateFormat'], ‪$recoveryConfiguration['lifeTimeTimestamp']),
87  ];
88 
89  $configurationManager = $this->prophesize(ConfigurationManager::class);
90  $configurationManager->getConfiguration(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS)->willReturn(
91  $settings
92  );
93 
94  $languageService = $this->prophesize(LanguageService::class);
95  $languageService->sL(Argument::containingString('password_recovery_mail_header'))->willReturn('translation');
96 
97  $uriBuilder = $this->prophesize(UriBuilder::class);
98  $uriBuilder->reset()->willReturn($uriBuilder->reveal());
99  $uriBuilder->setCreateAbsoluteUri(true)->willReturn($uriBuilder->reveal());
100  $uriBuilder->uriFor(
101  'showChangePassword',
102  ['hash' => ‪$recoveryConfiguration['forgotHash']],
103  'PasswordRecovery',
104  'felogin',
105  'Login'
106  )->willReturn('some uri');
107 
108  $fluidEmailProphecy = $this->‪setupFluidEmailProphecy($receiver, $expectedViewVariables, ‪$recoveryConfiguration);
109 
110  $mailer = $this->prophesize(Mailer::class);
111  $mailer->send($fluidEmailProphecy)->shouldBeCalledOnce();
112 
113  $eventDispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
114  $subject = $this->getMockBuilder(RecoveryService::class)
115  ->onlyMethods(['getEmailSubject'])
116  ->setConstructorArgs(
117  [
118  $mailer->reveal(),
119  $eventDispatcherProphecy->reveal(),
120  $configurationManager->reveal(),
121  $this->recoveryConfiguration->reveal(),
122  $uriBuilder->reveal(),
123  $this->userRepository->reveal(),
124  ]
125  )->getMock();
126  $subject->method('getEmailSubject')->willReturn('translation');
127 
128  $subject->sendRecoveryEmail($emailAddress);
129  }
130 
131  public function ‪configurationDataProvider(): Generator
132  {
133  yield 'minimal configuration' => [
134  'email' => 'max@mustermann.de',
135  'recoveryConfiguration' => [
136  'lifeTimeTimestamp' => 1234567899,
137  'forgotHash' => '0123456789|some hash',
138  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
139  'mailTemplateName' => 'MailTemplateName',
140  'replyTo' => null,
141  ],
142  'userInformation' => [
143  'first_name' => '',
144  'middle_name' => '',
145  'last_name' => '',
146  'username' => 'm.mustermann',
147  ],
148  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
149  'settings' => ['dateFormat' => 'Y-m-d H:i'],
150  ];
151  yield 'minimal configuration add replyTo Address' => [
152  'email' => 'max@mustermann.de',
153  'recoveryConfiguration' => [
154  'lifeTimeTimestamp' => 1234567899,
155  'forgotHash' => '0123456789|some hash',
156  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
157  'mailTemplateName' => 'MailTemplateName',
158  'replyTo' => new Address('reply_to@typo3.typo3', 'reply to TYPO3 Installation'),
159  ],
160  'userInformation' => [
161  'first_name' => '',
162  'middle_name' => '',
163  'last_name' => '',
164  'username' => 'm.mustermann',
165  ],
166  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
167  'settings' => ['dateFormat' => 'Y-m-d H:i'],
168  ];
169  yield 'html mail provided' => [
170  'email' => 'max@mustermann.de',
171  'recoveryConfiguration' => [
172  'lifeTimeTimestamp' => 123456789,
173  'forgotHash' => '0123456789|some hash',
174  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
175  'mailTemplateName' => 'MailTemplateName',
176  'replyTo' => null,
177  ],
178  'userInformation' => [
179  'first_name' => '',
180  'middle_name' => '',
181  'last_name' => '',
182  'username' => 'm.mustermann',
183  ],
184  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
185  'settings' => ['dateFormat' => 'Y-m-d H:i'],
186  ];
187  yield 'complex display name instead of username' => [
188  'email' => 'max@mustermann.de',
189  'recoveryConfiguration' => [
190  'lifeTimeTimestamp' => 123456789,
191  'forgotHash' => '0123456789|some hash',
192  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
193  'mailTemplateName' => 'MailTemplateName',
194  'replyTo' => null,
195  ],
196  'userInformation' => [
197  'first_name' => 'Max',
198  'middle_name' => 'Maximus',
199  'last_name' => 'Mustermann',
200  'username' => 'm.mustermann',
201  ],
202  'receiver' => new Address('max@mustermann.de', 'Max Maximus Mustermann (m.mustermann)'),
203  'settings' => ['dateFormat' => 'Y-m-d H:i'],
204  ];
205  yield 'custom dateFormat and no middle name' => [
206  'email' => 'max@mustermann.de',
207  'recoveryConfiguration' => [
208  'lifeTimeTimestamp' => 987654321,
209  'forgotHash' => '0123456789|some hash',
210  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
211  'mailTemplateName' => 'MailTemplateName',
212  'replyTo' => null,
213  ],
214  'userInformation' => [
215  'first_name' => 'Max',
216  'middle_name' => '',
217  'last_name' => 'Mustermann',
218  'username' => 'm.mustermann',
219  ],
220  'receiver' => new Address('max@mustermann.de', 'Max Mustermann (m.mustermann)'),
221  'settings' => ['dateFormat' => 'Y-m-d'],
222  ];
223  }
224 
226  string $emailAddress,
228  array $userInformation
229  ): void {
230  $this->recoveryConfiguration->getForgotHash()->willReturn(‪$recoveryConfiguration['forgotHash']);
231  $this->recoveryConfiguration->getLifeTimeTimestamp()->willReturn(‪$recoveryConfiguration['lifeTimeTimestamp']);
232  $this->recoveryConfiguration->getSender()->willReturn(‪$recoveryConfiguration['sender']);
233  $this->recoveryConfiguration->getMailTemplateName()->willReturn(‪$recoveryConfiguration['mailTemplateName']);
234  $this->recoveryConfiguration->getReplyTo()->willReturn(‪$recoveryConfiguration['replyTo']);
235 
236  $this->templatePathsProphecy->setTemplateRootPaths(['/some/path/to/a/template/folder/']);
237  $this->recoveryConfiguration->getMailTemplatePaths()->willReturn($this->templatePathsProphecy->reveal());
238 
239  $this->userRepository->updateForgotHashForUserByEmail(
240  $emailAddress,
241  GeneralUtility::hmac(‪$recoveryConfiguration['forgotHash'])
242  )->shouldBeCalledOnce();
243 
244  $this->userRepository->fetchUserInformationByEmail($emailAddress)->willReturn($userInformation);
245  }
246 
253  private function ‪setupFluidEmailProphecy(
254  Address $receiver,
255  array $expectedViewVariables,
257  ): ObjectProphecy {
258  $fluidEmailProphecy = $this->prophesize(FluidEmail::class);
259  GeneralUtility::addInstance(FluidEmail::class, $fluidEmailProphecy->reveal());
260  $fluidEmailProphecy->subject('translation')->willReturn($fluidEmailProphecy);
261  $fluidEmailProphecy->from(‪$recoveryConfiguration['sender'])->willReturn($fluidEmailProphecy);
262  $fluidEmailProphecy->to($receiver)->willReturn($fluidEmailProphecy);
263  $fluidEmailProphecy->assignMultiple($expectedViewVariables)->willReturn($fluidEmailProphecy);
264  $fluidEmailProphecy->setTemplate(‪$recoveryConfiguration['mailTemplateName'])->willReturn($fluidEmailProphecy);
265 
266  if (!empty(‪$recoveryConfiguration['replyTo'])) {
267  $fluidEmailProphecy->addReplyTo(‪$recoveryConfiguration['replyTo'])->willReturn($fluidEmailProphecy);
268  }
269 
270  return $fluidEmailProphecy;
271  }
272 }
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:37
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:30
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:41
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\mockRecoveryConfigurationAndUserRepository
‪mockRecoveryConfigurationAndUserRepository(string $emailAddress, array $recoveryConfiguration, array $userInformation)
Definition: RecoveryServiceTest.php:223
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService
Definition: RecoveryService.php:40
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\configurationDataProvider
‪configurationDataProvider()
Definition: RecoveryServiceTest.php:129
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\sendRecoveryEmailShouldGenerateMailFromConfiguration
‪sendRecoveryEmailShouldGenerateMailFromConfiguration(string $emailAddress, array $recoveryConfiguration, array $userInformation, Address $receiver, array $settings)
Definition: RecoveryServiceTest.php:68
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: RecoveryServiceTest.php:41
‪TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration
Definition: RecoveryConfiguration.php:34
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:30
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$userRepository
‪ObjectProphecy $userRepository
Definition: RecoveryServiceTest.php:44
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$templatePathsProphecy
‪ObjectProphecy $templatePathsProphecy
Definition: RecoveryServiceTest.php:50
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$recoveryConfiguration
‪ObjectProphecy $recoveryConfiguration
Definition: RecoveryServiceTest.php:47
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service
Definition: RecoveryServiceTest.php:18
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest
Definition: RecoveryServiceTest.php:38
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\setUp
‪setUp()
Definition: RecoveryServiceTest.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\setupFluidEmailProphecy
‪ObjectProphecy< FluidEmail > setupFluidEmailProphecy(Address $receiver, array $expectedViewVariables, array $recoveryConfiguration)
Definition: RecoveryServiceTest.php:251