3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use PhpMyAdmin\Message
;
12 * @covers \PhpMyAdmin\Message
14 class MessageTest
extends AbstractTestCase
20 * Sets up the fixture, for example, opens a network connection.
21 * This method is called before a test is executed.
23 protected function setUp(): void
26 $this->object = new Message();
30 * to String casting test
32 public function testToString(): void
34 $this->object->setMessage('test<&>', true);
35 $this->assertEquals('test<&>', (string) $this->object);
41 public function testSuccess(): void
43 $this->object = new Message('test<&>', Message
::SUCCESS
);
44 $this->assertEquals($this->object, Message
::success('test<&>'));
46 'Your SQL query has been executed successfully.',
47 Message
::success()->getString()
54 public function testError(): void
56 $this->object = new Message('test<&>', Message
::ERROR
);
57 $this->assertEquals($this->object, Message
::error('test<&>'));
58 $this->assertEquals('Error', Message
::error()->getString());
64 public function testNotice(): void
66 $this->object = new Message('test<&>', Message
::NOTICE
);
67 $this->assertEquals($this->object, Message
::notice('test<&>'));
71 * test rawError method
73 public function testRawError(): void
75 $this->object = new Message('', Message
::ERROR
);
76 $this->object->setMessage('test<&>');
77 $this->object->setBBCode(false);
79 $this->assertEquals($this->object, Message
::rawError('test<&>'));
83 * test rawNotice method
85 public function testRawNotice(): void
87 $this->object = new Message('', Message
::NOTICE
);
88 $this->object->setMessage('test<&>');
89 $this->object->setBBCode(false);
91 $this->assertEquals($this->object, Message
::rawNotice('test<&>'));
95 * test rawSuccess method
97 public function testRawSuccess(): void
99 $this->object = new Message('', Message
::SUCCESS
);
100 $this->object->setMessage('test<&>');
101 $this->object->setBBCode(false);
103 $this->assertEquals($this->object, Message
::rawSuccess('test<&>'));
107 * testing isSuccess method
109 public function testIsSuccess(): void
111 $this->assertFalse($this->object->isSuccess());
112 $this->assertTrue($this->object->isSuccess(true));
116 * testing isNotice method
118 public function testIsNotice(): void
120 $this->assertTrue($this->object->isNotice());
121 $this->object->isError(true);
122 $this->assertFalse($this->object->isNotice());
123 $this->assertTrue($this->object->isNotice(true));
127 * testing isError method
129 public function testIsError(): void
131 $this->assertFalse($this->object->isError());
132 $this->assertTrue($this->object->isError(true));
136 * testing setter of message
138 public function testSetMessage(): void
140 $this->object->setMessage('test&<>', false);
141 $this->assertEquals('test&<>', $this->object->getMessage());
142 $this->object->setMessage('test&<>', true);
143 $this->assertEquals('test&<>', $this->object->getMessage());
147 * testing setter of string
149 public function testSetString(): void
151 $this->object->setString('test&<>', false);
152 $this->assertEquals('test&<>', $this->object->getString());
153 $this->object->setString('test&<>', true);
154 $this->assertEquals('test&<>', $this->object->getString());
158 * testing add param method
160 public function testAddParam(): void
162 $this->object->addParam(Message
::notice('test'));
164 [Message
::notice('test')],
165 $this->object->getParams()
167 $this->object->addParam('test');
170 Message
::notice('test'),
173 $this->object->getParams()
175 $this->object->addParam('test');
178 Message
::notice('test'),
180 Message
::notice('test'),
182 $this->object->getParams()
187 * Test adding html markup
189 public function testAddParamHtml(): void
191 $this->object->setMessage('Hello %s%s%s');
192 $this->object->addParamHtml('<a href="">');
193 $this->object->addParam('user<>');
194 $this->object->addParamHtml('</a>');
196 'Hello <a href="">user<></a>',
197 $this->object->getMessage()
202 * testing add string method
204 public function testAddString(): void
206 $this->object->addText('test', '*');
210 Message
::notice('test'),
212 $this->object->getAddedMessages()
214 $this->object->addText('test', '');
218 Message
::notice('test'),
219 Message
::notice('test'),
221 $this->object->getAddedMessages()
226 * testing add message method
228 public function testAddMessage(): void
230 $this->object->addText('test<>', '');
232 [Message
::notice('test<>')],
233 $this->object->getAddedMessages()
235 $this->object->addHtml('<b>test</b>');
238 Message
::notice('test<>'),
240 Message
::rawNotice('<b>test</b>'),
242 $this->object->getAddedMessages()
244 $this->object->addMessage(Message
::notice('test<>'));
246 'test<> <b>test</b> test<>',
247 $this->object->getMessage()
252 * testing add messages method
254 public function testAddMessages(): void
257 $messages[] = new Message('Test1');
258 $messages[] = new Message('PMA_Test2', Message
::ERROR
);
259 $messages[] = new Message('Test3');
260 $this->object->addMessages($messages, '');
264 Message
::notice('Test1'),
265 Message
::error('PMA_Test2'),
266 Message
::notice('Test3'),
268 $this->object->getAddedMessages()
273 * testing add messages method
275 public function testAddMessagesString(): void
282 $this->object->addMessagesString($messages, '');
286 Message
::notice('test1'),
287 Message
::notice('test<b>'),
288 Message
::notice('test2'),
290 $this->object->getAddedMessages()
294 'test1test<b>test2',
295 $this->object->getMessage()
300 * testing setter of params
302 public function testSetParams(): void
304 $this->object->setParams(['test&<>']);
305 $this->assertEquals(['test&<>'], $this->object->getParams());
306 $this->object->setParams(['test&<>'], true);
307 $this->assertEquals(['test&<>'], $this->object->getParams());
311 * testing sanitize method
313 public function testSanitize(): void
315 $this->object->setString('test&string<>', false);
317 'test&string<>',
318 Message
::sanitize($this->object)
322 'test&string<>',
323 'test&string<>',
325 Message
::sanitize([$this->object, $this->object])
330 * Data provider for testDecodeBB
332 * @return array Test data
334 public function decodeBBDataProvider(): array
338 '[em]test[/em][em]aa[em/][em]test[/em]',
339 '<em>test</em><em>aa[em/]<em>test</em>',
342 '[strong]test[/strong][strong]test[/strong]',
343 '<strong>test</strong><strong>test</strong>',
346 '[code]test[/code][code]test[/code]',
347 '<code>test</code><code>test</code>',
350 '[kbd]test[/kbd][br][sup]test[/sup]',
351 '<kbd>test</kbd><br><sup>test</sup>',
354 '[a@https://example.com/@Documentation]link[/a]',
355 '<a href="./url.php?url=https%3A%2F%2Fexample.com%2F" target="Documentation">link</a>',
358 '[a@./non-existing@Documentation]link[/a]',
359 '[a@./non-existing@Documentation]link</a>',
362 '[doc@foo]link[/doc]',
363 '<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
364 . 'latest%2Fsetup.html%23foo" '
365 . 'target="documentation">link</a>',
368 '[doc@page@anchor]link[/doc]',
369 '<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
370 . 'latest%2Fpage.html%23anchor" '
371 . 'target="documentation">link</a>',
374 '[doc@faqmysql]link[/doc]',
375 '<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
376 . 'latest%2Ffaq.html%23faqmysql" '
377 . 'target="documentation">link</a>',
383 * testing decodeBB method
385 * @param string $actual BB code string
386 * @param string $expected Expected decoded string
388 * @dataProvider decodeBBDataProvider
390 public function testDecodeBB(string $actual, string $expected): void
392 unset($GLOBALS['server']);
393 $this->assertEquals($expected, Message
::decodeBB($actual));
397 * testing format method
399 public function testFormat(): void
403 Message
::format('test string')
407 Message
::format('test string', 'a')
411 Message
::format('test string', [])
415 Message
::format('%s string', ['test'])
420 * testing getHash method
422 public function testGetHash(): void
424 $this->object->setString('<&>test', false);
425 $this->object->setMessage('<&>test', false);
427 md5(Message
::NOTICE
. '<&>test<&>test'),
428 $this->object->getHash()
433 * getMessage test - with empty message and with non-empty string -
434 * not key in globals additional params are defined
436 public function testGetMessageWithoutMessageWithStringWithParams(): void
438 $this->object->setMessage('');
439 $this->object->setString('test string %s %s');
440 $this->object->addParam('test param 1');
441 $this->object->addParam('test param 2');
443 'test string test param 1 test param 2',
444 $this->object->getMessage()
449 * getMessage test - with empty message and with empty string
451 public function testGetMessageWithoutMessageWithEmptyString(): void
453 $this->object->setMessage('');
454 $this->object->setString('');
455 $this->assertEquals('', $this->object->getMessage());
459 * getMessage test - message is defined
460 * message with BBCode defined
462 public function testGetMessageWithMessageWithBBCode(): void
464 $this->object->setMessage('[kbd]test[/kbd] [doc@cfg_Example]test[/doc]');
466 '<kbd>test</kbd> <a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.'
467 . 'net%2Fen%2Flatest%2Fconfig.html%23cfg_Example"'
468 . ' target="documentation">test</a>',
469 $this->object->getMessage()
476 public function testGetLevel(): void
478 $this->assertEquals('notice', $this->object->getLevel());
479 $this->object->setNumber(Message
::SUCCESS
);
480 $this->assertEquals('success', $this->object->getLevel());
481 $this->object->setNumber(Message
::ERROR
);
482 $this->assertEquals('error', $this->object->getLevel());
488 public function testGetDisplay(): void
490 $this->assertFalse($this->object->isDisplayed());
491 $this->object->setMessage('Test Message');
493 '<div class="alert alert-primary" role="alert">' . "\n"
494 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> Test Message' . "\n"
496 $this->object->getDisplay()
498 $this->assertTrue($this->object->isDisplayed());
504 public function testIsDisplayed(): void
506 $this->assertFalse($this->object->isDisplayed(false));
507 $this->assertTrue($this->object->isDisplayed(true));
508 $this->assertTrue($this->object->isDisplayed(false));
512 * Data provider for testAffectedRows
514 * @return array Test-data
516 public function providerAffectedRows(): array
521 '<div class="alert alert-primary" role="alert">' . "\n"
522 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row affected.' . "\n"
527 '<div class="alert alert-primary" role="alert">' . "\n"
528 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows affected.' . "\n"
533 '<div class="alert alert-primary" role="alert">' . "\n"
534 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 10000 rows affected.' . "\n"
541 * Test for getMessageForAffectedRows() method
543 * @param int $rows Number of rows
544 * @param string $output Expected string
546 * @dataProvider providerAffectedRows
548 public function testAffectedRows(int $rows, string $output): void
550 $this->object = new Message();
551 $msg = $this->object->getMessageForAffectedRows($rows);
552 $this->object->addMessage($msg);
553 $this->assertEquals($output, $this->object->getDisplay());
557 * Data provider for testInsertedRows
559 * @return array Test-data
561 public function providerInsertedRows(): array
566 '<div class="alert alert-primary" role="alert">' . "\n"
567 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row inserted.' . "\n"
572 '<div class="alert alert-primary" role="alert">' . "\n"
573 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows inserted.' . "\n"
578 '<div class="alert alert-primary" role="alert">' . "\n"
579 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 100000 rows inserted.' . "\n"
586 * Test for getMessageForInsertedRows() method
588 * @param int $rows Number of rows
589 * @param string $output Expected string
591 * @dataProvider providerInsertedRows
593 public function testInsertedRows(int $rows, string $output): void
595 $this->object = new Message();
596 $msg = $this->object->getMessageForInsertedRows($rows);
597 $this->object->addMessage($msg);
598 $this->assertEquals($output, $this->object->getDisplay());
602 * Data provider for testDeletedRows
604 * @return array Test-data
606 public function providerDeletedRows(): array
611 '<div class="alert alert-primary" role="alert">' . "\n"
612 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row deleted.' . "\n"
617 '<div class="alert alert-primary" role="alert">' . "\n"
618 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows deleted.' . "\n"
623 '<div class="alert alert-primary" role="alert">' . "\n"
624 . ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 500000 rows deleted.' . "\n"
631 * Test for getMessageForDeletedRows() method
633 * @param int $rows Number of rows
634 * @param string $output Expected string
636 * @dataProvider providerDeletedRows
638 public function testDeletedRows(int $rows, string $output): void
640 $this->object = new Message();
641 $msg = $this->object->getMessageForDeletedRows($rows);
642 $this->object->addMessage($msg);
643 $this->assertEquals($output, $this->object->getDisplay());