AkismetTest.php 8.4 KB

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