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