table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users'); $this->Behavior = $this->getMockBuilder('CakeDC\Users\Model\Behavior\PasswordBehavior') ->onlyMethods(['_sendResetPasswordEmail']) ->setConstructorArgs([$this->table]) ->getMock(); TransportFactory::drop('test'); TransportFactory::setConfig('test', ['className' => 'Debug']); Mailer::drop('default'); Mailer::setConfig('default', [ 'transport' => 'test', 'from' => 'cakedc@example.com', ]); } /** * tearDown * * @return void */ public function tearDown(): void { unset($this->table, $this->Behavior); Mailer::drop('default'); TransportFactory::drop('test'); parent::tearDown(); } /** * Test resetToken */ public function testResetToken() { $user = $this->table->findByUsername('user-1')->first(); $token = $user->token; $this->Behavior->expects($this->never()) ->method('_sendResetPasswordEmail') ->with($user); $result = $this->Behavior->resetToken('user-1', [ 'expiration' => 3600, 'checkActive' => true, ]); $this->assertNotEquals($token, $result->token); $this->assertEmpty($result->activation_date); $this->assertFalse($result->active); } /** * Test resetToken */ public function testResetTokenSendEmail() { $user = $this->table->findByUsername('user-1')->first(); $token = $user->token; $tokenExpires = $user->token_expires; $this->Behavior->expects($this->once()) ->method('_sendResetPasswordEmail'); $result = $this->Behavior->resetToken('user-1', [ 'expiration' => 3600, 'checkActive' => true, 'sendEmail' => true, 'type' => 'password', ]); $this->assertNotEquals($token, $result->token); $this->assertNotEquals($tokenExpires, $result->token_expires); $this->assertEmpty($result->activation_date); $this->assertFalse($result->active); } /** * Test resetToken */ public function testResetTokenWithNullParams() { $this->expectException(\InvalidArgumentException::class); $this->Behavior->resetToken(null); } /** * Test resetTokenNoExpiration */ public function testResetTokenNoExpiration() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Token expiration cannot be empty'); $this->Behavior->resetToken('ref'); } /** * Test resetToken */ public function testResetTokenNotExistingUser() { $this->expectException(UserNotFoundException::class); $this->Behavior->resetToken('user-not-found', [ 'expiration' => 3600, ]); } /** * Test resetToken */ public function testResetTokenUserAlreadyActive() { $this->expectException(UserAlreadyActiveException::class); $activeUser = TableRegistry::getTableLocator()->get('CakeDC/Users.Users')->findByUsername('user-4')->first(); $this->assertTrue($activeUser->active); $this->table = $this->getMockForModel('CakeDC/Users.Users', ['save']); $this->table->expects($this->never()) ->method('save'); $this->Behavior->expects($this->never()) ->method('_sendResetPasswordEmail'); $this->Behavior->resetToken('user-4', [ 'expiration' => 3600, 'checkActive' => true, ]); } /** * Test resetToken */ public function testResetTokenUserNotActive() { $this->expectException(UserNotActiveException::class); $this->table->findByUsername('user-1')->firstOrFail(); $this->Behavior->resetToken('user-1', [ 'ensureActive' => true, 'expiration' => 3600, ]); } /** * Test resetToken */ public function testResetTokenUserActive() { $user = TableRegistry::getTableLocator()->get('CakeDC/Users.Users')->findByUsername('user-2')->first(); $result = $this->Behavior->resetToken('user-2', [ 'ensureActive' => true, 'expiration' => 3600, ]); $this->assertEquals($user->id, $result->id); } /** * Test changePassword */ public function testChangePassword() { $user = TableRegistry::getTableLocator()->get('CakeDC/Users.Users')->findByUsername('user-6')->first(); $user->current_password = '12345'; $user->password = 'new'; $user->password_confirmation = 'new'; $result = $this->Behavior->changePassword($user); $this->assertInstanceOf(User::class, $result); $this->assertEmpty($result->getErrors()); } /** * test Email Override */ public function testEmailOverride() { $overrideMailer = $this->getMockBuilder(OverrideMailer::class) ->onlyMethods(['send']) ->getMock(); Configure::write('Users.Email.mailerClass', OverrideMailer::class); $this->Behavior = $this->getMockBuilder(PasswordBehavior::class) ->setConstructorArgs([$this->table]) ->onlyMethods(['getMailer']) ->getMock(); $responseEmail = ['headers' => ['A' => 111, 'B' => 33], 'message' => 'My message' . time()]; $overrideMailer->expects($this->once()) ->method('send') ->with('resetPassword') ->willReturn($responseEmail); $this->Behavior->expects($this->once()) ->method('getMailer') ->with(OverrideMailer::class) ->willReturn($overrideMailer); $result = $this->Behavior->resetToken('user-1', [ 'expiration' => 3600, 'checkActive' => true, 'sendEmail' => true, 'type' => 'password', ]); $this->assertInstanceOf(User::class, $result); } }