AkismetTest.php 8.2 KB

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