File: CoreTest.php

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (466 lines) | stat: -rw-r--r-- 13,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php

namespace Twig\Tests\Extension;

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\CoreExtension;
use Twig\Extension\SandboxExtension;
use Twig\Loader\ArrayLoader;
use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityPolicy;

class CoreTest extends TestCase
{
    #[DataProvider('provideCycleCases')]
    public function testCycleFunction($values, $position, $expected)
    {
        $this->assertSame($expected, CoreExtension::cycle($values, $position));
    }

    public static function provideCycleCases()
    {
        return [
            [[1, 2, 3], 0, 1],
            [[1, 2, 3], 1, 2],
            [[1, 2, 3], 2, 3],
            [[1, 2, 3], 3, 1],
            [[false, 0, null], 0, false],
            [[false, 0, null], 1, 0],
            [[false, 0, null], 2, null],

            [[['a', 'b'], ['c', 'd']], 3, ['c', 'd']],
        ];
    }

    #[DataProvider('provideCycleInvalidCases')]
    public function testCycleFunctionThrowRuntimeError($values, mixed $position = null)
    {
        $this->expectException(RuntimeError::class);
        CoreExtension::cycle($values, $position ?? 0);
    }

    public static function provideCycleInvalidCases()
    {
        return [
            'empty' => [[]],
            'non-countable' => [new class extends \ArrayObject {
            }],
        ];
    }

    #[DataProvider('getRandomFunctionTestData')]
    public function testRandomFunction(array $expectedInArray, $value1, $value2 = null)
    {
        for ($i = 0; $i < 100; ++$i) {
            $this->assertTrue(\in_array(CoreExtension::random('UTF-8', $value1, $value2), $expectedInArray, true)); // assertContains() would not consider the type
        }
    }

    public static function getRandomFunctionTestData()
    {
        return [
            'array' => [
                ['apple', 'orange', 'citrus'],
                ['apple', 'orange', 'citrus'],
            ],
            'Traversable' => [
                ['apple', 'orange', 'citrus'],
                new \ArrayObject(['apple', 'orange', 'citrus']),
            ],
            'unicode string' => [
                ['Ä', '€', 'é'],
                'Ä€é',
            ],
            'numeric but string' => [
                ['1', '2', '3'],
                '123',
            ],
            'integer' => [
                range(0, 5, 1),
                5,
            ],
            'float' => [
                range(0, 5, 1),
                5.9,
            ],
            'negative' => [
                [0, -1, -2],
                -2,
            ],
            'min max int' => [
                range(50, 100),
                50,
                100,
            ],
            'min max float' => [
                range(-10, 10),
                -9.5,
                9.5,
            ],
            'min null' => [
                range(0, 100),
                null,
                100,
            ],
        ];
    }

    public function testRandomFunctionWithoutParameter()
    {
        $max = mt_getrandmax();

        for ($i = 0; $i < 100; ++$i) {
            $val = CoreExtension::random('UTF-8');
            $this->assertTrue(\is_int($val) && $val >= 0 && $val <= $max);
        }
    }

    public function testRandomFunctionReturnsAsIs()
    {
        $this->assertSame('', CoreExtension::random('UTF-8', ''));

        $instance = new \stdClass();
        $this->assertSame($instance, CoreExtension::random('UTF-8', $instance));
    }

    public function testRandomFunctionOfEmptyArrayThrowsException()
    {
        $this->expectException(RuntimeError::class);
        CoreExtension::random('UTF-8', []);
    }

    public function testRandomFunctionOnNonUTF8String()
    {
        $text = iconv('UTF-8', 'ISO-8859-1', 'Äé');
        for ($i = 0; $i < 30; ++$i) {
            $rand = CoreExtension::random('ISO-8859-1', $text);
            $this->assertTrue(\in_array(iconv('ISO-8859-1', 'UTF-8', $rand), ['Ä', 'é'], true));
        }
    }

    public function testReverseFilterOnNonUTF8String()
    {
        $input = iconv('UTF-8', 'ISO-8859-1', 'Äé');
        $output = iconv('ISO-8859-1', 'UTF-8', CoreExtension::reverse('ISO-8859-1', $input));

        $this->assertEquals($output, 'éÄ');
    }

    #[DataProvider('provideTwigFirstCases')]
    public function testTwigFirst($expected, $input)
    {
        $this->assertSame($expected, CoreExtension::first('UTF-8', $input));
    }

    public static function provideTwigFirstCases()
    {
        $i = [1 => 'a', 2 => 'b', 3 => 'c'];

        return [
            ['a', 'abc'],
            [1, [1, 2, 3]],
            ['', null],
            ['', ''],
            ['a', new CoreTestIterator($i, array_keys($i), true, 3)],
        ];
    }

    #[DataProvider('provideTwigLastCases')]
    public function testTwigLast($expected, $input)
    {
        $this->assertSame($expected, CoreExtension::last('UTF-8', $input));
    }

    public static function provideTwigLastCases()
    {
        $i = [1 => 'a', 2 => 'b', 3 => 'c'];

        return [
            ['c', 'abc'],
            [3, [1, 2, 3]],
            ['', null],
            ['', ''],
            ['c', new CoreTestIterator($i, array_keys($i), true)],
        ];
    }

    #[DataProvider('provideArrayKeyCases')]
    public function testArrayKeysFilter(array $expected, $input)
    {
        $this->assertSame($expected, CoreExtension::keys($input));
    }

    public static function provideArrayKeyCases()
    {
        $array = ['a' => 'a1', 'b' => 'b1', 'c' => 'c1'];
        $keys = array_keys($array);

        return [
            [$keys, $array],
            [$keys, new CoreTestIterator($array, $keys)],
            [$keys, new CoreTestIteratorAggregate($array, $keys)],
            [$keys, new CoreTestIteratorAggregateAggregate($array, $keys)],
            [[], null],
            [['a'], new \SimpleXMLElement('<xml><a></a></xml>')],
        ];
    }

    #[DataProvider('provideInFilterCases')]
    public function testInFilter($expected, $value, $compare)
    {
        $this->assertSame($expected, CoreExtension::inFilter($value, $compare));
    }

    public static function provideInFilterCases()
    {
        $array = [1, 2, 'a' => 3, 5, 6, 7];
        $keys = array_keys($array);

        return [
            [true, 1, $array],
            [true, '3', $array],
            [true, '3', 'abc3def'],
            [true, 1, new CoreTestIterator($array, $keys, true, 1)],
            [true, '3', new CoreTestIterator($array, $keys, true, 3)],
            [true, '3', new CoreTestIteratorAggregateAggregate($array, $keys, true, 3)],
            [false, 4, $array],
            [false, 4, new CoreTestIterator($array, $keys, true)],
            [false, 4, new CoreTestIteratorAggregateAggregate($array, $keys, true)],
            [false, 1, 1],
            [true, 'b', new \SimpleXMLElement('<xml><a>b</a></xml>')],
        ];
    }

    #[DataProvider('provideSliceFilterCases')]
    public function testSliceFilter($expected, $input, $start, $length = null, $preserveKeys = false)
    {
        $this->assertSame($expected, CoreExtension::slice('UTF-8', $input, $start, $length, $preserveKeys));
    }

    public static function provideSliceFilterCases()
    {
        $i = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
        $keys = array_keys($i);

        return [
            [['a' => 1], $i, 0, 1, true],
            [['a' => 1], $i, 0, 1, false],
            [['b' => 2, 'c' => 3], $i, 1, 2],
            [[1], [1, 2, 3, 4], 0, 1],
            [[2, 3], [1, 2, 3, 4], 1, 2],
            [[2, 3], new CoreTestIterator($i, $keys, true), 1, 2],
            [['c' => 3, 'd' => 4], new CoreTestIteratorAggregate($i, $keys, true), 2, null, true],
            [$i, new CoreTestIterator($i, $keys, true), 0, \count($keys) + 10, true],
            [[], new CoreTestIterator($i, $keys, true), \count($keys) + 10],
            ['de', 'abcdef', 3, 2],
            [[], new \SimpleXMLElement('<items><item>1</item><item>2</item></items>'), 3],
            [[], new \ArrayIterator([1, 2]), 3],
        ];
    }

    #[DataProvider('provideCompareCases')]
    public function testCompare($expected, $a, $b)
    {
        $this->assertSame($expected, CoreExtension::compare($a, $b));
        $this->assertSame($expected, -CoreExtension::compare($b, $a));
    }

    public function testCompareNAN()
    {
        $this->assertSame(1, CoreExtension::compare(\NAN, 'NAN'));
        $this->assertSame(1, CoreExtension::compare('NAN', \NAN));
        $this->assertSame(1, CoreExtension::compare(\NAN, 'foo'));
        $this->assertSame(1, CoreExtension::compare('foo', \NAN));
    }

    public static function provideCompareCases()
    {
        return [
            [0, 'a', 'a'],

            // from https://wiki.php.net/rfc/string_to_number_comparison
            [0, 0, '0'],
            [0, 0, '0.0'],

            [-1, 0, 'foo'],
            [1, 0, ''],
            [0, 42, '   42'],
            [-1, 42, '42foo'],

            [0, '0', '0'],
            [0, '0', '0.0'],
            [-1, '0', 'foo'],
            [1, '0', ''],
            [0, '42', '   42'],
            [-1, '42', '42foo'],

            [0, 42, '000042'],
            [0, 42, '42.0'],
            [0, 42.0, '+42.0E0'],
            [0, 0, '0e214987142012'],

            [0, '42', '000042'],
            [0, '42', '42.0'],
            [0, '42.0', '+42.0E0'],
            [0, '0', '0e214987142012'],

            [0, 42, '   42'],
            [0, 42, '42   '],
            [-1, 42, '42abc'],
            [-1, 42, 'abc42'],
            [-1, 0, 'abc42'],

            [0, 42.0, '   42.0'],
            [0, 42.0, '42.0   '],
            [-1, 42.0, '42.0abc'],
            [-1, 42.0, 'abc42.0'],
            [-1, 0.0, 'abc42.0'],

            [0, \INF, 'INF'],
            [0, -\INF, '-INF'],
            [0, \INF, '1e1000'],
            [0, -\INF, '-1e1000'],

            [-1, 10, 20],
            [-1, '10', 20],
            [-1, 10, '20'],

            [1, 42, ' foo'],
            [0, 42, "42\f"],
            [1, 42, "\x00\x34\x32"],
        ];
    }

    public function testSandboxedInclude()
    {
        $twig = new Environment(new ArrayLoader([
            'index' => '{{ include("included", sandboxed: true) }}',
            'included' => '{{ "included"|e }}',
        ]));
        $policy = new SecurityPolicy(allowedFunctions: ['include']);
        $sandbox = new SandboxExtension($policy, false);
        $twig->addExtension($sandbox);

        // We expect a compile error
        $this->expectException(SecurityError::class);
        $twig->render('index');
    }

    public function testSandboxedIncludeWithPreloadedTemplate()
    {
        $twig = new Environment(new ArrayLoader([
            'index' => '{{ include("included", sandboxed: true) }}',
            'included' => '{{ "included"|e }}',
        ]));
        $policy = new SecurityPolicy(allowedFunctions: ['include']);
        $sandbox = new SandboxExtension($policy, false);
        $twig->addExtension($sandbox);

        // The template is loaded without the sandbox enabled
        // so, no compile error
        $twig->load('included');

        // We expect a runtime error
        $this->expectException(SecurityError::class);
        $twig->render('index');
    }

    public function testLastModified()
    {
        $this->assertGreaterThan(1000000000, (new CoreExtension())->getLastModified());
    }
}

final class CoreTestIteratorAggregate implements \IteratorAggregate
{
    private $iterator;

    public function __construct(array $array, array $keys, $allowAccess = false, $maxPosition = false)
    {
        $this->iterator = new CoreTestIterator($array, $keys, $allowAccess, $maxPosition);
    }

    public function getIterator(): \Traversable
    {
        return $this->iterator;
    }
}

final class CoreTestIteratorAggregateAggregate implements \IteratorAggregate
{
    private $iterator;

    public function __construct(array $array, array $keys, $allowValueAccess = false, $maxPosition = false)
    {
        $this->iterator = new CoreTestIteratorAggregate($array, $keys, $allowValueAccess, $maxPosition);
    }

    public function getIterator(): \Traversable
    {
        return $this->iterator;
    }
}

final class CoreTestIterator implements \Iterator
{
    private $position;
    private $array;
    private $arrayKeys;
    private $allowValueAccess;
    private $maxPosition;

    public function __construct(array $values, array $keys, $allowValueAccess = false, $maxPosition = false)
    {
        $this->array = $values;
        $this->arrayKeys = $keys;
        $this->position = 0;
        $this->allowValueAccess = $allowValueAccess;
        $this->maxPosition = false === $maxPosition ? \count($values) + 1 : $maxPosition;
    }

    public function rewind(): void
    {
        $this->position = 0;
    }

    #[\ReturnTypeWillChange]
    public function current()
    {
        if ($this->allowValueAccess) {
            return $this->array[$this->key()];
        }

        throw new \LogicException('Code should only use the keys, not the values provided by iterator.');
    }

    #[\ReturnTypeWillChange]
    public function key()
    {
        return $this->arrayKeys[$this->position];
    }

    public function next(): void
    {
        ++$this->position;
        if ($this->position === $this->maxPosition) {
            throw new \LogicException(\sprintf('Code should not iterate beyond %d.', $this->maxPosition));
        }
    }

    public function valid(): bool
    {
        return isset($this->arrayKeys[$this->position]);
    }
}