Created
August 10, 2018 03:03
-
-
Save lincolnbrito/58f8b6ceff4475d4e299023598a8def0 to your computer and use it in GitHub Desktop.
Testing login actions CakePHP 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Test\TestCase\Controller; | |
use App\Controller\UsersController; | |
use Cake\Auth\WeakPasswordHasher; | |
use Cake\TestSuite\IntegrationTestCase; | |
/** | |
* App\Controller\UsersController Test Case | |
*/ | |
class UsersControllerTest extends IntegrationTestCase | |
{ | |
/** @var \Cake\ORM\Table */ | |
protected $Users; | |
/** | |
* Fixtures | |
* | |
* @var array | |
*/ | |
public $fixtures = [ | |
'app.users', | |
'app.roles', | |
'app.stores', | |
'app.users_stores' | |
]; | |
public function setUp() | |
{ | |
parent::setUp(); // TODO: Change the autogenerated stub | |
} | |
public function controllerSpy($event, $controller=null) | |
{ | |
parent::controllerSpy($event, $controller); | |
if (isset($this->_controller)) { | |
//override auth settings | |
$this->_controller->Auth->setConfig('authenticate', ['Form' => [ | |
'passwordHasher' => [ | |
'className' => 'Weak', | |
'hashType' => 'sha512' //passing sha512 as the hash type | |
], | |
'finder' => 'auth' | |
]]); | |
$hasher = new WeakPasswordHasher(['hashType'=>'sha512']); | |
$password = $hasher->hash('password'); | |
$this->_controller->getTableLocator()->clear(); | |
$this->Users = $this->_controller->getTableLocator()->get('Users'); | |
$this->Users->updateAll(['password' => $password], []); | |
} | |
} | |
/** @teste */ | |
public function test_user_can_make_login() | |
{ | |
$this->disableErrorHandlerMiddleware(); | |
$this->enableCsrfToken(); | |
$this->enableSecurityToken(); | |
$data = [ | |
'username' => '[email protected]', | |
'password' => 'password' | |
]; | |
$this->post(['controller'=>'Users','action'=>'login'],$data); | |
$this->assertRedirect('/clients'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Test\Fixture; | |
use Cake\Auth\WeakPasswordHasher; | |
use Cake\TestSuite\Fixture\TestFixture; | |
/** | |
* UsersFixture | |
* | |
*/ | |
class UsersFixture extends TestFixture | |
{ | |
/** | |
* Fields | |
* | |
* @var array | |
*/ | |
// @codingStandardsIgnoreStart | |
public $fields = [ | |
'id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], | |
'name' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], | |
'username' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], | |
'password' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], | |
'role_id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null, 'autoIncrement' => null], | |
'store_id' => ['type' => 'integer', 'length' => 11, 'unsigned' => false, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null, 'autoIncrement' => null], | |
'recovery_token' => ['type' => 'string', 'length' => 255, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], | |
'created' => ['type' => 'datetime', 'length' => null, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null], | |
'modified' => ['type' => 'datetime', 'length' => null, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null], | |
'active' => ['type' => 'boolean'], | |
'_constraints' => [ | |
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], | |
], | |
'_options' => [ | |
'engine' => 'InnoDB', | |
'collation' => 'utf8_general_ci' | |
], | |
]; | |
// @codingStandardsIgnoreEnd | |
public function init() { | |
$this->records = [ | |
[ | |
'id' => 1, | |
'name' => 'user', | |
'username' => '[email protected]', | |
'password' => 'pass1', | |
'role_id' => 1, | |
'store_id' => 1, | |
'active' => 1, | |
'recovery_token' => 'Lorem ipsum dolor sit amet', | |
'created' => '2018-01-10 18:27:33', | |
'modified' => '2018-01-10 18:27:33' | |
], | |
[ | |
'id' => 2, | |
'name' => 'user2', | |
'username' => '[email protected]', | |
'password' => 'pass2', | |
'role_id' => 1, | |
'store_id' => 1, | |
'active' => 0, | |
'recovery_token' => 'Lorem ipsum dolor sit amet', | |
'created' => '2018-01-10 18:27:33', | |
'modified' => '2018-01-10 18:27:33' | |
] | |
]; | |
parent::init(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Table; | |
use App\Event\User\UserListener; | |
use Cake\ORM\Query; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
use Cake\Mailer\Email; | |
/** | |
* Users Model | |
* | |
* @property \App\Model\Table\RolesTable|\Cake\ORM\Association\BelongsTo $Roles | |
* @property \App\Model\Table\StoresTable|\Cake\ORM\Association\BelongsTo $Stores | |
* | |
* @method \App\Model\Entity\User get($primaryKey, $options = []) | |
* @method \App\Model\Entity\User newEntity($data = null, array $options = []) | |
* @method \App\Model\Entity\User[] newEntities(array $data, array $options = []) | |
* @method \App\Model\Entity\User|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) | |
* @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | |
* @method \App\Model\Entity\User[] patchEntities($entities, array $data, array $options = []) | |
* @method \App\Model\Entity\User findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin \Cake\ORM\Behavior\TimestampBehavior | |
*/ | |
class UsersTable extends Table | |
{ | |
public function __construct(array $config = []) | |
{ | |
parent::__construct($config); | |
$this->getEventManager()->on(new UserListener()); | |
} | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('users'); | |
$this->setDisplayField('name'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->belongsTo('Roles', [ | |
'foreignKey' => 'role_id', | |
'joinType' => 'INNER' | |
]); | |
$this->belongsToMany('Stores', [ | |
'foreignKey' => 'user_id', | |
'targetForeignKey' => 'store_id', | |
'joinTable' => 'users_stores' | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param \Cake\Validation\Validator $validator Validator instance. | |
* @return \Cake\Validation\Validator | |
*/ | |
public function validationDefault(Validator $validator) | |
{ | |
$validator | |
->integer('id') | |
->allowEmpty('id', 'create'); | |
$validator | |
->scalar('name') | |
->maxLength('name', 255) | |
->requirePresence('name', 'create') | |
->notEmpty('name'); | |
$validator | |
->scalar('username') | |
->maxLength('username', 255) | |
->requirePresence('username', 'create') | |
->notEmpty('username'); | |
$validator | |
->scalar('password') | |
->maxLength('password', 255) | |
->requirePresence('password', 'create') | |
->notEmpty('password'); | |
return $validator; | |
} | |
/** | |
* Returns a rules checker object that will be used for validating | |
* application integrity. | |
* | |
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. | |
* @return \Cake\ORM\RulesChecker | |
*/ | |
public function buildRules(RulesChecker $rules) | |
{ | |
$rules->add($rules->isUnique(['username'])); | |
$rules->add($rules->existsIn(['role_id'], 'Roles')); | |
return $rules; | |
} | |
//finder used by AuthComponent | |
public function findAuth(\Cake\ORM\Query $query, array $options) | |
{ | |
$query | |
->contain([ | |
'Stores', | |
]) | |
->where(['active' => 1]); | |
return $query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment