ldc-legaulois/tests/Back/SurveysAnswerTest.php

201 lines
7.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Tests\Back;
use App\Enums\Users\RoleEnum;
use App\Models\SurveysAnswer;
use App\Models\User as AuthModel;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class SurveysAnswerTest extends TestCase
{
/**
* TESTS GUEST CANNOT ACCESS VIEWS.
*/
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_access_surveys_answers_index_view(): void
{
$response = $this->get(route(
config('unit-tests.route.prefix') .
'surveys_answers.' .
config('unit-tests.view.resources-index')
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_access_surveys_answers_read_view(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->get(route(
config('unit-tests.route.prefix') .
'surveys_answers.' .
config('unit-tests.view.resources-read'),
$model->getKey()
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_access_surveys_answers_create_view(): void
{
$response = $this->get(route(
config('unit-tests.route.prefix') .
'surveys_answers.' .
config('unit-tests.view.resources-create')
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_access_surveys_answers_update_view(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->get(route(
config('unit-tests.route.prefix') .
'surveys_answers.' .
config('unit-tests.view.resources-update'),
$model->getKey()
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/**
* TESTS GUEST CANNOT POST MODEL.
*/
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_create_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->post(
route(
config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.route.action-create'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_update_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->patch(
route(
config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.route.action-update'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
2026-09-25 16:25:31 +02:00
public function test_guest_cannot_delete_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->delete(
route(
config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.route.action-delete'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/**
* TESTS USER CONCEPTOR ACCESS VIEWS.
*/
2026-09-25 16:25:31 +02:00
public function test_user_conceptor_can_access_surveys_answers_index_view(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.view.resources-index'))
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.surveys_answers.' .
config('unit-tests.view.resources-index')
);
}
2026-09-25 16:25:31 +02:00
public function test_user_conceptor_can_access_surveys_answers_read_view(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.view.resources-read'), $model)
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.surveys_answers.' .
config('unit-tests.view.resources-read')
);
}
2026-09-25 16:25:31 +02:00
public function test_user_conceptor_can_access_surveys_answers_create_view(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.view.resources-create'))
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.surveys_answers.' .
config('unit-tests.view.resources-create')
);
}
2026-09-25 16:25:31 +02:00
public function test_user_conceptor_can_access_surveys_answers_update_view(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$model = SurveysAnswer::factory()->createOneQuietly();
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'surveys_answers.' . config('unit-tests.view.resources-update'), $model)
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.surveys_answers.' .
config('unit-tests.view.resources-update')
);
}
/**
* TESTS CAN ACTIONS ON MODEL.
*/
2026-09-25 16:25:31 +02:00
public function test_can_create_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$this->assertModelExists($model);
}
2026-09-25 16:25:31 +02:00
public function test_can_update_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
2026-09-25 16:25:31 +02:00
$fieldTest = '';
foreach (config('unit-tests.list-fields') as $field) {
if (Schema::hasColumn($model->getTable(), $field)) {
2026-09-25 16:25:31 +02:00
$model->update([$field => 'test']);
$fieldTest = $field;
break;
}
}
$this->assertTrue($model->wasChanged());
$this->assertTrue(array_key_exists($fieldTest, $model->getChanges()));
$this->assertModelExists($model);
}
2026-09-25 16:25:31 +02:00
public function test_can_delete_surveys_answer(): void
{
$model = SurveysAnswer::factory()->createOneQuietly();
$model->delete();
$this->assertModelMissing($model);
}
}