BxAntispamModuleTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Test Antispam module
  4. */
  5. class BxAntispamModuleTest extends BxDolTestCase
  6. {
  7. protected $_sSampleEmail = 'some@email.com';
  8. protected $_sSampleIP = '0.0.0.0';
  9. protected $_oModule;
  10. protected $_oMockIP;
  11. protected $_oMockDNSBlacklists;
  12. protected $_oMockDNSURIBlacklists;
  13. protected $_oMockStopForumSpam;
  14. protected $_oMockAkismet;
  15. protected function setUp()
  16. {
  17. bx_import('BxDolModule');
  18. $this->_oModule = BxDolModule::getInstance('bx_antispam');
  19. $this->_oMockIP = $this->bxMockGet('BxAntispamIP', $this->_oModule->_aModule);
  20. $this->_oMockDNSBlacklists = $this->bxMockGet('BxAntispamDNSBlacklists', $this->_oModule->_aModule, true);
  21. $this->_oMockDNSURIBlacklists = $this->bxMockGet('BxAntispamDNSURIBlacklists', $this->_oModule->_aModule, true);
  22. $this->_oMockStopForumSpam = $this->bxMockGet('BxAntispamStopForumSpam', $this->_oModule->_aModule);
  23. $this->_oMockAkismet = $this->bxMockGet('BxAntispamAkismet', $this->_oModule->_aModule);
  24. }
  25. protected function tearDown()
  26. {
  27. // free all mock objects after each test call
  28. $this->bxMockFree($this->_oMockIP);
  29. $this->bxMockFree($this->_oMockDNSBlacklists);
  30. $this->bxMockFree($this->_oMockDNSURIBlacklists);
  31. $this->bxMockFree($this->_oMockStopForumSpam);
  32. $this->bxMockFree($this->_oMockAkismet);
  33. // restore options after each test call
  34. $this->_oModule->_oConfig->restoreAntispamOptions();
  35. }
  36. public function providerForServiceIsSpam()
  37. {
  38. $any = $this->anything();
  39. return array(
  40. array(true, $any, $any, $any, $any, $any, $any, false), // no spam detection for admin account
  41. array(false, true, $any, $any, $any, $any, $any, false), // no spam detection for whitelisted IP
  42. array(false, false, 'on', false, $any, $any, 'on', false), // no spam detection if not listed in URIDNSBL (in "blocking" mode)
  43. array(false, false, 'on', true, $any, $any, 'on', true), // it is spam if listed in URIDNSBL (in "blocking" mode)
  44. array(false, false, '', $any, 'on', false, 'on', false), // no spam detection if not listed in Akismet (in "blocking" mode)
  45. array(false, false, '', $any, 'on', true, 'on', true), // it is spam if listed in Akismet (in "blocking" mode)
  46. array(false, false, 'on', true, 'on', true, '', false), // no spam detection in URIDNSBL nor Akismet if not in "blocking" mode
  47. );
  48. }
  49. /**
  50. * @dataProvider providerForServiceIsSpam
  51. */
  52. public function testServiceIsSpam($isAdmin, $bIpWhitelisted, $sUriDnsblEnable, $bUriDnsblBlacklisted, $sAkismetlEnable, $bAkismetBlacklisted, $sBlock, $bRes)
  53. {
  54. $this->_oModule->_oConfig->setAntispamOption('antispam_report', ''); // turn off reporting during testing
  55. $this->_oModule->_oConfig->setAntispamOption('uridnsbl_enable', $sUriDnsblEnable);
  56. $this->_oModule->_oConfig->setAntispamOption('akismet_enable', $sAkismetlEnable);
  57. $this->_oModule->_oConfig->setAntispamOption('antispam_block', $sBlock);
  58. $GLOBALS['logged']['admin'] = $isAdmin;
  59. if ($isAdmin) { // if admin no futher call are performed
  60. $this->_oMockIP->expects($this->never())->method('isIpWhitelisted');
  61. $this->_oMockDNSURIBlacklists->expects($this->never())->method('isSpam');
  62. $this->_oMockAkismet->expects($this->never())->method('isSpam');
  63. } else { // if not admin then check for whitelisting
  64. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  65. ->will($this->returnValue($bIpWhitelisted));
  66. }
  67. if ('on' != $sUriDnsblEnable || $isAdmin || $bIpWhitelisted) { // don't check in DNDBL if anything is true
  68. $this->_oMockDNSURIBlacklists->expects($this->never())->method('isSpam');
  69. } else { // check in URIDNSBL if URIDNSBL is enabled, not admin and IP is not whitelisted
  70. $this->_oMockDNSURIBlacklists->expects($this->once())->method('isSpam')
  71. ->will($this->returnValue($bUriDnsblBlacklisted));
  72. }
  73. if ('on' != $sAkismetlEnable || $isAdmin || $bIpWhitelisted || ($bUriDnsblBlacklisted && $sUriDnsblEnable)) { // don't check in Akismet if anything is true
  74. $this->_oMockAkismet->expects($this->never())->method('isSpam');
  75. } else { // check in Akismet if Akismet is enabled, not admin, IP is not whitelisted and not previouslu detected in DNSBL
  76. $this->_oMockAkismet->expects($this->once())->method('isSpam')
  77. ->will($this->returnValue($bAkismetBlacklisted));
  78. }
  79. // check result boolean value
  80. $sContent = $this->anything();
  81. $this->assertEquals($bRes, $this->_oModule->serviceIsSpam($sContent, $this->_sSampleIP));
  82. }
  83. public function providerForServiceCheckJoin()
  84. {
  85. $any = $this->anything();
  86. return array(
  87. array(true, $any, $any, $any, $any, false, false), // join is NOT allowed for blocked IPs
  88. array(false, '', $any, $any, false, false, true), // join is allowed if IP isn't blocked, DNSBL checking disabled, not listed in StopForumSpam
  89. array(false, 'on', 'approval', false, false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" mode, but not listed
  90. array(false, 'on', 'block', false, false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "block", but not listed
  91. array(false, 'on', 'approval', true, false, true, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" and IP listed
  92. array(false, 'on', 'block', true, $any, false, false), // join is NOT allowed if IP isn't blocked and DNSBL enabled in "block" and IP listed
  93. array(false, '', $any, $any, true, false, false), // join is NOT allowed if IP isn't blocked but listed in StopForumSpam
  94. );
  95. }
  96. /**
  97. * @dataProvider providerForServiceCheckJoin
  98. */
  99. public function testServiceCheckJoin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bStopForumSpamSpammer, $bResultSetApprove, $bResultEmptyString)
  100. {
  101. // set different options
  102. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  103. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_join', $sDnsblBehaviour);
  104. // first IP address block checking is called
  105. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  106. ->with($this->equalTo($this->_sSampleIP))
  107. ->will($this->returnValue($bIpBlocked));
  108. if ($bIpBlocked) { // if ip is blocked no other methods should be called
  109. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  110. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  111. } elseif (!$bIpBlocked) { // if ip is NOT blocked - perform further checks
  112. if ('on' != $sDnsblEnable) { // DNSBL shouldn't be called if it isn't enabled
  113. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  114. } elseif ('on' == $sDnsblEnable) {
  115. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  116. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  117. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  118. }
  119. if ('on' == $sDnsblEnable && $bDnsblIpBlacklisted && 'block' == $sDnsblBehaviour) { // StopForumSpam shouldn't be called if DNSBL detected spam
  120. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  121. } else {
  122. $this->_oMockStopForumSpam->expects($this->once())->method('isSpammer')
  123. ->will($this->returnValue($bStopForumSpamSpammer));
  124. }
  125. }
  126. // empty string - no spam detected
  127. $bSetApprove = false;
  128. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckJoin($this->_sSampleEmail, $bSetApprove, $this->_sSampleIP)));
  129. $this->assertTrue($bResultSetApprove == $bSetApprove);
  130. }
  131. public function providerForServiceCheckLogin()
  132. {
  133. $any = $this->anything();
  134. return array(
  135. array(true, $any, $any, $any, false),
  136. array(false, false, $any, $any, true),
  137. array(false, true, 'log', false, true),
  138. array(false, true, 'block', false, true),
  139. array(false, true, 'log', true, true),
  140. array(false, true, 'block', true, false),
  141. );
  142. }
  143. /**
  144. * @dataProvider providerForServiceCheckLogin
  145. */
  146. public function testServiceCheckLogin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bResultEmptyString)
  147. {
  148. // set different options
  149. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  150. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_login', $sDnsblBehaviour);
  151. // first IP address block checking is called
  152. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  153. ->will($this->returnValue($bIpBlocked));
  154. if ($bIpBlocked || 'on' != $sDnsblEnable) { // if ip is blocked, or DNSBL isn't enabled - DNSBL checking shouldn't be called
  155. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  156. } elseif (!$bIpBlocked && 'on' == $sDnsblEnable) { // call DNSBL checking only if enabled and IP isn't already blocked
  157. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  158. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->_sSampleIP)
  159. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  160. }
  161. // empty string - no spam detected
  162. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckLogin($this->_sSampleIP)));
  163. }
  164. public function providerForServiceIsIpDnsBlacklisted()
  165. {
  166. return array(
  167. array(false, false, false),
  168. array(true, false, true),
  169. array(true, true, false),
  170. array(false, true, false),
  171. );
  172. }
  173. /**
  174. * @dataProvider providerForServiceIsIpDnsBlacklisted
  175. */
  176. public function testServiceIsIpDnsBlacklisted($bDnsblSpammer, $bDnsblWhitelisted, $bRes)
  177. {
  178. // check of whitelisting should be called once
  179. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  180. ->with($this->equalTo($this->_sSampleIP))
  181. ->will($this->returnValue(false));
  182. // check in 'spammers' chain
  183. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  184. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  185. ->will($this->returnValue($bDnsblSpammer ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  186. if ($bDnsblSpammer) { // if found in 'spammers' chain, then check if it is whitelisted in 'whitelist' chain
  187. $this->_oMockDNSBlacklists->expects($this->at(1))->method('dnsbl_lookup_ip')
  188. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_WHITELIST), $this->equalTo($this->_sSampleIP))
  189. ->will($this->returnValue($bDnsblWhitelisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  190. }
  191. // check result boolean value
  192. $this->assertEquals($bRes, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  193. }
  194. public function testServiceIsIpDnsBlacklistedWhenIpIsWhiletisted()
  195. {
  196. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  197. ->will($this->returnValue(true));
  198. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  199. $this->assertEquals(false, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  200. }
  201. public function testServiceIsIpWhitelisted()
  202. {
  203. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted');
  204. $this->_oModule->serviceIsIpWhitelisted($this->_sSampleIP);
  205. }
  206. public function testServiceIsIpBlocked()
  207. {
  208. $this->_oMockIP->expects($this->once())->method('isIpBlocked');
  209. $this->_oModule->serviceIsIpBlocked($this->_sSampleIP);
  210. }
  211. public function testServiceBlockIp()
  212. {
  213. $this->_oMockIP->expects($this->once())->method('blockIp');
  214. $this->_oModule->serviceBlockIp($this->_sSampleIP);
  215. }
  216. }