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

218 lines
7.3 KiB
PHP

<?php
namespace Tests\Back;
use App\Enums\Users\RoleEnum;
use App\Models\Testimonial;
use App\Models\User as AuthModel;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class TestimonialTest extends TestCase
{
/**
* TESTS GUEST CANNOT ACCESS VIEWS.
*/
/** @return void */
public function testGuestCannotAccessTestimonialsIndexView(): void
{
$response = $this->get(route(
config('unit-tests.route.prefix') .
'testimonials.' .
config('unit-tests.view.resources-index')
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/** @return void */
public function testGuestCannotAccessTestimonialsReadView(): void
{
$model = Testimonial::factory()->createOneQuietly();
$response = $this->get(route(
config('unit-tests.route.prefix') .
'testimonials.' .
config('unit-tests.view.resources-read'),
$model->getKey()
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/** @return void */
public function testGuestCannotAccessTestimonialsCreateView(): void
{
$response = $this->get(route(
config('unit-tests.route.prefix') .
'testimonials.' .
config('unit-tests.view.resources-create')
));
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/** @return void */
public function testGuestCannotAccessTestimonialsUpdateView(): void
{
$model = Testimonial::factory()->createOneQuietly();
$response = $this->get(route(
config('unit-tests.route.prefix') .
'testimonials.' .
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 testGuestCannotCreateTestimonial(): void
{
$model = Testimonial::factory()->createOneQuietly();
$response = $this->post(
route(
config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.route.action-create'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/** @return void */
public function testGuestCannotUpdateTestimonial(): void
{
$model = Testimonial::factory()->createOneQuietly();
$response = $this->patch(
route(
config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.route.action-update'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/** @return void */
public function testGuestCannotDeleteTestimonial(): void
{
$model = Testimonial::factory()->createOneQuietly();
$response = $this->delete(
route(
config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.route.action-delete'),
$model->getKey()
),
$model->toArray()
);
$response->assertRedirect(route(config('unit-tests.route.prefix') . 'login'));
}
/**
* TESTS USER CONCEPTOR ACCESS VIEWS.
*/
/** @return void */
public function testUserConceptorCanAccessTestimonialsIndexView(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.view.resources-index'))
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.testimonials.' .
config('unit-tests.view.resources-index')
);
}
/** @return void */
public function testUserConceptorCanAccessTestimonialsReadView(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$model = Testimonial::factory()->createOneQuietly();
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.view.resources-read'), $model)
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.testimonials.' .
config('unit-tests.view.resources-read')
);
}
/** @return void */
public function testUserConceptorCanAccessTestimonialsCreateView(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.view.resources-create'))
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.testimonials.' .
config('unit-tests.view.resources-create')
);
}
/** @return void */
public function testUserConceptorCanAccessTestimonialsUpdateView(): void
{
$authModel = AuthModel::factory()->createOneQuietly();
$authModel->update(['role' => RoleEnum::conceptor]);
$model = Testimonial::factory()->createOneQuietly();
$response = $this->actingAs($authModel, 'backend')->get(
route(config('unit-tests.route.prefix') . 'testimonials.' . config('unit-tests.view.resources-update'), $model)
);
$response->assertSuccessful();
$response->assertViewIs(
config('unit-tests.view.prefix') .
'pages.testimonials.' .
config('unit-tests.view.resources-update')
);
}
/**
* TESTS CAN ACTIONS ON MODEL.
*/
/** @return void */
public function testCanCreateTestimonial(): void
{
$model = Testimonial::factory()->createOneQuietly();
$this->assertModelExists($model);
}
/** @return void */
public function testCanUpdateTestimonial(): void
{
$model = Testimonial::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);
}
/** @return void */
public function testCanDeleteTestimonial(): void
{
$model = Testimonial::factory()->createOneQuietly();
$model->delete();
$this->assertModelMissing($model);
}
}