public function testRegister() { $this->specify('user should be registered', function () { $user = new User(['scenario' => 'register']); $user->username = '******'; $user->email = '*****@*****.**'; $user->password = '******'; verify($user->register())->true(); verify($user->username)->equals('tester'); verify($user->email)->equals('*****@*****.**'); verify(Yii::$app->getSecurity()->validatePassword('tester', $user->password_hash))->true(); }); $this->specify('profile should be created after registration', function () { $user = new User(['scenario' => 'register']); $user->username = '******'; $user->email = '*****@*****.**'; $user->password = '******'; verify($user->register())->true(); verify($user->profile->gravatar_email)->equals('*****@*****.**'); }); }
public function testRegister() { $this->model = new RegistrationForm(); $this->model->setAttributes(['email' => '*****@*****.**', 'username' => 'foobar', 'password' => 'foobar']); /** @var User $user */ verify($this->model->register())->true(); $user = User::findOne(['email' => '*****@*****.**']); verify('$user is instance of User', $user instanceof User)->true(); verify('email is valid', $user->email)->equals($this->model->email); verify('username is valid', $user->username)->equals($this->model->username); verify('password is valid', Password::validate($this->model->password, $user->password_hash))->true(); $token = Token::findOne(['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); verify($token)->notNull(); $mock = $this->getMock(RegistrationForm::className(), ['validate']); $mock->expects($this->once())->method('validate')->will($this->returnValue(false)); verify($mock->register())->false(); }
<?php use mata\user\models\Token; use mata\user\models\User; use tests\codeception\_pages\RegistrationPage; use yii\helpers\Html; $I = new FunctionalTester($scenario); $I->wantTo('ensure that registration works'); $page = RegistrationPage::openBy($I); $I->amGoingTo('try to register with empty credentials'); $page->register('', '', ''); $I->see('Username cannot be blank'); $I->see('Email cannot be blank'); $I->see('Password cannot be blank'); $I->amGoingTo('try to register with already used email and username'); $user = $I->getFixture('user')->getModel('user'); $page->register($user->username, $user->email, 'qwerty'); $I->see(Html::encode('This username has already been taken')); $I->see(Html::encode('This email address has already been taken')); $I->amGoingTo('try to register'); $page->register('tester', '*****@*****.**', 'tester'); $I->see('A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'); $user = $I->grabRecord(User::className(), ['email' => '*****@*****.**']); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); $I->seeInEmail(Html::encode($token->url));
$user = $I->grabRecord(User::className(), ['id' => $user->id]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRM_NEW_EMAIL]); $I->seeInEmail(Html::encode($token->getUrl())); $I->seeInEmailRecipients($user->unconfirmed_email); Yii::$app->user->logout(); $I->amGoingTo('log in using new email address before clicking the confirmation link'); $page = LoginPage::openBy($I); $page->login('*****@*****.**', 'qwerty'); $I->see('Invalid login or password'); $I->amGoingTo('log in using new email address after clicking the confirmation link'); $user->attemptEmailChange($token->code); $page->login('*****@*****.**', 'qwerty'); $I->see('Logout'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => null]); $I->amGoingTo('reset email changing process'); $page = SettingsPage::openBy($I); $page->update('*****@*****.**', $user->username, 'qwerty'); $I->see('A confirmation message has been sent to your new email address'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => '*****@*****.**']); $page->update('*****@*****.**', $user->username, 'qwerty'); $I->see('Your account details have been updated'); $I->seeRecord(User::className(), ['id' => 1, 'email' => '*****@*****.**', 'unconfirmed_email' => null]); $I->amGoingTo('change username and password'); $page->update('*****@*****.**', 'nickname', 'qwerty', '123654'); $I->see('Your account details have been updated'); $I->seeRecord(User::className(), ['username' => 'nickname', 'email' => '*****@*****.**']); Yii::$app->user->logout(); $I->amGoingTo('login with new credentials'); $page = LoginPage::openBy($I); $page->login('nickname', '123654'); $I->see('Logout');
use yii\helpers\Html; use yii\helpers\Url; use mata\user\models\User; use mata\user\models\Token; $I = new FunctionalTester($scenario); $I->wantTo('ensure that password recovery works'); $page = RecoveryPage::openBy($I); $I->amGoingTo('try to request recovery token for unconfirmed account'); $user = $I->getFixture('user')->getModel('unconfirmed'); $page->recover($user->email); $I->see('You need to confirm your email address'); $I->amGoingTo('try to request recovery token'); $user = $I->getFixture('user')->getModel('user'); $page->recover($user->email); $I->see('An email has been sent with instructions for resetting your password'); $user = $I->grabRecord(User::className(), ['email' => $user->email]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->seeInEmail(Html::encode($token->getUrl())); $I->seeInEmailRecipients($user->email); $I->amGoingTo('reset password with invalid token'); $user = $I->getFixture('user')->getModel('user_with_expired_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->see('Recovery link is invalid or expired. Please try requesting a new one.'); $I->amGoingTo('reset password'); $user = $I->getFixture('user')->getModel('user_with_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/user/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->fillField('#recovery-form-password', 'newpass'); $I->click('Finish'); $I->see('Your password has been changed successfully.');