ldc-legaulois/tests/Back/StaticPageTest.php
2026-09-25 16:25:31 +02:00

83 lines
2.6 KiB
PHP

<?php
namespace Tests\Back;
use App\Enums\Users\RoleEnum;
use App\Models\StaticPage;
use App\Models\User as AuthModel;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class StaticPageTest extends TestCase
{
/**
* TESTS GUEST CANNOT ACCESS VIEWS.
*/
public function test_guest_cannot_access_static_pages_update_view(): void
{
$model = StaticPage::factory()->createOneQuietly();
$response = $this->get(route(
config('unit-tests.route.prefix') .
'static_pages.' .
config('unit-tests.view.resources-update'),
$model->getKey()
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/**
* TESTS GUEST CANNOT POST MODEL.
*/
public function test_guest_cannot_update_static_page(): void
{
$model = StaticPage::factory()->createOneQuietly();
$response = $this->patch(
route(
config('unit-tests.route.prefix') . 'static_pages.' . config('unit-tests.route.action-update'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/**
* TESTS USER CONCEPTOR ACCESS VIEWS.
*/
public function test_user_conceptor_can_access_static_pages_update_view(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$model = StaticPage::factory()->createOneQuietly();
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'static_pages.' . config('unit-tests.view.resources-update'), $model)
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.static_pages.' .
config('unit-tests.view.resources-update')
);
}
/**
* TESTS CAN ACTIONS ON MODEL.
*/
public function test_can_update_static_page(): void
{
$model = StaticPage::factory()->createOneQuietly();
$fieldTest = '';
foreach (config('unit-tests.list-fields') as $field) {
if (Schema::hasColumn($model->getTable(), $field)) {
$model->update([$field => 'test']);
$fieldTest = $field;
break;
}
}
$this->assertTrue($model->wasChanged());
$this->assertTrue(array_key_exists($fieldTest, $model->getChanges()));
$this->assertModelExists($model);
}
}