Files
backend/tests/Feature/Call/UpdateTest.php

209 lines
6.2 KiB
PHP

<?php
namespace Tests\Feature\Call;
use App\Enums\CallEvents;
use App\Models\Call;
use App\Models\Category;
use App\Models\Event;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_non_owner_user_cannot_update_call(): void
{
$callOwner = User::factory()->create([
'telephone_id' => 'tel-' . $this->faker->unique()->numerify('###'),
]);
$call = Call::factory()->create([
'telephone_id' => $callOwner->telephone_id,
]);
$unauthorizedUser = User::factory()->create(); // no permission
$this->actingAs($unauthorizedUser);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 1,
'subcategory_id' => 2,
'description' => '...',
]);
$response->assertForbidden();
}
public function test_validation_fails_when_required_fields_are_missing(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), []);
$response->assertUnprocessable()
->assertInvalid([
'category_id' => __('validation.required', ['attribute' => 'موضوع']),
'subcategory_id' => __('validation.required', ['attribute' => 'زیر موضوع']),
]);
}
public function test_category_id_and_subcategory_id_must_be_integers(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 'not-an-int',
'subcategory_id' => 'also-string',
'description' => 'توضیح',
]);
$response->assertUnprocessable()
->assertInvalid([
'category_id' => __('validation.integer', ['attribute' => 'موضوع']),
'subcategory_id' => __('validation.integer', ['attribute' => 'زیر موضوع']),
]);
}
public function test_description_field_must_be_string(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 1,
'subcategory_id' => 2,
'description' => ['not', 'a', 'string'],
]);
$response->assertUnprocessable()
->assertInvalid([
'description' => __('validation.string', ['attribute' => 'توضیحات']),
]);
}
public function test_combination_of_category_and_subcategory_must_exist(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => 12345,
'subcategory_id' => 67890,
'description' => '...',
]);
$response->assertUnprocessable()
->assertInvalid([
'category' => __('messages.combination_of_category_and_subcategory_does_not_exist'),
]);
}
public function test_operator_can_update_call_successfully(): void
{
$telephoneId = 'tel-' . $this->faker->unique()->numerify('###');
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create([
'telephone_id' => $telephoneId,
]);
$call = Call::factory()->create([
'telephone_id' => $telephoneId,
]);
$category = Category::factory()->create();
$this->actingAs($user);
$response = $this->postJson(route('calls.update', $call->id), [
'category_id' => $category->category_id,
'subcategory_id' => $category->subcategory_id,
'description' => 'توضیحات تستی',
]);
$response->assertOk()
->assertJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'category_id' => $category->category_id,
'subcategory_id' => $category->subcategory_id,
'description' => 'توضیحات تستی',
]);
$this->assertDatabaseHas('call_histories', [
'call_id' => $call->id,
'event_id' => CallEvents::OPERATOR_STORED_CALL_DATA,
]);
}
}