2
0

AkismetTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_Akismet
  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$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Service_Akismet
  28. */
  29. require_once 'Zend/Service/Akismet.php';
  30. /**
  31. * @see Zend_Http_Client_Adapter_Test
  32. */
  33. require_once 'Zend/Http/Client/Adapter/Test.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Service_Akismet
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Service
  41. * @group Zend_Service_Akismet
  42. */
  43. class Zend_Service_AkismetTest extends PHPUnit_Framework_TestCase
  44. {
  45. public function setUp()
  46. {
  47. $this->akismet = new Zend_Service_Akismet('somebogusapikey', 'http://framework.zend.com/wiki/');
  48. $adapter = new Zend_Http_Client_Adapter_Test();
  49. $client = new Zend_Http_Client(null, array(
  50. 'adapter' => $adapter
  51. ));
  52. $this->adapter = $adapter;
  53. Zend_Service_Akismet::setHttpClient($client);
  54. $this->comment = array(
  55. 'user_ip' => '71.161.221.76',
  56. 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1)',
  57. 'comment_type' => 'comment',
  58. 'comment_content' => 'spam check'
  59. );
  60. }
  61. public function testBlogUrl()
  62. {
  63. $this->assertEquals('http://framework.zend.com/wiki/', $this->akismet->getBlogUrl());
  64. $this->akismet->setBlogUrl('http://framework.zend.com/');
  65. $this->assertEquals('http://framework.zend.com/', $this->akismet->getBlogUrl());
  66. }
  67. public function testApiKey()
  68. {
  69. $this->assertEquals('somebogusapikey', $this->akismet->getApiKey());
  70. $this->akismet->setApiKey('invalidapikey');
  71. $this->assertEquals('invalidapikey', $this->akismet->getApiKey());
  72. }
  73. public function testCharset()
  74. {
  75. $this->assertEquals('UTF-8', $this->akismet->getCharset());
  76. $this->akismet->setCharset('ISO-8859-1');
  77. $this->assertEquals('ISO-8859-1', $this->akismet->getCharset());
  78. }
  79. public function testPort()
  80. {
  81. $this->assertEquals(80, $this->akismet->getPort());
  82. $this->akismet->setPort(8080);
  83. $this->assertEquals(8080, $this->akismet->getPort());
  84. }
  85. public function testUserAgent()
  86. {
  87. $this->akismet->setUserAgent('MyUserAgent/1.0 | Akismet/1.11');
  88. $this->assertEquals('MyUserAgent/1.0 | Akismet/1.11', $this->akismet->getUserAgent());
  89. }
  90. public function testUserAgentDefaultMatchesFrameworkVersion()
  91. {
  92. $this->assertContains('Zend Framework/' . Zend_Version::VERSION, $this->akismet->getUserAgent());
  93. }
  94. public function testVerifyKey()
  95. {
  96. $response = "HTTP/1.0 200 OK\r\n"
  97. . "Content-type: text/plain; charset=utf-8\r\n"
  98. . "Content-length: 5\r\n"
  99. . "Server: LiteSpeed\r\n"
  100. . "Date: Tue, 06 Feb 2007 14:41:24 GMT\r\n"
  101. . "Connection: close\r\n"
  102. . "\r\n"
  103. . "valid";
  104. $this->adapter->setResponse($response);
  105. $this->assertTrue($this->akismet->verifyKey());
  106. $response = "HTTP/1.0 200 OK\r\n"
  107. . "Content-type: text/plain; charset=utf-8\r\n"
  108. . "Content-length: 7\r\n"
  109. . "Server: LiteSpeed\r\n"
  110. . "Date: Tue, 06 Feb 2007 14:41:24 GMT\r\n"
  111. . "Connection: close\r\n"
  112. . "\r\n"
  113. . "invalid";
  114. $this->adapter->setResponse($response);
  115. $this->assertFalse($this->akismet->verifyKey());
  116. }
  117. public function testIsSpamThrowsExceptionOnInvalidKey()
  118. {
  119. $response = "HTTP/1.0 200 OK\r\n"
  120. . "X-powered-by: PHP/4.4.2\r\n"
  121. . "Content-type: text/plain; charset=utf-8\r\n"
  122. . "X-akismet-server: 72.21.44.242\r\n"
  123. . "Content-length: 7\r\n"
  124. . "Server: LiteSpeed\r\n"
  125. . "Date: Tue, 06 Feb 2007 14:50:24 GMT\r\n"
  126. . "Connection: close\r\n"
  127. . "\r\n"
  128. . "invalid";
  129. $this->adapter->setResponse($response);
  130. try {
  131. $this->akismet->isSpam($this->comment);
  132. $this->fail('Response of "invalid" should trigger exception');
  133. } catch (Exception $e) {
  134. // success
  135. }
  136. }
  137. public function testIsSpam()
  138. {
  139. $response = "HTTP/1.0 200 OK\r\n"
  140. . "X-powered-by: PHP/4.4.2\r\n"
  141. . "Content-type: text/plain; charset=utf-8\r\n"
  142. . "X-akismet-server: 72.21.44.242\r\n"
  143. . "Content-length: 4\r\n"
  144. . "Server: LiteSpeed\r\n"
  145. . "Date: Tue, 06 Feb 2007 14:50:24 GMT\r\n"
  146. . "Connection: close\r\n"
  147. . "\r\n"
  148. . "true";
  149. $this->adapter->setResponse($response);
  150. $this->assertTrue($this->akismet->isSpam($this->comment));
  151. $response = "HTTP/1.0 200 OK\r\n"
  152. . "X-powered-by: PHP/4.4.2\r\n"
  153. . "Content-type: text/plain; charset=utf-8\r\n"
  154. . "X-akismet-server: 72.21.44.242\r\n"
  155. . "Content-length: 5\r\n"
  156. . "Server: LiteSpeed\r\n"
  157. . "Date: Tue, 06 Feb 2007 14:50:24 GMT\r\n"
  158. . "Connection: close\r\n"
  159. . "\r\n"
  160. . "false";
  161. $this->adapter->setResponse($response);
  162. $this->assertFalse($this->akismet->isSpam($this->comment));
  163. }
  164. public function testSubmitSpamThrowsExceptionOnInvalidKey()
  165. {
  166. $response = "HTTP/1.0 200 OK\r\n"
  167. . "X-powered-by: PHP/4.4.2\r\n"
  168. . "Content-type: text/plain; charset=utf-8\r\n"
  169. . "X-akismet-server: 72.21.44.242\r\n"
  170. . "Content-length: 7\r\n"
  171. . "Server: LiteSpeed\r\n"
  172. . "Date: Tue, 06 Feb 2007 14:50:24 GMT\r\n"
  173. . "Connection: close\r\n"
  174. . "\r\n"
  175. . "invalid";
  176. $this->adapter->setResponse($response);
  177. try {
  178. $this->akismet->submitSpam($this->comment);
  179. $this->fail('Response of "invalid" should trigger exception');
  180. } catch (Exception $e) {
  181. // success
  182. }
  183. }
  184. public function testSubmitSpam()
  185. {
  186. $response = "HTTP/1.0 200 OK\r\n"
  187. . "X-powered-by: PHP/4.4.2\r\n"
  188. . "Content-type: text/html\r\n"
  189. . "Content-length: 41\r\n"
  190. . "Server: LiteSpeed\r\n"
  191. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  192. . "Connection: close\r\n"
  193. . "\r\n"
  194. . "Thanks for making the web a better place.";
  195. $this->adapter->setResponse($response);
  196. try {
  197. $this->akismet->submitSpam($this->comment);
  198. } catch (Exception $e) {
  199. $this->fail('Valid key should not throw exceptions');
  200. }
  201. }
  202. public function testSubmitHam()
  203. {
  204. $response = "HTTP/1.0 200 OK\r\n"
  205. . "X-powered-by: PHP/4.4.2\r\n"
  206. . "Content-type: text/html\r\n"
  207. . "Content-length: 41\r\n"
  208. . "Server: LiteSpeed\r\n"
  209. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  210. . "Connection: close\r\n"
  211. . "\r\n"
  212. . "Thanks for making the web a better place.";
  213. $this->adapter->setResponse($response);
  214. try {
  215. $this->akismet->submitHam($this->comment);
  216. } catch (Exception $e) {
  217. $this->fail('Valid key should not throw exceptions');
  218. }
  219. }
  220. }