From 4fb199ccce268bd6d5118a19a3e2b50658da1959 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Thu, 5 Feb 2015 16:35:49 +0200 Subject: [PATCH 1/2] initiate translation DI object after bb-di is included $di['translate'](); fix #249 --- src/bb-cron.php | 1 + src/install/index.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bb-cron.php b/src/bb-cron.php index 6275e287d..c692d46c9 100644 --- a/src/bb-cron.php +++ b/src/bb-cron.php @@ -13,6 +13,7 @@ require_once dirname(__FILE__) . '/bb-load.php'; $di = include dirname(__FILE__) . '/bb-di.php'; +$di['translate'](); $interval = isset($argv[1]) ? $argv[1] : null; $service = $di['mod_service']('cron'); diff --git a/src/install/index.php b/src/install/index.php index 6a046c94f..f5fbc7e6d 100644 --- a/src/install/index.php +++ b/src/install/index.php @@ -455,6 +455,7 @@ final class Box_Installer $emailService = new \Box\Mod\Email\Service(); $di = $di = include BB_PATH_ROOT . '/bb-di.php'; + $di['translate'](); $emailService->setDi($di); return $emailService->templateBatchGenerate(); } From 81810838fea32e7b6bcea02b7b27701ce11eb902 Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Fri, 6 Feb 2015 10:40:32 +0200 Subject: [PATCH 2/2] updated spamchecker module issue ##248 --- src/bb-modules/Spamchecker/Api/Guest.php | 3 +- src/bb-modules/Spamchecker/Service.php | 120 ++++--- .../bb-modules/Spamchecker/Api/GuestTest.php | 88 +++++ tests/bb-modules/Spamchecker/ServiceTest.php | 300 ++++++++++++++++++ .../mod_spamchecker/ServiceTest.php | 17 +- 5 files changed, 481 insertions(+), 47 deletions(-) create mode 100644 tests/bb-modules/Spamchecker/Api/GuestTest.php create mode 100644 tests/bb-modules/Spamchecker/ServiceTest.php diff --git a/src/bb-modules/Spamchecker/Api/Guest.php b/src/bb-modules/Spamchecker/Api/Guest.php index d1bbc4c0f..4c269cfde 100644 --- a/src/bb-modules/Spamchecker/Api/Guest.php +++ b/src/bb-modules/Spamchecker/Api/Guest.php @@ -23,8 +23,7 @@ class Guest extends \Api_Abstract */ public function recaptcha($data) { - $api = $this->getApiAdmin(); - $config = $api->extension_config_get(array("ext"=>"mod_spamchecker")); + $config = $this->di['mod_config']('Spamchecker'); $result = array( 'publickey' => isset($config['captcha_recaptcha_publickey']) ? $config['captcha_recaptcha_publickey'] : null, 'enabled' => isset($config['captcha_enabled']) ? $config['captcha_enabled'] : false, diff --git a/src/bb-modules/Spamchecker/Service.php b/src/bb-modules/Spamchecker/Service.php index efe98303e..73c415bfa 100644 --- a/src/bb-modules/Spamchecker/Service.php +++ b/src/bb-modules/Spamchecker/Service.php @@ -11,62 +11,92 @@ */ namespace Box\Mod\Spamchecker; -class Service + +use Box\InjectionAwareInterface; + +class Service implements InjectionAwareInterface { - public static function onBeforeClientCreateForumTopic(\Box_Event $event) + + /** + * @var \Box_Di + */ + protected $di = null; + + /** + * @param \Box_Di $di + */ + public function setDi($di) { - $api = $event->getApiAdmin(); + $this->di = $di; + } + + /** + * @return \Box_Di + */ + public function getDi() + { + return $this->di; + } + + public function forumSpamChecker(\Box_Event $event) + { + $di = $event->getDi(); $params = $event->getParameters(); - $client = $api->client_get(array('id'=>$params['client_id'])); + $client = $di['db']->load('Client', $params['client_id']); $comment = array( 'comment_type' => 'comment', - 'comment_author' => $client['first_name'] . ' ' . $client['last_name'], - 'comment_author_email' => $client['email'], + 'comment_author' => $client->first_name . ' ' . $client->last_name, + 'comment_author_email' => $client->email, 'comment_content' => $params['message'], ); - self::_isCommentSpam($event, $comment); - self::_isBlockedIp($event); + + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->isCommentSpam($event, $comment); + $spamCheckerService->isBlockedIp($event); + } + + public static function onBeforeClientCreateForumTopic(\Box_Event $event) + { + $di = $event->getDi(); + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->forumSpamChecker($event); } public static function onBeforeClientRepliedInForum(\Box_Event $event) { - $api = $event->getApiAdmin(); - $params = $event->getParameters(); - $client = $api->client_get(array('id'=>$params['client_id'])); - $comment = array( - 'comment_type' => 'comment', - 'comment_author' => $client['first_name'] . ' ' . $client['last_name'], - 'comment_author_email' => $client['email'], - 'comment_content' => $params['message'], - ); - self::_isCommentSpam($event, $comment); - self::_isBlockedIp($event); + $di = $event->getDi(); + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->forumSpamChecker($event); } public static function onBeforeClientSignUp(\Box_Event $event) { - self::_isBlockedIp($event); - self::_isSpam($event); + $di = $event->getDi(); + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->isBlockedIp($event); + $spamCheckerService->isSpam($event); } public static function onBeforeGuestPublicTicketOpen(\Box_Event $event) { - self::_isBlockedIp($event); - self::_isSpam($event); + $di = $event->getDi(); + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->isBlockedIp($event); + $spamCheckerService->isSpam($event); } /** * @param \Box_Event $event */ - private static function _isBlockedIp($event) + public function isBlockedIp($event) { - $api = $event->getApiAdmin(); - $config = $api->extension_config_get(array("ext"=>"mod_spamchecker")); + $di = $event->getDi(); + $config = $di['mod_config']('Spamchecker'); if(isset($config['block_ips']) && $config['block_ips'] && isset($config['blocked_ips'])) { $blocked_ips = explode(PHP_EOL, $config['blocked_ips']); array_walk($blocked_ips, create_function('&$val', '$val = trim($val);')); - if(in_array(Box_Tools::getIpv4(), $blocked_ips)) { - throw new \Box_Exception('IP :ip is blocked', array(':ip'=>Box_Tools::getIpv4()), 403); + if(in_array($di['tools']->getIpv4(), $blocked_ips)) { + throw new \Box_Exception('IP :ip is blocked', array(':ip'=>$di['tools']->getIpv4()), 403); } } } @@ -74,21 +104,22 @@ class Service /** * @param \Box_Event $event */ - private static function _isCommentSpam($event, $comment) + public function isCommentSpam($event, $comment) { - $api = $event->getApiAdmin(); - - $config = $api->extension_config_get(array("ext"=>"mod_spamchecker")); + $di = $event->getDi(); + $config = $di['mod_config']('Spamchecker'); if(!isset($config['akismet_enabled']) || !$config['akismet_enabled']) { return false; } require_once BB_PATH_MODS . '/Spamchecker/akismet.curl.class.php'; - $akismet = new akismet($config['akismet_api_key'], BB_URL); + $akismet = new \akismet($config['akismet_api_key'], $di['config']['url']); if(!$akismet->valid_key()) { - if($event->getApiGuest()->extension_is_on(array('mod'=>'notification'))) { - $api->notification_add(array('message'=>'Akismet Key is not valid!')); + $extensionService = $di['mod_service']('Extension'); + if($extensionService->isExtensionActive('mod', 'notification')) { + $notificationService = $di['mod_service']('Notification'); + $notificationService->create('Akismet Key is not valid!'); } else { error_log('Akismet Key is not valid!'); } @@ -99,10 +130,10 @@ class Service throw new \Box_Exception('Akismet detected this message is spam'); } } - - private static function _isSpam(\Box_Event $event) + + public function isSpam(\Box_Event $event) { - $api = $event->getApiAdmin(); + $di = $event->getDi(); $params = $event->getParameters(); $data = array( 'ip' => isset($params['ip']) ? $params['ip'] : NULL, @@ -110,8 +141,8 @@ class Service 'recaptcha_challenge_field' => isset($params['recaptcha_challenge_field']) ? $params['recaptcha_challenge_field'] : NULL, 'recaptcha_response_field' => isset($params['recaptcha_response_field']) ? $params['recaptcha_response_field'] : NULL, ); - - $config = $api->extension_config_get(array("ext"=>"mod_spamchecker")); + + $config = $di['mod_config']('Spamchecker'); if(isset($config['captcha_enabled']) && $config['captcha_enabled']) { @@ -133,7 +164,8 @@ class Service } if(isset($config['sfs']) && $config['sfs']) { - self::_isInStopForumSpamDatabase($data); + $spamCheckerService = $di['mod_service']('Spamchecker'); + $spamCheckerService->isInStopForumSpamDatabase($data); } } @@ -147,14 +179,14 @@ class Service * @param array $data * @return bool */ - private static function _isInStopForumSpamDatabase(array $data) + public function isInStopForumSpamDatabase(array $data) { $data['f'] = 'json'; $url = 'http://www.stopforumspam.com/api?'.http_build_query($data); - $file_contents = file_get_contents($url); + $file_contents = $this->di['tools']->file_get_contents($url); $json = json_decode($file_contents); - if(!is_object($json) || !$json->success) { + if(!is_object($json) || isset($json->success) && !$json->success) { return false; } diff --git a/tests/bb-modules/Spamchecker/Api/GuestTest.php b/tests/bb-modules/Spamchecker/Api/GuestTest.php new file mode 100644 index 000000000..e352602c2 --- /dev/null +++ b/tests/bb-modules/Spamchecker/Api/GuestTest.php @@ -0,0 +1,88 @@ +api = new \Box\Mod\Spamchecker\Api\Guest(); + } + + public function testgetDi() + { + $di = new \Box_Di(); + $this->api->setDi($di); + $getDi = $this->api->getDi(); + $this->assertEquals($di, $getDi); + } + + public function datarecaptchaConfig() + { + return array( + array( + array( + 'captcha_recaptcha_publickey' => 1234, + 'captcha_enabled' => true, + ), + array( + 'publickey' => 1234, + 'enabled' => true, + ), + ), + array( + array( + + 'captcha_enabled' => true, + ), + array( + 'publickey' => null, + 'enabled' => true, + ), + ), + array( + array( + 'captcha_recaptcha_publickey' => 1234, + 'captcha_enabled' => false, + ), + array( + 'publickey' => 1234, + 'enabled' => false, + ), + ), + array( + array( + 'captcha_enabled' => false, + ), + array( + 'publickey' => null, + 'enabled' => false, + ), + ), + ); + } + + /** + * @dataProvider datarecaptchaConfig + */ + public function testrecaptcha($config, $expected) + { + $di = new \Box_Di(); + $di['mod_config'] = $di->protect(function () use ($config){ + return $config; + }); + + $this->api->setDi($di); + $result = $this->api->recaptcha(array()); + + $this->assertEquals($expected, $result); + + + } +} \ No newline at end of file diff --git a/tests/bb-modules/Spamchecker/ServiceTest.php b/tests/bb-modules/Spamchecker/ServiceTest.php new file mode 100644 index 000000000..3bc3ec24f --- /dev/null +++ b/tests/bb-modules/Spamchecker/ServiceTest.php @@ -0,0 +1,300 @@ +service= new \Box\Mod\Spamchecker\Service(); + } + + public function testgetDi() + { + $di = new \Box_Di(); + $this->service->setDi($di); + $getDi = $this->service->getDi(); + $this->assertEquals($di, $getDi); + } + + public function testforumSpamChecker() + { + $params = array( + 'client_id' => 1, + 'message' => 'Hello world', + ); + + $model = new \Model_Client(); + $model->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->willReturn($model); + + $spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock(); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isCommentSpam'); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isBlockedIp'); + + $di = new \Box_Di(); + $di['db'] = $dbMock; + $di['mod_service'] = $di->protect(function () use ($spamCheckerService){ + return $spamCheckerService; + }); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $this->service->forumSpamChecker($boxEventMock); + } + + public function testonBeforeClientCreateForumTopic() + { + $spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock(); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('forumSpamChecker'); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function () use ($spamCheckerService){ + return $spamCheckerService; + }); + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->onBeforeClientCreateForumTopic($boxEventMock); + } + + public function testonBeforeClientRepliedInForum() + { + $spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock(); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('forumSpamChecker'); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function () use ($spamCheckerService){ + return $spamCheckerService; + }); + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->onBeforeClientRepliedInForum($boxEventMock); + } + + public function testonBeforeClientSignUp() + { + $spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock(); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isBlockedIp'); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isSpam'); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function () use ($spamCheckerService){ + return $spamCheckerService; + }); + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->onBeforeClientSignUp($boxEventMock); + } + + public function testonBeforeGuestPublicTicketOpen() + { + $spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock(); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isBlockedIp'); + $spamCheckerService->expects($this->atLeastOnce()) + ->method('isSpam'); + + $di = new \Box_Di(); + $di['mod_service'] = $di->protect(function () use ($spamCheckerService){ + return $spamCheckerService; + }); + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->onBeforeGuestPublicTicketOpen($boxEventMock); + } + + public function testisBlockedIp_IpBlocked() + { + $clientIp = '1.1.1.1'; + $modConfig = array( + 'block_ips' => true, + 'blocked_ips' => '1.1.1.1'.PHP_EOL.'2.2.2.2', + ); + + + $toolsMock = $this->getMockBuilder('\Box_Tools')->getMock(); + $toolsMock->expects($this->atLeastOnce()) + ->method('getIpv4') + ->willReturn($clientIp); + + $di = new \Box_Di(); + $di['mod_config'] = $di->protect(function($modName) use ($modConfig ){ + if ($modName == 'Spamchecker'){ + return $modConfig; + } + }); + $di['tools'] = $toolsMock; + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->setExpectedException('\Box_Exception', sprintf("IP %s is blocked", $clientIp), 403); + $this->service->isBlockedIp($boxEventMock); + } + + public function testisBlockedIp_IpNotBlocked() + { + $clientIp = '214.1.4.99'; + $modConfig = array( + 'block_ips' => true, + 'blocked_ips' => '1.1.1.1'.PHP_EOL.'2.2.2.2', + ); + + + $toolsMock = $this->getMockBuilder('\Box_Tools')->getMock(); + $toolsMock->expects($this->atLeastOnce()) + ->method('getIpv4') + ->willReturn($clientIp); + + $di = new \Box_Di(); + $di['mod_config'] = $di->protect(function($modName) use ($modConfig ){ + if ($modName == 'Spamchecker'){ + return $modConfig; + } + }); + $di['tools'] = $toolsMock; + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->isBlockedIp($boxEventMock); + } + + public function testisBlockedIp_BlockIpsNotEnabled() + { + $modConfig = array( + 'block_ips' => false, + ); + + + $di = new \Box_Di(); + $di['mod_config'] = $di->protect(function($modName) use ($modConfig ){ + if ($modName == 'Spamchecker'){ + return $modConfig; + } + }); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor() + ->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + + $this->service->isBlockedIp($boxEventMock); + } + + public function testisInStopForumSpamDatabase_InvalidResponse() + { + $toolsMock = $this->getMockBuilder('\Box_Tools')->getMock(); + $toolsMock->expects($this->atLeastOnce()) + ->method('file_get_contents') + ->willReturn('{}'); + + $di = new \Box_Di(); + $di['tools'] = $toolsMock; + + $data = array(); + $this->service->setDi($di); + $result = $this->service->isInStopForumSpamDatabase($data); + $this->assertFalse($result); + } + + public function dataProviderSpamResponses() + { + return array( + array( + '{"success" : "true", "username" : {"appears" : "true" }}', 'Your Username is blacklisted in global database' + ), + array( + '{"success" : "true", "email" : {"appears" : "true" }}', 'Your Email is blacklisted in global database' + ), + array( + '{"success" : "true", "ip" : {"appears" : "true" }}', 'Your IP is blacklisted in global database' + ), + ); + } + + /** + * @dataProvider dataProviderSpamResponses + */ + public function testisInStopForumSpamDatabase_UserNameBlackListed($json, $exceptionMessage) + { + $toolsMock = $this->getMockBuilder('\Box_Tools')->getMock(); + $toolsMock->expects($this->atLeastOnce()) + ->method('file_get_contents') + ->willReturn($json); + + $di = new \Box_Di(); + $di['tools'] = $toolsMock; + + $data = array(); + $this->service->setDi($di); + $this->setExpectedException('Box_Exception', $exceptionMessage); + $this->service->isInStopForumSpamDatabase($data); + } + + public function testisInStopForumSpamDatabase_NotExists() + { + $json = '{"success" : "true", "username" : {}}'; + $toolsMock = $this->getMockBuilder('\Box_Tools')->getMock(); + $toolsMock->expects($this->atLeastOnce()) + ->method('file_get_contents') + ->willReturn($json); + + $di = new \Box_Di(); + $di['tools'] = $toolsMock; + + $data = array(); + $this->service->setDi($di); + $result = $this->service->isInStopForumSpamDatabase($data); + $this->assertFalse($result); + } + + +} \ No newline at end of file diff --git a/tests/integration/bb-modules/mod_spamchecker/ServiceTest.php b/tests/integration/bb-modules/mod_spamchecker/ServiceTest.php index dd1b74626..d4c29b968 100644 --- a/tests/integration/bb-modules/mod_spamchecker/ServiceTest.php +++ b/tests/integration/bb-modules/mod_spamchecker/ServiceTest.php @@ -12,9 +12,24 @@ class Box_Mod_Spamchecker_ServiceTest extends BBDbApiTestCase 'ip' => '127.0.0.1', ); $event = new Box_Event(null, 'any', $parameters, $this->api_admin); - + $event->setDi($this->di); + $object = new \Box\Mod\Spamchecker\Service(); $object->onBeforeClientSignUp($event); } + public function testonBeforeClientCreateForumTopic() + { + $parameters = array( + 'ip' => '127.0.0.1', + 'message' => 'TestUnit', + 'client_id' => 1, + ); + $event = new Box_Event(null, 'any', $parameters, $this->api_admin); + $event->setDi($this->di); + + $object = new \Box\Mod\Spamchecker\Service(); + $object->onBeforeClientCreateForumTopic($event); + } + } \ No newline at end of file