EmailValidatorTest.php 4.56 KB
Newer Older
Alexander Makarov committed
1 2
<?php
namespace yiiunit\framework\validators;
Alexander Makarov committed
3

Alexander Makarov committed
4
use yii\validators\EmailValidator;
5
use yiiunit\data\validators\models\FakedValidationModel;
Alexander Makarov committed
6 7 8 9
use yiiunit\TestCase;

/**
 * EmailValidatorTest
10
 * @group validators
Alexander Makarov committed
11 12 13
 */
class EmailValidatorTest extends TestCase
{
14 15 16 17 18 19
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Alexander Makarov committed
20 21 22 23 24 25 26
	public function testValidateValue()
	{
		$validator = new EmailValidator();

		$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
		$this->assertTrue($validator->validateValue('5011@gmail.com'));
		$this->assertFalse($validator->validateValue('rmcreative.ru'));
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
		$this->assertFalse($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('<mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('info@örtliches.de'));
		$this->assertFalse($validator->validateValue('sam@рмкреатиф.ru'));

		$validator->allowName = true;

		$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
		$this->assertTrue($validator->validateValue('5011@gmail.com'));
		$this->assertFalse($validator->validateValue('rmcreative.ru'));
		$this->assertTrue($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
		$this->assertTrue($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
		$this->assertTrue($validator->validateValue('<mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('info@örtliches.de'));
		$this->assertFalse($validator->validateValue('sam@рмкреатиф.ru'));
		$this->assertFalse($validator->validateValue('Informtation info@oertliches.de'));
44 45 46
		$this->assertTrue($validator->validateValue('test@example.com'));
		$this->assertTrue($validator->validateValue('John Smith <john.smith@example.com>'));
		$this->assertFalse($validator->validateValue('John Smith <example.com>'));
47 48
	}

49
	public function testValidateValueIdn()
50
	{
51 52 53
		if (!function_exists('idn_to_ascii')) {
			$this->markTestSkipped('Intl extension required');
			return;
54 55 56 57
		}
		$validator = new EmailValidator();
		$validator->enableIDN = true;

58 59 60
		$this->assertTrue($validator->validateValue('5011@example.com'));
		$this->assertTrue($validator->validateValue('example@äüößìà.de'));
		$this->assertTrue($validator->validateValue('example@xn--zcack7ayc9a.de'));
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		$this->assertTrue($validator->validateValue('info@örtliches.de'));
		$this->assertTrue($validator->validateValue('sam@рмкреатиф.ru'));
		$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
		$this->assertTrue($validator->validateValue('5011@gmail.com'));
		$this->assertFalse($validator->validateValue('rmcreative.ru'));
		$this->assertFalse($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
		$this->assertFalse($validator->validateValue('<mail@cebe.cc>'));

		$validator->allowName = true;

		$this->assertTrue($validator->validateValue('info@örtliches.de'));
		$this->assertTrue($validator->validateValue('Informtation <info@örtliches.de>'));
		$this->assertFalse($validator->validateValue('Informtation info@örtliches.de'));
		$this->assertTrue($validator->validateValue('sam@рмкреатиф.ru'));
		$this->assertTrue($validator->validateValue('sam@rmcreative.ru'));
		$this->assertTrue($validator->validateValue('5011@gmail.com'));
		$this->assertFalse($validator->validateValue('rmcreative.ru'));
		$this->assertTrue($validator->validateValue('Carsten Brandt <mail@cebe.cc>'));
		$this->assertTrue($validator->validateValue('"Carsten Brandt" <mail@cebe.cc>'));
		$this->assertTrue($validator->validateValue('<mail@cebe.cc>'));
82 83 84
		$this->assertTrue($validator->validateValue('test@example.com'));
		$this->assertTrue($validator->validateValue('John Smith <john.smith@example.com>'));
		$this->assertFalse($validator->validateValue('John Smith <example.com>'));
Alexander Makarov committed
85 86 87 88 89 90
	}

	public function testValidateValueMx()
	{
		$validator = new EmailValidator();

91
		$validator->checkDNS = true;
Qiang Xue committed
92
		$this->assertTrue($validator->validateValue('5011@gmail.com'));
93 94 95 96 97

		$validator->checkDNS = false;
		$this->assertTrue($validator->validateValue('test@nonexistingsubdomain.example.com'));
		$validator->checkDNS = true;
		$this->assertFalse($validator->validateValue('test@nonexistingsubdomain.example.com'));
Alexander Makarov committed
98
	}
99 100 101 102 103 104 105 106 107

	public function testValidateAttribute()
	{
		$val = new EmailValidator();
		$model = new FakedValidationModel();
		$model->attr_email = '5011@gmail.com';
		$val->validateAttribute($model, 'attr_email');
		$this->assertFalse($model->hasErrors('attr_email'));
	}
Zander Baldwin committed
108
}