EmailAddressTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Validate_EmailAddressTest::main');
  24. }
  25. /**
  26. * @see Zend_Validate_EmailAddress
  27. */
  28. require_once 'Zend/Validate/EmailAddress.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Validate
  36. */
  37. class Zend_Validate_EmailAddressTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Default instance created for all test methods
  41. *
  42. * @var Zend_Validate_EmailAddress
  43. */
  44. protected $_validator;
  45. /**
  46. * Runs this test suite
  47. *
  48. * @return void
  49. */
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Creates a new Zend_Validate_EmailAddress object for each test method
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. $this->_validator = new Zend_Validate_EmailAddress();
  63. }
  64. /**
  65. * Ensures that a basic valid e-mail address passes validation
  66. *
  67. * @return void
  68. */
  69. public function testBasic()
  70. {
  71. $this->assertTrue($this->_validator->isValid('username@example.com'));
  72. }
  73. /**
  74. * Ensures that localhost address is valid
  75. *
  76. * @return void
  77. */
  78. public function testLocalhostAllowed()
  79. {
  80. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
  81. $this->assertTrue($validator->isValid('username@localhost'));
  82. }
  83. /**
  84. * Ensures that local domain names are valid
  85. *
  86. * @return void
  87. */
  88. public function testLocaldomainAllowed()
  89. {
  90. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
  91. $this->assertTrue($validator->isValid('username@localhost.localdomain'));
  92. }
  93. /**
  94. * Ensures that IP hostnames are valid
  95. *
  96. * @return void
  97. */
  98. public function testIPAllowed()
  99. {
  100. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP);
  101. $valuesExpected = array(
  102. array(Zend_Validate_Hostname::ALLOW_DNS, true, array('bob@212.212.20.4')),
  103. array(Zend_Validate_Hostname::ALLOW_DNS, false, array('bob@localhost'))
  104. );
  105. foreach ($valuesExpected as $element) {
  106. foreach ($element[2] as $input) {
  107. $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()));
  108. }
  109. }
  110. }
  111. /**
  112. * Ensures that validation fails when the local part is missing
  113. *
  114. * @return void
  115. */
  116. public function testLocalPartMissing()
  117. {
  118. $this->assertFalse($this->_validator->isValid('@example.com'));
  119. $messages = $this->_validator->getMessages();
  120. $this->assertEquals(1, count($messages));
  121. $this->assertContains('local-part@hostname', current($messages));
  122. }
  123. /**
  124. * Ensures that validation fails and produces the expected messages when the local part is invalid
  125. *
  126. * @return void
  127. */
  128. public function testLocalPartInvalid()
  129. {
  130. $this->assertFalse($this->_validator->isValid('Some User@example.com'));
  131. $messages = $this->_validator->getMessages();
  132. $this->assertEquals(3, count($messages));
  133. $this->assertContains('Some User', current($messages));
  134. $this->assertContains('dot-atom', current($messages));
  135. $this->assertContains('Some User', next($messages));
  136. $this->assertContains('quoted-string', current($messages));
  137. $this->assertContains('Some User', next($messages));
  138. $this->assertContains('not a valid local part', current($messages));
  139. }
  140. /**
  141. * Ensures that no validation failure message is produced when the local part follows the quoted-string format
  142. *
  143. * @return void
  144. */
  145. public function testLocalPartQuotedString()
  146. {
  147. $this->assertTrue($this->_validator->isValid('"Some User"@example.com'));
  148. $messages = $this->_validator->getMessages();
  149. $this->assertTrue(is_array($messages));
  150. $this->assertEquals(0, count($messages));
  151. }
  152. /**
  153. * Ensures that validation fails when the hostname is invalid
  154. *
  155. * @return void
  156. */
  157. public function testHostnameInvalid()
  158. {
  159. $this->assertFalse($this->_validator->isValid('username@ example . com'));
  160. $messages = $this->_validator->getMessages();
  161. $this->assertThat(count($messages), $this->greaterThanOrEqual(1));
  162. $this->assertContains('not a valid hostname', current($messages));
  163. }
  164. /**
  165. * Ensures that quoted-string local part is considered valid
  166. *
  167. * @return void
  168. */
  169. public function testQuotedString()
  170. {
  171. $emailAddresses = array(
  172. '"username"@example.com',
  173. '"bob%jones"@domain.com',
  174. '"bob jones"@domain.com',
  175. '"bob@jones"@domain.com',
  176. '"[[ bob ]]"@domain.com',
  177. '"jones"@domain.com'
  178. );
  179. foreach ($emailAddresses as $input) {
  180. $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
  181. . implode("\n", $this->_validator->getMessages()));
  182. }
  183. }
  184. /**
  185. * Ensures that validation fails when the e-mail is given as for display,
  186. * with angle brackets around the actual address
  187. *
  188. * @return void
  189. */
  190. public function testEmailDisplay()
  191. {
  192. $this->assertFalse($this->_validator->isValid('User Name <username@example.com>'));
  193. $messages = $this->_validator->getMessages();
  194. $this->assertThat(count($messages), $this->greaterThanOrEqual(3));
  195. $this->assertContains('not a valid hostname', current($messages));
  196. $this->assertContains('cannot match TLD', next($messages));
  197. $this->assertContains('does not appear to be a valid local network name', next($messages));
  198. }
  199. /**
  200. * Ensures that the validator follows expected behavior for valid email addresses
  201. *
  202. * @return void
  203. */
  204. public function testBasicValid()
  205. {
  206. $emailAddresses = array(
  207. 'bob@domain.com',
  208. 'bob.jones@domain.co.uk',
  209. 'bob.jones.smythe@domain.co.uk',
  210. 'BoB@domain.museum',
  211. 'bobjones@domain.info',
  212. "B.O'Callaghan@domain.com",
  213. 'bob+jones@domain.us',
  214. 'bob+jones@domain.co.uk',
  215. 'bob@some.domain.uk.com',
  216. 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugar.com'
  217. );
  218. foreach ($emailAddresses as $input) {
  219. $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
  220. . implode("\n", $this->_validator->getMessages()));
  221. }
  222. }
  223. /**
  224. * Ensures that the validator follows expected behavior for invalid email addresses
  225. *
  226. * @return void
  227. */
  228. public function testBasicInvalid()
  229. {
  230. $emailAddresses = array(
  231. '',
  232. 'bob
  233. @domain.com',
  234. 'bob jones@domain.com',
  235. '.bobJones@studio24.com',
  236. 'bobJones.@studio24.com',
  237. 'bob.Jones.@studio24.com',
  238. '"bob%jones@domain.com',
  239. 'bob@verylongdomainsupercalifragilisticexpialidociousaspoonfulofsugar.com',
  240. 'bob+domain.com',
  241. 'bob.domain.com',
  242. 'bob @domain.com',
  243. 'bob@ domain.com',
  244. 'bob @ domain.com',
  245. 'Abc..123@example.com'
  246. );
  247. foreach ($emailAddresses as $input) {
  248. $this->assertFalse($this->_validator->isValid($input), implode("\n", $this->_validator->getMessages()) . $input);
  249. }
  250. }
  251. /**
  252. * Ensures that the validator follows expected behavior for valid email addresses with complex local parts
  253. *
  254. * @return void
  255. */
  256. public function testComplexLocalValid()
  257. {
  258. $emailAddresses = array(
  259. 'Bob.Jones@domain.com',
  260. 'Bob.Jones!@domain.com',
  261. 'Bob&Jones@domain.com',
  262. '/Bob.Jones@domain.com',
  263. '#Bob.Jones@domain.com',
  264. 'Bob.Jones?@domain.com',
  265. 'Bob~Jones@domain.com'
  266. );
  267. foreach ($emailAddresses as $input) {
  268. $this->assertTrue($this->_validator->isValid($input));
  269. }
  270. }
  271. /**
  272. * Ensures that the validator follows expected behavior for checking MX records
  273. *
  274. * @return void
  275. */
  276. public function testMXRecords()
  277. {
  278. if (!defined('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
  279. || !constant('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
  280. ) {
  281. $this->markTestSkipped('Testing MX records only works when a valid internet connection is available');
  282. return;
  283. }
  284. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS, true);
  285. // Are MX checks supported by this system?
  286. if (!$validator->validateMxSupported()) {
  287. $this->markTestSkipped('Testing MX records is not supported with this configuration');
  288. return;
  289. }
  290. $valuesExpected = array(
  291. array(true, array('Bob.Jones@zend.com', 'Bob.Jones@php.net')),
  292. array(false, array('Bob.Jones@bad.example.com', 'Bob.Jones@anotherbad.example.com'))
  293. );
  294. foreach ($valuesExpected as $element) {
  295. foreach ($element[1] as $input) {
  296. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  297. }
  298. }
  299. // Try a check via setting the option via a method
  300. unset($validator);
  301. $validator = new Zend_Validate_EmailAddress();
  302. $validator->setValidateMx(true);
  303. foreach ($valuesExpected as $element) {
  304. foreach ($element[1] as $input) {
  305. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  306. }
  307. }
  308. }
  309. /**
  310. * Test changing hostname settings via EmailAddress object
  311. *
  312. * @return void
  313. */
  314. public function testHostnameSettings()
  315. {
  316. $validator = new Zend_Validate_EmailAddress();
  317. // Check no IDN matching
  318. $validator->getHostnameValidator()->setValidateIdn(false);
  319. $valuesExpected = array(
  320. array(false, array('name@b�rger.de', 'name@h�llo.de', 'name@h�llo.se'))
  321. );
  322. foreach ($valuesExpected as $element) {
  323. foreach ($element[1] as $input) {
  324. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  325. }
  326. }
  327. // Check no TLD matching
  328. $validator->getHostnameValidator()->setValidateTld(false);
  329. $valuesExpected = array(
  330. array(true, array('name@domain.xx', 'name@domain.zz', 'name@domain.madeup'))
  331. );
  332. foreach ($valuesExpected as $element) {
  333. foreach ($element[1] as $input) {
  334. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  335. }
  336. }
  337. }
  338. /**
  339. * Ensures that getMessages() returns expected default value (an empty array)
  340. *
  341. * @return void
  342. */
  343. public function testGetMessages()
  344. {
  345. $this->assertEquals(array(), $this->_validator->getMessages());
  346. }
  347. /**
  348. * @group ZF-2861
  349. */
  350. public function testHostnameValidatorMessagesShouldBeTranslated()
  351. {
  352. require_once 'Zend/Validate/Hostname.php';
  353. $hostnameValidator = new Zend_Validate_Hostname();
  354. require_once 'Zend/Translate.php';
  355. $translations = array(
  356. 'hostnameIpAddressNotAllowed' => 'hostnameIpAddressNotAllowed translation',
  357. 'hostnameUnknownTld' => 'hostnameUnknownTld translation',
  358. 'hostnameDashCharacter' => 'hostnameDashCharacter translation',
  359. 'hostnameInvalidHostnameSchema' => 'hostnameInvalidHostnameSchema translation',
  360. 'hostnameUndecipherableTld' => 'hostnameUndecipherableTld translation',
  361. 'hostnameInvalidHostname' => 'hostnameInvalidHostname translation',
  362. 'hostnameInvalidLocalName' => 'hostnameInvalidLocalName translation',
  363. 'hostnameLocalNameNotAllowed' => 'hostnameLocalNameNotAllowed translation',
  364. );
  365. $translator = new Zend_Translate('array', $translations);
  366. $this->_validator->setTranslator($translator)->setHostnameValidator($hostnameValidator);
  367. $this->_validator->isValid('_XX.!!3xx@0.239,512.777');
  368. $messages = $hostnameValidator->getMessages();
  369. $found = false;
  370. foreach ($messages as $code => $message) {
  371. if (array_key_exists($code, $translations)) {
  372. $this->assertEquals($translations[$code], $message);
  373. $found = true;
  374. break;
  375. }
  376. }
  377. $this->assertTrue($found);
  378. }
  379. /**
  380. * @group ZF-4888
  381. */
  382. public function testEmailsExceedingLength()
  383. {
  384. $emailAddresses = array(
  385. 'thislocalpathoftheemailadressislongerthantheallowedsizeof64characters@domain.com',
  386. 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarexpialidociousspoonfulofsugar.com',
  387. );
  388. foreach ($emailAddresses as $input) {
  389. $this->assertFalse($this->_validator->isValid($input));
  390. }
  391. }
  392. /**
  393. * @group ZF-4352
  394. */
  395. public function testNonStringValidation()
  396. {
  397. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  398. }
  399. /**
  400. * @group ZF-7490
  401. */
  402. public function testSettingHostnameMessagesThroughEmailValidator()
  403. {
  404. $translations = array(
  405. 'hostnameIpAddressNotAllowed' => 'hostnameIpAddressNotAllowed translation',
  406. 'hostnameUnknownTld' => 'hostnameUnknownTld translation',
  407. 'hostnameDashCharacter' => 'hostnameDashCharacter translation',
  408. 'hostnameInvalidHostnameSchema' => 'hostnameInvalidHostnameSchema translation',
  409. 'hostnameUndecipherableTld' => 'hostnameUndecipherableTld translation',
  410. 'hostnameInvalidHostname' => 'hostnameInvalidHostname translation',
  411. 'hostnameInvalidLocalName' => 'hostnameInvalidLocalName translation',
  412. 'hostnameLocalNameNotAllowed' => 'hostnameLocalNameNotAllowed translation',
  413. );
  414. $this->_validator->setMessages($translations);
  415. $this->_validator->isValid('_XX.!!3xx@0.239,512.777');
  416. $messages = $this->_validator->getMessages();
  417. $found = false;
  418. foreach ($messages as $code => $message) {
  419. if (array_key_exists($code, $translations)) {
  420. $this->assertEquals($translations[$code], $message);
  421. $found = true;
  422. break;
  423. }
  424. }
  425. $this->assertTrue($found);
  426. }
  427. /**
  428. * Testing initializing with several options
  429. */
  430. public function testInstanceWithOldOptions()
  431. {
  432. $handler = set_error_handler(array($this, 'errorHandler'), E_USER_NOTICE);
  433. $validator = new Zend_Validate_EmailAddress();
  434. $options = $validator->getOptions();
  435. $this->assertEquals(Zend_Validate_Hostname::ALLOW_DNS, $options['allow']);
  436. $this->assertFalse($options['mx']);
  437. try {
  438. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL, true, new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
  439. $options = $validator->getOptions();
  440. $this->assertEquals(Zend_Validate_Hostname::ALLOW_ALL, $options['allow']);
  441. $this->assertTrue($options['mx']);
  442. set_error_handler($handler);
  443. } catch (Zend_Exception $e) {
  444. $this->markTestSkipped('MX not available on this system');
  445. }
  446. }
  447. /**
  448. * Testing setOptions
  449. */
  450. public function testSetOptions()
  451. {
  452. $this->_validator->setOptions(array('messages' => array(Zend_Validate_EmailAddress::INVALID => 'TestMessage')));
  453. $messages = $this->_validator->getMessageTemplates();
  454. $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
  455. $oldHostname = $this->_validator->getHostnameValidator();
  456. $this->_validator->setOptions(array('hostname' => new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL)));
  457. $hostname = $this->_validator->getHostnameValidator();
  458. $this->assertNotEquals($oldHostname, $hostname);
  459. }
  460. /**
  461. * Testing setMessage
  462. */
  463. public function testSetSingleMessage()
  464. {
  465. $messages = $this->_validator->getMessageTemplates();
  466. $this->assertNotEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
  467. $this->_validator->setMessage('TestMessage');
  468. $messages = $this->_validator->getMessageTemplates();
  469. $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
  470. }
  471. /**
  472. * Testing setMessage for all messages
  473. *
  474. * @group ZF-10690
  475. */
  476. public function testSetMultipleMessages()
  477. {
  478. $messages = $this->_validator->getMessageTemplates();
  479. $this->assertNotEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
  480. $this->_validator->setMessage('TestMessage');
  481. $messages = $this->_validator->getMessageTemplates();
  482. $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID]);
  483. $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::INVALID_FORMAT]);
  484. $this->assertEquals('TestMessage', $messages[Zend_Validate_EmailAddress::DOT_ATOM]);
  485. }
  486. /**
  487. * Testing validateMxSupported
  488. */
  489. public function testValidateMxSupported()
  490. {
  491. if (function_exists('getmxrr')) {
  492. $this->assertTrue($this->_validator->validateMxSupported());
  493. } else {
  494. $this->assertFalse($this->_validator->validateMxSupported());
  495. }
  496. }
  497. /**
  498. * Testing getValidateMx
  499. */
  500. public function testGetValidateMx()
  501. {
  502. $this->assertFalse($this->_validator->getValidateMx());
  503. }
  504. /**
  505. * Testing getDeepMxCheck
  506. */
  507. public function testGetDeepMxCheck()
  508. {
  509. $this->assertFalse($this->_validator->getDeepMxCheck());
  510. }
  511. /**
  512. * Testing getDomainCheck
  513. */
  514. public function testGetDomainCheck()
  515. {
  516. $this->assertTrue($this->_validator->getDomainCheck());
  517. }
  518. public function errorHandler($errno, $errstr)
  519. {
  520. if (strstr($errstr, 'deprecated')) {
  521. $this->multipleOptionsDetected = true;
  522. }
  523. }
  524. /**
  525. * @group ZF-11239
  526. */
  527. public function testNotSetHostnameValidator()
  528. {
  529. $hostname = $this->_validator->getHostnameValidator();
  530. $this->assertTrue($hostname instanceof Zend_Validate_Hostname);
  531. }
  532. }
  533. if (PHPUnit_MAIN_METHOD == 'Zend_Validate_EmailAddressTest::main') {
  534. Zend_Validate_EmailAddressTest::main();
  535. }