‪TYPO3CMS  ‪main
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 Symfony\Component\Yaml\Yaml;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪SiteConfigurationTest extends UnitTestCase
32 {
33  protected bool ‪$resetSingletonInstances = true;
34 
36 
41  protected ?string ‪$fixturePath;
42 
43  protected function ‪setUp(): void
44  {
45  parent::setUp();
46  $basePath = ‪Environment::getVarPath() . '/tests/unit';
47  $this->fixturePath = $basePath . '/fixture/config/sites';
48  if (!file_exists($this->fixturePath)) {
49  ‪GeneralUtility::mkdir_deep($this->fixturePath);
50  }
51  $this->testFilesToDelete[] = $basePath;
52  $this->siteConfiguration = new ‪SiteConfiguration(
53  $this->fixturePath,
55  new ‪NullFrontend('test')
56  );
57  }
58 
63  {
64  self::assertEmpty($this->siteConfiguration->resolveAllExistingSites());
65  }
66 
71  {
72  $configuration = [
73  'rootPageId' => 42,
74  'base' => 'https://example.com',
75  ];
76  $yamlFileContents = Yaml::dump($configuration, 99, 2);
77  ‪GeneralUtility::mkdir($this->fixturePath . '/home');
78  ‪GeneralUtility::writeFile($this->fixturePath . '/home/config.yaml', $yamlFileContents);
79  $sites = $this->siteConfiguration->resolveAllExistingSites();
80  self::assertCount(1, $sites);
81  $currentSite = current($sites);
82  self::assertSame(42, $currentSite->getRootPageId());
83  self::assertEquals(new ‪Uri('https://example.com'), $currentSite->getBase());
84  }
85 
89  public function ‪writeOnlyWritesModifiedKeys(): void
90  {
91  ‪$identifier = 'testsite';
92  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
93  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config1.yaml';
94  $expected = __DIR__ . '/Fixtures/SiteConfigs/config1_expected.yaml';
95  $siteConfig = $this->fixturePath . '/' . ‪$identifier . '/config.yaml';
96  copy($configFixture, $siteConfig);
97 
98  // load with resolved imports as the module does
99  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
100  ->load(
101  GeneralUtility::fixWindowsFilePath($siteConfig),
103  );
104  // modify something on base level
105  $configuration['base'] = 'https://example.net/';
106  // modify something nested
107  $configuration['languages'][0]['title'] = 'English';
108  // delete values
109  unset($configuration['someOtherValue'], $configuration['languages'][1]);
110 
111  $this->siteConfiguration->write(‪$identifier, $configuration, true);
112 
113  // expect modified base but intact imports
114  self::assertFileEquals($expected, $siteConfig);
115  }
116 
121  {
122  ‪$identifier = 'testsite';
123  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
124  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
125  $expected = __DIR__ . '/Fixtures/SiteConfigs/config2_expected.yaml';
126  $siteConfig = $this->fixturePath . '/' . ‪$identifier . '/config.yaml';
127  copy($configFixture, $siteConfig);
128 
129  // load with resolved imports as the module does
130  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
131  ->load(
132  GeneralUtility::fixWindowsFilePath($siteConfig),
134  );
135  // add new language
136  $languageConfig = [
137  'title' => 'English',
138  'enabled' => true,
139  'languageId' => '0',
140  'base' => '/en',
141  'locale' => 'en_US.utf8',
142  'flag' => 'en',
143  'navigationTitle' => 'English',
144  ];
145  array_unshift($configuration['languages'], $languageConfig);
146  $this->siteConfiguration->write(‪$identifier, $configuration, true);
147 
148  // expect modified base but intact imports
149  self::assertFileEquals($expected, $siteConfig);
150  }
151 
152  public static function ‪writingPlaceholdersIsHandledDataProvider(): \Generator
153  {
154  yield 'unchanged' => [
155  ['customProperty' => 'Using %env("existing")% variable'],
156  false,
157  ];
158  yield 'removed placeholder variable' => [
159  ['customProperty' => 'Not using any variable'],
160  false,
161  ];
162  yield 'changed raw text only' => [
163  ['customProperty' => 'Using %env("existing")% variable from system environment'],
164  false,
165  ];
166  yield 'added new placeholder variable' => [
167  ['customProperty' => 'Using %env("existing")% and %env("secret")% variable'],
168  true,
169  ];
170  }
171 
176  public function ‪writingPlaceholdersIsHandled(array $changes, bool $expectedException): void
177  {
178  if ($expectedException) {
179  $this->expectException(SiteConfigurationWriteException::class);
180  $this->expectExceptionCode(1670361271);
181  }
182 
183  ‪$identifier = 'testsite';
184  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
185  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
186  $siteConfig = $this->fixturePath . '/' . ‪$identifier . '/config.yaml';
187  copy($configFixture, $siteConfig);
188  // load with resolved imports as the module does
189  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
190  ->load(
191  GeneralUtility::fixWindowsFilePath($siteConfig),
193  );
194  $configuration = array_merge($configuration, $changes);
195  $this->siteConfiguration->write(‪$identifier, $configuration, true);
196  }
197 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PROCESS_IMPORTS
‪const PROCESS_IMPORTS
Definition: YamlFileLoader.php:52
‪TYPO3\CMS\Core\Configuration\Exception\SiteConfigurationWriteException
Definition: SiteConfigurationWriteException.php:28
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\setUp
‪setUp()
Definition: SiteConfigurationTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$fixturePath
‪string $fixturePath
Definition: SiteConfigurationTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: SiteConfigurationTest.php:33
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪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:1638
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingPlaceholdersIsHandled
‪writingPlaceholdersIsHandled(array $changes, bool $expectedException)
Definition: SiteConfigurationTest.php:176
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\$siteConfiguration
‪SiteConfiguration $siteConfiguration
Definition: SiteConfigurationTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound
‪resolveAllExistingSitesReturnsEmptyArrayForNoSiteConfigsFound()
Definition: SiteConfigurationTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\resolveAllExistingSitesReadsConfiguration
‪resolveAllExistingSitesReadsConfiguration()
Definition: SiteConfigurationTest.php:70
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writeOnlyWritesModifiedKeys
‪writeOnlyWritesModifiedKeys()
Definition: SiteConfigurationTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingOfNestedStructuresPreservesOrder
‪writingOfNestedStructuresPreservesOrder()
Definition: SiteConfigurationTest.php:120
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:1621
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1452
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteConfigurationTest\writingPlaceholdersIsHandledDataProvider
‪static writingPlaceholdersIsHandledDataProvider()
Definition: SiteConfigurationTest.php:152
‪TYPO3\CMS\Core\Tests\Unit\Configuration
Definition: CKEditor5MigratorTest.php:18