‪TYPO3CMS  10.4
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 {
42  protected ‪$resetSingletonInstances = true;
43 
47  protected ‪$userRepository;
48 
52  protected ‪$recoveryConfiguration;
53 
57  protected ‪$templatePathsProphecy;
58 
59  protected function ‪setUp(): void
60  {
61  $this->userRepository = $this->prophesize(FrontendUserRepository::class);
62  $this->recoveryConfiguration = $this->prophesize(RecoveryConfiguration::class);
63  $this->templatePathsProphecy = $this->prophesize(TemplatePaths::class);
64  }
65 
80  string $emailAddress,
82  array $userInformation,
83  Address $receiver,
84  array $settings
85  ): void {
87  $emailAddress,
89  $userInformation
90  );
91 
92  $expectedViewVariables = [
93  'receiverName' => $receiver->getName(),
94  'url' => 'some uri',
95  'validUntil' => date($settings['dateFormat'], ‪$recoveryConfiguration['lifeTimeTimestamp'])
96  ];
97 
98  $configurationManager = $this->prophesize(ConfigurationManager::class);
99  $configurationManager->getConfiguration(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS)->willReturn(
100  $settings
101  );
102 
103  $languageService = $this->prophesize(LanguageService::class);
104  $languageService->sL(Argument::containingString('password_recovery_mail_header'))->willReturn('translation');
105 
106  $uriBuilder = $this->prophesize(UriBuilder::class);
107  $uriBuilder->setCreateAbsoluteUri(true)->willReturn($uriBuilder->reveal());
108  $uriBuilder->uriFor(
109  'showChangePassword',
110  ['hash' => ‪$recoveryConfiguration['forgotHash']],
111  'PasswordRecovery',
112  'felogin',
113  'Login'
114  )->willReturn('some uri');
115 
116  $fluidEmailProphecy = $this->‪setupFluidEmailProphecy($receiver, $expectedViewVariables, ‪$recoveryConfiguration);
117 
118  $mailer = $this->prophesize(Mailer::class);
119  $mailer->send($fluidEmailProphecy)->shouldBeCalledOnce();
120 
121  $eventDispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
122  $subject = $this->getMockBuilder(RecoveryService::class)
123  ->onlyMethods(['getEmailSubject'])
124  ->setConstructorArgs(
125  [
126  $mailer->reveal(),
127  $eventDispatcherProphecy->reveal(),
128  $configurationManager->reveal(),
129  $this->recoveryConfiguration->reveal(),
130  $uriBuilder->reveal(),
131  $this->userRepository->reveal()
132  ]
133  )->getMock();
134  $subject->method('getEmailSubject')->willReturn('translation');
135 
136  $subject->sendRecoveryEmail($emailAddress);
137  }
138 
139  public function ‪configurationDataProvider(): Generator
140  {
141  yield 'minimal configuration' => [
142  'email' => 'max@mustermann.de',
143  'recoveryConfiguration' => [
144  'lifeTimeTimestamp' => 1234567899,
145  'forgotHash' => '0123456789|some hash',
146  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
147  'mailTemplateName' => 'MailTemplateName',
148  'replyTo' => null
149  ],
150  'userInformation' => [
151  'first_name' => '',
152  'middle_name' => '',
153  'last_name' => '',
154  'username' => 'm.mustermann'
155  ],
156  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
157  'settings' => ['dateFormat' => 'Y-m-d H:i']
158  ];
159  yield 'minimal configuration add replyTo Address' => [
160  'email' => 'max@mustermann.de',
161  'recoveryConfiguration' => [
162  'lifeTimeTimestamp' => 1234567899,
163  'forgotHash' => '0123456789|some hash',
164  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
165  'mailTemplateName' => 'MailTemplateName',
166  'replyTo' => new Address('reply_to@typo3.typo3', 'reply to TYPO3 Installation'),
167  ],
168  'userInformation' => [
169  'first_name' => '',
170  'middle_name' => '',
171  'last_name' => '',
172  'username' => 'm.mustermann'
173  ],
174  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
175  'settings' => ['dateFormat' => 'Y-m-d H:i']
176  ];
177  yield 'html mail provided' => [
178  'email' => 'max@mustermann.de',
179  'recoveryConfiguration' => [
180  'lifeTimeTimestamp' => 123456789,
181  'forgotHash' => '0123456789|some hash',
182  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
183  'mailTemplateName' => 'MailTemplateName',
184  'replyTo' => null
185  ],
186  'userInformation' => [
187  'first_name' => '',
188  'middle_name' => '',
189  'last_name' => '',
190  'username' => 'm.mustermann'
191  ],
192  'receiver' => new Address('max@mustermann.de', 'm.mustermann'),
193  'settings' => ['dateFormat' => 'Y-m-d H:i']
194  ];
195  yield 'complex display name instead of username' => [
196  'email' => 'max@mustermann.de',
197  'recoveryConfiguration' => [
198  'lifeTimeTimestamp' => 123456789,
199  'forgotHash' => '0123456789|some hash',
200  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
201  'mailTemplateName' => 'MailTemplateName',
202  'replyTo' => null
203  ],
204  'userInformation' => [
205  'first_name' => 'Max',
206  'middle_name' => 'Maximus',
207  'last_name' => 'Mustermann',
208  'username' => 'm.mustermann'
209  ],
210  'receiver' => new Address('max@mustermann.de', 'Max Maximus Mustermann'),
211  'settings' => ['dateFormat' => 'Y-m-d H:i']
212  ];
213  yield 'custom dateFormat and no middle name' => [
214  'email' => 'max@mustermann.de',
215  'recoveryConfiguration' => [
216  'lifeTimeTimestamp' => 987654321,
217  'forgotHash' => '0123456789|some hash',
218  'sender' => new Address('typo3@typo3.typo3', 'TYPO3 Installation'),
219  'mailTemplateName' => 'MailTemplateName',
220  'replyTo' => null
221  ],
222  'userInformation' => [
223  'first_name' => 'Max',
224  'middle_name' => '',
225  'last_name' => 'Mustermann',
226  'username' => 'm.mustermann'
227  ],
228  'receiver' => new Address('max@mustermann.de', 'Max Mustermann'),
229  'settings' => ['dateFormat' => 'Y-m-d']
230  ];
231  }
232 
239  string $emailAddress,
241  array $userInformation
242  ): void {
243  $this->recoveryConfiguration->getForgotHash()->willReturn(‪$recoveryConfiguration['forgotHash']);
244  $this->recoveryConfiguration->getLifeTimeTimestamp()->willReturn(‪$recoveryConfiguration['lifeTimeTimestamp']);
245  $this->recoveryConfiguration->getSender()->willReturn(‪$recoveryConfiguration['sender']);
246  $this->recoveryConfiguration->getMailTemplateName()->willReturn(‪$recoveryConfiguration['mailTemplateName']);
247  $this->recoveryConfiguration->getReplyTo()->willReturn(‪$recoveryConfiguration['replyTo']);
248 
249  $this->templatePathsProphecy->setTemplateRootPaths(['/some/path/to/a/template/folder/']);
250  $this->recoveryConfiguration->getMailTemplatePaths()->willReturn($this->templatePathsProphecy->reveal());
251 
252  $this->userRepository->updateForgotHashForUserByEmail(
253  $emailAddress,
254  GeneralUtility::hmac(‪$recoveryConfiguration['forgotHash'])
255  )->shouldBeCalledOnce();
256 
257  $this->userRepository->fetchUserInformationByEmail($emailAddress)->willReturn($userInformation);
258  }
259 
267  private function ‪setupFluidEmailProphecy(
268  Address $receiver,
269  array $expectedViewVariables,
271  ) {
272  $fluidEmailProphecy = $this->prophesize(FluidEmail::class);
273  GeneralUtility::addInstance(FluidEmail::class, $fluidEmailProphecy->reveal());
274  $fluidEmailProphecy->subject('translation')->willReturn($fluidEmailProphecy);
275  $fluidEmailProphecy->from(‪$recoveryConfiguration['sender'])->willReturn($fluidEmailProphecy);
276  $fluidEmailProphecy->to($receiver)->willReturn($fluidEmailProphecy);
277  $fluidEmailProphecy->assignMultiple($expectedViewVariables)->willReturn($fluidEmailProphecy);
278  $fluidEmailProphecy->setTemplate(‪$recoveryConfiguration['mailTemplateName'])->willReturn($fluidEmailProphecy);
279 
280  if (!empty(‪$recoveryConfiguration['replyTo'])) {
281  $fluidEmailProphecy->addReplyTo(‪$recoveryConfiguration['replyTo'])->willReturn($fluidEmailProphecy);
282  }
283 
284  return $fluidEmailProphecy;
285  }
286 }
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\setupFluidEmailProphecy
‪ObjectProphecy FluidEmail setupFluidEmailProphecy(Address $receiver, array $expectedViewVariables, array $recoveryConfiguration)
Definition: RecoveryServiceTest.php:263
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:35
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:31
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:39
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\mockRecoveryConfigurationAndUserRepository
‪mockRecoveryConfigurationAndUserRepository(string $emailAddress, array $recoveryConfiguration, array $userInformation)
Definition: RecoveryServiceTest.php:234
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService
Definition: RecoveryService.php:41
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\configurationDataProvider
‪configurationDataProvider()
Definition: RecoveryServiceTest.php:135
‪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:75
‪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
‪FrontendUserRepository ObjectProphecy $userRepository
Definition: RecoveryServiceTest.php:45
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$recoveryConfiguration
‪RecoveryConfiguration ObjectProphecy $recoveryConfiguration
Definition: RecoveryServiceTest.php:49
‪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:55
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Service\RecoveryServiceTest\$templatePathsProphecy
‪TemplatePaths ObjectProphecy $templatePathsProphecy
Definition: RecoveryServiceTest.php:53