107 خطوط
3.5 KiB
PHP
107 خطوط
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Company;
|
|
use App\Models\Deal;
|
|
use App\Models\Lead;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class CoreCrmTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function admin(): User
|
|
{
|
|
$role = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
|
|
$user = User::factory()->create();
|
|
$user->assignRole($role);
|
|
return $user;
|
|
}
|
|
|
|
public function test_company_create_returns_duplicate_suggestions(): void
|
|
{
|
|
$admin = $this->admin();
|
|
Company::create([
|
|
'name' => 'Acme Co',
|
|
'normalized_name' => 'acme co',
|
|
'website' => 'https://acme.test',
|
|
'normalized_website' => 'acme.test',
|
|
'owner_id' => $admin->id,
|
|
]);
|
|
|
|
$this->actingAs($admin)
|
|
->postJson('/api/companies', [
|
|
'name' => 'Acme Co',
|
|
'website' => 'http://www.acme.test',
|
|
'owner_id' => $admin->id,
|
|
])
|
|
->assertCreated()
|
|
->assertJsonStructure(['duplicate_suggestions']);
|
|
}
|
|
|
|
public function test_product_and_deal_can_be_created(): void
|
|
{
|
|
$admin = $this->admin();
|
|
$company = Company::create(['name' => 'Pars Co', 'owner_id' => $admin->id]);
|
|
|
|
$productId = $this->actingAs($admin)
|
|
->postJson('/api/products', ['name' => 'CRM Service', 'base_price' => 1000000])
|
|
->assertCreated()
|
|
->json('id');
|
|
|
|
$this->actingAs($admin)
|
|
->postJson('/api/deals', [
|
|
'title' => 'CRM annual deal',
|
|
'company_id' => $company->id,
|
|
'product_id' => $productId,
|
|
'estimated_value' => 1500000,
|
|
'win_probability' => 40,
|
|
'owner_id' => $admin->id,
|
|
])
|
|
->assertCreated()
|
|
->assertJsonPath('title', 'CRM annual deal');
|
|
}
|
|
|
|
public function test_note_appears_in_timeline(): void
|
|
{
|
|
$admin = $this->admin();
|
|
$lead = Lead::create(['company' => 'Timeline Co', 'first_name' => 'Ali', 'last_name' => 'Rezaei', 'phone' => '09120000001']);
|
|
|
|
$this->actingAs($admin)
|
|
->postJson('/api/notes', ['entity_type' => 'lead', 'entity_id' => $lead->id, 'content' => 'تماس بعدی با مدیر فروش'])
|
|
->assertCreated();
|
|
|
|
$this->actingAs($admin)
|
|
->getJson("/api/timeline?entity_type=lead&entity_id={$lead->id}")
|
|
->assertOk()
|
|
->assertJsonFragment(['title' => 'یادداشت ثبت شد']);
|
|
}
|
|
|
|
public function test_attachment_upload_lists_file(): void
|
|
{
|
|
Storage::fake('local');
|
|
$admin = $this->admin();
|
|
$company = Company::create(['name' => 'File Co', 'owner_id' => $admin->id]);
|
|
|
|
$this->actingAs($admin)
|
|
->postJson('/api/attachments', [
|
|
'entity_type' => 'company',
|
|
'entity_id' => $company->id,
|
|
'file' => UploadedFile::fake()->create('contract.pdf', 20, 'application/pdf'),
|
|
])
|
|
->assertCreated();
|
|
|
|
$this->actingAs($admin)
|
|
->getJson("/api/attachments?entity_type=company&entity_id={$company->id}")
|
|
->assertOk()
|
|
->assertJsonFragment(['original_name' => 'contract.pdf']);
|
|
}
|
|
}
|