2FAuth/tests/Unit/UserModelTest.php

148 lines
4.2 KiB
PHP
Raw Normal View History

2021-11-30 17:39:33 +01:00
<?php
namespace Tests\Unit;
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;
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
{
#[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-10 22:59:46 +01:00
'id' => 'int',
'email_verified_at' => 'datetime',
2023-08-01 16:33:55 +02:00
'password' => 'hashed',
'is_admin' => 'boolean',
'twofaccounts_count' => 'integer',
'groups_count' => 'integer',
]
2021-11-30 17:39:33 +01: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);
}
#[Test]
public function test_twofaccounts_relation()
{
2024-09-26 23:50:01 +02:00
$user = new User;
$accounts = $user->twofaccounts();
2024-09-26 23:50:01 +02:00
$this->assertHasManyRelation($accounts, $user, new TwoFAccount);
}
#[Test]
public function test_groups_relation()
{
2024-09-26 23:50:01 +02:00
$user = new User;
$groups = $user->groups();
2024-09-26 23:50:01 +02:00
$this->assertHasManyRelation($groups, $user, new Group);
}
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
}