comparison vendor/nikic/php-parser/test/PhpParser/ConstExprEvaluatorTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php declare(strict_types=1);
2
3 namespace PhpParser;
4
5 use PhpParser\Node\Expr;
6 use PhpParser\Node\Scalar;
7 use PHPUnit\Framework\TestCase;
8
9 class ConstExprEvaluatorTest extends TestCase
10 {
11 /** @dataProvider provideTestEvaluate */
12 public function testEvaluate($exprString, $expected) {
13 $parser = new Parser\Php7(new Lexer());
14 $expr = $parser->parse('<?php ' . $exprString . ';')[0]->expr;
15 $evaluator = new ConstExprEvaluator();
16 $this->assertSame($expected, $evaluator->evaluateDirectly($expr));
17 }
18
19 public function provideTestEvaluate() {
20 return [
21 ['1', 1],
22 ['1.0', 1.0],
23 ['"foo"', "foo"],
24 ['[0, 1]', [0, 1]],
25 ['["foo" => "bar"]', ["foo" => "bar"]],
26 ['NULL', null],
27 ['False', false],
28 ['true', true],
29 ['+1', 1],
30 ['-1', -1],
31 ['~0', -1],
32 ['!true', false],
33 ['[0][0]', 0],
34 ['"a"[0]', "a"],
35 ['true ? 1 : (1/0)', 1],
36 ['false ? (1/0) : 1', 1],
37 ['42 ?: (1/0)', 42],
38 ['false ?: 42', 42],
39 ['false ?? 42', false],
40 ['null ?? 42', 42],
41 ['[0][0] ?? 42', 0],
42 ['[][0] ?? 42', 42],
43 ['0b11 & 0b10', 0b10],
44 ['0b11 | 0b10', 0b11],
45 ['0b11 ^ 0b10', 0b01],
46 ['1 << 2', 4],
47 ['4 >> 2', 1],
48 ['"a" . "b"', "ab"],
49 ['4 + 2', 6],
50 ['4 - 2', 2],
51 ['4 * 2', 8],
52 ['4 / 2', 2],
53 ['4 % 2', 0],
54 ['4 ** 2', 16],
55 ['1 == 1.0', true],
56 ['1 != 1.0', false],
57 ['1 < 2.0', true],
58 ['1 <= 2.0', true],
59 ['1 > 2.0', false],
60 ['1 >= 2.0', false],
61 ['1 <=> 2.0', -1],
62 ['1 === 1.0', false],
63 ['1 !== 1.0', true],
64 ['true && true', true],
65 ['true and true', true],
66 ['false && (1/0)', false],
67 ['false and (1/0)', false],
68 ['false || false', false],
69 ['false or false', false],
70 ['true || (1/0)', true],
71 ['true or (1/0)', true],
72 ['true xor false', true],
73 ];
74 }
75
76 /**
77 * @expectedException \PhpParser\ConstExprEvaluationException
78 * @expectedExceptionMessage Expression of type Expr_Variable cannot be evaluated
79 */
80 public function testEvaluateFails() {
81 $evaluator = new ConstExprEvaluator();
82 $evaluator->evaluateDirectly(new Expr\Variable('a'));
83 }
84
85 public function testEvaluateFallback() {
86 $evaluator = new ConstExprEvaluator(function(Expr $expr) {
87 if ($expr instanceof Scalar\MagicConst\Line) {
88 return 42;
89 }
90 throw new ConstExprEvaluationException();
91 });
92 $expr = new Expr\BinaryOp\Plus(
93 new Scalar\LNumber(8),
94 new Scalar\MagicConst\Line()
95 );
96 $this->assertSame(50, $evaluator->evaluateDirectly($expr));
97 }
98
99 /**
100 * @dataProvider provideTestEvaluateSilently
101 */
102 public function testEvaluateSilently($expr, $exception, $msg) {
103 $evaluator = new ConstExprEvaluator();
104
105 try {
106 $evaluator->evaluateSilently($expr);
107 } catch (ConstExprEvaluationException $e) {
108 $this->assertSame(
109 'An error occurred during constant expression evaluation',
110 $e->getMessage()
111 );
112
113 $prev = $e->getPrevious();
114 $this->assertInstanceOf($exception, $prev);
115 $this->assertSame($msg, $prev->getMessage());
116 }
117 }
118
119 public function provideTestEvaluateSilently() {
120 return [
121 [
122 new Expr\BinaryOp\Mod(new Scalar\LNumber(42), new Scalar\LNumber(0)),
123 \Error::class,
124 'Modulo by zero'
125 ],
126 [
127 new Expr\BinaryOp\Div(new Scalar\LNumber(42), new Scalar\LNumber(0)),
128 \ErrorException::class,
129 'Division by zero'
130 ],
131 ];
132 }
133 }