‪TYPO3CMS  9.5
SiteConfigurationTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Symfony\Component\Yaml\Yaml;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 class ‪SiteConfigurationTest extends UnitTestCase
31 {
32  protected ‪$resetSingletonInstances = true;
33 
37  protected ‪$siteConfiguration;
38 
44  protected ‪$fixturePath;
45 
52  protected ‪$filesToDelete;
53 
54  protected function ‪setUp(): void
55  {
56  parent::setUp();
57  $basePath = ‪Environment::getVarPath() . '/tests/unit';
58  $this->fixturePath = $basePath . '/fixture/config/sites';
59  if (!file_exists($this->fixturePath)) {
60  GeneralUtility::mkdir_deep($this->fixturePath);
61  }
62  $this->filesToDelete[] = $basePath;
63  $cacheManager = $this->prophesize(CacheManager::class);
64  $cacheManager->getCache('cache_core')->willReturn($this->prophesize(FrontendInterface::class)->reveal());
65  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManager->reveal());
66  $this->subject = new ‪SiteConfiguration($this->fixturePath);
67  $this->siteConfiguration = new ‪SiteConfiguration($this->fixturePath);
68  }
69 
74  {
75  $this->assertEmpty($this->siteConfiguration->resolveAllExistingSites());
76  }
77 
81  public function ‪resolveAllExistingSitesReadsConfiguration(): void
82  {
83  $configuration = [
84  'rootPageId' => 42,
85  'base' => 'https://example.com',
86  ];
87  $yamlFileContents = Yaml::dump($configuration, 99, 2);
88  GeneralUtility::mkdir($this->fixturePath . '/home');
89  GeneralUtility::writeFile($this->fixturePath . '/home/config.yaml', $yamlFileContents);
90  $sites = $this->siteConfiguration->resolveAllExistingSites();
91  $this->assertCount(1, $sites);
92  $currentSite = current($sites);
93  $this->assertSame(42, $currentSite->getRootPageId());
94  $this->assertEquals(new Uri('https://example.com'), $currentSite->getBase());
95  }
96 
100  public function ‪writeOnlyWritesModifiedKeys(): void
101  {
102  $identifier = 'testsite';
103  GeneralUtility::mkdir_deep($this->fixturePath . '/' . $identifier);
104  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config1.yaml';
105  $expected = __DIR__ . '/Fixtures/SiteConfigs/config1_expected.yaml';
106  $siteConfig = $this->fixturePath . '/' . $identifier . '/config.yaml';
107  copy($configFixture, $siteConfig);
108 
109  // load with resolved imports as the module does
110  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
111  ->load(
112  GeneralUtility::fixWindowsFilePath($siteConfig),
114  );
115  // modify something on base level
116  $configuration['base'] = 'https://example.net/';
117  // modify something nested
118  $configuration['languages'][0]['title'] = 'English';
119  // delete values
120  unset($configuration['someOtherValue'], $configuration['languages'][1]);
121 
122  $this->siteConfiguration->write($identifier, $configuration);
123 
124  // expect modified base but intact imports
125  self::assertFileEquals($expected, $siteConfig);
126  }
127 
128  protected function ‪tearDown(): void
129  {
130  // Delete registered test files and directories
131  foreach ($this->filesToDelete as $absoluteFileName) {
132  $absoluteFileName = GeneralUtility::fixWindowsFilePath(‪PathUtility::getCanonicalPath($absoluteFileName));
133  if (!GeneralUtility::validPathStr($absoluteFileName)) {
134  throw new \RuntimeException('tearDown() cleanup: Filename contains illegal characters', 1410633087);
135  }
136  if (strpos($absoluteFileName, ‪Environment::getVarPath()) !== 0) {
137  throw new \RuntimeException(
138  'tearDown() cleanup: Files to delete must be within ' . ‪Environment::getVarPath(),
139  1410633412
140  );
141  }
142  // file_exists returns false for links pointing to not existing targets, so handle links before next check.
143  if (@is_link($absoluteFileName) || @is_file($absoluteFileName)) {
144  unlink($absoluteFileName);
145  } elseif (@is_dir($absoluteFileName)) {
146  GeneralUtility::rmdir($absoluteFileName, true);
147  } else {
148  throw new \RuntimeException('tearDown() cleanup: File, link or directory does not exist', 1410633510);
149  }
150  }
151  $this->filesToDelete = [];
152  parent::tearDown();
153  }
154 
158  public function ‪writingOfNestedStructuresPreservesOrder(): void
159  {
160  $identifier = 'testsite';
161  GeneralUtility::mkdir_deep($this->fixturePath . '/' . $identifier);
162  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
163  $expected = __DIR__ . '/Fixtures/SiteConfigs/config2_expected.yaml';
164  $siteConfig = $this->fixturePath . '/' . $identifier . '/config.yaml';
165  copy($configFixture, $siteConfig);
166 
167  // load with resolved imports as the module does
168  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
169  ->load(
170  GeneralUtility::fixWindowsFilePath($siteConfig),
172  );
173  // add new language
174  $languageConfig = [
175  'title' => 'English',
176  'enabled' => true,
177  'languageId' => '0',
178  'base' => '/en',
179  'typo3Language' => 'default',
180  'locale' => 'en_US.utf8',
181  'iso-639-1' => 'en',
182  'hreflang' => '',
183  'direction' => '',
184  'flag' => 'en',
185  'navigationTitle' => 'English',
186  ];
187  array_unshift($configuration['languages'], $languageConfig);
188  $this->siteConfiguration->write($identifier, $configuration);
189 
190  // expect modified base but intact imports
191  self::assertFileEquals($expected, $siteConfig);
192  }
193 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PROCESS_IMPORTS
‪const PROCESS_IMPORTS
Definition: YamlFileLoader.php:45
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\setUp
‪setUp()
Definition: SiteConfigurationTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$fixturePath
‪string $fixturePath
Definition: SiteConfigurationTest.php:42
‪TYPO3\CMS\Core\Utility\PathUtility\getCanonicalPath
‪static string getCanonicalPath($path)
Definition: PathUtility.php:306
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:38
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:27
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest
Definition: SiteConfigurationTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$filesToDelete
‪array $filesToDelete
Definition: SiteConfigurationTest.php:49
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$siteConfiguration
‪TYPO3 CMS Core Configuration SiteConfiguration $siteConfiguration
Definition: SiteConfigurationTest.php:36
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound
‪resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound()
Definition: SiteConfigurationTest.php:70
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReadsConfiguration
‪resolveAllExistingSitesReadsConfiguration()
Definition: SiteConfigurationTest.php:78
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:41
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\tearDown
‪tearDown()
Definition: SiteConfigurationTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writeOnlyWritesModifiedKeys
‪writeOnlyWritesModifiedKeys()
Definition: SiteConfigurationTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingOfNestedStructuresPreservesOrder
‪writingOfNestedStructuresPreservesOrder()
Definition: SiteConfigurationTest.php:155
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Unit\Configuration
Definition: ConfigurationManagerTest.php:4
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: SiteConfigurationTest.php:32
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165