table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users'); $this->Behavior = $this->getMockBuilder('CakeDC\Users\Model\Behavior\PasswordBehavior') ->setMethods(['_sendResetPasswordEmail']) ->setConstructorArgs([$this->table]) ->getMock(); TransportFactory::drop('test'); TransportFactory::setConfig('test', ['className' => 'Debug']); //$this->configEmail = Email::getConfig('default'); Email::setConfig('default', [ 'transport' => 'test', 'from' => 'cakedc@example.com' ]); } /** * tearDown * * @return void */ public function tearDown() { unset($this->table, $this->Behavior); Email::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 * * @expectedException InvalidArgumentException */ public function testResetTokenWithNullParams() { $this->Behavior->resetToken(null); } /** * Test resetTokenNoExpiration * * @expectedException InvalidArgumentException * @expectedExceptionMessage Token expiration cannot be empty */ public function testResetTokenNoExpiration() { $this->Behavior->resetToken('ref'); } /** * Test resetToken * * @expectedException \CakeDC\Users\Exception\UserNotFoundException */ public function testResetTokenNotExistingUser() { $this->Behavior->resetToken('user-not-found', [ 'expiration' => 3600 ]); } /** * Test resetToken * * @expectedException \CakeDC\Users\Exception\UserAlreadyActiveException */ public function testResetTokenUserAlreadyActive() { $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 * * @expectedException \CakeDC\Users\Exception\UserNotActiveException */ public function testResetTokenUserNotActive() { $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) ->setMethods(['send']) ->getMock(); Configure::write('Users.Email.mailerClass', OverrideMailer::class); $this->Behavior = $this->getMockBuilder(PasswordBehavior::class) ->setConstructorArgs([$this->table]) ->setMethods(['getMailer']) ->getMock(); $overrideMailer->expects($this->once()) ->method('send') ->with('resetPassword') ->willReturn(true); $this->Behavior->expects($this->once()) ->method('getMailer') ->with(OverrideMailer::class) ->willReturn($overrideMailer); $this->Behavior->resetToken('user-1', [ 'expiration' => 3600, 'checkActive' => true, 'sendEmail' => true, 'type' => 'password' ]); } }