1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Message;
use function md5;
/**
* @covers \PhpMyAdmin\Message
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Message::class)]
class MessageTest extends AbstractTestCase
{
/** @var Message */
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
parent::setUp();
$this->object = new Message();
}
/**
* to String casting test
*/
public function testToString(): void
{
$this->object->setMessage('test<&>', true);
self::assertSame('test<&>', (string) $this->object);
}
/**
* test success method
*/
public function testSuccess(): void
{
$this->object = new Message('test<&>', Message::SUCCESS);
self::assertEquals($this->object, Message::success('test<&>'));
self::assertSame('Your SQL query has been executed successfully.', Message::success()->getString());
}
/**
* test error method
*/
public function testError(): void
{
$this->object = new Message('test<&>', Message::ERROR);
self::assertEquals($this->object, Message::error('test<&>'));
self::assertSame('Error', Message::error()->getString());
}
/**
* test notice method
*/
public function testNotice(): void
{
$this->object = new Message('test<&>', Message::NOTICE);
self::assertEquals($this->object, Message::notice('test<&>'));
}
/**
* test rawError method
*/
public function testRawError(): void
{
$this->object = new Message('', Message::ERROR);
$this->object->setMessage('test<&>');
$this->object->setBBCode(false);
self::assertEquals($this->object, Message::rawError('test<&>'));
}
/**
* test rawNotice method
*/
public function testRawNotice(): void
{
$this->object = new Message('', Message::NOTICE);
$this->object->setMessage('test<&>');
$this->object->setBBCode(false);
self::assertEquals($this->object, Message::rawNotice('test<&>'));
}
/**
* test rawSuccess method
*/
public function testRawSuccess(): void
{
$this->object = new Message('', Message::SUCCESS);
$this->object->setMessage('test<&>');
$this->object->setBBCode(false);
self::assertEquals($this->object, Message::rawSuccess('test<&>'));
}
/**
* testing isSuccess method
*/
public function testIsSuccess(): void
{
self::assertFalse($this->object->isSuccess());
self::assertTrue($this->object->isSuccess(true));
}
/**
* testing isNotice method
*/
public function testIsNotice(): void
{
self::assertTrue($this->object->isNotice());
$this->object->isError(true);
self::assertFalse($this->object->isNotice());
self::assertTrue($this->object->isNotice(true));
}
/**
* testing isError method
*/
public function testIsError(): void
{
self::assertFalse($this->object->isError());
self::assertTrue($this->object->isError(true));
}
/**
* testing setter of message
*/
public function testSetMessage(): void
{
$this->object->setMessage('test&<>', false);
self::assertSame('test&<>', $this->object->getMessage());
$this->object->setMessage('test&<>', true);
self::assertSame('test&<>', $this->object->getMessage());
}
/**
* testing setter of string
*/
public function testSetString(): void
{
$this->object->setString('test&<>', false);
self::assertSame('test&<>', $this->object->getString());
$this->object->setString('test&<>', true);
self::assertSame('test&<>', $this->object->getString());
}
/**
* testing add param method
*/
public function testAddParam(): void
{
$this->object->addParam(Message::notice('test'));
self::assertEquals([Message::notice('test')], $this->object->getParams());
$this->object->addParam('test');
self::assertEquals([
Message::notice('test'),
'test',
], $this->object->getParams());
$this->object->addParam('test');
self::assertEquals([
Message::notice('test'),
'test',
Message::notice('test'),
], $this->object->getParams());
}
/**
* Test adding html markup
*/
public function testAddParamHtml(): void
{
$this->object->setMessage('Hello %s%s%s');
$this->object->addParamHtml('<a href="">');
$this->object->addParam('user<>');
$this->object->addParamHtml('</a>');
self::assertSame('Hello <a href="">user<></a>', $this->object->getMessage());
}
/**
* testing add string method
*/
public function testAddString(): void
{
$this->object->addText('test', '*');
self::assertEquals([
'*',
Message::notice('test'),
], $this->object->getAddedMessages());
$this->object->addText('test', '');
self::assertEquals([
'*',
Message::notice('test'),
Message::notice('test'),
], $this->object->getAddedMessages());
}
/**
* testing add message method
*/
public function testAddMessage(): void
{
$this->object->addText('test<>', '');
self::assertEquals([Message::notice('test<>')], $this->object->getAddedMessages());
$this->object->addHtml('<b>test</b>');
self::assertEquals([
Message::notice('test<>'),
' ',
Message::rawNotice('<b>test</b>'),
], $this->object->getAddedMessages());
$this->object->addMessage(Message::notice('test<>'));
self::assertSame('test<> <b>test</b> test<>', $this->object->getMessage());
}
/**
* testing add messages method
*/
public function testAddMessages(): void
{
$messages = [];
$messages[] = new Message('Test1');
$messages[] = new Message('PMA_Test2', Message::ERROR);
$messages[] = new Message('Test3');
$this->object->addMessages($messages, '');
self::assertEquals([
Message::notice('Test1'),
Message::error('PMA_Test2'),
Message::notice('Test3'),
], $this->object->getAddedMessages());
}
/**
* testing add messages method
*/
public function testAddMessagesString(): void
{
$messages = [
'test1',
'test<b>',
'test2',
];
$this->object->addMessagesString($messages, '');
self::assertEquals([
Message::notice('test1'),
Message::notice('test<b>'),
Message::notice('test2'),
], $this->object->getAddedMessages());
self::assertSame('test1test<b>test2', $this->object->getMessage());
}
/**
* testing setter of params
*/
public function testSetParams(): void
{
$this->object->setParams(['test&<>']);
self::assertSame(['test&<>'], $this->object->getParams());
$this->object->setParams(['test&<>'], true);
self::assertSame(['test&<>'], $this->object->getParams());
}
/**
* testing sanitize method
*/
public function testSanitize(): void
{
$this->object->setString('test&string<>', false);
self::assertSame('test&string<>', Message::sanitize($this->object));
self::assertSame([
'test&string<>',
'test&string<>',
], Message::sanitize([$this->object, $this->object]));
}
/**
* Data provider for testDecodeBB
*
* @return array Test data
*/
public static function decodeBBDataProvider(): array
{
return [
[
'[em]test[/em][em]aa[em/][em]test[/em]',
'<em>test</em><em>aa[em/]<em>test</em>',
],
[
'[strong]test[/strong][strong]test[/strong]',
'<strong>test</strong><strong>test</strong>',
],
[
'[code]test[/code][code]test[/code]',
'<code>test</code><code>test</code>',
],
[
'[kbd]test[/kbd][br][sup]test[/sup]',
'<kbd>test</kbd><br><sup>test</sup>',
],
[
'[a@https://example.com/@Documentation]link[/a]',
'<a href="./url.php?url=https%3A%2F%2Fexample.com%2F" target="Documentation">link</a>',
],
[
'[a@./non-existing@Documentation]link[/a]',
'[a@./non-existing@Documentation]link</a>',
],
[
'[doc@foo]link[/doc]',
'<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
. 'latest%2Fsetup.html%23foo" '
. 'target="documentation">link</a>',
],
[
'[doc@page@anchor]link[/doc]',
'<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
. 'latest%2Fpage.html%23anchor" '
. 'target="documentation">link</a>',
],
[
'[doc@faqmysql]link[/doc]',
'<a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2F'
. 'latest%2Ffaq.html%23faqmysql" '
. 'target="documentation">link</a>',
],
];
}
/**
* testing decodeBB method
*
* @param string $actual BB code string
* @param string $expected Expected decoded string
*
* @dataProvider decodeBBDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('decodeBBDataProvider')]
public function testDecodeBB(string $actual, string $expected): void
{
unset($GLOBALS['server']);
self::assertSame($expected, Message::decodeBB($actual));
}
/**
* testing format method
*/
public function testFormat(): void
{
self::assertSame('test string', Message::format('test string'));
self::assertSame('test string', Message::format('test string', 'a'));
self::assertSame('test string', Message::format('test string', []));
self::assertSame('test string', Message::format('%s string', ['test']));
}
/**
* testing getHash method
*/
public function testGetHash(): void
{
$this->object->setString('<&>test', false);
$this->object->setMessage('<&>test', false);
self::assertSame(md5(Message::NOTICE . '<&>test<&>test'), $this->object->getHash());
}
/**
* getMessage test - with empty message and with non-empty string -
* not key in globals additional params are defined
*/
public function testGetMessageWithoutMessageWithStringWithParams(): void
{
$this->object->setMessage('');
$this->object->setString('test string %s %s');
$this->object->addParam('test param 1');
$this->object->addParam('test param 2');
self::assertSame('test string test param 1 test param 2', $this->object->getMessage());
}
/**
* getMessage test - with empty message and with empty string
*/
public function testGetMessageWithoutMessageWithEmptyString(): void
{
$this->object->setMessage('');
$this->object->setString('');
self::assertSame('', $this->object->getMessage());
}
/**
* getMessage test - message is defined
* message with BBCode defined
*/
public function testGetMessageWithMessageWithBBCode(): void
{
$this->object->setMessage('[kbd]test[/kbd] [doc@cfg_Example]test[/doc]');
self::assertSame('<kbd>test</kbd> <a href="./url.php?url=https%3A%2F%2Fdocs.phpmyadmin.'
. 'net%2Fen%2Flatest%2Fconfig.html%23cfg_Example"'
. ' target="documentation">test</a>', $this->object->getMessage());
}
/**
* getLevel test
*/
public function testGetLevel(): void
{
self::assertSame('notice', $this->object->getLevel());
$this->object->setNumber(Message::SUCCESS);
self::assertSame('success', $this->object->getLevel());
$this->object->setNumber(Message::ERROR);
self::assertSame('error', $this->object->getLevel());
}
/**
* getDisplay test
*/
public function testGetDisplay(): void
{
self::assertFalse($this->object->isDisplayed());
$this->object->setMessage('Test Message');
self::assertSame('<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> Test Message' . "\n"
. '</div>' . "\n", $this->object->getDisplay());
self::assertTrue($this->object->isDisplayed());
}
/**
* isDisplayed test
*/
public function testIsDisplayed(): void
{
self::assertFalse($this->object->isDisplayed(false));
self::assertTrue($this->object->isDisplayed(true));
self::assertTrue($this->object->isDisplayed(false));
}
/**
* Data provider for testAffectedRows
*
* @return array Test-data
*/
public static function providerAffectedRows(): array
{
return [
[
1,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row affected.' . "\n"
. '</div>' . "\n",
],
[
2,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows affected.' . "\n"
. '</div>' . "\n",
],
[
10000,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 10000 rows affected.' . "\n"
. '</div>' . "\n",
],
];
}
/**
* Test for getMessageForAffectedRows() method
*
* @param int $rows Number of rows
* @param string $output Expected string
*
* @dataProvider providerAffectedRows
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerAffectedRows')]
public function testAffectedRows(int $rows, string $output): void
{
$this->object = new Message();
$msg = $this->object->getMessageForAffectedRows($rows);
$this->object->addMessage($msg);
self::assertSame($output, $this->object->getDisplay());
}
/**
* Data provider for testInsertedRows
*
* @return array Test-data
*/
public static function providerInsertedRows(): array
{
return [
[
1,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row inserted.' . "\n"
. '</div>' . "\n",
],
[
2,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows inserted.' . "\n"
. '</div>' . "\n",
],
[
100000,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 100000 rows inserted.' . "\n"
. '</div>' . "\n",
],
];
}
/**
* Test for getMessageForInsertedRows() method
*
* @param int $rows Number of rows
* @param string $output Expected string
*
* @dataProvider providerInsertedRows
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerInsertedRows')]
public function testInsertedRows(int $rows, string $output): void
{
$this->object = new Message();
$msg = $this->object->getMessageForInsertedRows($rows);
$this->object->addMessage($msg);
self::assertSame($output, $this->object->getDisplay());
}
/**
* Data provider for testDeletedRows
*
* @return array Test-data
*/
public static function providerDeletedRows(): array
{
return [
[
1,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 1 row deleted.' . "\n"
. '</div>' . "\n",
],
[
2,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 2 rows deleted.' . "\n"
. '</div>' . "\n",
],
[
500000,
'<div class="alert alert-primary" role="alert">' . "\n"
. ' <img src="themes/dot.gif" title="" alt="" class="icon ic_s_notice"> 500000 rows deleted.' . "\n"
. '</div>' . "\n",
],
];
}
/**
* Test for getMessageForDeletedRows() method
*
* @param int $rows Number of rows
* @param string $output Expected string
*
* @dataProvider providerDeletedRows
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerDeletedRows')]
public function testDeletedRows(int $rows, string $output): void
{
$this->object = new Message();
$msg = $this->object->getMessageForDeletedRows($rows);
$this->object->addMessage($msg);
self::assertSame($output, $this->object->getDisplay());
}
}
|