diff --git a/.travis.yml b/.travis.yml index 1e56838..8586f3a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 5.4 - 5.5 - 5.6 + - 7.0 env: matrix: @@ -13,7 +14,7 @@ env: matrix: allow_failures: - - php: 5.4 + - php: 7.0 env: COVERALLS=1 DEFAULT=0 fast_finish: true @@ -25,10 +26,10 @@ matrix: - php: 5.4 env: DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:' - - php: 5.4 + - php: 7.0 env: PHPCS=1 DEFAULT=0 - - php: 5.4 + - php: 7.0 env: COVERALLS=1 DEFAULT=0 before_script: @@ -39,9 +40,6 @@ before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi" - - sh -c "if [ '$PHPCS' = '1' ]; then pear channel-discover pear.cakephp.org; fi" - - sh -c "if [ '$PHPCS' = '1' ]; then pear install --alldeps cakephp/CakePHP_CodeSniffer; fi" - - sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi" - sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi" @@ -53,7 +51,7 @@ script: - sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi" - sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi" - sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi" - - sh -c "if [ '$PHPCS' = '1' ]; then phpcs -p --extensions=php --standard=CakePHP --ignore=vendor --ignore=docs . ; fi" + - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi" notifications: email: false diff --git a/composer.json b/composer.json index 4a10812..a80f61d 100644 --- a/composer.json +++ b/composer.json @@ -1,37 +1,38 @@ { - "name": "xety/cake3-cookieauth", - "type": "cakephp-plugin", - "description": "A simple Cake3 plugin to authenticate users with Cookies.", - "keywords": ["cakephp", "cake3", "plugin", "cookie", "auth", "authenticate", "login"], - "homepage": "https://github.com/Xety/Cake3-CookieAuth", - "license": "MIT", - "support":{ - "source":"https://github.com/Xety/Cake3-CookieAuth", - "issues":"https://github.com/Xety/Cake3-CookieAuth/issues" - }, - "authors": [ - { - "name": "Xety", - "email": "zoro.fmt@gmail.com", - "homepage": "https://github.com/Xety" - } - ], - "require": { - "php": ">=5.4.0", - "cakephp/cakephp": "~3.0" - }, - "require-dev": { - "phpunit/phpunit": "4.1.*" - }, - "autoload": { - "psr-4": { - "Xety\\Cake3CookieAuth\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "Xety\\Cake3CookieAuth\\Test\\": "tests" - } - } + "name": "xety/cake3-cookieauth", + "type": "cakephp-plugin", + "description": "A simple Cake3 plugin to authenticate users with Cookies.", + "keywords": ["cakephp", "cake3", "plugin", "cookie", "auth", "authenticate", "login"], + "homepage": "https://github.com/Xety/Cake3-CookieAuth", + "license": "MIT", + "support":{ + "source":"https://github.com/Xety/Cake3-CookieAuth", + "issues":"https://github.com/Xety/Cake3-CookieAuth/issues" + }, + "authors": [ + { + "name": "Xety", + "email": "zoro.fmt@gmail.com", + "homepage": "https://github.com/Xety" + } + ], + "require": { + "php": ">=5.4.16", + "cakephp/cakephp": "~3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.*", + "cakephp/cakephp-codesniffer": "master-dev" + }, + "autoload": { + "psr-4": { + "Xety\\Cake3CookieAuth\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Xety\\Cake3CookieAuth\\Test\\": "tests" + } + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index efec277..fed5463 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,39 @@ - - - - + colors="true" + processIsolation="false" + stopOnFailure="false" + syntaxCheck="false" + bootstrap="./tests/bootstrap.php" + > + + + + - - - - ./tests/TestCase - - + + + + ./tests/TestCase + + - - - - - - - - + + + + + + + + + + + + + ./src/ + ./src/ + + diff --git a/src/Auth/CookieAuthenticate.php b/src/Auth/CookieAuthenticate.php index 8afa588..624281d 100644 --- a/src/Auth/CookieAuthenticate.php +++ b/src/Auth/CookieAuthenticate.php @@ -8,72 +8,77 @@ use Cake\Network\Request; use Cake\Network\Response; -class CookieAuthenticate extends BaseAuthenticate { +class CookieAuthenticate extends BaseAuthenticate +{ -/** - * Constructor. - * - * @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request. - * @param array $config Array of config to use. - */ - public function __construct(ComponentRegistry $registry, array $config = []) { - $this->_registry = $registry; - $this->config($config); - } + /** + * Constructor. + * + * @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request. + * @param array $config Array of config to use. + */ + public function __construct(ComponentRegistry $registry, array $config = []) + { + $this->_registry = $registry; + $this->config($config); + } -/** - * Authenticate a user based on the cookies information. - * - * @param \Cake\Network\Request $request The request instance. - * @param \Cake\Network\Response $response The response instance. - * - * @return mixed - * - * @throws \RuntimeException When the CookieComponent is not loaded. - */ - public function authenticate(Request $request, Response $response) { - if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) { - throw new \RuntimeException('You need to load the CookieComponent.'); - } + /** + * Authenticate a user based on the cookies information. + * + * @param \Cake\Network\Request $request The request instance. + * @param \Cake\Network\Response $response The response instance. + * + * @return mixed + * + * @throws \RuntimeException When the CookieComponent is not loaded. + */ + public function authenticate(Request $request, Response $response) + { + if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) { + throw new \RuntimeException('You need to load the CookieComponent.'); + } - $cookies = $this->_registry->Cookie->read('CookieAuth'); - if (empty($cookies)) { - return false; - } + $cookies = $this->_registry->Cookie->read('CookieAuth'); + if (empty($cookies)) { + return false; + } - extract($this->_config['fields']); - if (empty($cookies[$username]) || empty($cookies[$password])) { - return false; - } + extract($this->_config['fields']); + if (empty($cookies[$username]) || empty($cookies[$password])) { + return false; + } - $user = $this->_findUser($cookies[$username], $cookies[$password]); - if ($user) { - return $user; - } + $user = $this->_findUser($cookies[$username], $cookies[$password]); + if ($user) { + return $user; + } - return false; - } + return false; + } -/** - * Returns a list of all events that this authenticate class will listen to. - * - * @return array - */ - public function implementedEvents() { - return [ - 'Auth.logout' => 'logout' - ]; - } + /** + * Returns a list of all events that this authenticate class will listen to. + * + * @return array + */ + public function implementedEvents() + { + return [ + 'Auth.logout' => 'logout' + ]; + } -/** - * Delete cookies when an user logout. - * - * @param \Cake\Event\Event $event The logout Event. - * @param array $user The user about to be logged out. - * - * @return void - */ - public function logout(Event $event, array $user) { - $this->_registry->Cookie->delete('CookieAuth'); - } + /** + * Delete cookies when an user logout. + * + * @param \Cake\Event\Event $event The logout Event. + * @param array $user The user about to be logged out. + * + * @return void + */ + public function logout(Event $event, array $user) + { + $this->_registry->Cookie->delete('CookieAuth'); + } } diff --git a/tests/Fixture/UsersFixture.php b/tests/Fixture/UsersFixture.php index 39528f7..6250b3a 100644 --- a/tests/Fixture/UsersFixture.php +++ b/tests/Fixture/UsersFixture.php @@ -3,39 +3,40 @@ use Cake\TestSuite\Fixture\TestFixture; -class UsersFixture extends TestFixture { +class UsersFixture extends TestFixture +{ -/** - * Fields - * - * @var array - */ - public $fields = [ - 'id' => ['type' => 'integer'], - 'username' => ['type' => 'string', 'length' => 20], - 'password' => ['type' => 'string', 'length' => 255], - '_constraints' => [ - 'primary' => ['type' => 'primary', 'columns' => ['id']], - 'username' => ['type' => 'unique', 'columns' => ['username']] - ], - '_options' => [ - 'engine' => 'InnoDB', 'collation' => 'utf8_general_ci' - ], - ]; + /** + * Fields + * + * @var array + */ + public $fields = [ + 'id' => ['type' => 'integer'], + 'username' => ['type' => 'string', 'length' => 20], + 'password' => ['type' => 'string', 'length' => 255], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']], + 'username' => ['type' => 'unique', 'columns' => ['username']] + ], + '_options' => [ + 'engine' => 'InnoDB', 'collation' => 'utf8_general_ci' + ], + ]; -/** - * Records - * - * @var array - */ - public $records = [ - [ - 'username' => 'Mariano', - 'password' => 'passmariano' - ], - [ - 'username' => 'Larry', - 'password' => 'passlarry' - ] - ]; + /** + * Records + * + * @var array + */ + public $records = [ + [ + 'username' => 'Mariano', + 'password' => 'passmariano' + ], + [ + 'username' => 'Larry', + 'password' => 'passlarry' + ] + ]; } diff --git a/tests/TestCase/Auth/CookieAuthenticateTest.php b/tests/TestCase/Auth/CookieAuthenticateTest.php index 70fb3ea..21f2d70 100644 --- a/tests/TestCase/Auth/CookieAuthenticateTest.php +++ b/tests/TestCase/Auth/CookieAuthenticateTest.php @@ -14,136 +14,143 @@ use Cake\Utility\Security; use Xety\Cake3CookieAuth\Auth\CookieAuthenticate; -class CookieAuthenticateTest extends TestCase { - -/** - * Fixtures - * - * @var array - */ - public $fixtures = ['plugin.Xety\Cake3CookieAuth.users']; - -/** - * setup - * - * @return void - */ - public function setUp() { - parent::setUp(); - - $this->request = new Request('posts/index'); - Router::setRequestInfo($this->request); - $this->response = $this->getMock('Cake\Network\Response'); - - Security::salt('Xety-Cake3CookieAuth_Xety-Cake3CookieAuth'); - $this->registry = new ComponentRegistry(new Controller($this->request, $this->response)); - $this->registry->load('Cookie'); - $this->registry->load('Auth'); - $this->auth = new CookieAuthenticate($this->registry); - - $password = password_hash('password', PASSWORD_DEFAULT); - $Users = TableRegistry::get('Users'); - $Users->updateAll(['password' => $password], []); - } - -/** - * tearDown - * - * @return void - */ - public function tearDown() { - parent::tearDown(); - $this->registry->Cookie->delete('CookieAuth'); - } - -/** - * test authenticate - * - * @return void - */ - public function testAuthenticate() { - $expected = array( - 'id' => 1, - 'username' => 'Mariano' - ); - - $result = $this->auth->authenticate($this->request, $this->response); - $this->assertFalse($result); - - $this->registry->Cookie->write( - 'CookieAuth', - ['username' => 'Mariano', 'password' => 'password'] - ); - $result = $this->auth->authenticate($this->request, $this->response); - $this->assertEquals($expected, $result); - } - -/** - * test authenticateNoUsername - * - * @return void - */ - public function testAuthenticateNoUsername() { - $this->registry->Cookie->write( - 'CookieAuth', - ['username' => '', 'password' => 'password'] - ); - - $result = $this->auth->authenticate($this->request, $this->response); - $this->assertFalse($result); - } - -/** - * test authenticateFail - * - * @return void - */ - public function testAuthenticateFail() { - $this->registry->Cookie->write( - 'CookieAuth', - ['username' => 'Mariano', 'password' => 'passwordfail'] - ); - $result = $this->auth->authenticate($this->request, $this->response); - $this->assertFalse($result); - } - -/** - * test authenticateNoCookieComponent - * - * @return void - */ - public function testAuthenticateNoCookieComponent() { - $this->_registry = new ComponentRegistry(new Controller($this->request, $this->response)); - $this->_registry->load('Cookie'); - $this->_auth = new CookieAuthenticate($this->_registry); - - $this->assertTrue($this->_registry->has('Cookie')); - - $this->_registry->unload('Cookie'); - - $this->setExpectedException('RuntimeException'); - $this->_auth->authenticate($this->request, $this->response); - } - -/** - * test logout - * - * @return void - */ - public function testLogout() { - $this->registry->Cookie->write( - 'CookieAuth', - ['username' => 'Mariano', 'password' => 'password'] - ); - $event = new Event('Auth.logout'); - $user = $this->auth->authenticate($this->request, $this->response); - - $resultTrue = $this->registry->Cookie->check('CookieAuth'); - $this->assertTrue($resultTrue); - - $this->auth->logout($event, $user); - $resultFalse = $this->registry->Cookie->check('CookieAuth'); - $this->assertFalse($resultFalse); - } - +class CookieAuthenticateTest extends TestCase +{ + + /** + * Fixtures + * + * @var array + */ + public $fixtures = ['plugin.Xety\Cake3CookieAuth.users']; + + /** + * setup + * + * @return void + */ + public function setUp() + { + parent::setUp(); + + $this->request = new Request('posts/index'); + Router::setRequestInfo($this->request); + $this->response = $this->getMock('Cake\Network\Response'); + + Security::salt('Xety-Cake3CookieAuth_Xety-Cake3CookieAuth'); + $this->registry = new ComponentRegistry(new Controller($this->request, $this->response)); + $this->registry->load('Cookie'); + $this->registry->load('Auth'); + $this->auth = new CookieAuthenticate($this->registry); + + $password = password_hash('password', PASSWORD_DEFAULT); + $Users = TableRegistry::get('Users'); + $Users->updateAll(['password' => $password], []); + } + + /** + * tearDown + * + * @return void + */ + public function tearDown() + { + parent::tearDown(); + $this->registry->Cookie->delete('CookieAuth'); + } + + /** + * test authenticate + * + * @return void + */ + public function testAuthenticate() + { + $expected = [ + 'id' => 1, + 'username' => 'Mariano' + ]; + + $result = $this->auth->authenticate($this->request, $this->response); + $this->assertFalse($result); + + $this->registry->Cookie->write( + 'CookieAuth', + ['username' => 'Mariano', 'password' => 'password'] + ); + $result = $this->auth->authenticate($this->request, $this->response); + $this->assertEquals($expected, $result); + } + + /** + * test authenticateNoUsername + * + * @return void + */ + public function testAuthenticateNoUsername() + { + $this->registry->Cookie->write( + 'CookieAuth', + ['username' => '', 'password' => 'password'] + ); + + $result = $this->auth->authenticate($this->request, $this->response); + $this->assertFalse($result); + } + + /** + * test authenticateFail + * + * @return void + */ + public function testAuthenticateFail() + { + $this->registry->Cookie->write( + 'CookieAuth', + ['username' => 'Mariano', 'password' => 'passwordfail'] + ); + $result = $this->auth->authenticate($this->request, $this->response); + $this->assertFalse($result); + } + + /** + * test authenticateNoCookieComponent + * + * @return void + */ + public function testAuthenticateNoCookieComponent() + { + $this->_registry = new ComponentRegistry(new Controller($this->request, $this->response)); + $this->_registry->load('Cookie'); + $this->_auth = new CookieAuthenticate($this->_registry); + + $this->assertTrue($this->_registry->has('Cookie')); + + $this->_registry->unload('Cookie'); + + $this->setExpectedException('RuntimeException'); + $this->_auth->authenticate($this->request, $this->response); + } + + /** + * test logout + * + * @return void + */ + public function testLogout() + { + $this->registry->Cookie->write( + 'CookieAuth', + ['username' => 'Mariano', 'password' => 'password'] + ); + $event = new Event('Auth.logout'); + $user = $this->auth->authenticate($this->request, $this->response); + + $resultTrue = $this->registry->Cookie->check('CookieAuth'); + $this->assertTrue($resultTrue); + + $this->auth->logout($event, $user); + $resultFalse = $this->registry->Cookie->check('CookieAuth'); + $this->assertFalse($resultFalse); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e1ca368..acc941d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,7 +9,7 @@ // Path constants to a few helpful things. if (!defined('DS')) { - define('DS', DIRECTORY_SEPARATOR); + define('DS', DIRECTORY_SEPARATOR); } define('ROOT', dirname(__DIR__) . DS); define('CAKE_CORE_INCLUDE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp'); @@ -35,72 +35,72 @@ Configure::write('debug', true); Configure::write('App', [ - 'namespace' => 'App', - 'encoding' => 'UTF-8', - 'base' => false, - 'baseUrl' => false, - 'dir' => 'src', - 'webroot' => 'webroot', - 'www_root' => APP . 'webroot', - 'fullBaseUrl' => 'http://localhost', - 'imageBaseUrl' => 'img/', - 'jsBaseUrl' => 'js/', - 'cssBaseUrl' => 'css/', - 'paths' => [ - 'plugins' => [APP . 'Plugin' . DS], - 'templates' => [APP . 'Template' . DS] - ] + 'namespace' => 'App', + 'encoding' => 'UTF-8', + 'base' => false, + 'baseUrl' => false, + 'dir' => 'src', + 'webroot' => 'webroot', + 'www_root' => APP . 'webroot', + 'fullBaseUrl' => 'http://localhost', + 'imageBaseUrl' => 'img/', + 'jsBaseUrl' => 'js/', + 'cssBaseUrl' => 'css/', + 'paths' => [ + 'plugins' => [APP . 'Plugin' . DS], + 'templates' => [APP . 'Template' . DS] + ] ]); Configure::write('Session', [ - 'defaults' => 'php' + 'defaults' => 'php' ]); Cache::config([ - '_cake_core_' => [ - 'engine' => 'File', - 'prefix' => 'cake_core_', - 'serialize' => true - ], - '_cake_model_' => [ - 'engine' => 'File', - 'prefix' => 'cake_model_', - 'serialize' => true - ], - 'default' => [ - 'engine' => 'File', - 'prefix' => 'default_', - 'serialize' => true - ] + '_cake_core_' => [ + 'engine' => 'File', + 'prefix' => 'cake_core_', + 'serialize' => true + ], + '_cake_model_' => [ + 'engine' => 'File', + 'prefix' => 'cake_model_', + 'serialize' => true + ], + 'default' => [ + 'engine' => 'File', + 'prefix' => 'default_', + 'serialize' => true + ] ]); // Ensure default test connection is defined if (!getenv('db_class')) { - putenv('db_class=Cake\Database\Driver\Sqlite'); - putenv('db_dsn=sqlite::memory:'); + putenv('db_class=Cake\Database\Driver\Sqlite'); + putenv('db_dsn=sqlite::memory:'); } ConnectionManager::config('test', [ - 'className' => 'Cake\Database\Connection', - 'driver' => getenv('db_class'), - 'dsn' => getenv('db_dsn'), - 'database' => getenv('db_database'), - 'username' => getenv('db_login'), - 'password' => getenv('db_password'), - 'timezone' => 'UTC' + 'className' => 'Cake\Database\Connection', + 'driver' => getenv('db_class'), + 'dsn' => getenv('db_dsn'), + 'database' => getenv('db_database'), + 'username' => getenv('db_login'), + 'password' => getenv('db_password'), + 'timezone' => 'UTC' ]); Log::config([ - 'debug' => [ - 'engine' => 'Cake\Log\Engine\FileLog', - 'levels' => ['notice', 'info', 'debug'], - 'file' => 'debug', - ], - 'error' => [ - 'engine' => 'Cake\Log\Engine\FileLog', - 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], - 'file' => 'error', - ] + 'debug' => [ + 'engine' => 'Cake\Log\Engine\FileLog', + 'levels' => ['notice', 'info', 'debug'], + 'file' => 'debug', + ], + 'error' => [ + 'engine' => 'Cake\Log\Engine\FileLog', + 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], + 'file' => 'error', + ] ]); Plugin::load('Xety/Cake3CookieAuth', ['path' => ROOT]);