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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
|
<?php
namespace Illuminate\Tests\Validation;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Facade;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Email;
use Illuminate\Validation\ValidationServiceProvider;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
class ValidationEmailRuleTest extends TestCase
{
private const ATTRIBUTE = 'my_email';
private const ATTRIBUTE_REPLACED = 'my email';
public function testBasic()
{
$this->fails(
Email::default(),
'foo',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email(),
'foo',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
Email::default(),
'taylor@laravel.com'
);
$this->passes(
Rule::email(),
'taylor@laravel.com'
);
$this->passes(Email::default(), null);
$this->passes(Rule::email(), null);
}
/**
* @param mixed $rule
* @param string|array $values
* @param array $expectedMessages
* @param string|null $customValidationMessage
* @return void
*/
protected function fails($rule, $values, $expectedMessages, $customValidationMessage = null)
{
$this->assertValidationRules($rule, $values, false, $expectedMessages, $customValidationMessage);
}
/**
* @param mixed $rule
* @param string|array $values
* @param bool $expectToPass
* @param array $expectedMessages
* @param string|null $customValidationMessage
* @return void
*/
protected function assertValidationRules($rule, $values, $expectToPass, $expectedMessages = [], $customValidationMessage = null)
{
$values = Arr::wrap($values);
$translator = resolve('translator');
foreach ($values as $value) {
$v = new Validator(
$translator,
[self::ATTRIBUTE => $value],
[self::ATTRIBUTE => is_object($rule) ? clone $rule : $rule],
$customValidationMessage ? [self::ATTRIBUTE.'.email' => $customValidationMessage] : []
);
$this->assertSame($expectToPass, $v->passes(), 'Expected email input '.$value.' to '.($expectToPass ? 'pass' : 'fail').'.');
$this->assertSame(
$expectToPass ? [] : [self::ATTRIBUTE => $expectedMessages],
$v->messages()->toArray(),
'Expected different message for email input '.$value,
);
}
}
/**
* @param mixed $rule
* @param string|array $values
* @return void
*/
protected function passes($rule, $values)
{
$this->assertValidationRules($rule, $values, true);
}
public function testRfcCompliantStrict()
{
$emailThatFailsBothNonStrictButFailsInStrict = 'username@sub..example.com';
$emailThatPassesNonStrictButFailsInStrict = '"has space"@example.com';
$emailThatPassesBothNonStrictAndInStrict = 'plainaddress@example.com';
$this->fails(
(new Email())->rfcCompliant(strict: true),
$emailThatPassesNonStrictButFailsInStrict,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(strict: true),
$emailThatPassesNonStrictButFailsInStrict,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
(new Email())->rfcCompliant(strict: true),
$emailThatFailsBothNonStrictButFailsInStrict,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(strict: true),
$emailThatFailsBothNonStrictButFailsInStrict,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->rfcCompliant(strict: true),
$emailThatPassesBothNonStrictAndInStrict
);
$this->passes(
Rule::email()->rfcCompliant(strict: true),
$emailThatPassesBothNonStrictAndInStrict
);
}
#[RequiresPhpExtension('intl')]
public function testValidateMxRecord()
{
$this->fails(
(new Email())->validateMxRecord(),
'plainaddress@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->validateMxRecord(),
'plainaddress@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->validateMxRecord(),
'taylor@laravel.com'
);
$this->passes(
Rule::email()->validateMxRecord(),
'taylor@laravel.com'
);
}
public function testPreventSpoofing()
{
$this->fails(
(new Email())->preventSpoofing(),
'admin@examрle.com',// Contains a Cyrillic 'р' (U+0440), not a Latin 'p'
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->preventSpoofing(),
'admin@examрle.com',// Contains a Cyrillic 'р' (U+0440), not a Latin 'p'
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$spoofingEmail = 'admin@exam'."\u{0440}".'le.com';
$this->fails(
(new Email())->preventSpoofing(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->preventSpoofing(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->preventSpoofing(),
'admin@example.com'
);
$this->passes(
Rule::email()->preventSpoofing(),
'admin@example.com'
);
$this->passes(
(new Email())->preventSpoofing(),
'test👨💻@domain.com'
);
$this->passes(
Rule::email()->preventSpoofing(),
'test👨💻@domain.com'
);
}
public function testWithNativeValidation()
{
$this->fails(
(new Email())->withNativeValidation(),
'tést@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->withNativeValidation(),
'tést@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->withNativeValidation(),
'admin@example.com'
);
$this->passes(
Rule::email()->withNativeValidation(),
'admin@example.com'
);
}
public function testWithNativeValidationAllowUnicode()
{
$this->fails(
(new Email())->withNativeValidation(allowUnicode: true),
'invalid.@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->withNativeValidation(allowUnicode: true),
'invalid.@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->withNativeValidation(allowUnicode: true),
'tést@domain.com'
);
$this->passes(
Rule::email()->withNativeValidation(allowUnicode: true),
'tést@domain.com'
);
$this->passes(
(new Email())->withNativeValidation(allowUnicode: true),
'admin@example.com'
);
$this->passes(
Rule::email()->withNativeValidation(allowUnicode: true),
'admin@example.com'
);
}
public function testRfcCompliantNonStrict()
{
$this->fails(
(new Email())->rfcCompliant(),
'invalid.@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(),
'invalid.@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
(new Email())->rfcCompliant(),
'test👨💻@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(),
'test👨💻@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->rfcCompliant(),
'admin@example.com'
);
$this->passes(
Rule::email()->rfcCompliant(),
'admin@example.com'
);
$this->passes(
(new Email())->rfcCompliant(),
'tést@domain.com'
);
$this->passes(
Rule::email()->rfcCompliant(),
'tést@domain.com'
);
}
#[TestWith(['"has space"@example.com'])] // Quoted local part with space
#[TestWith(['some(comment)@example.com'])] // Comment in local part
#[TestWith(['abc."test"@example.com'])] // Mixed quoted/unquoted local part
#[TestWith(['"escaped\\\"quote"@example.com'])] // Escaped quote inside quoted local part
#[TestWith(['test@example'])] // Domain without TLD
#[TestWith(['test@localhost'])] // Domain without TLD
#[TestWith(['name@[127.0.0.1]'])] // Local-part with domain-literal IPv4 address
#[TestWith(['user@[IPv6:::1]'])] // Domain-literal with unusual IPv6 short form
#[TestWith(['a@[IPv6:2001:db8::1]'])] // Domain-literal with normal IPv6
#[TestWith(['user@[IPv6:::]'])] // invalid shorthand IPv6
#[TestWith(['"ab\\(c"@example.com'])]
public function testEmailsThatPassOnRfcCompliantButFailOnStrict($email)
{
$this->passes(
Rule::email()->rfcCompliant(),
$email
);
$this->fails(
Rule::email()->rfcCompliant(strict: true),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
#[TestWith(['plainaddress@example.com'])]
#[TestWith(['joe.smith@example.io'])]
#[TestWith(['custom-tag+dev@example.org'])]
#[TestWith(['hyphens--@example.org'])]
#[TestWith(['underscore_name@example.co.uk'])]
#[TestWith(['underscores__@example.org'])]
#[TestWith(['user@subdomain.example.com'])]
#[TestWith(['numbers123@domain.com'])]
#[TestWith(['john-doe@some-domain.com'])]
#[TestWith(['UPPERlower@example.org'])]
#[TestWith(['dots.ok@sub.domain.io'])]
#[TestWith(['some_email+tag@domain.dev'])]
#[TestWith(['a@b.c'])]
#[TestWith(['user@xn--bcher-kva.example'])]
#[TestWith(['user@bücher.example'])]
public function testEmailsThatPassOnBothRfcCompliantAndStrict($email)
{
$this->passes(
Rule::email()->rfcCompliant(),
$email
);
$this->passes(
Rule::email()->rfcCompliant(strict: true),
$email
);
}
#[TestWith(['invalid.@example.com'])]
#[TestWith(['invalid@.example.com'])]
#[TestWith(['.invalid@example.com'])]
#[TestWith(['invalid@example.com.'])]
#[TestWith(['some..dots@example.com'])]
#[TestWith(['username@sub..example.com'])]
#[TestWith(['test@example..com'])]
#[TestWith(['test@@example.com'])]
#[TestWith(['test👨💻@domain.com'])]
#[TestWith(['username@domain-with-hyphen-.com'])]
#[TestWith(['()<>[]:,;@example.com'])]
#[TestWith(['@example.com'])]
#[TestWith(['[test]@example.com'])]
#[TestWith(['user@example.com:3000'])]
#[TestWith(['"unescaped"quote@example.com'])]
#[TestWith(['https://example.com'])]
#[TestWith(['with\\escape@example.com'])]
public function testEmailsThatFailOnBothRfcCompliantAndStrict($email)
{
$this->fails(
Rule::email()->rfcCompliant(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(strict: true),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
#[TestWith(['plainaddress@example.com'])] // Simple valid address
#[TestWith(['joe.smith@example.io'])] // Dotted local part with TLD
#[TestWith(['custom-tag+dev@example.org'])] // Plus tag in local part
#[TestWith(['hyphens--@example.org'])] // Hyphens in local part
#[TestWith(['underscore_name@example.co.uk'])] // Underscore in local part
#[TestWith(['underscores__@example.org'])] // Double underscores in local part
#[TestWith(['user@subdomain.example.com'])] // Subdomain in domain part
#[TestWith(['numbers123@domain.com'])] // Numbers in local part
#[TestWith(['john-doe@some-domain.com'])] // Hyphenated domain
#[TestWith(['UPPERlower@example.org'])] // Mixed case local part
#[TestWith(['dots.ok@sub.domain.io'])] // Dots in local and subdomain
#[TestWith(['some_email+tag@domain.dev'])] // Email with plus tag and underscore
#[TestWith(['a@b.c'])] // Minimal email
#[TestWith(['user@xn--bcher-kva.example'])] // Punycode domain (bücher)
#[TestWith(['user@bücher.example'])] // Unicode domain
public function testEmailsThatPassOnBothRfcCompliantAndRfcCompliantStrict($email)
{
$this->passes(
Rule::email()->rfcCompliant(),
$email
);
$this->passes(
Rule::email()->rfcCompliant(strict: true),
$email
);
}
#[TestWith(['déjà@example.com'])]
#[TestWith(['测试@example.com'])]
public function testEmailsThatFailWithNativeValidationAsciiPassUnicode($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
Rule::email()->withNativeValidation(allowUnicode: true),
$email
);
}
#[TestWith(['test@üñîçødé.com'])] // Unicode domain
#[TestWith(['user@domain..com'])] // Double dots in domain
#[TestWith(['test@.example.com'])] // Domain starts with a dot
#[TestWith(['username@domain-with-hyphen-.com'])]
#[TestWith(['пример@пример.рф'])] // Cyrillic domain
#[TestWith(['例子@例子.公司'])] // Chinese domain
#[TestWith(['name@123.123.123.123'])] // Numeric domain
public function testEmailsThatFailOnBothWithNativeValidationAsciiAndUnicode($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->withNativeValidation(allowUnicode: true),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
#[TestWith(['user@example.com'])]
#[TestWith(['user.name+tag@example.co.uk'])]
#[TestWith(['joe_smith@example.org'])]
#[TestWith(['user@[IPv6:2001:db8:1ff::a0b:dbd0]'])]
#[TestWith(['test@xn--bcher-kva.com'])] // Punycode for bücher.com
public function testEmailsThatPassBothWithNativeValidationAsciiAndUnicode($email)
{
$this->passes(
Rule::email()->withNativeValidation(),
$email
);
$this->passes(
Rule::email()->withNativeValidation(allowUnicode: true),
$email
);
}
#[TestWith(['some(comment)@example.com'])] // Comment in local part
#[TestWith(['tést@example.com'])] // Accented local part
#[TestWith(['user@üñîçødé.com'])] // Unicode domain
#[TestWith(['user@bücher.example'])] // Unicode domain
#[TestWith(['"has space"@example.com'])] // Quoted local part with space
#[TestWith(['"escaped\\\"quote"@example.com'])] // Escaped quote inside quoted local part
#[TestWith(['test@localhost'])] // Domain without TLD
#[TestWith(['test@example'])] // Domain without TLD
#[TestWith(['пример@пример.рф'])] // Cyrillic local and domain
#[TestWith(['例子@例子.公司'])] // Chinese local and domain
#[TestWith(['name@123.123.123.123'])] // Numeric domain
public function testEmailsThatFailWithNativeValidationAsciiPassRfcCompliant($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
Rule::email()->rfcCompliant(),
$email
);
}
#[TestWith(['plainaddress@example.com'])] // Simple valid address
#[TestWith(['joe.smith@example.io'])] // Dot in local part
#[TestWith(['custom-tag+dev@example.org'])] // Plus tag in local part
#[TestWith(['hyphens--@example.org'])] // Double hyphen in local part
#[TestWith(['underscore_name@example.co.uk'])] // Underscore in local part
#[TestWith(['underscores__@example.org'])] // Double underscores in local part
#[TestWith(['user@subdomain.example.com'])] // Subdomain in domain
#[TestWith(['numbers123@domain.com'])] // Numbers in local part
#[TestWith(['john-doe@some-domain.com'])] // Hyphen in domain
#[TestWith(['UPPERlower@example.org'])] // Mixed-case local part
#[TestWith(['dots.ok@sub.domain.io'])] // Subdomain with dot in local part
#[TestWith(['some_email+tag@domain.dev'])] // Underscore and tag in local part
#[TestWith(['a@b.c'])] // Minimal valid address
#[TestWith(['user@xn--bcher-kva.example'])] // Punycode domain (bücher.example)
#[TestWith(['user_name+tag@example.io'])] // Underscore with tag
#[TestWith(['UPPERCASE@EXAMPLE.IO'])] // All uppercase local and domain
#[TestWith(['abc."test"@example.com'])] // Mixed quoted/unquoted local part
#[TestWith(['name@[127.0.0.1]'])] // IPv4 domain literal
#[TestWith(['user@[IPv6:::1]'])] // IPv6 domain with unusual short form
#[TestWith(['a@[IPv6:2001:db8::1]'])] // IPv6 domain normal form
#[TestWith(['user@[IPv6:2001:db8:1ff::a0b:dbd0]'])] // Fully expanded IPv6
public function testEmailsThatPassWithNativeValidationAndRfcCompliant($email)
{
$this->passes(
Rule::email()->withNativeValidation(),
$email
);
$this->passes(
Rule::email()->rfcCompliant(),
$email
);
}
#[TestWith(['test@@example.com'])] // Multiple @ symbols
#[TestWith(['user@domain..com'])] // Double dots in domain
#[TestWith(['.leadingdot@example.com'])] // Leading dot in local part
#[TestWith(['with\\escape@example.com'])] // Backslash in local part
#[TestWith(['@example.com'])] // Missing local part
#[TestWith(['some)@example.com'])] // Unmatched parenthesis in local part
#[TestWith([' space@domain.com'])] // Leading space in local part
#[TestWith(['user@domain:port.com'])] // Colon in domain (mimics a port)
#[TestWith(['username@domain-with-hyphen-.com'])] // Trailing hyphen in domain
public function testEmailsThatFailWithNativeValidationAndRfcCompliant($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
public function testNativeValidationVsRfcCompliant()
{
$emailsThatPassNativeFailRfc = [
// none I could find
];
foreach ($emailsThatPassNativeFailRfc as $email) {
$this->passes(
Rule::email()->withNativeValidation(),
$email
);
$this->fails(
Rule::email()->rfcCompliant(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
}
#[TestWith(['abc."test"@example.com'])] // Mixed quotes in local part
#[TestWith(['name@[127.0.0.1]'])] // Local-part with domain-literal IPv4 address
#[TestWith(['user@[IPv6:2001:db8::1]'])] // Domain-literal with normal IPv6
#[TestWith(['user@[IPv6:2001:db8:1ff::a0b:dbd0]'])] // Domain-literal with full IPv6 address
#[TestWith(['"ab\\(c"@example.com'])] // Quoted local part with escaped character
public function testEmailsThatPassNativeValidationFailRfcCompliantStrict($email)
{
$this->passes(
Rule::email()->withNativeValidation(),
$email
);
$this->fails(
Rule::email()->rfcCompliant(true),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
#[TestWith(['пример@пример.рф'])] // Unicode domain in Cyrillic script
#[TestWith(['例子@例子.公司'])] // Unicode domain in Chinese script
#[TestWith(['name@123.123.123.123'])] // IP address in domain part
public function testEmailsThatFailNativeValidationPassRfcCompliantStrict($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
Rule::email()->rfcCompliant(true),
$email
);
}
#[TestWith(['user@example.com'])] // Simple, valid email
#[TestWith(['joe.smith+dev@example.co.uk'])] // Plus-tagged email with subdomain TLD
#[TestWith(['user!#$%&\'*+/=?^_`{|}~@example.com'])] // Unusual valid characters in local part
public function testEmailsThatPassBothNativeValidationAndRfcCompliantStrict($email)
{
$this->passes(
Rule::email()->withNativeValidation(),
$email
);
$this->passes(
Rule::email()->rfcCompliant(true),
$email
);
}
#[TestWith(['test@@example.com'])] // Multiple @
#[TestWith(['.leadingdot@example.com'])] // Leading dot in local part
#[TestWith(['user@domain..com'])] // Double dots in domain
#[TestWith(['test@'])] // Missing domain
#[TestWith(['abc"quote@example.com'])] // Unescaped quote in local part
#[TestWith(['some(comment)@example.com'])] // Local part comment
#[TestWith(['"has space"@example.com'])] // Quoted local part with space
#[TestWith(['user@domain(comment)'])] // Comment in domain
#[TestWith(['user@[127.0.0.1(comment)]'])] // Comment in domain-literal IPv4 address
#[TestWith(['some((double))comment@example.com'])] // Nested comment in local part
#[TestWith(['"test\\\"quote"@example.com'])] // Escaped quote in quoted local part
#[TestWith(['" leading.space"@example.com'])] // Leading space in quoted local part
public function testEmailsThatFailBothNativeValidationAndRfcCompliantStrict($email)
{
$this->fails(
Rule::email()->withNativeValidation(),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(true),
$email,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
#[RequiresPhpExtension('intl')]
public function testCombiningRules()
{
$this->passes(
(new Email())->rfcCompliant(strict: true)->preventSpoofing(),
'test@example.com'
);
$this->passes(
Rule::email()->rfcCompliant(strict: true)->preventSpoofing(),
'test@example.com'
);
$this->fails(
(new Email())->rfcCompliant(strict: true)->preventSpoofing()->validateMxRecord(),
'test@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant(strict: true)->preventSpoofing()->validateMxRecord(),
'test@example.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->passes(
(new Email())->preventSpoofing(),
'test👨💻@domain.com'
);
$this->passes(
Rule::email()->preventSpoofing(),
'test👨💻@domain.com'
);
$this->fails(
(new Email())->preventSpoofing()->rfcCompliant(),
'test👨💻@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->preventSpoofing()->rfcCompliant(),
'test👨💻@domain.com',
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$spoofingEmail = 'admin@exam'."\u{0440}".'le.com';
$this->passes(
(new Email())->rfcCompliant(),
$spoofingEmail
);
$this->passes(
Rule::email()->rfcCompliant(),
$spoofingEmail
);
$this->fails(
(new Email())->rfcCompliant()->preventSpoofing(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
Rule::email()->rfcCompliant()->preventSpoofing(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
public function testMacro()
{
Email::macro('laravelEmployee', function () {
return static::default()->rules('ends_with:@laravel.com');
});
$this->fails(
Email::laravelEmployee(),
'taylor@example.com',
['validation.ends_with']
);
$this->fails(
Rule::email()->laravelEmployee(),
'taylor@example.com',
['validation.ends_with']
);
$this->passes(
Email::laravelEmployee(),
'taylor@laravel.com'
);
$this->passes(
Rule::email()->laravelEmployee(),
'taylor@laravel.com'
);
}
public function testItCanSetDefaultUsing()
{
$this->assertInstanceOf(Email::class, Email::default());
$spoofingEmail = 'admin@exam'."\u{0440}".'le.com';
$this->passes(
Email::default(),
$spoofingEmail
);
Email::defaults(function () {
return (new Email())->preventSpoofing();
});
$this->fails(
Email::default(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
Email::defaults(function () {
return Rule::email()->rfcCompliant();
});
$this->passes(
Email::default(),
$spoofingEmail
);
Email::defaults(function () {
return Rule::email()->preventSpoofing();
});
$this->fails(
Email::default(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
}
public function testValidationMessages()
{
Email::defaults(function () {
return Rule::email()->preventSpoofing();
});
$spoofingEmail = 'admin@exam'."\u{0440}".'le.com';
$this->fails(
Email::default(),
$spoofingEmail,
['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.']
);
$this->fails(
rule: Email::default(),
values: $spoofingEmail,
expectedMessages: ['The '.self::ATTRIBUTE_REPLACED.' must be a valid email address.'],
customValidationMessage: 'The :attribute must be a valid email address.'
);
$this->fails(
rule: Email::default(),
values: $spoofingEmail,
expectedMessages: ['Please check the entered '.self::ATTRIBUTE_REPLACED.", it must be a valid email address, {$spoofingEmail} given."],
customValidationMessage: 'Please check the entered :attribute, it must be a valid email address, :input given.'
);
$this->fails(
rule: Email::default(),
values: $spoofingEmail,
expectedMessages: ['Plain text value'],
customValidationMessage: 'Plain text value'
);
}
protected function setUp(): void
{
$container = Container::getInstance();
$container->bind('translator', function () {
$translator = new Translator(
new ArrayLoader, 'en'
);
$translator->addLines([
'validation.email' => 'The :attribute must be a valid email address.',
], 'en');
return $translator;
});
Facade::setFacadeApplication($container);
(new ValidationServiceProvider($container))->register();
}
protected function tearDown(): void
{
Container::setInstance(null);
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);
}
}
|