EmailAddressTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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-2009 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. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. /**
  30. * @see Zend_Validate_EmailAddress
  31. */
  32. require_once 'Zend/Validate/EmailAddress.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Validate
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Validate_EmailAddressTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Default instance created for all test methods
  44. *
  45. * @var Zend_Validate_EmailAddress
  46. */
  47. protected $_validator;
  48. /**
  49. * Runs this test suite
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Creates a new Zend_Validate_EmailAddress object for each test method
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. $this->_validator = new Zend_Validate_EmailAddress();
  66. }
  67. /**
  68. * Ensures that a basic valid e-mail address passes validation
  69. *
  70. * @return void
  71. */
  72. public function testBasic()
  73. {
  74. $this->assertTrue($this->_validator->isValid('username@example.com'));
  75. }
  76. /**
  77. * Ensures that localhost address is valid
  78. *
  79. * @return void
  80. */
  81. public function testLocalhostAllowed()
  82. {
  83. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
  84. $this->assertTrue($validator->isValid('username@localhost'));
  85. }
  86. /**
  87. * Ensures that local domain names are valid
  88. *
  89. * @return void
  90. */
  91. public function testLocaldomainAllowed()
  92. {
  93. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_ALL);
  94. $this->assertTrue($validator->isValid('username@localhost.localdomain'));
  95. }
  96. /**
  97. * Ensures that IP hostnames are valid
  98. *
  99. * @return void
  100. */
  101. public function testIPAllowed()
  102. {
  103. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_IP);
  104. $valuesExpected = array(
  105. array(Zend_Validate_Hostname::ALLOW_DNS, true, array('bob@212.212.20.4')),
  106. array(Zend_Validate_Hostname::ALLOW_DNS, false, array('bob@localhost'))
  107. );
  108. foreach ($valuesExpected as $element) {
  109. foreach ($element[2] as $input) {
  110. $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()));
  111. }
  112. }
  113. }
  114. /**
  115. * Ensures that validation fails when the local part is missing
  116. *
  117. * @return void
  118. */
  119. public function testLocalPartMissing()
  120. {
  121. $this->assertFalse($this->_validator->isValid('@example.com'));
  122. $messages = $this->_validator->getMessages();
  123. $this->assertEquals(1, count($messages));
  124. $this->assertContains('local-part@hostname', current($messages));
  125. }
  126. /**
  127. * Ensures that validation fails and produces the expected messages when the local part is invalid
  128. *
  129. * @return void
  130. */
  131. public function testLocalPartInvalid()
  132. {
  133. $this->assertFalse($this->_validator->isValid('Some User@example.com'));
  134. $messages = $this->_validator->getMessages();
  135. $this->assertEquals(3, count($messages));
  136. $this->assertContains('Some User', current($messages));
  137. $this->assertContains('dot-atom', current($messages));
  138. $this->assertContains('Some User', next($messages));
  139. $this->assertContains('quoted-string', current($messages));
  140. $this->assertContains('Some User', next($messages));
  141. $this->assertContains('not a valid local part', current($messages));
  142. }
  143. /**
  144. * Ensures that no validation failure message is produced when the local part follows the quoted-string format
  145. *
  146. * @return void
  147. */
  148. public function testLocalPartQuotedString()
  149. {
  150. $this->assertTrue($this->_validator->isValid('"Some User"@example.com'));
  151. $messages = $this->_validator->getMessages();
  152. $this->assertType('array', $messages);
  153. $this->assertEquals(0, count($messages));
  154. }
  155. /**
  156. * Ensures that validation fails when the hostname is invalid
  157. *
  158. * @return void
  159. */
  160. public function testHostnameInvalid()
  161. {
  162. $this->assertFalse($this->_validator->isValid('username@ example . com'));
  163. $messages = $this->_validator->getMessages();
  164. $this->assertThat(count($messages), $this->greaterThanOrEqual(1));
  165. $this->assertContains('not a valid hostname', current($messages));
  166. }
  167. /**
  168. * Ensures that quoted-string local part is considered valid
  169. *
  170. * @return void
  171. */
  172. public function testQuotedString()
  173. {
  174. $emailAddresses = array(
  175. '"username"@example.com',
  176. '"bob%jones"@domain.com',
  177. '"bob jones"@domain.com',
  178. '"bob@jones"@domain.com',
  179. '"[[ bob ]]"@domain.com',
  180. '"jones"@domain.com'
  181. );
  182. foreach ($emailAddresses as $input) {
  183. $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
  184. . implode("\n", $this->_validator->getMessages()));
  185. }
  186. }
  187. /**
  188. * Ensures that validation fails when the e-mail is given as for display,
  189. * with angle brackets around the actual address
  190. *
  191. * @return void
  192. */
  193. public function testEmailDisplay()
  194. {
  195. $this->assertFalse($this->_validator->isValid('User Name <username@example.com>'));
  196. $messages = $this->_validator->getMessages();
  197. $this->assertThat(count($messages), $this->greaterThanOrEqual(3));
  198. $this->assertContains('not a valid hostname', current($messages));
  199. $this->assertContains('cannot match TLD', next($messages));
  200. $this->assertContains('does not appear to be a valid local network name', next($messages));
  201. }
  202. /**
  203. * Ensures that the validator follows expected behavior for valid email addresses
  204. *
  205. * @return void
  206. */
  207. public function testBasicValid()
  208. {
  209. $emailAddresses = array(
  210. 'bob@domain.com',
  211. 'bob.jones@domain.co.uk',
  212. 'bob.jones.smythe@domain.co.uk',
  213. 'BoB@domain.museum',
  214. 'bobjones@domain.info',
  215. "B.O'Callaghan@domain.com",
  216. 'bob+jones@domain.us',
  217. 'bob+jones@domain.co.uk',
  218. 'bob@some.domain.uk.com',
  219. 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugar.com'
  220. );
  221. foreach ($emailAddresses as $input) {
  222. $this->assertTrue($this->_validator->isValid($input), "$input failed to pass validation:\n"
  223. . implode("\n", $this->_validator->getMessages()));
  224. }
  225. }
  226. /**
  227. * Ensures that the validator follows expected behavior for invalid email addresses
  228. *
  229. * @return void
  230. */
  231. public function testBasicInvalid()
  232. {
  233. $emailAddresses = array(
  234. '',
  235. 'bob
  236. @domain.com',
  237. 'bob jones@domain.com',
  238. '.bobJones@studio24.com',
  239. 'bobJones.@studio24.com',
  240. 'bob.Jones.@studio24.com',
  241. '"bob%jones@domain.com',
  242. 'bob@verylongdomainsupercalifragilisticexpialidociousaspoonfulofsugar.com',
  243. 'bob+domain.com',
  244. 'bob.domain.com',
  245. 'bob @domain.com',
  246. 'bob@ domain.com',
  247. 'bob @ domain.com',
  248. 'Abc..123@example.com'
  249. );
  250. foreach ($emailAddresses as $input) {
  251. $this->assertFalse($this->_validator->isValid($input), implode("\n", $this->_validator->getMessages()) . $input);
  252. }
  253. }
  254. /**
  255. * Ensures that the validator follows expected behavior for valid email addresses with complex local parts
  256. *
  257. * @return void
  258. */
  259. public function testComplexLocalValid()
  260. {
  261. $emailAddresses = array(
  262. 'Bob.Jones@domain.com',
  263. 'Bob.Jones!@domain.com',
  264. 'Bob&Jones@domain.com',
  265. '/Bob.Jones@domain.com',
  266. '#Bob.Jones@domain.com',
  267. 'Bob.Jones?@domain.com',
  268. 'Bob~Jones@domain.com'
  269. );
  270. foreach ($emailAddresses as $input) {
  271. $this->assertTrue($this->_validator->isValid($input));
  272. }
  273. }
  274. /**
  275. * Ensures that the validator follows expected behavior for checking MX records
  276. *
  277. * @return void
  278. */
  279. public function testMXRecords()
  280. {
  281. if (!defined('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
  282. || !constant('TESTS_ZEND_VALIDATE_ONLINE_ENABLED')
  283. ) {
  284. $this->markTestSkipped('Testing MX records only works when a valid internet connection is available');
  285. return;
  286. }
  287. $validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS, true);
  288. // Are MX checks supported by this system?
  289. if (!$validator->validateMxSupported()) {
  290. return true;
  291. }
  292. $valuesExpected = array(
  293. array(true, array('Bob.Jones@zend.com', 'Bob.Jones@studio24.net')),
  294. array(false, array('Bob.Jones@madeupdomain242424a.com', 'Bob.Jones@madeupdomain242424b.net'))
  295. );
  296. foreach ($valuesExpected as $element) {
  297. foreach ($element[1] as $input) {
  298. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  299. }
  300. }
  301. // Try a check via setting the option via a method
  302. unset($validator);
  303. $validator = new Zend_Validate_EmailAddress();
  304. $validator->setValidateMx(true);
  305. foreach ($valuesExpected as $element) {
  306. foreach ($element[1] as $input) {
  307. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  308. }
  309. }
  310. }
  311. /**
  312. * Test changing hostname settings via EmailAddress object
  313. *
  314. * @return void
  315. */
  316. public function testHostnameSettings()
  317. {
  318. $validator = new Zend_Validate_EmailAddress();
  319. // Check no IDN matching
  320. $validator->getHostnameValidator()->setValidateIdn(false);
  321. $valuesExpected = array(
  322. array(false, array('name@b�rger.de', 'name@h�llo.de', 'name@h�llo.se'))
  323. );
  324. foreach ($valuesExpected as $element) {
  325. foreach ($element[1] as $input) {
  326. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  327. }
  328. }
  329. // Check no TLD matching
  330. $validator->getHostnameValidator()->setValidateTld(false);
  331. $valuesExpected = array(
  332. array(true, array('name@domain.xx', 'name@domain.zz', 'name@domain.madeup'))
  333. );
  334. foreach ($valuesExpected as $element) {
  335. foreach ($element[1] as $input) {
  336. $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()));
  337. }
  338. }
  339. }
  340. /**
  341. * Ensures that getMessages() returns expected default value (an empty array)
  342. *
  343. * @return void
  344. */
  345. public function testGetMessages()
  346. {
  347. $this->assertEquals(array(), $this->_validator->getMessages());
  348. }
  349. /**
  350. * @see ZF-2861
  351. */
  352. public function testHostnameValidatorMessagesShouldBeTranslated()
  353. {
  354. require_once 'Zend/Validate/Hostname.php';
  355. $hostnameValidator = new Zend_Validate_Hostname();
  356. require_once 'Zend/Translate.php';
  357. $translations = array(
  358. 'hostnameIpAddressNotAllowed' => 'hostnameIpAddressNotAllowed translation',
  359. 'hostnameUnknownTld' => 'hostnameUnknownTld translation',
  360. 'hostnameDashCharacter' => 'hostnameDashCharacter translation',
  361. 'hostnameInvalidHostnameSchema' => 'hostnameInvalidHostnameSchema translation',
  362. 'hostnameUndecipherableTld' => 'hostnameUndecipherableTld translation',
  363. 'hostnameInvalidHostname' => 'hostnameInvalidHostname translation',
  364. 'hostnameInvalidLocalName' => 'hostnameInvalidLocalName translation',
  365. 'hostnameLocalNameNotAllowed' => 'hostnameLocalNameNotAllowed translation',
  366. );
  367. $translator = new Zend_Translate('array', $translations);
  368. $this->_validator->setTranslator($translator)->setHostnameValidator($hostnameValidator);
  369. $this->_validator->isValid('_XX.!!3xx@0.239,512.777');
  370. $messages = $hostnameValidator->getMessages();
  371. $found = false;
  372. foreach ($messages as $code => $message) {
  373. if (array_key_exists($code, $translations)) {
  374. $this->assertEquals($translations[$code], $message);
  375. $found = true;
  376. break;
  377. }
  378. }
  379. $this->assertTrue($found);
  380. }
  381. /**
  382. * @see ZF-4888
  383. */
  384. public function testEmailsExceedingLength()
  385. {
  386. $emailAddresses = array(
  387. 'thislocalpathoftheemailadressislongerthantheallowedsizeof64characters@domain.com',
  388. 'bob@verylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarverylongdomainsupercalifragilisticexpialidociousspoonfulofsugarexpialidociousspoonfulofsugar.com',
  389. );
  390. foreach ($emailAddresses as $input) {
  391. $this->assertFalse($this->_validator->isValid($input));
  392. }
  393. }
  394. /**
  395. * @ZF-4352
  396. */
  397. public function testNonStringValidation()
  398. {
  399. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  400. }
  401. }
  402. if (PHPUnit_MAIN_METHOD == 'Zend_Validate_EmailAddressTest::main') {
  403. Zend_Validate_EmailAddressTest::main();
  404. }