applicant-site/tests/Feature/UserTest.php

135 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2024-01-29 09:44:53 +03:00
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class UserTest extends TestCase
{
private User $adminUser;
private User $noAdminUser;
private array $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->make()->only([
'name',
'email',
'password'
]);
$this->adminUser = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
$this->noAdminUser = User::factory()->create([
'name' => 'noadmin',
'email' => 'notest@example.com',
'password' => 'no123456'
]);
}
public function testUsersPage(): void
{
$response = $this->actingAs($this->adminUser)
->withSession(['banned' => false])
->get(route('users.index'));
$response->assertOk();
}
public function testNoAdminNoSeeUsersPage(): void
{
$response = $this->actingAs($this->noAdminUser)
->withSession(['banned' => false])
->get(route('users.index'));
$response->assertStatus(403);
}
public function testCreateUserPage(): void
{
$response = $this->actingAs($this->adminUser)
->withSession(['banned' => false])
->get(route('users.create'));
$response->assertOk();
}
public function testNoAdminCreateUserPage(): void
{
$response = $this->actingAs($this->noAdminUser)
->withSession(['banned' => false])
->get(route('users.create'));
$response->assertStatus(403);
}
public function testStoreUser(): void
{
$response = $this->actingAs($this->adminUser)
->withSession(['banned' => false])
->post(route('users.store', $this->user));
$response->assertRedirect(route('users.index'));
$this->assertDatabaseHas('users', $this->user);
}
public function testNoAdminNoStoreUser(): void
{
$response = $this->actingAs($this->noAdminUser)
->withSession(['banned' => false])
->post(route('users.store', $this->user));
$response->assertStatus(403);
$this->assertDatabaseMissing('users', $this->user);
}
public function testEditUserPage(): void
{
$response = $this->actingAs($this->adminUser)
->withSession(['banned' => false])
->get(route('users.edit', $this->adminUser));
$response->assertOk();
}
public function testNoAdminEditUserPage(): void
{
$response = $this->actingAs($this->noAdminUser)
->withSession(['banned' => false])
->get(route('users.edit', $this->noAdminUser));
$response->assertStatus(403);
}
public function testUpdateUser(): void
{
$response = $this->actingAs($this->adminUser)
->withSession(['banned' => false])
->patch(route('users.update', $this->noAdminUser), $this->user);
$response->assertRedirect(route('users.index'));
$dataWithOutHashPassword = $this->user;
unset($dataWithOutHashPassword['password']);
$this->assertDatabaseHas('users', $dataWithOutHashPassword);
}
public function testNoAdminNoUpdateUser(): void
{
$response = $this->actingAs($this->noAdminUser)
->withSession(['banned' => false])
->patch(route('users.update', $this->noAdminUser), $this->user);
$response->assertStatus(403);
$noAdminData = $this->noAdminUser->only(['name', 'email', 'password']);
$this->assertDatabaseHas('users', $noAdminData);
$this->assertDatabaseMissing('users', $this->user);
}
}