‪TYPO3CMS  10.4
SiteConfigurationTest.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 Symfony\Component\Yaml\Yaml;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 class ‪SiteConfigurationTest extends UnitTestCase
32 {
33  protected ‪$resetSingletonInstances = true;
34 
38  protected ‪$siteConfiguration;
39 
45  protected ‪$fixturePath;
46 
47  protected function ‪setUp(): void
48  {
49  parent::setUp();
50  $basePath = ‪Environment::getVarPath() . '/tests/unit';
51  $this->fixturePath = $basePath . '/fixture/config/sites';
52  if (!file_exists($this->fixturePath)) {
53  ‪GeneralUtility::mkdir_deep($this->fixturePath);
54  }
55  $this->testFilesToDelete[] = $basePath;
56  $cacheManager = $this->prophesize(CacheManager::class);
57  $coreCacheProphecy = $this->prophesize(PhpFrontend::class);
58  $coreCacheProphecy->require(Argument::any())->willReturn(false);
59  $coreCacheProphecy->set(Argument::any(), Argument::any())->willReturn(null);
60  $coreCacheProphecy->remove(Argument::any(), Argument::any())->willReturn(null);
61  $cacheManager->getCache('core')->willReturn($coreCacheProphecy->reveal());
62  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManager->reveal());
63 
64  $this->siteConfiguration = new ‪SiteConfiguration($this->fixturePath);
65  }
66 
71  {
72  self::assertEmpty($this->siteConfiguration->resolveAllExistingSites());
73  }
74 
78  public function ‪resolveAllExistingSitesReadsConfiguration(): void
79  {
80  $configuration = [
81  'rootPageId' => 42,
82  'base' => 'https://example.com',
83  ];
84  $yamlFileContents = Yaml::dump($configuration, 99, 2);
85  ‪GeneralUtility::mkdir($this->fixturePath . '/home');
86  ‪GeneralUtility::writeFile($this->fixturePath . '/home/config.yaml', $yamlFileContents);
87  $sites = $this->siteConfiguration->resolveAllExistingSites();
88  self::assertCount(1, $sites);
89  $currentSite = current($sites);
90  self::assertSame(42, $currentSite->getRootPageId());
91  self::assertEquals(new Uri('https://example.com'), $currentSite->getBase());
92  }
93 
97  public function ‪writeOnlyWritesModifiedKeys(): void
98  {
99  $identifier = 'testsite';
100  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . $identifier);
101  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config1.yaml';
102  $expected = __DIR__ . '/Fixtures/SiteConfigs/config1_expected.yaml';
103  $siteConfig = $this->fixturePath . '/' . $identifier . '/config.yaml';
104  copy($configFixture, $siteConfig);
105 
106  // load with resolved imports as the module does
107  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
108  ->load(
109  GeneralUtility::fixWindowsFilePath($siteConfig),
111  );
112  // modify something on base level
113  $configuration['base'] = 'https://example.net/';
114  // modify something nested
115  $configuration['languages'][0]['title'] = 'English';
116  // delete values
117  unset($configuration['someOtherValue'], $configuration['languages'][1]);
118 
119  $this->siteConfiguration->write($identifier, $configuration, true);
120 
121  // expect modified base but intact imports
122  self::assertFileEquals($expected, $siteConfig);
123  }
124 
128  public function ‪writingOfNestedStructuresPreservesOrder(): void
129  {
130  $identifier = 'testsite';
131  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . $identifier);
132  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
133  $expected = __DIR__ . '/Fixtures/SiteConfigs/config2_expected.yaml';
134  $siteConfig = $this->fixturePath . '/' . $identifier . '/config.yaml';
135  copy($configFixture, $siteConfig);
136 
137  // load with resolved imports as the module does
138  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
139  ->load(
140  GeneralUtility::fixWindowsFilePath($siteConfig),
142  );
143  // add new language
144  $languageConfig = [
145  'title' => 'English',
146  'enabled' => true,
147  'languageId' => '0',
148  'base' => '/en',
149  'typo3Language' => 'default',
150  'locale' => 'en_US.utf8',
151  'iso-639-1' => 'en',
152  'hreflang' => '',
153  'direction' => '',
154  'flag' => 'en',
155  'navigationTitle' => 'English',
156  ];
157  array_unshift($configuration['languages'], $languageConfig);
158  $this->siteConfiguration->write($identifier, $configuration, true);
159 
160  // expect modified base but intact imports
161  self::assertFileEquals($expected, $siteConfig);
162  }
163 
164  public static function ‪writingPlaceholdersIsHandledDataProvider(): \Generator
165  {
166  yield 'unchanged' => [
167  ['customProperty' => 'Using %env("existing")% variable'],
168  false,
169  ];
170  yield 'removed placeholder variable' => [
171  ['customProperty' => 'Not using any variable'],
172  false,
173  ];
174  yield 'changed raw text only' => [
175  ['customProperty' => 'Using %env("existing")% variable from system environment'],
176  false,
177  ];
178  yield 'added new placeholder variable' => [
179  ['customProperty' => 'Using %env("existing")% and %env("secret")% variable'],
180  true,
181  ];
182  }
183 
188  public function ‪writingPlaceholdersIsHandled(array $changes, bool $expectedException): void
189  {
190  if ($expectedException) {
191  $this->expectException(\RuntimeException::class);
192  $this->expectExceptionCode(1670361271);
193  }
194 
195  $identifier = 'testsite';
196  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . $identifier);
197  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
198  $siteConfig = $this->fixturePath . '/' . $identifier . '/config.yaml';
199  copy($configFixture, $siteConfig);
200  // load with resolved imports as the module does
201  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
202  ->load(
203  GeneralUtility::fixWindowsFilePath($siteConfig),
205  );
206  $configuration = array_merge($configuration, $changes);
207  $this->siteConfiguration->write($identifier, $configuration, true);
208  }
209 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PROCESS_IMPORTS
‪const PROCESS_IMPORTS
Definition: YamlFileLoader.php:53
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\setUp
‪setUp()
Definition: SiteConfigurationTest.php:45
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$fixturePath
‪string $fixturePath
Definition: SiteConfigurationTest.php:43
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:41
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest
Definition: SiteConfigurationTest.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:2022
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$siteConfiguration
‪TYPO3 CMS Core Configuration SiteConfiguration $siteConfiguration
Definition: SiteConfigurationTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingPlaceholdersIsHandled
‪writingPlaceholdersIsHandled(array $changes, bool $expectedException)
Definition: SiteConfigurationTest.php:186
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound
‪resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound()
Definition: SiteConfigurationTest.php:68
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReadsConfiguration
‪resolveAllExistingSitesReadsConfiguration()
Definition: SiteConfigurationTest.php:76
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:48
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writeOnlyWritesModifiedKeys
‪writeOnlyWritesModifiedKeys()
Definition: SiteConfigurationTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingOfNestedStructuresPreservesOrder
‪writingOfNestedStructuresPreservesOrder()
Definition: SiteConfigurationTest.php:126
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:2005
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1836
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingPlaceholdersIsHandledDataProvider
‪static writingPlaceholdersIsHandledDataProvider()
Definition: SiteConfigurationTest.php:162
‪TYPO3\CMS\Core\Tests\Unit\Configuration
Definition: ConfigurationManagerTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: SiteConfigurationTest.php:33
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192