‪TYPO3CMS  ‪main
YamlFileLoaderTest.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\Test;
21 use Psr\Log\AbstractLogger;
22 use Psr\Log\LogLevel;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 final class ‪YamlFileLoaderTest extends FunctionalTestCase
27 {
28  protected bool ‪$resetSingletonInstances = true;
29 
33  #[Test]
34  public function ‪load(): void
35  {
36  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/Berta.yaml';
37 
38  $expected = [
39  'options' => [
40  'option1',
41  'option2',
42  ],
43  'betterthanbefore' => 1,
44  ];
45  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
46  self::assertSame($expected, ‪$output);
47  }
48 
52  #[Test]
53  public function ‪loadWithAnImport(): void
54  {
55  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithAnImport.yaml';
56 
57  $expected = [
58  'options' => [
59  'optionBefore',
60  'option1',
61  'option2',
62  ],
63  'betterthanbefore' => 1,
64  ];
65 
66  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
67  self::assertSame($expected, ‪$output);
68  }
69 
73  #[Test]
74  public function ‪loadWithMultipleImports(): void
75  {
76  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithMultipleImports.yaml';
77 
78  $expected = [
79  'options' => [
80  'optionBefore',
81  'optionAfterBefore',
82  'option1',
83  'option2',
84  ],
85  'betterthanbefore' => 1,
86  ];
87 
88  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
89  self::assertSame($expected, ‪$output);
90  }
91 
95  #[Test]
96  public function ‪loadWithImportAndRelativePaths(): void
97  {
98  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithImportAndRelativeFiles.yaml';
99  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
100  self::assertSame(
101  [
102  'enable' => [
103  'frontend' => false,
104  'json.api' => true,
105  'backend' => true,
106  'rest.api' => true,
107  ],
108  ],
109  ‪$output
110  );
111  }
112 
116  #[Test]
117  public function ‪loadWithPlaceholders(): void
118  {
119  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithPlaceholders.yaml';
120  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
121 
122  $expected = [
123  'firstset' => [
124  'myinitialversion' => 13,
125  ],
126  'options' => [
127  'option1',
128  'option2',
129  ],
130  'betterthanbefore' => 13,
131  'muchbetterthanbefore' => 'some::option1::option',
132  ];
133 
134  self::assertSame($expected, ‪$output);
135  }
136 
140  #[Test]
141  public function ‪loadWithNestedPlaceholders(): void
142  {
143  $fileName = 'EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithNestedPlaceholders.yaml';
144 
145  $expected = [
146  'firstset' => [
147  'myinitialversion' => 13,
148  ],
149  'options' => [
150  'option1',
151  'option2',
152  ],
153  'betterthanbefore' => 13,
154  ];
155 
156  putenv('foo=%firstset.myinitialversion%');
157  ‪$output = (new ‪YamlFileLoader())->‪load($fileName);
158  putenv('foo=');
159  self::assertSame($expected, ‪$output);
160  }
161 
165  #[Test]
166  public function ‪loadWithImportAndEnvVars(): void
167  {
168  $loader = new ‪YamlFileLoader();
169 
170  putenv('foo=barbaz');
171  ‪$output = $loader->load('EXT:core/Tests/Functional/Configuration/Loader/Fixtures/Env/Berta.yaml');
172  putenv('foo=');
173 
174  $expected = [
175  'loadedWithEnvVars' => 1,
176  'options' => [
177  'optionBefore',
178  'option1',
179  'option2',
180  ],
181  ];
182 
183  self::assertSame($expected, ‪$output);
184  }
185 
189  #[Test]
191  {
192  ‪$output = (new ‪YamlFileLoader())->‪load('EXT:core/Tests/Functional/Configuration/Loader/Fixtures/Placeholder/Berta.yaml');
193 
194  $expected = [
195  'loadedWithPlaceholder' => 1,
196  'settings' => [
197  'dynamicOption' => 'Foo',
198  ],
199  ];
200 
201  self::assertSame($expected, ‪$output);
202  }
203 
207  #[Test]
208  public function ‪loadWithGlobbedImports(): void
209  {
210  ‪$output = (new ‪YamlFileLoader())->‪load('EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithGlobbedImports.yaml');
211 
212  $expected = [
213  'options' => [
214  'optionBefore',
215  'optionAfterBefore',
216  'option1',
217  'option2',
218  ],
219  'betterthanbefore' => 1,
220  ];
221 
222  self::assertSame($expected, ‪$output);
223  }
224 
228  #[Test]
230  {
231  $logger = new class () extends AbstractLogger {
232  public array $logEntries = [];
233 
234  public function log($level, \Stringable|string $message, array $context = []): void
235  {
236  $this->logEntries[$level][0] = $message;
237  }
238  };
239  $loader = new ‪YamlFileLoader();
240  $loader->setLogger($logger);
241  ‪$output = $loader->load('EXT:core/Tests/Functional/Configuration/Loader/Fixtures/LoadWithGlobbedImportsWithPathTraversal.yaml');
242 
243  self::assertEmpty(‪$output);
244  self::assertSame('Referencing a file which is outside of TYPO3s main folder', $logger->logEntries[LogLevel::ERROR][0] ?? '');
245  }
246 }
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithGlobbedImports
‪loadWithGlobbedImports()
Definition: YamlFileLoaderTest.php:208
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: YamlFileLoaderTest.php:28
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithImportAndEnvVars
‪loadWithImportAndEnvVars()
Definition: YamlFileLoaderTest.php:166
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithNestedPlaceholders
‪loadWithNestedPlaceholders()
Definition: YamlFileLoaderTest.php:141
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithPlaceholders
‪loadWithPlaceholders()
Definition: YamlFileLoaderTest.php:117
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithImportAndPlaceholderInFileName
‪loadWithImportAndPlaceholderInFileName()
Definition: YamlFileLoaderTest.php:190
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader
Definition: YamlFileLoaderTest.php:18
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\load
‪load()
Definition: YamlFileLoaderTest.php:34
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithImportAndRelativePaths
‪loadWithImportAndRelativePaths()
Definition: YamlFileLoaderTest.php:96
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest
Definition: YamlFileLoaderTest.php:27
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithMultipleImports
‪loadWithMultipleImports()
Definition: YamlFileLoaderTest.php:74
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithGlobbedImportsWithPathTraversalShouldFail
‪loadWithGlobbedImportsWithPathTraversalShouldFail()
Definition: YamlFileLoaderTest.php:229
‪TYPO3\CMS\Core\Tests\Functional\Configuration\Loader\YamlFileLoaderTest\loadWithAnImport
‪loadWithAnImport()
Definition: YamlFileLoaderTest.php:53