‪TYPO3CMS  10.4
RecoveryConfigurationTest.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\Prophecy\ObjectProphecy;
22 use Psr\Log\LoggerInterface;
23 use Symfony\Component\Mime\Address;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
34 class ‪RecoveryConfigurationTest extends UnitTestCase
35 {
39  protected ‪$context;
40 
44  protected ‪$configurationManager;
45 
49  protected ‪$hashService;
50 
54  protected ‪$settings = [
55  'email_from' => 'example@example.com',
56  'email_fromName' => 'TYPO3 Installation',
57  'email' => [
58  'layoutRootPaths' => [20 => '/some/path/to/a/layout/folder/'],
59  'templateRootPaths' => [20 => '/some/path/to/a/template/folder/'],
60  'partialRootPaths' => [20 => '/some/path/to/a/partial/folder/'],
61  'templateName' => 'someTemplateFileName',
62  ],
63  'forgotLinkHashValidTime' => 1,
64  'replyTo' => ''
65  ];
66 
70  protected ‪$subject;
71 
75  protected ‪$logger;
76 
77  protected function ‪setUp(): void
78  {
79  $this->context = $this->prophesize(Context::class);
80  $this->configurationManager = $this->prophesize(ConfigurationManager::class);
81  $this->hashService = $this->prophesize(HashService::class);
82  $this->logger = $this->prophesize(LoggerInterface::class);
83 
84  $this->hashService->generateHmac(Argument::type('string'))->willReturn('some hash');
85 
86  parent::setUp();
87  }
88 
93  protected function ‪setupSubject(): void
94  {
95  $this->configurationManager->getConfiguration(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS)->willReturn(
96  $this->settings
97  );
98 
99  $this->subject = new ‪RecoveryConfiguration(
100  $this->context->reveal(),
101  $this->configurationManager->reveal(),
102  new ‪Random(),
103  $this->hashService->reveal()
104  );
105 
106  $this->subject->setLogger($this->logger->reveal());
107  }
108 
113  {
114  $this->settings['email_from'] = null;
115  $this->settings['email_fromName'] = null;
116  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = 'no-reply@example.com';
117  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = 'Example Inc.';
118 
119  $this->‪setupSubject();
120 
121  $sender = $this->subject->getSender();
122 
123  self::assertSame(
124  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'],
125  $sender->getAddress()
126  );
127  self::assertSame(
128  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'],
129  $sender->getName()
130  );
131  }
132 
137  {
138  $this->‪setupSubject();
139 
140  $sender = $this->subject->getSender();
141 
142  self::assertSame(
143  $this->settings['email_from'],
144  $sender->getAddress()
145  );
146  self::assertSame(
147  $this->settings['email_fromName'],
148  $sender->getName()
149  );
150  }
151 
156  {
157  $this->settings['email']['templateName'] = '';
158  $this->expectException(IncompleteConfigurationException::class);
159  $this->expectExceptionCode(1584998393);
160  $this->‪setupSubject();
161  $this->subject->getMailTemplateName();
162  }
163 
167  public function ‪getLifeTimeTimestampShouldReturnTimestamp(): void
168  {
169  $timestamp = time();
170  $expected = $timestamp + 3600 * $this->settings['forgotLinkHashValidTime'];
171  $this->context->getPropertyFromAspect('date', 'timestamp')->willReturn($timestamp);
172  $this->‪setupSubject();
173 
174  $actual = $this->subject->getLifeTimeTimestamp();
175 
176  $this->context->getPropertyFromAspect('date', 'timestamp')->shouldHaveBeenCalledTimes(1);
177  self::assertSame($expected, $actual);
178  }
179 
184  {
185  $timestamp = time();
186  $expectedTimestamp = $timestamp + 3600 * $this->settings['forgotLinkHashValidTime'];
187  $expected = "{$expectedTimestamp}|some hash";
188  $this->context->getPropertyFromAspect('date', 'timestamp')->willReturn($timestamp);
189  $this->‪setupSubject();
190 
191  self::assertSame(
192  $expected,
193  $this->subject->getForgotHash()
194  );
195  }
196 
200  public function ‪getReplyToShouldReturnNullIfNoneAreSet(): void
201  {
202  $this->‪setupSubject();
203 
204  self::assertNull($this->subject->getReplyTo());
205  }
206 
211  {
212  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] = [
213  0 => 'EXT:core/Resources/Private/Templates/',
214  10 => 'EXT:backend/Resources/Private/Templates/'
215  ];
216  $this->‪setupSubject();
217  $actualTemplatePaths = $this->subject->getMailTemplatePaths();
218  self::assertSame(
219  [
220  ‪Environment::getPublicPath() . '/typo3/sysext/core/Resources/Private/Templates/',
221  ‪Environment::getPublicPath() . '/typo3/sysext/backend/Resources/Private/Templates/',
222  '/some/path/to/a/template/folder/'
223  ],
224  $actualTemplatePaths->getTemplateRootPaths()
225  );
226  }
227 
232  {
233  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] = [
234  0 => 'EXT:core/Resources/Private/Templates/',
235  10 => 'EXT:backend/Resources/Private/Templates/'
236  ];
237  $this->settings['email']['templateRootPaths'] = [10 => '/some/path/to/a/template/folder/'];
238  $this->‪setupSubject();
239  $actualTemplatePaths = $this->subject->getMailTemplatePaths();
240  self::assertSame(
241  [
242  ‪Environment::getPublicPath() . '/typo3/sysext/core/Resources/Private/Templates/',
243  '/some/path/to/a/template/folder/'
244  ],
245  $actualTemplatePaths->getTemplateRootPaths()
246  );
247  }
248 
253  {
254  $this->‪setupSubject();
255  self::assertSame($this->settings['email']['templateName'], $this->subject->getMailTemplateName());
256  }
257 
262  {
263  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToAddress'] = 'typo3@example.com';
264  $this->‪setupSubject();
265  self::assertInstanceOf(Address::class, $this->subject->getReplyTo());
266  }
267 
272  {
273  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToName'] = 'TYPO3';
274  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToAddress'] = 'typo3@example.com';
275  $this->‪setupSubject();
276 
277  self::assertSame('typo3@example.com', $this->subject->getReplyTo()->getAddress());
278  self::assertSame('TYPO3', $this->subject->getReplyTo()->getName());
279  }
280 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getMailTemplatePathsReturnsAnInstanceOfTemplatePathsObjectWithConfigurationOfTypoScript
‪getMailTemplatePathsReturnsAnInstanceOfTemplatePathsObjectWithConfigurationOfTypoScript()
Definition: RecoveryConfigurationTest.php:204
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\recoveryConfigurationWillCreateAnInstanceOfAddressIfDefaultMailReplyToAddressIsSet
‪recoveryConfigurationWillCreateAnInstanceOfAddressIfDefaultMailReplyToAddressIsSet()
Definition: RecoveryConfigurationTest.php:255
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\setupSubject
‪setupSubject()
Definition: RecoveryConfigurationTest.php:87
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getSenderShouldReturnAddressWithConfigFromTypoScript
‪getSenderShouldReturnAddressWithConfigFromTypoScript()
Definition: RecoveryConfigurationTest.php:130
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getLifeTimeTimestampShouldReturnTimestamp
‪getLifeTimeTimestampShouldReturnTimestamp()
Definition: RecoveryConfigurationTest.php:161
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getReplyToShouldReturnNullIfNoneAreSet
‪getReplyToShouldReturnNullIfNoneAreSet()
Definition: RecoveryConfigurationTest.php:194
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\recoveryConfigurationWillCreateAnInstanceOfAddressWithName
‪recoveryConfigurationWillCreateAnInstanceOfAddressWithName()
Definition: RecoveryConfigurationTest.php:265
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$hashService
‪ObjectProphecy HashService $hashService
Definition: RecoveryConfigurationTest.php:46
‪TYPO3\CMS\FrontendLogin\Configuration\IncompleteConfigurationException
Definition: IncompleteConfigurationException.php:24
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$subject
‪RecoveryConfiguration $subject
Definition: RecoveryConfigurationTest.php:65
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\setUp
‪setUp()
Definition: RecoveryConfigurationTest.php:71
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest
Definition: RecoveryConfigurationTest.php:35
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getForgotHashShouldReturnHashWithLifeTimeTimestamp
‪getForgotHashShouldReturnHashWithLifeTimeTimestamp()
Definition: RecoveryConfigurationTest.php:177
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getSenderShouldReturnAddressWithFallbackFromGlobals
‪getSenderShouldReturnAddressWithFallbackFromGlobals()
Definition: RecoveryConfigurationTest.php:106
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$settings
‪array $settings
Definition: RecoveryConfigurationTest.php:50
‪TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration
Definition: RecoveryConfiguration.php:34
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getMailTemplateNameWillReturnTemplateNameConfiguredInTypoScript
‪getMailTemplateNameWillReturnTemplateNameConfiguredInTypoScript()
Definition: RecoveryConfigurationTest.php:246
‪TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
Definition: InvalidConfigurationTypeException.php:26
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:30
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$context
‪ObjectProphecy Context $context
Definition: RecoveryConfigurationTest.php:38
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getMailTemplatePathsReplacesTemplatePathsWithPathsConfiguredInTypoScript
‪getMailTemplatePathsReplacesTemplatePathsWithPathsConfiguredInTypoScript()
Definition: RecoveryConfigurationTest.php:225
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$configurationManager
‪ObjectProphecy ConfigurationManager $configurationManager
Definition: RecoveryConfigurationTest.php:42
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\getEmailTemplateNameThrowsExceptionIfTemplateNameIsEmpty
‪getEmailTemplateNameThrowsExceptionIfTemplateNameIsEmpty()
Definition: RecoveryConfigurationTest.php:149
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:24
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration
Definition: RecoveryConfigurationTest.php:18
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$logger
‪ObjectProphecy LoggerInterface $logger
Definition: RecoveryConfigurationTest.php:69