‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
22 use Psr\Log\LoggerInterface;
23 use Psr\Log\NullLogger;
24 use Symfony\Component\Mime\Address;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
35 final class ‪RecoveryConfigurationTest extends UnitTestCase
36 {
38 
39  protected array $settings = [
40  'email_from' => 'example@example.com',
41  'email_fromName' => 'TYPO3 Installation',
42  'email' => [
43  'layoutRootPaths' => [20 => '/some/path/to/a/layout/folder/'],
44  'templateRootPaths' => [20 => '/some/path/to/a/template/folder/'],
45  'partialRootPaths' => [20 => '/some/path/to/a/partial/folder/'],
46  'templateName' => 'someTemplateFileName',
47  ],
48  'forgotLinkHashValidTime' => 1,
49  'replyTo' => '',
50  ];
51 
52  protected ‪RecoveryConfiguration $subject;
53  protected LoggerInterface $logger;
54 
55  protected function setUp(): void
56  {
57  $this->configurationManager = $this->getMockBuilder(ConfigurationManager::class)->disableOriginalConstructor()->getMock();
58  $this->logger = new NullLogger();
59 
60  parent::setUp();
61  }
62 
63  protected function setupSubject(‪Context $context = null): void
64  {
65  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'bar';
66 
67  $context ??= new ‪Context();
68 
69  $this->configurationManager->method('getConfiguration')->with(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS)
70  ->willReturn($this->settings);
71 
72  $this->subject = new ‪RecoveryConfiguration(
73  $context,
74  $this->configurationManager,
75  new ‪Random(),
76  new ‪HashService()
77  );
78 
79  $this->subject->setLogger($this->logger);
80  }
81 
82  #[Test]
83  public function getSenderShouldReturnAddressWithFallbackFromGlobals(): void
84  {
85  $this->settings['email_from'] = null;
86  $this->settings['email_fromName'] = null;
87  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = 'no-reply@example.com';
88  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = 'Example Inc.';
89 
90  $this->setupSubject();
91 
92  $sender = $this->subject->getSender();
93 
94  self::assertSame(
95  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'],
96  $sender->getAddress()
97  );
98  self::assertSame(
99  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'],
100  $sender->getName()
101  );
102  }
103 
104  #[Test]
105  public function getSenderShouldReturnAddressWithConfigFromTypoScript(): void
106  {
107  $this->setupSubject();
108 
109  $sender = $this->subject->getSender();
110 
111  self::assertSame(
112  $this->settings['email_from'],
113  $sender->getAddress()
114  );
115  self::assertSame(
116  $this->settings['email_fromName'],
117  $sender->getName()
118  );
119  }
120 
121  #[Test]
122  public function getEmailTemplateNameThrowsExceptionIfTemplateNameIsEmpty(): void
123  {
124  $this->settings['email']['templateName'] = '';
125  $this->expectException(IncompleteConfigurationException::class);
126  $this->expectExceptionCode(1584998393);
127  $this->setupSubject();
128  $this->subject->getMailTemplateName();
129  }
130 
131  #[Test]
132  public function getLifeTimeTimestampShouldReturnTimestamp(): void
133  {
134  $timestamp = time();
135  $expected = $timestamp + 3600 * $this->settings['forgotLinkHashValidTime'];
136 
137  $context = new Context();
138  $context->setAspect('date', new DateTimeAspect(new \DateTimeImmutable('@' . $timestamp)));
139  $this->setupSubject($context);
140 
141  $actual = $this->subject->getLifeTimeTimestamp();
142 
143  self::assertSame($expected, $actual);
144  }
145 
146  #[Test]
147  public function getForgotHashShouldReturnHashWithLifeTimeTimestamp(): void
148  {
149  $timestamp = time();
150  $expectedTimestamp = $timestamp + 3600 * $this->settings['forgotLinkHashValidTime'];
151 
152  $context = new Context();
153  $context->setAspect('date', new DateTimeAspect(new \DateTimeImmutable('@' . $timestamp)));
154  $this->setupSubject($context);
155 
156  self::assertStringStartsWith((string)$expectedTimestamp, $this->subject->getForgotHash());
157  }
158 
159  #[Test]
160  public function getReplyToShouldReturnNullIfNoneAreSet(): void
161  {
162  $this->setupSubject();
163 
164  self::assertNull($this->subject->getReplyTo());
165  }
166 
167  #[Test]
168  public function getMailTemplatePathsReturnsAnInstanceOfTemplatePathsObjectWithConfigurationOfTypoScript(): void
169  {
170  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] = [
171  0 => 'EXT:core/Resources/Private/Templates/',
172  10 => 'EXT:backend/Resources/Private/Templates/',
173  ];
174  $this->setupSubject();
175  $actualTemplatePaths = $this->subject->getMailTemplatePaths();
176  self::assertSame(
177  [
178  ‪Environment::getPublicPath() . '/typo3/sysext/core/Resources/Private/Templates/',
179  ‪Environment::getPublicPath() . '/typo3/sysext/backend/Resources/Private/Templates/',
180  '/some/path/to/a/template/folder/',
181  ],
182  $actualTemplatePaths->getTemplateRootPaths()
183  );
184  }
185 
186  #[Test]
187  public function getMailTemplatePathsReplacesTemplatePathsWithPathsConfiguredInTypoScript(): void
188  {
189  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'] = [
190  0 => 'EXT:core/Resources/Private/Templates/',
191  10 => 'EXT:backend/Resources/Private/Templates/',
192  ];
193  $this->settings['email']['templateRootPaths'] = [10 => '/some/path/to/a/template/folder/'];
194  $this->setupSubject();
195  $actualTemplatePaths = $this->subject->getMailTemplatePaths();
196  self::assertSame(
197  [
198  ‪Environment::getPublicPath() . '/typo3/sysext/core/Resources/Private/Templates/',
199  '/some/path/to/a/template/folder/',
200  ],
201  $actualTemplatePaths->getTemplateRootPaths()
202  );
203  }
204 
205  #[Test]
206  public function getMailTemplateNameWillReturnTemplateNameConfiguredInTypoScript(): void
207  {
208  $this->setupSubject();
209  self::assertSame($this->settings['email']['templateName'], $this->subject->getMailTemplateName());
210  }
211 
212  #[Test]
213  public function recoveryConfigurationWillCreateAnInstanceOfAddressIfDefaultMailReplyToAddressIsSet(): void
214  {
215  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToAddress'] = 'typo3@example.com';
216  $this->setupSubject();
217  self::assertInstanceOf(Address::class, $this->subject->getReplyTo());
218  }
219 
220  #[Test]
221  public function recoveryConfigurationWillCreateAnInstanceOfAddressWithName(): void
222  {
223  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToName'] = 'TYPO3';
224  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailReplyToAddress'] = 'typo3@example.com';
225  $this->setupSubject();
226 
227  self::assertSame('typo3@example.com', $this->subject->getReplyTo()->getAddress());
228  self::assertSame('TYPO3', $this->subject->getReplyTo()->getName());
229  }
230 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\FrontendLogin\Configuration\IncompleteConfigurationException
Definition: IncompleteConfigurationException.php:23
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest
Definition: RecoveryConfigurationTest.php:36
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration\RecoveryConfigurationTest\$configurationManager
‪MockObject &ConfigurationManager $configurationManager
Definition: RecoveryConfigurationTest.php:37
‪TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration
Definition: RecoveryConfiguration.php:33
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:27
‪TYPO3\CMS\FrontendLogin\Tests\Unit\Configuration
Definition: RecoveryConfigurationTest.php:18
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35