2
0

MailHideTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_Service_ReCaptcha
  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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /** @see Zend_Service_ReCaptcha_MailHide */
  27. require_once 'Zend/Service/ReCaptcha/MailHide.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Service_ReCaptcha
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Service
  35. * @group Zend_Service_ReCaptcha
  36. */
  37. class Zend_Service_ReCaptcha_MailHideTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_publicKey = TESTS_ZEND_SERVICE_RECAPTCHA_MAILHIDE_PUBLIC_KEY;
  40. protected $_privateKey = TESTS_ZEND_SERVICE_RECAPTCHA_MAILHIDE_PRIVATE_KEY;
  41. protected $_mailHide = null;
  42. public function setUp() {
  43. $this->_mailHide = new Zend_Service_ReCaptcha_MailHide();
  44. }
  45. public function testSetGetPrivateKey() {
  46. $this->_mailHide->setPrivateKey($this->_privateKey);
  47. $this->assertSame($this->_privateKey, $this->_mailHide->getPrivateKey());
  48. }
  49. public function testSetGetEmail() {
  50. $mail = 'mail@example.com';
  51. $this->_mailHide->setEmail($mail);
  52. $this->assertSame($mail, $this->_mailHide->getEmail());
  53. $this->assertSame('example.com', $this->_mailHide->getEmailDomainPart());
  54. }
  55. public function testEmailLocalPart() {
  56. $this->_mailHide->setEmail('abcd@example.com');
  57. $this->assertSame('a', $this->_mailHide->getEmailLocalPart());
  58. $this->_mailHide->setEmail('abcdef@example.com');
  59. $this->assertSame('abc', $this->_mailHide->getEmailLocalPart());
  60. $this->_mailHide->setEmail('abcdefg@example.com');
  61. $this->assertSame('abcd', $this->_mailHide->getEmailLocalPart());
  62. }
  63. public function testConstructor() {
  64. $mail = 'mail@example.com';
  65. $options = array(
  66. 'theme' => 'black',
  67. 'lang' => 'no',
  68. );
  69. $config = new Zend_Config($options);
  70. $mailHide = new Zend_Service_ReCaptcha_MailHide($this->_publicKey, $this->_privateKey, $mail, $config);
  71. $_options = $mailHide->getOptions();
  72. $this->assertSame($this->_publicKey, $mailHide->getPublicKey());
  73. $this->assertSame($this->_privateKey, $mailHide->getPrivateKey());
  74. $this->assertSame($mail, $mailHide->getEmail());
  75. $this->assertSame($options['theme'], $_options['theme']);
  76. $this->assertSame($options['lang'], $_options['lang']);
  77. }
  78. protected function _checkHtml($html) {
  79. $server = Zend_Service_ReCaptcha_MailHide::MAILHIDE_SERVER;
  80. $pubKey = $this->_publicKey;
  81. // Static value of the encrypter version of mail@example.com
  82. $encryptedEmail = 'XydrEdd6Eo90PE-LpxkmTEsq2G6SCeDzWkEQpF6f7v8=';
  83. $this->assertNotSame(false, strstr($html, 'm<a href="' . $server . '?k=' . $pubKey . '&amp;c=' . $encryptedEmail . '" onclick="window.open(\'' . $server . '?k=' . $pubKey . '&amp;c=' . $encryptedEmail . '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300\'); return false;" title="Reveal this e-mail address">...</a>@example.com'));
  84. }
  85. public function testGetHtml() {
  86. $mail = 'mail@example.com';
  87. $this->_mailHide->setEmail($mail);
  88. $this->_mailHide->setPublicKey($this->_publicKey);
  89. $this->_mailHide->setPrivateKey($this->_privateKey);
  90. $html = $this->_mailHide->getHtml();
  91. $this->_checkHtml($html);
  92. }
  93. public function testGetHtmlWithNoEmail() {
  94. $this->setExpectedException('Zend_Service_ReCaptcha_MailHide_Exception');
  95. $html = $this->_mailHide->getHtml();
  96. }
  97. public function testGetHtmlWithMissingPublicKey() {
  98. $this->setExpectedException('Zend_Service_ReCaptcha_MailHide_Exception');
  99. $mail = 'mail@example.com';
  100. $this->_mailHide->setEmail($mail);
  101. $this->_mailHide->setPrivateKey($this->_privateKey);
  102. $html = $this->_mailHide->getHtml();
  103. }
  104. public function testGetHtmlWithMissingPrivateKey() {
  105. $this->setExpectedException('Zend_Service_ReCaptcha_MailHide_Exception');
  106. $mail = 'mail@example.com';
  107. $this->_mailHide->setEmail($mail);
  108. $this->_mailHide->setPublicKey($this->_publicKey);
  109. $html = $this->_mailHide->getHtml();
  110. }
  111. public function testGetHtmlWithParamter() {
  112. $mail = 'mail@example.com';
  113. $this->_mailHide->setPublicKey($this->_publicKey);
  114. $this->_mailHide->setPrivateKey($this->_privateKey);
  115. $html = $this->_mailHide->getHtml($mail);
  116. $this->_checkHtml($html);
  117. }
  118. }