From b1d5c9cec2a3acbd6ce3be0bdb4d1e152b9a9cd0 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Thu, 29 Dec 2016 12:43:02 +0700 Subject: [PATCH] PHP Censor fixes --- .../Security/Authentication/ServiceTest.php | 89 ------------------- phpunit.xml | 3 + .../Command/RegisterLdapUserCommand.php | 10 +-- .../Command/RegisterUserCommand.php | 10 +-- .../Controller/SessionController.php | 8 +- src/PHPCensor/Helper/Email.php | 6 +- .../20150308074509_add_user_providers.php | 0 .../Authentication/UserProvider/Ldap.php | 14 +-- .../Authentication/LoginPasswordProvider.php | 12 +-- .../Security/Authentication/Service.php | 17 ++-- .../Security/Authentication/UserProvider.php | 12 +-- .../UserProvider/AbstractProvider.php | 12 +-- .../Authentication/UserProvider/Internal.php | 17 ++-- .../Security/Authentication/ServiceTest.php | 86 ++++++++++++++++++ .../UserProvider/InternalTest.php | 29 +++--- 15 files changed, 164 insertions(+), 161 deletions(-) delete mode 100644 Tests/PHPCI/Security/Authentication/ServiceTest.php rename src/{PHPCI => PHPCensor}/Command/RegisterLdapUserCommand.php (93%) rename src/{PHPCI => PHPCensor}/Command/RegisterUserCommand.php (93%) rename src/{PHPCI => PHPCensor}/Migrations/20150308074509_add_user_providers.php (100%) rename src/{PHPCI => PHPCensor}/Security/Authentication/UserProvider/Ldap.php (70%) rename src/{PHPCI => PHPCensor}/Security/Security/Authentication/LoginPasswordProvider.php (59%) rename src/{PHPCI => PHPCensor}/Security/Security/Authentication/Service.php (80%) rename src/{PHPCI => PHPCensor}/Security/Security/Authentication/UserProvider.php (69%) rename src/{PHPCI => PHPCensor}/Security/Security/Authentication/UserProvider/AbstractProvider.php (56%) rename src/{PHPCI => PHPCensor}/Security/Security/Authentication/UserProvider/Internal.php (53%) create mode 100644 tests/PHPCensor/Security/Authentication/ServiceTest.php rename {Tests/PHPCI => tests/PHPCensor}/Security/Authentication/UserProvider/InternalTest.php (53%) diff --git a/Tests/PHPCI/Security/Authentication/ServiceTest.php b/Tests/PHPCI/Security/Authentication/ServiceTest.php deleted file mode 100644 index 79de61ea..00000000 --- a/Tests/PHPCI/Security/Authentication/ServiceTest.php +++ /dev/null @@ -1,89 +0,0 @@ -assertInstanceOf('PHPCI\Security\Authentication\Service', Service::getInstance()); - } - - /** - * @covers PHPCI\Security\Authentication\Service::buildProvider - */ - public function testBuildBuiltinProvider() - { - $provider = Service::buildProvider("test", array('type' => 'internal')); - - $this->assertInstanceOf('PHPCI\Security\Authentication\UserProvider\Internal', $provider); - } - - /** - * @covers PHPCI\Security\Authentication\Service::buildProvider - */ - public function testBuildAnyProvider() - { - $config = array('type' => 'PHPCI\Security\Authentication\Tests\DummyProvider'); - $provider = Service::buildProvider("test", $config); - - $this->assertInstanceOf('PHPCI\Security\Authentication\Tests\DummyProvider', $provider); - $this->assertEquals('test', $provider->key); - $this->assertEquals($config, $provider->config); - } - - /** - * @covers PHPCI\Security\Authentication\Service::getProviders - */ - public function testGetProviders() - { - $a = $this->prophesize('PHPCI\Security\Authentication\UserProvider')->reveal(); - $b = $this->prophesize('PHPCI\Security\Authentication\UserProvider')->reveal(); - $providers = array('a' => $a, 'b' => $b); - - $service = new Service($providers); - - $this->assertEquals($providers, $service->getProviders()); - } - - /** - * @covers PHPCI\Security\Authentication\Service::getLoginPasswordProviders - * @todo Implement testGetLoginPasswordProviders(). - */ - public function testGetLoginPasswordProviders() - { - $a = $this->prophesize('PHPCI\Security\Authentication\UserProvider')->reveal(); - $b = $this->prophesize('PHPCI\Security\Authentication\LoginPasswordProvider')->reveal(); - $providers = array('a' => $a, 'b' => $b); - - $service = new Service($providers); - - $this->assertEquals(array('b' => $b), $service->getLoginPasswordProviders()); - } -} - -class DummyProvider -{ - public $key; - public $config; - public function __construct($key, array $config) - { - $this->key = $key; - $this->config = $config; - } -} diff --git a/phpunit.xml b/phpunit.xml index c2bbf131..5c1a9c76 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -40,6 +40,9 @@ ./tests/PHPCensor/ProcessControl + + ./tests/PHPCensor/Security + diff --git a/src/PHPCI/Command/RegisterLdapUserCommand.php b/src/PHPCensor/Command/RegisterLdapUserCommand.php similarity index 93% rename from src/PHPCI/Command/RegisterLdapUserCommand.php rename to src/PHPCensor/Command/RegisterLdapUserCommand.php index 1106195e..27cea119 100644 --- a/src/PHPCI/Command/RegisterLdapUserCommand.php +++ b/src/PHPCensor/Command/RegisterLdapUserCommand.php @@ -7,11 +7,11 @@ * @link https://www.phptesting.org/ */ -namespace PHPCI\Command; +namespace PHPCensor\Command; -use PHPCI\Service\UserService; -use PHPCI\Helper\Lang; -use PHPCI\Store\UserStore; +use PHPCensor\Helper\Lang; +use PHPCensor\Service\UserService; +use PHPCensor\Store\UserStore; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -43,7 +43,7 @@ class RegisterLdapUserCommand extends Command protected function configure() { $this - ->setName('phpci:register-ldap-user') + ->setName('php-censor:register-ldap-user') ->setDescription(Lang::get('register_ldap_user')); } diff --git a/src/PHPCI/Command/RegisterUserCommand.php b/src/PHPCensor/Command/RegisterUserCommand.php similarity index 93% rename from src/PHPCI/Command/RegisterUserCommand.php rename to src/PHPCensor/Command/RegisterUserCommand.php index eb6c1ce9..2360b4d1 100644 --- a/src/PHPCI/Command/RegisterUserCommand.php +++ b/src/PHPCensor/Command/RegisterUserCommand.php @@ -7,11 +7,11 @@ * @link https://www.phptesting.org/ */ -namespace PHPCI\Command; +namespace PHPCensor\Command; -use PHPCI\Service\UserService; -use PHPCI\Helper\Lang; -use PHPCI\Store\UserStore; +use PHPCensor\Helper\Lang; +use PHPCensor\Service\UserService; +use PHPCensor\Store\UserStore; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -42,7 +42,7 @@ class RegisterUserCommand extends Command protected function configure() { $this - ->setName('phpci:register-user') + ->setName('php-censor:register-user') ->setDescription(Lang::get('register_user')); } diff --git a/src/PHPCensor/Controller/SessionController.php b/src/PHPCensor/Controller/SessionController.php index db0e3a36..7c4807fb 100644 --- a/src/PHPCensor/Controller/SessionController.php +++ b/src/PHPCensor/Controller/SessionController.php @@ -14,6 +14,8 @@ use b8; use PHPCensor\Helper\Email; use PHPCensor\Helper\Lang; use PHPCensor\Controller; +use PHPCensor\Security\Authentication\Service; +use PHPCensor\Store\UserStore; /** * Session Controller - Handles user login / logout. @@ -25,12 +27,12 @@ use PHPCensor\Controller; class SessionController extends Controller { /** - * @var \PHPCensor\Store\UserStore + * @var UserStore */ protected $userStore; /** - * @var \PHPCI\Security\Authentication\Service + * @var Service */ protected $authentication; @@ -41,7 +43,7 @@ class SessionController extends Controller { $this->response->disableLayout(); $this->userStore = b8\Store\Factory::getStore('User'); - $this->authentication = \PHPCI\Security\Authentication\Service::getInstance(); + $this->authentication = Service::getInstance(); } /** diff --git a/src/PHPCensor/Helper/Email.php b/src/PHPCensor/Helper/Email.php index c9db49c5..eafecfff 100644 --- a/src/PHPCensor/Helper/Email.php +++ b/src/PHPCensor/Helper/Email.php @@ -119,10 +119,12 @@ class Email * * @return integer */ - public function send(Builder $builder) + public function send(Builder $builder = null) { $smtpServer = $this->config->get('php-censor.email_settings.smtp_address'); - $builder->logDebug(sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false')); + if (null !== $builder) { + $builder->logDebug(sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false')); + } $factory = new MailerFactory($this->config->get('php-censor')); $mailer = $factory->getSwiftMailerFromConfig(); diff --git a/src/PHPCI/Migrations/20150308074509_add_user_providers.php b/src/PHPCensor/Migrations/20150308074509_add_user_providers.php similarity index 100% rename from src/PHPCI/Migrations/20150308074509_add_user_providers.php rename to src/PHPCensor/Migrations/20150308074509_add_user_providers.php diff --git a/src/PHPCI/Security/Authentication/UserProvider/Ldap.php b/src/PHPCensor/Security/Authentication/UserProvider/Ldap.php similarity index 70% rename from src/PHPCI/Security/Authentication/UserProvider/Ldap.php rename to src/PHPCensor/Security/Authentication/UserProvider/Ldap.php index 7f362605..996fb690 100644 --- a/src/PHPCI/Security/Authentication/UserProvider/Ldap.php +++ b/src/PHPCensor/Security/Authentication/UserProvider/Ldap.php @@ -3,16 +3,16 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2014, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2014, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication\UserProvider; +namespace PHPCensor\Security\Authentication\UserProvider; use b8\Config; -use PHPCI\Model\User; -use PHPCI\Security\Authentication\LoginPasswordProvider; +use PHPCensor\Model\User; +use PHPCensor\Security\Authentication\LoginPasswordProvider; /** * Ldap user provider. @@ -24,7 +24,7 @@ class Ldap extends AbstractProvider implements LoginPasswordProvider public function verifyPassword(User $user, $password) { - $config = Config::getInstance()->get('phpci.security.ldap', []); + $config = Config::getInstance()->get('php-censor.security.ldap', []); $server = $config["server"]; $mailAttribute = $config["mailAttribute"]; $ldap = ldap_connect($server); diff --git a/src/PHPCI/Security/Security/Authentication/LoginPasswordProvider.php b/src/PHPCensor/Security/Security/Authentication/LoginPasswordProvider.php similarity index 59% rename from src/PHPCI/Security/Security/Authentication/LoginPasswordProvider.php rename to src/PHPCensor/Security/Security/Authentication/LoginPasswordProvider.php index 5faef3dd..e5a2fc1c 100644 --- a/src/PHPCI/Security/Security/Authentication/LoginPasswordProvider.php +++ b/src/PHPCensor/Security/Security/Authentication/LoginPasswordProvider.php @@ -3,19 +3,19 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2015, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2015, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication; +namespace PHPCensor\Security\Authentication; -use PHPCI\Model\User; +use PHPCensor\Model\User; /** * User provider which authenticiation using a password. * - * @author Adirelle + * @author Adirelle */ interface LoginPasswordProvider extends UserProvider { diff --git a/src/PHPCI/Security/Security/Authentication/Service.php b/src/PHPCensor/Security/Security/Authentication/Service.php similarity index 80% rename from src/PHPCI/Security/Security/Authentication/Service.php rename to src/PHPCensor/Security/Security/Authentication/Service.php index ba1bf4ea..e58be8b8 100644 --- a/src/PHPCI/Security/Security/Authentication/Service.php +++ b/src/PHPCensor/Security/Security/Authentication/Service.php @@ -3,19 +3,19 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2015, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2015, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication; +namespace PHPCensor\Security\Authentication; use b8\Config; /** * Authentication facade. * - * @author Adirelle + * @author Adirelle */ class Service { @@ -33,7 +33,7 @@ class Service { if (self::$instance === null) { $config = Config::getInstance()->get( - 'phpci.security.authentication', + 'php-censor.security.authentication', ['internal' => ['type' => 'internal']] ); @@ -43,6 +43,7 @@ class Service } self::$instance = new self($providers); } + return self::$instance; } @@ -55,8 +56,8 @@ class Service public static function buildProvider($key, $config) { $class = ucfirst($config['type']); - if (class_exists('\\PHPCI\\Security\\Authentication\\UserProvider\\' . $class)) { - $class = '\\PHPCI\\Security\\Authentication\\UserProvider\\' . $class; + if (class_exists('\\PHPCensor\\Security\\Authentication\\UserProvider\\' . $class)) { + $class = '\\PHPCensor\\Security\\Authentication\\UserProvider\\' . $class; } return new $class($key, $config); diff --git a/src/PHPCI/Security/Security/Authentication/UserProvider.php b/src/PHPCensor/Security/Security/Authentication/UserProvider.php similarity index 69% rename from src/PHPCI/Security/Security/Authentication/UserProvider.php rename to src/PHPCensor/Security/Security/Authentication/UserProvider.php index b72777eb..5a5eab2e 100644 --- a/src/PHPCI/Security/Security/Authentication/UserProvider.php +++ b/src/PHPCensor/Security/Security/Authentication/UserProvider.php @@ -3,14 +3,14 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2015, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2015, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication; +namespace PHPCensor\Security\Authentication; -use PHPCI\Model\User; +use PHPCensor\Model\User; /** * User provider interface. @@ -22,7 +22,7 @@ interface UserProvider /** Check if all software requirements are met (libraries, extensions, ...) * - * @throws Exception + * @throws \Exception */ public function checkRequirements(); diff --git a/src/PHPCI/Security/Security/Authentication/UserProvider/AbstractProvider.php b/src/PHPCensor/Security/Security/Authentication/UserProvider/AbstractProvider.php similarity index 56% rename from src/PHPCI/Security/Security/Authentication/UserProvider/AbstractProvider.php rename to src/PHPCensor/Security/Security/Authentication/UserProvider/AbstractProvider.php index 6c745d62..d9f0835e 100644 --- a/src/PHPCI/Security/Security/Authentication/UserProvider/AbstractProvider.php +++ b/src/PHPCensor/Security/Security/Authentication/UserProvider/AbstractProvider.php @@ -3,19 +3,19 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2014, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2014, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication\UserProvider; +namespace PHPCensor\Security\Authentication\UserProvider; -use PHPCI\Security\Authentication\UserProvider; +use PHPCensor\Security\Authentication\UserProvider; /** * Abstract user provider. * - * @author Adirelle + * @author Adirelle */ abstract class AbstractProvider implements UserProvider { diff --git a/src/PHPCI/Security/Security/Authentication/UserProvider/Internal.php b/src/PHPCensor/Security/Security/Authentication/UserProvider/Internal.php similarity index 53% rename from src/PHPCI/Security/Security/Authentication/UserProvider/Internal.php rename to src/PHPCensor/Security/Security/Authentication/UserProvider/Internal.php index ca521bf2..79f8f52d 100644 --- a/src/PHPCI/Security/Security/Authentication/UserProvider/Internal.php +++ b/src/PHPCensor/Security/Security/Authentication/UserProvider/Internal.php @@ -3,19 +3,20 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2014, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2014, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication\UserProvider; +namespace PHPCensor\Security\Authentication\UserProvider; -use PHPCI\Model\User; -use PHPCI\Security\Authentication\LoginPasswordProvider; +use PHPCensor\Model\User; +use PHPCensor\Security\Authentication\LoginPasswordProvider; /** - * Internal user provider. - * @author Adirelle + * Internal user provider + * + * @author Adirelle */ class Internal extends AbstractProvider implements LoginPasswordProvider { diff --git a/tests/PHPCensor/Security/Authentication/ServiceTest.php b/tests/PHPCensor/Security/Authentication/ServiceTest.php new file mode 100644 index 00000000..b29e5351 --- /dev/null +++ b/tests/PHPCensor/Security/Authentication/ServiceTest.php @@ -0,0 +1,86 @@ +assertInstanceOf('\PHPCensor\Security\Authentication\Service', Service::getInstance()); + } + + /** + * @covers Service::buildProvider + */ + public function testBuildBuiltinProvider() + { + $provider = Service::buildProvider('test', ['internal' => ['type' => 'internal']]); + + $this->assertInstanceOf('\PHPCensor\Security\Authentication\UserProvider\Internal', $provider); + } + + /** + * @covers Service::buildProvider + */ + public function testBuildAnyProvider() + { + $config = array('type' => '\PHPCensor\Security\Authentication\Tests\DummyProvider'); + $provider = Service::buildProvider("test", $config); + + $this->assertInstanceOf('\PHPCensor\Security\Authentication\Tests\DummyProvider', $provider); + $this->assertEquals('test', $provider->key); + $this->assertEquals($config, $provider->config); + } + + /** + * @covers Service::getProviders + */ + public function testGetProviders() + { + $a = $this->prophesize('\PHPCensor\Security\Authentication\UserProvider')->reveal(); + $b = $this->prophesize('\PHPCensor\Security\Authentication\UserProvider')->reveal(); + $providers = array('a' => $a, 'b' => $b); + + $service = new Service($providers); + + $this->assertEquals($providers, $service->getProviders()); + } + + /** + * @covers Service::getLoginPasswordProviders + */ + public function testGetLoginPasswordProviders() + { + $a = $this->prophesize('\PHPCensor\Security\Authentication\UserProvider')->reveal(); + $b = $this->prophesize('\PHPCensor\Security\Authentication\LoginPasswordProvider')->reveal(); + $providers = array('a' => $a, 'b' => $b); + + $service = new Service($providers); + + $this->assertEquals(array('b' => $b), $service->getLoginPasswordProviders()); + } +} + +class DummyProvider +{ + public $key; + public $config; + public function __construct($key, array $config) + { + $this->key = $key; + $this->config = $config; + } +} diff --git a/Tests/PHPCI/Security/Authentication/UserProvider/InternalTest.php b/tests/PHPCensor/Security/Authentication/UserProvider/InternalTest.php similarity index 53% rename from Tests/PHPCI/Security/Authentication/UserProvider/InternalTest.php rename to tests/PHPCensor/Security/Authentication/UserProvider/InternalTest.php index 5068616a..1f8acb98 100644 --- a/Tests/PHPCI/Security/Authentication/UserProvider/InternalTest.php +++ b/tests/PHPCensor/Security/Authentication/UserProvider/InternalTest.php @@ -3,20 +3,17 @@ /** * PHPCI - Continuous Integration for PHP * - * @copyright Copyright 2014, Block 8 Limited. - * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md - * @link https://www.phptesting.org/ + * @copyright Copyright 2014, Block 8 Limited. + * @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md + * @link https://www.phptesting.org/ */ -namespace PHPCI\Security\Authentication\UserProvider\Tests; +namespace Tests\PHPCensor\Security\Authentication\UserProvider; -use PHPCI\Security\Authentication\UserProvider\Internal; -use PHPUnit_Framework_TestCase; +use PHPCensor\Model\User; +use PHPCensor\Security\Authentication\UserProvider\Internal; -/** - * Generated by PHPUnit_SkeletonGenerator on 2015-03-08 at 18:26:51. - */ -class InternalTest extends PHPUnit_Framework_TestCase +class InternalTest extends \PHPUnit_Framework_TestCase { /** * @var Internal @@ -29,11 +26,11 @@ class InternalTest extends PHPUnit_Framework_TestCase } /** - * @covers PHPCI\Security\Authentication\UserProvider\Internal::verifyPassword + * @covers Internal::verifyPassword */ public function testVerifyPassword() { - $user = new \PHPCI\Model\User; + $user = new User(); $password = 'bla'; $user->setHash(password_hash($password, PASSWORD_DEFAULT)); @@ -41,11 +38,11 @@ class InternalTest extends PHPUnit_Framework_TestCase } /** - * @covers PHPCI\Security\Authentication\UserProvider\Internal::verifyPassword + * @covers Internal::verifyPassword */ public function testVerifyInvaldPassword() { - $user = new \PHPCI\Model\User; + $user = new User(); $password = 'foo'; $user->setHash(password_hash($password, PASSWORD_DEFAULT)); @@ -53,7 +50,7 @@ class InternalTest extends PHPUnit_Framework_TestCase } /** - * @covers PHPCI\Security\Authentication\UserProvider\Internal::checkRequirements + * @covers Internal::checkRequirements */ public function testCheckRequirements() { @@ -61,7 +58,7 @@ class InternalTest extends PHPUnit_Framework_TestCase } /** - * @covers PHPCI\Security\Authentication\UserProvider\Internal::provisionUser + * @covers Internal::provisionUser */ public function testProvisionUser() {