Translated using Weblate (Slovenian)
[phpmyadmin.git] / test / classes / AdvisorTest.php
blob9c5385b52bbe2ff57793ee081832a218f7deb38f
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\Advisor;
8 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
10 class AdvisorTest extends AbstractTestCase
12 protected function setUp(): void
14 parent::setUp();
15 parent::setGlobalConfig();
16 $GLOBALS['server'] = 1;
19 /**
20 * test for Advisor::byTime
22 * @param float $time time
23 * @param string $expected expected result
25 * @dataProvider advisorTimes
27 public function testAdvisorBytime(float $time, string $expected): void
29 $result = Advisor::byTime($time, 2);
30 $this->assertEquals($expected, $result);
33 public function advisorTimes(): array
35 return [
37 10,
38 '10 per second',
41 0.02,
42 '1.2 per minute',
45 0.003,
46 '10.8 per hour',
49 0.00003,
50 '2.59 per day',
53 0.0000000003,
54 '<0.01 per day',
59 /**
60 * Test for adding rule
62 * @param array $rule Rule to test
63 * @param array $expected Expected rendered rule in fired/errors list
64 * @param string|null $error Expected error string (null if none error expected)
66 * @dataProvider rulesProvider
68 public function testAddRule(array $rule, array $expected, ?string $error): void
70 parent::loadDefaultConfig();
71 parent::setLanguage();
72 $advisor = new Advisor($GLOBALS['dbi'], new ExpressionLanguage());
73 $parseResult = include ROOT_PATH . 'libraries/advisory_rules_generic.php';
74 $this->assertIsArray($parseResult);
75 $this->assertArrayHasKey(0, $parseResult);
76 $this->assertIsArray($parseResult[0]);
77 $advisor->setVariable('value', 0);
78 $advisor->addRule('fired', $rule);
79 $runResult = $advisor->getRunResult();
80 if (isset($runResult['errors']) || $error !== null) {
81 $this->assertEquals([$error], $runResult['errors']);
83 if (! isset($runResult['fired']) && $expected == []) {
84 return;
87 $this->assertEquals([$expected], $runResult['fired']);
90 public function rulesProvider(): array
92 return [
95 'id' => 'Basic',
96 'justification' => 'foo',
97 'name' => 'Basic',
98 'issue' => 'issue',
99 'recommendation' => 'Recommend',
102 'justification' => 'foo',
103 'id' => 'Basic',
104 'name' => 'Basic',
105 'issue' => 'issue',
106 'recommendation' => 'Recommend',
108 null,
112 'id' => 'Variable',
113 'justification' => 'foo',
114 'name' => 'Variable',
115 'issue' => 'issue',
116 'recommendation' => 'Recommend {status_var}',
119 'justification' => 'foo',
120 'id' => 'Variable',
121 'name' => 'Variable',
122 'issue' => 'issue',
123 'recommendation' => 'Recommend <a href="index.php?route=/server/variables&amp;' .
124 'filter=status_var&amp;lang=en">status_var</a>',
126 null,
130 'id' => 'Format',
131 'justification' => '%s foo',
132 'justification_formula' => 'value',
133 'name' => 'Format',
134 'issue' => 'issue',
135 'recommendation' => 'Recommend',
138 'justification' => '0 foo',
139 'justification_formula' => 'value',
140 'id' => 'Format',
141 'name' => 'Format',
142 'issue' => 'issue',
143 'recommendation' => 'Recommend',
145 null,
149 'id' => 'Percent',
150 'justification' => '%s%% foo',
151 'justification_formula' => 'value',
152 'name' => 'Percent',
153 'issue' => 'issue',
154 'recommendation' => 'Recommend',
157 'justification' => '0% foo',
158 'justification_formula' => 'value',
159 'id' => 'Percent',
160 'name' => 'Percent',
161 'issue' => 'issue',
162 'recommendation' => 'Recommend',
164 null,
168 'id' => 'Double',
169 'justification' => '%s%% %d foo',
170 'justification_formula' => 'value, value',
171 'name' => 'Double',
172 'issue' => 'issue',
173 'recommendation' => 'Recommend',
176 'justification' => '0% 0 foo',
177 'justification_formula' => 'value, value',
178 'id' => 'Double',
179 'name' => 'Double',
180 'issue' => 'issue',
181 'recommendation' => 'Recommend',
183 null,
187 'id' => 'Quotes',
188 'justification' => '"\'foo',
189 'name' => 'Quotes',
190 'issue' => 'issue',
191 'recommendation' => 'Recommend"\'',
194 'justification' => '"\'foo',
195 'id' => 'Quotes',
196 'name' => 'Quotes',
197 'issue' => 'issue',
198 'recommendation' => 'Recommend"\'',
200 null,
204 'justification' => 'foo',
205 'justification_formula' => 'fsafdsa',
206 'name' => 'Failure',
207 'issue' => 'issue',
208 'recommendation' => 'Recommend',
211 'Failed formatting string for rule \'Failure\'. ' .
212 'Error when evaluating: Variable "fsafdsa" is not ' .
213 'valid around position 2 for expression `[fsafdsa]`.',
217 'id' => 'Distribution',
218 'justification' => 'Version string (%s)',
219 'justification_formula' => 'value',
220 'name' => 'Distribution',
221 'issue' => 'official MySQL binaries.',
222 'recommendation' => 'See <a href="https://example.com/">web</a>',
225 'justification' => 'Version string (0)',
226 'justification_formula' => 'value',
227 'name' => 'Distribution',
228 'issue' => 'official MySQL binaries.',
229 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
230 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
231 'id' => 'Distribution',
233 null,
237 'id' => 'Distribution',
238 'justification' => 'Timestamp (%s)',
239 'justification_formula' => 'ADVISOR_timespanFormat(1377027)',
240 'name' => 'Distribution',
241 'issue' => 'official MySQL binaries.',
242 'recommendation' => 'See <a href="https://example.com/">web</a>',
245 'justification' => 'Timestamp (15 days, 22 hours, 30 minutes and 27 seconds)',
246 'justification_formula' => 'ADVISOR_timespanFormat(1377027)',
247 'name' => 'Distribution',
248 'issue' => 'official MySQL binaries.',
249 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
250 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
251 'id' => 'Distribution',
253 null,
257 'id' => 'Distribution',
258 'justification' => 'Memory: %s',
259 'justification_formula' => 'ADVISOR_formatByteDown(1000000, 2, 2)',
260 'name' => 'Distribution',
261 'issue' => 'official MySQL binaries.',
262 'recommendation' => 'See <a href="https://example.com/">web</a>',
265 'justification' => 'Memory: 0.95 MiB',
266 'justification_formula' => 'ADVISOR_formatByteDown(1000000, 2, 2)',
267 'name' => 'Distribution',
268 'issue' => 'official MySQL binaries.',
269 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
270 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
271 'id' => 'Distribution',
273 null,
277 'id' => 'Distribution',
278 'justification' => 'Time: %s',
279 'justification_formula' => 'ADVISOR_bytime(0.02, 2)',
280 'name' => 'Distribution',
281 'issue' => 'official MySQL binaries.',
282 'recommendation' => 'See <a href="https://example.com/">web</a>',
285 'justification' => 'Time: 1.2 per minute',
286 'justification_formula' => 'ADVISOR_bytime(0.02, 2)',
287 'name' => 'Distribution',
288 'issue' => 'official MySQL binaries.',
289 'recommendation' => 'See <a href="./url.php?url=https%3A%2F%2F' .
290 'example.com%2F" target="_blank" rel="noopener noreferrer">web</a>',
291 'id' => 'Distribution',
293 null,
297 'id' => 'Minor Version',
298 'justification' => 'Current version: %s',
299 'justification_formula' => 'value',
300 'name' => 'Minor Version',
301 'precondition' => '! fired(\'Release Series\')',
302 'issue' => 'Version less than 5.1.30',
303 'recommendation' => 'You should upgrade',
304 'formula' => 'version',
305 'test' => "substr(value,0,2) <= '5.' && substr(value,2,1) <= 1 && substr(value,4,2) < 30",
308 'justification' => 'Current version: 0',
309 'justification_formula' => 'value',
310 'name' => 'Minor Version',
311 'issue' => 'Version less than 5.1.30',
312 'recommendation' => 'You should upgrade',
313 'id' => 'Minor Version',
314 'precondition' => '! fired(\'Release Series\')',
315 'formula' => 'version',
316 'test' => "substr(value,0,2) <= '5.' && substr(value,2,1) <= 1 && substr(value,4,2) < 30",
318 null,