PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Auth/AuthPasswordBrokerTest.php

https://gitlab.com/ezeql/framework
PHP | 159 lines | 126 code | 30 blank | 3 comment | 0 complexity | 922f9e508d2293f352e9d239732f4fde MD5 | raw file
  1. <?php
  2. use Mockery as m;
  3. use Illuminate\Contracts\Auth\PasswordBroker;
  4. class AuthPasswordBrokerTest extends PHPUnit_Framework_TestCase
  5. {
  6. public function tearDown()
  7. {
  8. m::close();
  9. }
  10. public function testIfUserIsNotFoundErrorRedirectIsReturned()
  11. {
  12. $mocks = $this->getMocks();
  13. $broker = $this->getMock('Illuminate\Auth\Passwords\PasswordBroker', ['getUser', 'makeErrorRedirect'], array_values($mocks));
  14. $broker->expects($this->once())->method('getUser')->will($this->returnValue(null));
  15. $this->assertEquals(PasswordBroker::INVALID_USER, $broker->sendResetLink(['credentials']));
  16. }
  17. /**
  18. * @expectedException UnexpectedValueException
  19. */
  20. public function testGetUserThrowsExceptionIfUserDoesntImplementCanResetPassword()
  21. {
  22. $broker = $this->getBroker($mocks = $this->getMocks());
  23. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn('bar');
  24. $broker->getUser(['foo']);
  25. }
  26. public function testUserIsRetrievedByCredentials()
  27. {
  28. $broker = $this->getBroker($mocks = $this->getMocks());
  29. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  30. $this->assertEquals($user, $broker->getUser(['foo']));
  31. }
  32. public function testBrokerCreatesTokenAndRedirectsWithoutError()
  33. {
  34. $mocks = $this->getMocks();
  35. $broker = $this->getMock('Illuminate\Auth\Passwords\PasswordBroker', ['emailResetLink', 'getUri'], array_values($mocks));
  36. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  37. $mocks['tokens']->shouldReceive('create')->once()->with($user)->andReturn('token');
  38. $callback = function () {};
  39. $broker->expects($this->once())->method('emailResetLink')->with($this->equalTo($user), $this->equalTo('token'), $this->equalTo($callback));
  40. $this->assertEquals(PasswordBroker::RESET_LINK_SENT, $broker->sendResetLink(['foo'], $callback));
  41. }
  42. public function testMailerIsCalledWithProperViewTokenAndCallback()
  43. {
  44. unset($_SERVER['__password.reset.test']);
  45. $broker = $this->getBroker($mocks = $this->getMocks());
  46. $callback = function ($message, $user) { $_SERVER['__password.reset.test'] = true; };
  47. $user = m::mock('Illuminate\Contracts\Auth\CanResetPassword');
  48. $mocks['mailer']->shouldReceive('send')->once()->with('resetLinkView', ['token' => 'token', 'user' => $user], m::type('Closure'))->andReturnUsing(function ($view, $data, $callback) {
  49. return $callback;
  50. });
  51. $user->shouldReceive('getEmailForPasswordReset')->once()->andReturn('email');
  52. $message = m::mock('StdClass');
  53. $message->shouldReceive('to')->once()->with('email');
  54. $result = $broker->emailResetLink($user, 'token', $callback);
  55. call_user_func($result, $message);
  56. $this->assertTrue($_SERVER['__password.reset.test']);
  57. }
  58. public function testRedirectIsReturnedByResetWhenUserCredentialsInvalid()
  59. {
  60. $broker = $this->getBroker($mocks = $this->getMocks());
  61. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['creds'])->andReturn(null);
  62. $this->assertEquals(PasswordBroker::INVALID_USER, $broker->reset(['creds'], function () {}));
  63. }
  64. public function testRedirectReturnedByRemindWhenPasswordsDontMatch()
  65. {
  66. $creds = ['password' => 'foo', 'password_confirmation' => 'bar'];
  67. $broker = $this->getBroker($mocks = $this->getMocks());
  68. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with($creds)->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  69. $this->assertEquals(PasswordBroker::INVALID_PASSWORD, $broker->reset($creds, function () {}));
  70. }
  71. public function testRedirectReturnedByRemindWhenPasswordNotSet()
  72. {
  73. $creds = ['password' => null, 'password_confirmation' => null];
  74. $broker = $this->getBroker($mocks = $this->getMocks());
  75. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with($creds)->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  76. $this->assertEquals(PasswordBroker::INVALID_PASSWORD, $broker->reset($creds, function () {}));
  77. }
  78. public function testRedirectReturnedByRemindWhenPasswordsLessThanSixCharacters()
  79. {
  80. $creds = ['password' => 'abc', 'password_confirmation' => 'abc'];
  81. $broker = $this->getBroker($mocks = $this->getMocks());
  82. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with($creds)->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  83. $this->assertEquals(PasswordBroker::INVALID_PASSWORD, $broker->reset($creds, function () {}));
  84. }
  85. public function testRedirectReturnedByRemindWhenPasswordDoesntPassValidator()
  86. {
  87. $creds = ['password' => 'abcdef', 'password_confirmation' => 'abcdef'];
  88. $broker = $this->getBroker($mocks = $this->getMocks());
  89. $broker->validator(function ($credentials) { return strlen($credentials['password']) >= 7; });
  90. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with($creds)->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  91. $this->assertEquals(PasswordBroker::INVALID_PASSWORD, $broker->reset($creds, function () {}));
  92. }
  93. public function testRedirectReturnedByRemindWhenRecordDoesntExistInTable()
  94. {
  95. $creds = ['token' => 'token'];
  96. $broker = $this->getMock('Illuminate\Auth\Passwords\PasswordBroker', ['validateNewPassword'], array_values($mocks = $this->getMocks()));
  97. $mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(array_except($creds, ['token']))->andReturn($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword'));
  98. $broker->expects($this->once())->method('validateNewPassword')->will($this->returnValue(true));
  99. $mocks['tokens']->shouldReceive('exists')->with($user, 'token')->andReturn(false);
  100. $this->assertEquals(PasswordBroker::INVALID_TOKEN, $broker->reset($creds, function () {}));
  101. }
  102. public function testResetRemovesRecordOnReminderTableAndCallsCallback()
  103. {
  104. unset($_SERVER['__password.reset.test']);
  105. $broker = $this->getMock('Illuminate\Auth\Passwords\PasswordBroker', ['validateReset', 'getPassword', 'getToken'], array_values($mocks = $this->getMocks()));
  106. $broker->expects($this->once())->method('validateReset')->will($this->returnValue($user = m::mock('Illuminate\Contracts\Auth\CanResetPassword')));
  107. $mocks['tokens']->shouldReceive('delete')->once()->with('token');
  108. $callback = function ($user, $password) {
  109. $_SERVER['__password.reset.test'] = compact('user', 'password');
  110. return 'foo';
  111. };
  112. $this->assertEquals(PasswordBroker::PASSWORD_RESET, $broker->reset(['password' => 'password', 'token' => 'token'], $callback));
  113. $this->assertEquals(['user' => $user, 'password' => 'password'], $_SERVER['__password.reset.test']);
  114. }
  115. protected function getBroker($mocks)
  116. {
  117. return new \Illuminate\Auth\Passwords\PasswordBroker($mocks['tokens'], $mocks['users'], $mocks['mailer'], $mocks['view']);
  118. }
  119. protected function getMocks()
  120. {
  121. $mocks = [
  122. 'tokens' => m::mock('Illuminate\Auth\Passwords\TokenRepositoryInterface'),
  123. 'users' => m::mock('Illuminate\Contracts\Auth\UserProvider'),
  124. 'mailer' => m::mock('Illuminate\Contracts\Mail\Mailer'),
  125. 'view' => 'resetLinkView',
  126. ];
  127. return $mocks;
  128. }
  129. }