ldc-legaulois/tests/Back/StaticPageTest.php
2024-10-24 08:45:29 +00:00

91 lines
2.7 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.
*/
/** @return void */
public function testGuestCannotAccessStaticPagesUpdateView(): 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.
*/
/** @return void */
public function testGuestCannotUpdateStaticPage(): 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.
*/
/** @return void */
public function testUserConceptorCanAccessStaticPagesUpdateView(): 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.
*/
/** @return void */
public function testCanUpdateStaticPage(): 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);
}
}