2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
use App\Models\Group;
|
|
|
|
use App\Models\TwoFAccount;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\User;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-06-26 14:29:13 +02:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Tests\ModelTestCase;
|
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* UserModelTest test class
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(User::class)]
|
2021-11-30 17:39:33 +01:00
|
|
|
class UserModelTest extends ModelTestCase
|
|
|
|
{
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_model_configuration()
|
|
|
|
{
|
2024-09-26 23:50:01 +02:00
|
|
|
$this->runConfigurationAssertions(new User,
|
2023-12-14 12:36:38 +01:00
|
|
|
['name', 'email', 'password', 'oauth_id', 'oauth_provider'],
|
2021-11-30 17:39:33 +01:00
|
|
|
['password', 'remember_token'],
|
|
|
|
['*'],
|
|
|
|
[],
|
2023-03-08 17:43:26 +01:00
|
|
|
[
|
2023-03-10 22:59:46 +01:00
|
|
|
'id' => 'int',
|
2023-03-08 17:43:26 +01:00
|
|
|
'email_verified_at' => 'datetime',
|
2023-08-01 16:33:55 +02:00
|
|
|
'password' => 'hashed',
|
2023-03-08 17:43:26 +01:00
|
|
|
'is_admin' => 'boolean',
|
|
|
|
'twofaccounts_count' => 'integer',
|
|
|
|
'groups_count' => 'integer',
|
|
|
|
]
|
2021-11-30 17:39:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_email_is_set_lowercased()
|
|
|
|
{
|
2021-12-02 13:15:53 +01:00
|
|
|
$user = User::factory()->make([
|
2021-11-30 17:39:33 +01:00
|
|
|
'email' => 'UPPERCASE@example.COM',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(strtolower('UPPERCASE@example.COM'), $user->email);
|
|
|
|
}
|
2023-03-08 17:43:26 +01:00
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_twofaccounts_relation()
|
|
|
|
{
|
2024-09-26 23:50:01 +02:00
|
|
|
$user = new User;
|
2023-03-08 17:43:26 +01:00
|
|
|
$accounts = $user->twofaccounts();
|
2024-09-26 23:50:01 +02:00
|
|
|
$this->assertHasManyRelation($accounts, $user, new TwoFAccount);
|
2023-03-08 17:43:26 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_groups_relation()
|
|
|
|
{
|
2024-09-26 23:50:01 +02:00
|
|
|
$user = new User;
|
2023-03-08 17:43:26 +01:00
|
|
|
$groups = $user->groups();
|
2024-09-26 23:50:01 +02:00
|
|
|
$this->assertHasManyRelation($groups, $user, new Group);
|
2023-03-08 17:43:26 +01:00
|
|
|
}
|
2024-09-25 08:37:37 +02:00
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function test_equals_is_true()
|
|
|
|
{
|
|
|
|
$user = User::factory()->make([
|
|
|
|
'oauth_id' => 'fake_id',
|
|
|
|
'oauth_provider' => 'fake_provider',
|
|
|
|
]);
|
|
|
|
$anotherUser = User::factory()->make([
|
|
|
|
'name' => $user->name,
|
|
|
|
'email ' => $user->email,
|
|
|
|
'oauth_id' => $user->oauth_id,
|
|
|
|
'oauth_provider' => $user->oauth_provider,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->equals($anotherUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function test_equals_is_false_if_name_differs()
|
|
|
|
{
|
|
|
|
$user = User::factory()->make([
|
|
|
|
'oauth_id' => 'fake_id',
|
|
|
|
'oauth_provider' => 'fake_provider',
|
|
|
|
]);
|
|
|
|
$anotherUser = User::factory()->make([
|
|
|
|
'name' => 'another name',
|
|
|
|
'email ' => $user->email,
|
|
|
|
'oauth_id' => $user->oauth_id,
|
|
|
|
'oauth_provider' => $user->oauth_provider,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->equals($anotherUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function test_equals_is_false_if_email_differs()
|
|
|
|
{
|
|
|
|
$user = User::factory()->make([
|
|
|
|
'oauth_id' => 'fake_id',
|
|
|
|
'oauth_provider' => 'fake_provider',
|
|
|
|
]);
|
|
|
|
$anotherUser = User::factory()->make([
|
|
|
|
'name' => $user->name,
|
|
|
|
'email ' => 'another@email.com',
|
|
|
|
'oauth_id' => $user->oauth_id,
|
|
|
|
'oauth_provider' => $user->oauth_provider,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->equals($anotherUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function test_equals_is_false_if_oauthid_differs()
|
|
|
|
{
|
|
|
|
$user = User::factory()->make([
|
|
|
|
'oauth_id' => 'fake_id',
|
|
|
|
'oauth_provider' => 'fake_provider',
|
|
|
|
]);
|
|
|
|
$anotherUser = User::factory()->make([
|
|
|
|
'name' => $user->name,
|
|
|
|
'email ' => $user->email,
|
|
|
|
'oauth_id' => 'another_fake_id',
|
|
|
|
'oauth_provider' => $user->oauth_provider,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->equals($anotherUser));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function test_equals_is_false_if_oauth_provider_differs()
|
|
|
|
{
|
|
|
|
$user = User::factory()->make([
|
|
|
|
'oauth_id' => 'fake_id',
|
|
|
|
'oauth_provider' => 'fake_provider',
|
|
|
|
]);
|
|
|
|
$anotherUser = User::factory()->make([
|
|
|
|
'name' => $user->name,
|
|
|
|
'email ' => $user->email,
|
|
|
|
'oauth_id' => $user->oauth_id,
|
|
|
|
'oauth_provider' => 'another_provider',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->equals($anotherUser));
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|