‪TYPO3CMS  ‪main
SysTemplateTreeBuilderTest.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;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 final class ‪SysTemplateTreeBuilderTest extends FunctionalTestCase
34 {
36 
40  protected const ‪LANGUAGE_PRESETS = [
41  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
42  ];
43 
44  public function ‪setUp(): void
45  {
46  parent::setUp();
47  // Register custom comparator to compare IncludeTree with its unserialized(serialized())
48  // representation since we don't serialize all properties. Custom comparators are
49  // unregistered after test by phpunit runBare() automatically.
50  $this->registerComparator(new ‪UnserializedIncludeTreeObjectComparator());
51  $this->get(CacheManager::class)->getCache('typoscript')->flush();
53  'website-local',
54  [
55  'rootPageId' => 1,
56  'base' => 'http://localhost/',
57  'settings' => [
58  'testConstantFromSite' => 'testValueFromSite',
59  'nestedConfiguration' => [
60  'foo' => 'bar',
61  ],
62  ],
63  ],
64  [
65  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
66  ]
67  );
68  }
69 
70  #[Test]
71  public function ‪singleRootTemplate(): void
72  {
73  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/singleRootTemplate.csv');
74  $rootline = [
75  [
76  'uid' => 1,
77  'pid' => 0,
78  'is_siteroot' => 0,
79  ],
80  ];
81  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
82  $subject = $this->get(SysTemplateTreeBuilder::class);
83  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
84  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
85  $ast = $this->‪getAst($includeTree);
86  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
87  }
88 
89  #[Test]
90  public function ‪singleRootTemplateLoadsFromGlobals(): void
91  {
92  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/singleRootTemplate.csv');
93  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_constants'] = 'bar = barValue';
94  $rootline = [
95  [
96  'uid' => 1,
97  'pid' => 0,
98  'is_siteroot' => 0,
99  ],
100  ];
101  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
102  $subject = $this->get(SysTemplateTreeBuilder::class);
103  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
104  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
105  $ast = $this->‪getAst($includeTree);
106  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
107  self::assertSame('barValue', $ast->getChildByName('bar')->getValue());
108  }
109 
110  #[Test]
111  public function ‪singleRootTemplateLoadConstantFromSite(): void
112  {
113  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/singleRootTemplate.csv');
114  $rootline = [
115  [
116  'uid' => 1,
117  'pid' => 0,
118  'is_siteroot' => 0,
119  ],
120  ];
121  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
122  $siteFinder = $this->get(SiteFinder::class);
123  $subject = $this->get(SysTemplateTreeBuilder::class);
124  $includeTree = $subject->getTreeBySysTemplateRowsAndSite(
125  'constants',
126  $sysTemplateRepository->getSysTemplateRowsByRootline($rootline),
127  new ‪LossyTokenizer(),
128  $siteFinder->getSiteByPageId(1)
129  );
130  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
131  $ast = $this->‪getAst($includeTree);
132  self::assertSame('testValueFromSite', $ast->getChildByName('testConstantFromSite')->getValue());
133  }
134 
135  #[Test]
136  public function ‪twoPagesTwoTemplates(): void
137  {
138  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/twoPagesTwoTemplates.csv');
139  $rootline = [
140  [
141  'uid' => 2,
142  'pid' => 1,
143  'is_siteroot' => 0,
144  ],
145  [
146  'uid' => 1,
147  'pid' => 0,
148  'is_siteroot' => 0,
149  ],
150  ];
151  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
152  $subject = $this->get(SysTemplateTreeBuilder::class);
153  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
154  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
155  $ast = $this->‪getAst($includeTree);
156  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
157  self::assertSame('barValue', $ast->getChildByName('bar')->getValue());
158  }
159 
160  #[Test]
162  {
163  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/twoPagesTwoTemplatesNoClearForConstants.csv');
164  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_constants'] = 'globalsConstant = globalsConstantValue';
165  $rootline = [
166  [
167  'uid' => 2,
168  'pid' => 1,
169  'is_siteroot' => 0,
170  ],
171  [
172  'uid' => 1,
173  'pid' => 0,
174  'is_siteroot' => 0,
175  ],
176  ];
177  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
178  $subject = $this->get(SysTemplateTreeBuilder::class);
179  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
180  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
181  $ast = $this->‪getAst($includeTree);
182  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
183  self::assertSame('barValue', $ast->getChildByName('bar')->getValue());
184  self::assertSame('globalsConstantValue', $ast->getChildByName('globalsConstant')->getValue());
185  }
186 
187  #[Test]
189  {
190  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/twoPagesTwoTemplatesNoClearForSetup.csv');
191  ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup'] = 'globalsKey = globalsValue';
192  $rootline = [
193  [
194  'uid' => 2,
195  'pid' => 1,
196  'is_siteroot' => 0,
197  ],
198  [
199  'uid' => 1,
200  'pid' => 0,
201  'is_siteroot' => 0,
202  ],
203  ];
204  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
205  $subject = $this->get(SysTemplateTreeBuilder::class);
206  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('setup', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
207  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
208  $ast = $this->‪getAst($includeTree);
209  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
210  self::assertSame('barValue', $ast->getChildByName('bar')->getValue());
211  self::assertSame('globalsValue', $ast->getChildByName('globalsKey')->getValue());
212  }
213 
214  #[Test]
215  public function ‪twoPagesTwoTemplatesBothClear(): void
216  {
217  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/twoPagesTwoTemplatesBothClear.csv');
218  $rootline = [
219  [
220  'uid' => 2,
221  'pid' => 1,
222  'is_siteroot' => 0,
223  ],
224  [
225  'uid' => 1,
226  'pid' => 0,
227  'is_siteroot' => 0,
228  ],
229  ];
230  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
231  $subject = $this->get(SysTemplateTreeBuilder::class);
232  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
233  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
234  $ast = $this->‪getAst($includeTree);
235  self::assertNull($ast->getChildByName('foo'));
236  self::assertSame('barValue', $ast->getChildByName('bar')->getValue());
237  }
238 
239  #[Test]
241  {
242  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/twoTemplatesOnPage.csv');
243  $rootline = [
244  [
245  'uid' => 1,
246  'pid' => 0,
247  'is_siteroot' => 0,
248  ],
249  ];
250  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
251  $subject = $this->get(SysTemplateTreeBuilder::class);
252  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
253  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
254  $ast = $this->‪getAst($includeTree);
255  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
256  self::assertNull($ast->getChildByName('bar'));
257  }
258 
259  #[Test]
260  public function ‪basedOnSimple(): void
261  {
262  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/basedOnSimple.csv');
263  $rootline = [
264  [
265  'uid' => 1,
266  'pid' => 0,
267  'is_siteroot' => 0,
268  ],
269  ];
270  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
271  $subject = $this->get(SysTemplateTreeBuilder::class);
272  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
273  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
274  $ast = $this->‪getAst($includeTree);
275  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
276  self::assertSame('loadedByBasedOn', $ast->getChildByName('bar')->getValue());
277  }
278 
279  #[Test]
280  public function ‪basedOnAfterIncludeStatic(): void
281  {
282  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/basedOnAfterIncludeStatic.csv');
283  $rootline = [
284  [
285  'uid' => 1,
286  'pid' => 0,
287  'is_siteroot' => 0,
288  ],
289  ];
290  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
291  $subject = $this->get(SysTemplateTreeBuilder::class);
292  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
293  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
294  $ast = $this->‪getAst($includeTree);
295  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
296  self::assertSame('loadedByBasedOn', $ast->getChildByName('bar')->getValue());
297  }
298 
299  #[Test]
300  public function ‪basedOnBeforeIncludeStatic(): void
301  {
302  $this->importCSVDataSet(__DIR__ . '/Fixtures/SysTemplate/basedOnBeforeIncludeStatic.csv');
303  $rootline = [
304  [
305  'uid' => 1,
306  'pid' => 0,
307  'is_siteroot' => 0,
308  ],
309  ];
310  $sysTemplateRepository = $this->get(SysTemplateRepository::class);
311  $subject = $this->get(SysTemplateTreeBuilder::class);
312  $includeTree = $subject->getTreeBySysTemplateRowsAndSite('constants', $sysTemplateRepository->getSysTemplateRowsByRootline($rootline), new ‪LossyTokenizer());
313  self::assertEquals($includeTree, unserialize(serialize($includeTree)));
314  $ast = $this->‪getAst($includeTree);
315  self::assertSame('fooValue', $ast->getChildByName('foo')->getValue());
316  self::assertSame('includeStaticTarget', $ast->getChildByName('bar')->getValue());
317  }
318 
324  private function ‪getAst(‪RootInclude $rootInclude): ‪RootNode
325  {
326  $astBuilderVisitor = $this->get(IncludeTreeAstBuilderVisitor::class);
327  $traverser = new ‪IncludeTreeTraverser();
328  $traverser->traverse($rootInclude, [$astBuilderVisitor]);
329  return $astBuilderVisitor->getAst();
330  }
331 }
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: SysTemplateTreeBuilderTest.php:39
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\getAst
‪getAst(RootInclude $rootInclude)
Definition: SysTemplateTreeBuilderTest.php:323
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\UnserializedIncludeTreeObjectComparator
Definition: UnserializedIncludeTreeObjectComparator.php:29
‪TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateRepository
Definition: SysTemplateRepository.php:38
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\twoPagesTwoTemplatesWithoutClearForConstantsStillLoadsFromGlobals
‪twoPagesTwoTemplatesWithoutClearForConstantsStillLoadsFromGlobals()
Definition: SysTemplateTreeBuilderTest.php:160
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Traverser\IncludeTreeTraverser
Definition: IncludeTreeTraverser.php:30
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\setUp
‪setUp()
Definition: SysTemplateTreeBuilderTest.php:43
‪TYPO3\CMS\Core\TypoScript\IncludeTree\Visitor\IncludeTreeAstBuilderVisitor
Definition: IncludeTreeAstBuilderVisitor.php:39
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\basedOnSimple
‪basedOnSimple()
Definition: SysTemplateTreeBuilderTest.php:259
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\singleRootTemplateLoadConstantFromSite
‪singleRootTemplateLoadConstantFromSite()
Definition: SysTemplateTreeBuilderTest.php:110
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\singleRootTemplateLoadsFromGlobals
‪singleRootTemplateLoadsFromGlobals()
Definition: SysTemplateTreeBuilderTest.php:89
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\twoPagesTwoTemplatesBothClear
‪twoPagesTwoTemplatesBothClear()
Definition: SysTemplateTreeBuilderTest.php:214
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\basedOnAfterIncludeStatic
‪basedOnAfterIncludeStatic()
Definition: SysTemplateTreeBuilderTest.php:279
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\twoPagesTwoTemplates
‪twoPagesTwoTemplates()
Definition: SysTemplateTreeBuilderTest.php:135
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\twoPagesTwoTemplatesWithoutClearForSetupStillLoadsFromGlobals
‪twoPagesTwoTemplatesWithoutClearForSetupStillLoadsFromGlobals()
Definition: SysTemplateTreeBuilderTest.php:187
‪TYPO3\CMS\Core\TypoScript\IncludeTree\IncludeNode\RootInclude
Definition: RootInclude.php:27
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\twoTemplatesOnPagePrefersTheOneWithLowerSorting
‪twoTemplatesOnPagePrefersTheOneWithLowerSorting()
Definition: SysTemplateTreeBuilderTest.php:239
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\singleRootTemplate
‪singleRootTemplate()
Definition: SysTemplateTreeBuilderTest.php:70
‪TYPO3\CMS\Core\TypoScript\Tokenizer\LossyTokenizer
Definition: LossyTokenizer.php:57
‪TYPO3\CMS\Core\TypoScript\IncludeTree\SysTemplateTreeBuilder
Definition: SysTemplateTreeBuilder.php:74
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest
Definition: SysTemplateTreeBuilderTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\TypoScript\IncludeTree\SysTemplateTreeBuilderTest\basedOnBeforeIncludeStatic
‪basedOnBeforeIncludeStatic()
Definition: SysTemplateTreeBuilderTest.php:299