[ZF-10060] Unittests:
[zend/radio.git] / tests / Zend / Validate / EmailAddressTest.php
blob7303f58e19d2ac154c84cba05e20516958236476
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Validate
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 if (!defined('PHPUnit_MAIN_METHOD')) {
24 define('PHPUnit_MAIN_METHOD', 'Zend_Validate_EmailAddressTest::main');
27 /**
28 * Test helper
30 require_once dirname(__FILE__) . '/../../TestHelper.php';
32 /**
33 * @see Zend_Validate_EmailAddress
35 require_once 'Zend/Validate/EmailAddress.php';
37 /**
38 * @category Zend
39 * @package Zend_Validate
40 * @subpackage UnitTests
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
43 * @group Zend_Validate
45 class Zend_Validate_EmailAddressTest extends PHPUnit_Framework_TestCase
47 /**
48 * Default instance created for all test methods
50 * @var Zend_Validate_EmailAddress
52 protected $_validator;
54 /**
55 * Runs this test suite
57 * @return void
59 public static function main()
61 $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
62 $result = PHPUnit_TextUI_TestRunner::run($suite);
65 /**
66 * Creates a new Zend_Validate_EmailAddress object for each test method
68 * @return void
70 public function setUp()
72 $this->_validator = new Zend_Validate_EmailAddress();
75 /**
76 * Ensures that a basic valid e-mail address passes validation
78 * @return void
80 public function testBasic()
82 $this->assertTrue($this->_validator->isValid('username@example.com'));
85 /**
86 * Ensures that localhost address is valid
88 * @return void
90 public function testLocalhostAllowed()
92 $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
93 $this->assertTrue($validator->isValid('username@localhost'));
96 /**
97 * Ensures that local domain names are valid
99 * @return void
101 public function testLocaldomainAllowed()
103 $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
104 $this->assertTrue($validator->isValid('username@localhost.localdomain'));
108 * Ensures that IP hostnames are valid
110 * @return void
112 public function testIPAllowed()
114 $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP);
115 $valuesExpected = array(
116 array(Zend_Validate_Hostname::ALLOW_DNS, true, array('bob@212.212.20.4')),
117 array(Zend_Validate_Hostname::ALLOW_DNS, false, array('bob@localhost'))
119 foreach ($valuesExpected as $element) {
120 foreach ($element[2] as $input) {
121 $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()));
127 * Ensures that validation fails when the local part is missing
129 * @return void
131 public function testLocalPartMissing()
133 $this->assertFalse($this->_validator->isValid('@example.com'));
134 $messages = $this->_validator->getMessages();
135 $this->assertEquals(1, count($messages));
136 $this->assertContains('local-part@hostname', current($messages));
140 * Ensures that validation fails and produces the expected messages when the local part is invalid
142 * @return void
144 public function testLocalPartInvalid()
146 $this->assertFalse($this->_validator->isValid('Some User@example.com'));
148 $messages = $this->_validator->getMessages();
150 $this->assertEquals(3, count($messages));
152 $this->assertContains('Some User', current($messages));
153 $this->assertContains('dot-atom', current($messages));
155 $this->assertContains('Some User', next($messages));
156 $this->assertContains('quoted-string', current($messages));
158 $this->assertContains('Some User', next($messages));
159 $this->assertContains('no valid local part', current($messages));
163 * Ensures that no validation failure message is produced when the local part follows the quoted-string format
165 * @return void
167 public function testLocalPartQuotedString()
169 $this->assertTrue($this->_validator->isValid('"Some User"@example.com'));
171 $messages = $this->_validator->getMessages();
173 $this->assertType('array', $messages);
174 $this->assertEquals(0, count($messages));
178 * Ensures that validation fails when the hostname is invalid
180 * @return void
182 public function testHostnameInvalid()
184 $this->assertFalse($this->_validator->isValid('username@ example . com'));
185 $messages = $this->_validator->getMessages();
186 $this->assertThat(count($messages), $this->greaterThanOrEqual(1));
187 $this->assertContains('no valid hostname', current($messages));
191 * Ensures that quoted-string local part is considered valid
193 * @return void
195 public function testQuotedString()
197 $emailAddresses = array(
198 '"username"@example.com',
199 '"bob%jones"@domain.com',
200 '"bob jones"@domain.com',
201 '"bob@jones"@domain.com',
202 '"[[ bob ]]"@domain.com',
203 '"jones"@domain.com'
205 foreach ($emailAddresses as $input) {
206 $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
207 . implode("\n", $this->_validator->getMessages()));
212 * Ensures that validation fails when the e-mail is given as for display,
213 * with angle brackets around the actual address
215 * @return void
217 public function testEmailDisplay()
219 $this->assertFalse($this->_validator->isValid('User Name <username@example.com>'));
220 $messages = $this->_validator->getMessages();
221 $this->assertThat(count($messages), $this->greaterThanOrEqual(3));
222 $this->assertContains('no valid hostname', current($messages));
223 $this->assertContains('cannot match TLD', next($messages));
224 $this->assertContains('does not appear to be a valid local network name', next($messages));
228 * Ensures that the validator follows expected behavior for valid email addresses
230 * @return void
232 public function testBasicValid()
234 $emailAddresses = array(
235 'bob@domain.com',
236 'bob.jones@domain.co.uk',
237 'bob.jones.smythe@domain.co.uk',
238 'BoB@domain.museum',
239 'bobjones@domain.info',
240 "B.O'Callaghan@domain.com",
241 'bob+jones@domain.us',
242 'bob+jones@domain.co.uk',
243 'bob@some.domain.uk.com',
244 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugar.com'
246 foreach ($emailAddresses as $input) {
247 $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
248 . implode("\n", $this->_validator->getMessages()));
253 * Ensures that the validator follows expected behavior for invalid email addresses
255 * @return void
257 public function testBasicInvalid()
259 $emailAddresses = array(
261 'bob
263 @domain.com',
264 'bob jones@domain.com',
265 '.bobJones@studio24.com',
266 'bobJones.@studio24.com',
267 'bob.Jones.@studio24.com',
268 '"bob%jones@domain.com',
269 'bob@verylongdomainsupercalifragilisticexpialidociousaspoonfulofsugar.com',
270 'bob+domain.com',
271 'bob.domain.com',
272 'bob @domain.com',
273 'bob@ domain.com',
274 'bob @ domain.com',
275 'Abc..123@example.com'
277 foreach ($emailAddresses as $input) {
278 $this->assertFalse($this->_validator->isValid($input), implode("\n", $this->_validator->getMessages()) . $input);
283 * Ensures that the validator follows expected behavior for valid email addresses with complex local parts
285 * @return void
287 public function testComplexLocalValid()
289 $emailAddresses = array(
290 'Bob.Jones@domain.com',
291 'Bob.Jones!@domain.com',
292 'Bob&Jones@domain.com',
293 '/Bob.Jones@domain.com',
294 '#Bob.Jones@domain.com',
295 'Bob.Jones?@domain.com',
296 'Bob~Jones@domain.com'
298 foreach ($emailAddresses as $input) {
299 $this->assertTrue($this->_validator->isValid($input));
305 * Ensures that the validator follows expected behavior for checking MX records
307 * @return void
309 public function testMXRecords()
311 if (!defined('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
312 || !constant('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
314 $this->markTestSkipped('Testing MX records only works when a valid internet connection is available');
315 return;
318 $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS, true);
320 // Are MX checks supported by this system?
321 if (!$validator->validateMxSupported()) {
322 $this->markTestSkipped('Testing MX records is not supported with this configuration');
323 return;
326 $valuesExpected = array(
327 array(true, array('Bob.Jones@zend.com', 'Bob.Jones@php.net')),
328 array(false, array('Bob.Jones@bad.example.com', 'Bob.Jones@anotherbad.example.com'))
330 foreach ($valuesExpected as $element) {
331 foreach ($element[1] as $input) {
332 $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
336 // Try a check via setting the option via a method
337 unset($validator);
338 $validator = new Zend_Validate_EmailAddress();
339 $validator->setValidateMx(true);
340 foreach ($valuesExpected as $element) {
341 foreach ($element[1] as $input) {
342 $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
348 * Test changing hostname settings via EmailAddress object
350 * @return void
352 public function testHostnameSettings()
354 $validator = new Zend_Validate_EmailAddress();
356 // Check no IDN matching
357 $validator->getHostnameValidator()->setValidateIdn(false);
358 $valuesExpected = array(
359 array(false, array('name@b�rger.de', 'name@h�llo.de', 'name@h�llo.se'))
361 foreach ($valuesExpected as $element) {
362 foreach ($element[1] as $input) {
363 $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
367 // Check no TLD matching
368 $validator->getHostnameValidator()->setValidateTld(false);
369 $valuesExpected = array(
370 array(true, array('name@domain.xx', 'name@domain.zz', 'name@domain.madeup'))
372 foreach ($valuesExpected as $element) {
373 foreach ($element[1] as $input) {
374 $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
380 * Ensures that getMessages() returns expected default value (an empty array)
382 * @return void
384 public function testGetMessages()
386 $this->assertEquals(array(), $this->_validator->getMessages());
390 * @see ZF-2861
392 public function testHostnameValidatorMessagesShouldBeTranslated()
394 require_once 'Zend/Validate/Hostname.php';
395 $hostnameValidator = new Zend_Validate_Hostname();
396 require_once 'Zend/Translate.php';
397 $translations = array(
398 'hostnameIpAddressNotAllowed' => 'hostnameIpAddressNotAllowed translation',
399 'hostnameUnknownTld' => 'hostnameUnknownTld translation',
400 'hostnameDashCharacter' => 'hostnameDashCharacter translation',
401 'hostnameInvalidHostnameSchema' => 'hostnameInvalidHostnameSchema translation',
402 'hostnameUndecipherableTld' => 'hostnameUndecipherableTld translation',
403 'hostnameInvalidHostname' => 'hostnameInvalidHostname translation',
404 'hostnameInvalidLocalName' => 'hostnameInvalidLocalName translation',
405 'hostnameLocalNameNotAllowed' => 'hostnameLocalNameNotAllowed translation',
407 $translator = new Zend_Translate('array', $translations);
408 $this->_validator->setTranslator($translator)->setHostnameValidator($hostnameValidator);
410 $this->_validator->isValid('_XX.!!3xx@0.239,512.777');
411 $messages = $hostnameValidator->getMessages();
412 $found = false;
413 foreach ($messages as $code => $message) {
414 if (array_key_exists($code, $translations)) {
415 $this->assertEquals($translations[$code], $message);
416 $found = true;
417 break;
420 $this->assertTrue($found);
424 * @see ZF-4888
426 public function testEmailsExceedingLength()
428 $emailAddresses = array(
429 'thislocalpathoftheemailadressislongerthantheallowedsizeof64characters@domain.com',
430 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarexpialidociousspoonfulofsugar.com',
432 foreach ($emailAddresses as $input) {
433 $this->assertFalse($this->_validator->isValid($input));
438 * @see ZF-4352
440 public function testNonStringValidation()
442 $this->assertFalse($this->_validator->isValid(array(1 => 1)));
446 * @see ZF-7490
448 public function testSettingHostnameMessagesThroughEmailValidator()
450 $translations = array(
451 'hostnameIpAddressNotAllowed' => 'hostnameIpAddressNotAllowed translation',
452 'hostnameUnknownTld' => 'hostnameUnknownTld translation',
453 'hostnameDashCharacter' => 'hostnameDashCharacter translation',
454 'hostnameInvalidHostnameSchema' => 'hostnameInvalidHostnameSchema translation',
455 'hostnameUndecipherableTld' => 'hostnameUndecipherableTld translation',
456 'hostnameInvalidHostname' => 'hostnameInvalidHostname translation',
457 'hostnameInvalidLocalName' => 'hostnameInvalidLocalName translation',
458 'hostnameLocalNameNotAllowed' => 'hostnameLocalNameNotAllowed translation',
461 $this->_validator->setMessages($translations);
462 $this->_validator->isValid('_XX.!!3xx@0.239,512.777');
463 $messages = $this->_validator->getMessages();
464 $found = false;
465 foreach ($messages as $code => $message) {
466 if (array_key_exists($code, $translations)) {
467 $this->assertEquals($translations[$code], $message);
468 $found = true;
469 break;
473 $this->assertTrue($found);
477 * Testing initializing with several options
479 public function testInstanceWithOldOptions()
481 $handler = set_error_handler(array($this, 'errorHandler'), E_USER_NOTICE);
482 $validator = new Zend_Validate_EmailAddress();
483 $options = $validator->getOptions();
485 $this->assertEquals(Zend_Validate_Hostname::ALLOW_DNS, $options['allow']);
486 $this->assertFalse($options['mx']);
488 try {
489 $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL, true, new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
490 $options = $validator->getOptions();
492 $this->assertEquals(Zend_Validate_Hostname::ALLOW_ALL, $options['allow']);
493 $this->assertTrue($options['mx']);
494 set_error_handler($handler);
495 } catch (Zend_Exception $e) {
496 $this->markTestSkipped('MX not available on this system');
501 * Testing setOptions
503 public function testSetOptions()
505 $this->_validator->setOptions(array('messages' => array(Zend_Validate_EmailAddress::INVALID => 'TestMessage')));
506 $messages = $this->_validator->getMessageTemplates();
507 $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
509 $oldHostname = $this->_validator->getHostnameValidator();
510 $this->_validator->setOptions(array('hostname' => new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL)));
511 $hostname = $this->_validator->getHostnameValidator();
512 $this->assertNotEquals($oldHostname, $hostname);
516 * Testing setMessage
518 public function testSetSingleMessage()
520 $messages = $this->_validator->getMessageTemplates();
521 $this->assertNotEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
522 $this->_validator->setMessage('TestMessage');
523 $messages = $this->_validator->getMessageTemplates();
524 $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
528 * Testing validateMxSupported
530 public function testValidateMxSupported()
532 if (function_exists('getmxrr')) {
533 $this->assertTrue($this->_validator->validateMxSupported());
534 } else {
535 $this->assertFalse($this->_validator->validateMxSupported());
540 * Testing getValidateMx
542 public function testGetValidateMx()
544 $this->assertFalse($this->_validator->getValidateMx());
548 * Testing getDeepMxCheck
550 public function testGetDeepMxCheck()
552 $this->assertFalse($this->_validator->getDeepMxCheck());
556 * Testing getDomainCheck
558 public function testGetDomainCheck()
560 $this->assertTrue($this->_validator->getDomainCheck());
563 public function errorHandler($errno, $errstr)
565 if (strstr($errstr, 'deprecated')) {
566 $this->multipleOptionsDetected = true;
571 if (PHPUnit_MAIN_METHOD == 'Zend_Validate_EmailAddressTest::main') {
572 Zend_Validate_EmailAddressTest::main();