forked from codeigniter4/shield
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthTest.php
More file actions
98 lines (76 loc) · 2.86 KB
/
AuthTest.php
File metadata and controls
98 lines (76 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Collectors;
use CodeIgniter\Shield\Auth as ShieldAuth;
use CodeIgniter\Shield\Authentication\Authenticators\Session;
use CodeIgniter\Shield\Collectors\Auth;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\TestCase;
/**
* @internal
*/
final class AuthTest extends TestCase
{
use DatabaseTestTrait;
protected $namespace;
protected $refresh = true;
private User $user;
private Auth $collector;
protected function setUp(): void
{
parent::setUp();
$this->user = fake(UserModel::class, ['username' => 'John Smith']);
$this->collector = new Auth();
}
public function testDisplayNotLoggedIn(): void
{
$output = $this->collector->display();
$this->assertStringContainsString('Not logged in', $output);
}
public function testDisplayLoggedIn(): void
{
$authenticator = service('auth')->getAuthenticator();
$this->assertInstanceOf(Session::class, $authenticator);
$authenticator->login($this->user);
$this->user->addGroup('admin', 'beta');
$this->user->addPermission('users.create', 'users.edit');
$output = $this->collector->display();
$this->assertStringContainsString('Current Use', $output);
$this->assertStringContainsString('<td>Username</td><td>John Smith</td>', $output);
$this->assertStringContainsString('<td>Groups</td><td>admin, beta</td>', $output);
$this->assertStringContainsString('<td>Permissions</td><td>users.create, users.edit</td>', $output);
}
public function testDisplayNotLoggedInAfterLogout(): void
{
$authenticator = service('auth')->getAuthenticator();
$this->assertInstanceOf(Session::class, $authenticator);
$authenticator->login($this->user);
$authenticator->logout();
$output = $this->collector->display();
$this->assertStringContainsString('Not logged in', $output);
}
public function testGetTitleDetails(): void
{
$output = $this->collector->getTitleDetails();
$this->assertStringContainsString(ShieldAuth::SHIELD_VERSION, $output);
$this->assertStringContainsString(Session::class, $output);
}
public function testGetBadgeValueReturnsUserId(): void
{
/** @var Session $authenticator */
$authenticator = service('auth')->getAuthenticator();
$authenticator->login($this->user);
$output = (string) $this->collector->getBadgeValue();
$this->assertStringContainsString('1', $output);
}
}