‪TYPO3CMS  ‪main
LinkVarsCalculatorTest.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;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪LinkVarsCalculatorTest extends UnitTestCase
27 {
28  public static function ‪calculateLinkVarsDataProvider(): array
29  {
30  return [
31  'simple variable' => [
32  'L',
33  [
34  'L' => 1,
35  ],
36  '&L=1',
37  ],
38  'missing variable' => [
39  'L',
40  [
41  ],
42  '',
43  ],
44  'restricted variables' => [
45  'L(1-3),bar(3),foo(array),blub(array)',
46  [
47  'L' => 1,
48  'bar' => 2,
49  'foo' => [ 1, 2, 'f' => [ 4, 5 ] ],
50  'blub' => 123,
51  ],
52  '&L=1&foo%5B0%5D=1&foo%5B1%5D=2&foo%5Bf%5D%5B0%5D=4&foo%5Bf%5D%5B1%5D=5',
53  ],
54  'nested variables' => [
55  'bar|foo(1-2)',
56  [
57  'bar' => [
58  'foo' => 1,
59  'unused' => 'never',
60  ],
61  ],
62  '&bar[foo]=1',
63  ],
64  ];
65  }
66 
67  #[DataProvider('calculateLinkVarsDataProvider')]
68  #[Test]
69  public function ‪calculateLinkVarsConsidersCorrectVariables(string $linkVars, array $getVars, string $expected): void
70  {
71  $subject = new ‪LinkVarsCalculator();
72  $result = $subject->getAllowedLinkVarsFromRequest($linkVars, $getVars, new ‪Context());
73  self::assertEquals($expected, $result);
74  }
75 
76  public static function ‪splitLinkVarsDataProvider(): array
77  {
78  return [
79  [
80  'L',
81  ['L'],
82  ],
83  [
84  'L,a',
85  [
86  'L',
87  'a',
88  ],
89  ],
90  [
91  'L, a',
92  [
93  'L',
94  'a',
95  ],
96  ],
97  [
98  'L , a',
99  [
100  'L',
101  'a',
102  ],
103  ],
104  [
105  ' L, a ',
106  [
107  'L',
108  'a',
109  ],
110  ],
111  [
112  'L(1)',
113  [
114  'L(1)',
115  ],
116  ],
117  [
118  'L(1),a',
119  [
120  'L(1)',
121  'a',
122  ],
123  ],
124  [
125  'L(1) , a',
126  [
127  'L(1)',
128  'a',
129  ],
130  ],
131  [
132  'a,L(1)',
133  [
134  'a',
135  'L(1)',
136  ],
137  ],
138  [
139  'L(1),a(2-3)',
140  [
141  'L(1)',
142  'a(2-3)',
143  ],
144  ],
145  [
146  'L(1),a((2-3))',
147  [
148  'L(1)',
149  'a((2-3))',
150  ],
151  ],
152  [
153  'L(1),a(a{2,4})',
154  [
155  'L(1)',
156  'a(a{2,4})',
157  ],
158  ],
159  [
160  'L(1),a(/a{2,4}\,()/)',
161  [
162  'L(1)',
163  'a(/a{2,4}\,()/)',
164  ],
165  ],
166  [
167  'L,a , b(c) , dd(/g{1,2}/), eee(, ()f) , 2',
168  [
169  'L',
170  'a',
171  'b(c)',
172  'dd(/g{1,2}/)',
173  'eee(, ()f)',
174  '2',
175  ],
176  ],
177  ];
178  }
179 
180  #[DataProvider('splitLinkVarsDataProvider')]
181  #[Test]
182  public function ‪splitLinkVarsStringSplitsStringByComma(string $string, array $expected): void
183  {
184  $subject = $this->getAccessibleMock(LinkVarsCalculator::class, null);
185  self::assertEquals($expected, $subject->_call('splitLinkVarsString', $string));
186  }
187 }
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54