-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Commands; | ||
|
||
use CodeIgniter\CodeIgniter; | ||
use CodeIgniter\Shield\Entities\User; | ||
use CodeIgniter\Shield\Models\UserModel; | ||
use CodeIgniter\Test\Filters\CITestStreamFilter; | ||
use CodeIgniter\Test\PhpStreamWrapper; | ||
use Tests\Support\DatabaseTestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class UserTest extends DatabaseTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if (version_compare(CodeIgniter::CI_VERSION, '4.3.0', '>=')) { | ||
CITestStreamFilter::registration(); | ||
CITestStreamFilter::addOutputFilter(); | ||
} else { | ||
$this->markTestSkipped('Cannot write tests with CI 4.2.x'); | ||
} | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
CITestStreamFilter::removeOutputFilter(); | ||
CITestStreamFilter::removeErrorFilter(); | ||
} | ||
|
||
private function setUserInput(string $input): void | ||
{ | ||
// Register the PhpStreamWrapper. | ||
PhpStreamWrapper::register(); | ||
|
||
PhpStreamWrapper::setContent($input); | ||
} | ||
|
||
private function resetUserInput(): void | ||
{ | ||
// Restore php protocol wrapper. | ||
PhpStreamWrapper::restore(); | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
$input = 'Secret Passw0rd!'; | ||
$this->setUserInput($input); | ||
|
||
command('shield:user create -n user1 -e [email protected]'); | ||
|
||
$this->resetUserInput(); | ||
|
||
$this->assertStringContainsString( | ||
'User user1 created', | ||
CITestStreamFilter::$buffer | ||
); | ||
|
||
$users = model(UserModel::class); | ||
$user = $users->findByCredentials(['email' => '[email protected]']); | ||
$this->seeInDatabase($this->tables['identities'], [ | ||
'user_id' => $user->id, | ||
'secret' => '[email protected]', | ||
]); | ||
$this->seeInDatabase($this->tables['users'], [ | ||
'id' => $user->id, | ||
'active' => 0, | ||
]); | ||
} | ||
|
||
public function testActivate(): void | ||
{ | ||
// Create a user. | ||
/** @var User $user */ | ||
$user = fake(UserModel::class, ['username' => 'user2']); | ||
$user->createEmailIdentity([ | ||
'email' => '[email protected]', | ||
'password' => 'secret123', | ||
]); | ||
|
||
$input = 'y'; | ||
$this->setUserInput($input); | ||
|
||
command('shield:user activate -n user2'); | ||
|
||
$this->resetUserInput(); | ||
|
||
$this->assertStringContainsString( | ||
'User user2 activated', | ||
CITestStreamFilter::$buffer | ||
); | ||
|
||
$users = model(UserModel::class); | ||
$user = $users->findByCredentials(['email' => '[email protected]']); | ||
$this->seeInDatabase($this->tables['users'], [ | ||
'id' => $user->id, | ||
'active' => 1, | ||
]); | ||
} | ||
|
||
public function testDeactivate(): void | ||
{ | ||
// Create a user. | ||
/** @var User $user */ | ||
$user = fake(UserModel::class, ['username' => 'user3', 'active' => 1]); | ||
$user->createEmailIdentity([ | ||
'email' => '[email protected]', | ||
'password' => 'secret123', | ||
]); | ||
|
||
$input = 'y'; | ||
$this->setUserInput($input); | ||
|
||
command('shield:user deactivate -n user3'); | ||
|
||
$this->resetUserInput(); | ||
|
||
$this->assertStringContainsString( | ||
'User user3 deactivated', | ||
CITestStreamFilter::$buffer | ||
); | ||
|
||
$users = model(UserModel::class); | ||
$user = $users->findByCredentials(['email' => '[email protected]']); | ||
$this->seeInDatabase($this->tables['users'], [ | ||
'id' => $user->id, | ||
'active' => 0, | ||
]); | ||
} | ||
} |