[TASK] Streamline expressionLanguage usage in core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / ExpressionLanguage / ResolverTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\ExpressionLanguage;
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
18 use Prophecy\Argument;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
20 use TYPO3\CMS\Core\Cache\CacheManager;
21 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
22 use TYPO3\CMS\Core\ExpressionLanguage\DefaultProvider;
23 use TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider;
24 use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
25 use TYPO3\CMS\Core\Http\ServerRequest;
26 use TYPO3\CMS\Core\Package\PackageInterface;
27 use TYPO3\CMS\Core\Package\PackageManager;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30
31 /**
32 * Class ResolverTest
33 */
34 class ResolverTest extends UnitTestCase
35 {
36 public function setUp()
37 {
38 parent::setUp();
39 $this->resetSingletonInstances = true;
40
41 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
42 $cacheFrontendProphecy->has(Argument::any())->willReturn(false);
43 $cacheFrontendProphecy->set(Argument::any(), Argument::any())->willReturn(null);
44 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
45 $cacheManagerProphecy->getCache('cache_core')->willReturn($cacheFrontendProphecy->reveal());
46 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
47
48 $packageManagerProphecy = $this->prophesize(PackageManager::class);
49 $corePackageProphecy = $this->prophesize(PackageInterface::class);
50 $corePackageProphecy->getPackagePath()->willReturn(__DIR__ . '/../../../../../../../sysext/core/');
51 $packageManagerProphecy->getActivePackages()->willReturn([
52 $corePackageProphecy->reveal()
53 ]);
54 GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
55 }
56
57 /**
58 * @return array
59 */
60 public function basicExpressionsDataHandler(): array
61 {
62 return [
63 '1+1' => ['1+1', true],
64 '1 < 2' => ['1 < 2', true],
65 '2 < 1' => ['2 < 1', false],
66 'true' => ['true', true],
67 'false' => ['false', false],
68 'true != false' => ['true != false', true],
69 'true < false' => ['true < false', false],
70 ];
71 }
72
73 /**
74 * @test
75 * @dataProvider basicExpressionsDataHandler
76 * @param string $expression
77 * @param mixed $expectedResult
78 */
79 public function basicExpressionHandlingResultsWorksAsExpected(string $expression, $expectedResult)
80 {
81 $request = new ServerRequest();
82 $expressionLanguageResolver = new Resolver('default', [], $request);
83 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
84 }
85
86 /**
87 * @return array
88 */
89 public function basicExpressionsWithVariablesDataHandler(): array
90 {
91 return [
92 'var1 + var2' => ['var1 + var2', true],
93 'var1 < var2' => ['var1 < var2', true],
94 'var2 < var1' => ['var2 < var1', false],
95 'varTrue' => ['varTrue', true],
96 'varFalse' => ['varFalse', false],
97 'varTrue != varFalse' => ['varTrue != varFalse', true],
98 'varTrue < varFalse' => ['varTrue < varFalse', false],
99 ];
100 }
101
102 /**
103 * @test
104 * @dataProvider basicExpressionsWithVariablesDataHandler
105 * @param string $expression
106 * @param mixed $expectedResult
107 */
108 public function basicExpressionHandlingWithCustomVariablesWorksAsExpected(string $expression, $expectedResult)
109 {
110 $contextProphecy = $this->prophesize(DefaultProvider::class);
111 $contextProphecy->getExpressionLanguageProviders()->willReturn([]);
112 $contextProphecy->getExpressionLanguageVariables()->willReturn([
113 'var1' => '1',
114 'var2' => '2',
115 'varTrue' => true,
116 'varFalse' => false,
117 ]);
118 $request = new ServerRequest();
119 GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
120 $expressionLanguageResolver = new Resolver('default', [], $request);
121 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
122 }
123
124 /**
125 * @return array
126 */
127 public function basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler(): array
128 {
129 return [
130 'testMeLowercase(var1) == var2' => ['testMeLowercase(var1) == var2', true],
131 'testMeLowercase(var2) == var1' => ['testMeLowercase(var2) == var1', false],
132 'testMeLowercase(var1) == var1' => ['testMeLowercase(var1) == var1', false],
133 'testMeLowercase(var2) == var2' => ['testMeLowercase(var2) == var2', true],
134 ];
135 }
136
137 /**
138 * @test
139 * @dataProvider basicExpressionsWithVariablesAndExpressionLanguageProviderDataHandler
140 * @param string $expression
141 * @param mixed $expectedResult
142 */
143 public function basicExpressionHandlingWithCustomVariablesAndExpressionLanguageProviderWorksAsExpected(string $expression, $expectedResult)
144 {
145 $expressionProvider = $this->prophesize(DefaultFunctionsProvider::class);
146 $expressionProvider->getFunctions()->willReturn([
147 new ExpressionFunction('testMeLowercase', function ($str) {
148 return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
149 }, function ($arguments, $str) {
150 return is_string($str) ? strtolower($str) : $str;
151 })
152 ]);
153 $contextProphecy = $this->prophesize(DefaultProvider::class);
154 $contextProphecy->getExpressionLanguageProviders()->willReturn([DefaultFunctionsProvider::class]);
155 $contextProphecy->getExpressionLanguageVariables()->willReturn([
156 'var1' => 'FOO',
157 'var2' => 'foo'
158 ]);
159 $request = new ServerRequest();
160 GeneralUtility::addInstance(DefaultProvider::class, $contextProphecy->reveal());
161 GeneralUtility::addInstance(DefaultFunctionsProvider::class, $expressionProvider->reveal());
162 $expressionLanguageResolver = new Resolver('default', [], $request);
163 $this->assertSame($expectedResult, $expressionLanguageResolver->evaluate($expression));
164 }
165 }