/** * Change user's forgotten password. * Sets new password to user's model and remove e-mail checker. * * @param ChangePasswordForm $form * @param User $user * @return boolean */ public function changeUserForgottenPassword(ChangePasswordForm $form, User $user) { if (!$form->validate()) { // form is not valid return false; } $user->newPassword = $form->password; $transaction = $user->getDb()->beginTransaction(); try { // save user model if (!$user->save()) { throw new Exception(); } // remove e-mail checker $checker = $user->checker; $checker->email_checker = null; if (!$checker->save(false, ['email_checker'])) { throw new Exception(); } $transaction->commit(); } catch (Exception $ex) { $transaction->rollBack(); return false; } return true; }
/** * Tests user create form */ public function testCreateUser() { $user = new UserForm(); $user->setScenario('create'); // create user and check every error $this->assertFalse($user->validate(), 'Check error validation'); $this->assertArrayHasKey('roles', $user->getErrors(), 'Check has roles error'); $user->roles[] = 'admin'; $this->assertFalse($user->validate()); $this->assertArrayHasKey('name', $user->getErrors(), 'Check has name error'); $user->name = 'Tester'; $this->assertFalse($user->validate()); $this->assertArrayHasKey('email', $user->getErrors(), 'Check has email error'); $user->email = 'wrong email format'; $this->assertArrayHasKey('email', $user->getErrors(), 'Check has wrong e-mail format'); $user->email = '*****@*****.**'; $user->sendNotification = true; $this->assertTrue($user->validate(), 'Check every field is validated'); // create user model $result = $this->userModule->createUser($user); $this->assertNotEmpty($user->id); $this->assertTrue($result, 'User successfully created'); $this->assertNotEmpty($user->password); // activate user $foundUser = $this->userModule->findUserByChecker('email_checker', $user->checker->email_checker); $this->assertInstanceOf(User::className(), $foundUser); $this->assertEquals($foundUser->id, $user->id); // change user password $changePasswordForm = new ChangePasswordForm(); $this->assertFalse($changePasswordForm->validate(), 'Check error validation'); $this->assertArrayHasKey('password', $changePasswordForm->getErrors(), 'Check has password error'); $changePasswordForm->password = '******'; $this->assertFalse($changePasswordForm->validate()); $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error'); $changePasswordForm->confirmPassword = '******'; $this->assertFalse($changePasswordForm->validate()); $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error'); $changePasswordForm->confirmPassword = '******'; $this->assertTrue($changePasswordForm->validate(), 'Check every field is validated'); $result = $this->userModule->changeUserForgottenPassword($changePasswordForm, $foundUser); $this->assertTrue($result, 'Password successfully changed'); $this->assertNull($foundUser->checker->email_checker); // user can authenticate $this->assertTrue($user->canSignIn()); // create new user with exists data $newUser = new UserForm(); $newUser->setAttributes($user->getAttributes()); $this->assertFalse($newUser->validate()); $this->assertArrayHasKey('email', $newUser->getErrors(), 'Check user already exists'); return $foundUser; }