2021-08-01 09:21:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Feature\Client;
|
|
|
|
|
|
|
|
use App\DataMapper\ClientSettings;
|
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
use App\Http\Livewire\CreditsTable;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\AppSetup;
|
|
|
|
use Faker\Factory;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Livewire\Livewire;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class ClientMergeTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use AppSetup;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
private $user;
|
|
|
|
|
|
|
|
private $company;
|
|
|
|
|
|
|
|
private $account;
|
|
|
|
|
|
|
|
public $client;
|
|
|
|
|
|
|
|
private $primary_contact;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp(): void
|
2021-08-01 09:21:08 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->faker = Factory::create();
|
|
|
|
$this->buildCache(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchingForContacts()
|
|
|
|
{
|
|
|
|
$account = Account::factory()->create();
|
|
|
|
|
|
|
|
$this->user = User::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'account_id' => $account->id,
|
2022-06-21 11:59:36 +02:00
|
|
|
'email' => $this->faker->safeEmail(),
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->company = Company::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'account_id' => $account->id,
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
$this->primary_contact = ClientContact::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->count(2)->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'email' => 'search@gmail.com',
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(4, $this->client->contacts->count());
|
|
|
|
$this->assertTrue($this->client->contacts->contains(function ($contact) {
|
|
|
|
return $contact->email == 'search@gmail.com';
|
|
|
|
}));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
$this->assertFalse($this->client->contacts->contains(function ($contact) {
|
|
|
|
return $contact->email == 'false@gmail.com';
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMergeClients()
|
|
|
|
{
|
|
|
|
$account = Account::factory()->create();
|
|
|
|
|
|
|
|
$user = User::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'account_id' => $account->id,
|
2022-06-21 11:59:36 +02:00
|
|
|
'email' => $this->faker->safeEmail(),
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$company = Company::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'account_id' => $account->id,
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $company->id,
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
$primary_contact = ClientContact::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->count(2)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $client->id,
|
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'email' => 'search@gmail.com',
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
//4contacts
|
|
|
|
|
|
|
|
$mergable_client = Client::factory()->create([
|
2022-06-21 11:57:17 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'company_id' => $company->id,
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
$primary_contact = ClientContact::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $mergable_client->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->count(2)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $mergable_client->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $mergable_client->id,
|
|
|
|
'company_id' => $company->id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'email' => 'search@gmail.com',
|
2021-08-01 09:21:08 +02:00
|
|
|
]);
|
|
|
|
//4 contacts
|
|
|
|
|
|
|
|
$this->assertEquals(4, $client->contacts->count());
|
|
|
|
$this->assertEquals(4, $mergable_client->contacts->count());
|
|
|
|
|
|
|
|
$client = $client->service()->merge($mergable_client)->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-08-01 09:21:08 +02:00
|
|
|
// nlog($client->contacts->fresh()->toArray());
|
|
|
|
// $this->assertEquals(7, $client->fresh()->contacts->count());
|
|
|
|
}
|
|
|
|
}
|