Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable password reminder table #406

Merged
merged 2 commits into from
Sep 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Confide/EloquentPasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public function requestChangePassword(RemindableInterface $user)
'created_at'=> new \DateTime
);

$table = $this->getTable();

$this->app['db']
->connection($user->getConnectionName())
->table('password_reminders')
->table($table)
->insert($values);

$this->sendEmail($user, $token);
Expand All @@ -68,10 +70,11 @@ public function requestChangePassword(RemindableInterface $user)
public function getEmailByToken($token)
{
$connection = $this->getConnection();
$table = $this->getTable();

$email = $this->app['db']
->connection($connection)
->table('password_reminders')
->table($table)
->select('email')
->where('token', '=', $token)
->where('created_at', '>=', $this->getOldestValidDate())
Expand All @@ -92,10 +95,11 @@ public function getEmailByToken($token)
public function destroyToken($token)
{
$connection = $this->getConnection();
$table = $this->getTable();

$affected = $this->app['db']
->connection($connection)
->table('password_reminders')
->table($table)
->where('token', '=', $token)
->delete();

Expand All @@ -115,6 +119,16 @@ protected function getConnection()
->model()->getConnectionName();
}

/**
* Returns the configured password reminders table.
*
* @return string Table name.
*/
protected function getTable()
{
return $this->app['config']->get('auth.reminder.table');
}

/**
* Generates a random password change token.
*
Expand Down
61 changes: 56 additions & 5 deletions tests/Confide/EloquentPasswordServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Mockery as m;
use PHPUnit_Framework_TestCase;
use Illuminate\Auth\Reminders\RemindableInterface;

class EloquentPasswordServiceTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -34,7 +33,7 @@ public function testSouldRequestChangePassword()
$generatedToken = '123456789';

$user = m::mock('Illuminate\Auth\Reminders\RemindableInterface');
$passService = m::mock('Zizaco\Confide\EloquentPasswordService[generateToken,sendEmail]', []);
$passService = m::mock('Zizaco\Confide\EloquentPasswordService[generateToken,sendEmail,getTable]', []);
$db = m::mock('connection');

$passService->shouldAllowMockingProtectedMethods();
Expand All @@ -60,6 +59,9 @@ public function testSouldRequestChangePassword()
$passService->shouldReceive('generateToken')
->andReturn($generatedToken);

$passService->shouldReceive('getTable')
->andReturn('tbl_name');

// The email containing the reset link should be sent
$passService->shouldReceive('sendEmail')
->once()->with($user, $generatedToken);
Expand All @@ -76,7 +78,7 @@ public function testSouldRequestChangePassword()
->andReturn($db);

$db->shouldReceive('table')
->with('password_reminders')
->with('tbl_name')
->once()
->andReturn($db);

Expand Down Expand Up @@ -128,6 +130,9 @@ public function testShouldGetEmailByToken()
$passService->shouldReceive('getConnection')
->once()->andReturn('db_name');

$passService->shouldReceive('getTable')
->andReturn('tbl_name');

$passService->shouldReceive('getOldestValidDate')
->once()->andReturn($oldestValidDate);

Expand All @@ -146,7 +151,7 @@ public function testShouldGetEmailByToken()
->andReturn($db);

$db->shouldReceive('table')
->with('password_reminders')
->with('tbl_name')
->andReturn($db)
->once();

Expand Down Expand Up @@ -205,6 +210,9 @@ public function testShouldDestroyToken()
$passService->shouldReceive('getConnection')
->once()->andReturn('db_name');

$passService->shouldReceive('getTable')
->andReturn('tbl_name');

// Mocks DB in order to check for the following query:
// DB::connection('db_name')
// ->table('password_reminders')
Expand All @@ -215,7 +223,7 @@ public function testShouldDestroyToken()
->andReturn($db);

$db->shouldReceive('table')
->with('password_reminders')
->with('tbl_name')
->andReturn($db)
->once();

Expand Down Expand Up @@ -277,6 +285,49 @@ public function testShouldGetConnection()
);
}

public function testShouldGetTable()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$passService = m::mock('Zizaco\Confide\EloquentPasswordService');
$config = m::mock('Config');

$passService->shouldAllowMockingProtectedMethods();
$passService->app['config'] = $config;

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$passService->shouldReceive('getTable')
->passthru();

$config->shouldReceive('get')
->with('auth.reminder.table')
->times(3)->andReturnValues(['password_reminders', 'the_table', null]);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertEquals(
'password_reminders',
$passService->getTable()
);
$this->assertEquals(
'the_table',
$passService->getTable()
);
$this->assertNull(
$passService->getTable()
);
}

public function testShouldGenerateToken()
{
/*
Expand Down