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
15 parent
::setGlobalConfig();
16 $GLOBALS['server'] = 1;
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
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 == []) {
87 $this->assertEquals([$expected], $runResult['fired']);
90 public function rulesProvider(): array
96 'justification' => 'foo',
99 'recommendation' => 'Recommend',
102 'justification' => 'foo',
106 'recommendation' => 'Recommend',
113 'justification' => 'foo',
114 'name' => 'Variable',
116 'recommendation' => 'Recommend {status_var}',
119 'justification' => 'foo',
121 'name' => 'Variable',
123 'recommendation' => 'Recommend <a href="index.php?route=/server/variables&' .
124 'filter=status_var&lang=en">status_var</a>',
131 'justification' => '%s foo',
132 'justification_formula' => 'value',
135 'recommendation' => 'Recommend',
138 'justification' => '0 foo',
139 'justification_formula' => 'value',
143 'recommendation' => 'Recommend',
150 'justification' => '%s%% foo',
151 'justification_formula' => 'value',
154 'recommendation' => 'Recommend',
157 'justification' => '0% foo',
158 'justification_formula' => 'value',
162 'recommendation' => 'Recommend',
169 'justification' => '%s%% %d foo',
170 'justification_formula' => 'value, value',
173 'recommendation' => 'Recommend',
176 'justification' => '0% 0 foo',
177 'justification_formula' => 'value, value',
181 'recommendation' => 'Recommend',
188 'justification' => '"\'foo',
191 'recommendation' => 'Recommend"\'',
194 'justification' => '"\'foo',
198 'recommendation' => 'Recommend"\'',
204 'justification' => 'foo',
205 'justification_formula' => 'fsafdsa',
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',
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',
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',
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',
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",