‪TYPO3CMS  ‪main
SiteWriterTest.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;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪SiteWriterTest 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->siteWriter = new ‪SiteWriter(
53  $this->fixturePath,
55  new ‪NullFrontend('test')
56  );
57  }
58 
59  #[Test]
60  public function ‪writeOnlyWritesModifiedKeys(): void
61  {
62  ‪$identifier = 'testsite';
63  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
64  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config1.yaml';
65  $expected = __DIR__ . '/Fixtures/SiteConfigs/config1_expected.yaml';
66  $siteConfig = $this->fixturePath . '/' . ‪$identifier . '/config.yaml';
67  copy($configFixture, $siteConfig);
68 
69  // load with resolved imports as the module does
70  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
71  ->load(
72  GeneralUtility::fixWindowsFilePath($siteConfig),
74  );
75  // modify something on base level
76  $configuration['base'] = 'https://example.net/';
77  // modify something nested
78  $configuration['languages'][0]['title'] = 'English';
79  // delete values
80  unset($configuration['someOtherValue'], $configuration['languages'][1]);
81 
82  $this->siteWriter->write(‪$identifier, $configuration, true);
83 
84  // expect modified base but intact imports
85  self::assertFileEquals($expected, $siteConfig);
86  }
87 
88  #[Test]
90  {
91  ‪$identifier = 'testsite';
92  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
93  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
94  $expected = __DIR__ . '/Fixtures/SiteConfigs/config2_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  // add new language
105  $languageConfig = [
106  'title' => 'English',
107  'enabled' => true,
108  'languageId' => '0',
109  'base' => '/en',
110  'locale' => 'en_US.utf8',
111  'flag' => 'en',
112  'navigationTitle' => 'English',
113  ];
114  array_unshift($configuration['languages'], $languageConfig);
115  $this->siteWriter->write(‪$identifier, $configuration, true);
116 
117  // expect modified base but intact imports
118  self::assertFileEquals($expected, $siteConfig);
119  }
120 
121  public static function ‪writingPlaceholdersIsHandledDataProvider(): \Generator
122  {
123  yield 'unchanged' => [
124  ['customProperty' => 'Using %env("existing")% variable'],
125  false,
126  ];
127  yield 'removed placeholder variable' => [
128  ['customProperty' => 'Not using any variable'],
129  false,
130  ];
131  yield 'changed raw text only' => [
132  ['customProperty' => 'Using %env("existing")% variable from system environment'],
133  false,
134  ];
135  yield 'added new placeholder variable' => [
136  ['customProperty' => 'Using %env("existing")% and %env("secret")% variable'],
137  true,
138  ];
139  }
140 
141  #[DataProvider('writingPlaceholdersIsHandledDataProvider')]
142  #[Test]
143  public function ‪writingPlaceholdersIsHandled(array $changes, bool $expectedException): void
144  {
145  if ($expectedException) {
146  $this->expectException(SiteConfigurationWriteException::class);
147  $this->expectExceptionCode(1670361271);
148  }
149 
150  ‪$identifier = 'testsite';
151  ‪GeneralUtility::mkdir_deep($this->fixturePath . '/' . ‪$identifier);
152  $configFixture = __DIR__ . '/Fixtures/SiteConfigs/config2.yaml';
153  $siteConfig = $this->fixturePath . '/' . ‪$identifier . '/config.yaml';
154  copy($configFixture, $siteConfig);
155  // load with resolved imports as the module does
156  $configuration = GeneralUtility::makeInstance(YamlFileLoader::class)
157  ->load(
158  GeneralUtility::fixWindowsFilePath($siteConfig),
160  );
161  $configuration = array_merge($configuration, $changes);
162  $this->siteWriter->write(‪$identifier, $configuration, true);
163  }
164 }
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\PROCESS_IMPORTS
‪const PROCESS_IMPORTS
Definition: YamlFileLoader.php:52
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\setUp
‪setUp()
Definition: SiteWriterTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: SiteWriterTest.php:33
‪TYPO3\CMS\Core\Configuration\Exception\SiteConfigurationWriteException
Definition: SiteConfigurationWriteException.php:27
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\$siteWriter
‪SiteWriter $siteWriter
Definition: SiteWriterTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest
Definition: SiteWriterTest.php:32
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:30
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\$fixturePath
‪string $fixturePath
Definition: SiteWriterTest.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1654
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\writingOfNestedStructuresPreservesOrder
‪writingOfNestedStructuresPreservesOrder()
Definition: SiteWriterTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\writingPlaceholdersIsHandledDataProvider
‪static writingPlaceholdersIsHandledDataProvider()
Definition: SiteWriterTest.php:121
‪TYPO3\CMS\Core\Configuration\SiteWriter
Definition: SiteWriter.php:39
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\writingPlaceholdersIsHandled
‪writingPlaceholdersIsHandled(array $changes, bool $expectedException)
Definition: SiteWriterTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Configuration\SiteWriterTest\writeOnlyWritesModifiedKeys
‪writeOnlyWritesModifiedKeys()
Definition: SiteWriterTest.php:60
‪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\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
Definition: CKEditor5MigratorTest.php:18