2019-04-24 07:18:48 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-10-28 11:10:49 +01:00
|
|
|
namespace Tests\Feature;
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2019-04-24 07:18:48 +02:00
|
|
|
use App\DataMapper\DefaultSettings;
|
2019-06-02 08:36:31 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2019-04-24 07:18:48 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
2020-10-01 13:34:05 +02:00
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
2019-04-24 07:18:48 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2020-01-29 05:25:08 +01:00
|
|
|
* @covers App\Models\InvoiceInvitation
|
2019-04-24 07:18:48 +02:00
|
|
|
*/
|
|
|
|
class InvitationTest extends TestCase
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MakesHash;
|
2020-06-25 13:52:04 +02:00
|
|
|
//use DatabaseTransactions;
|
|
|
|
//use RefreshDatabase;
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2019-04-24 07:18:48 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceCreationAfterInvoiceMarkedSent()
|
|
|
|
{
|
2020-10-01 12:49:47 +02:00
|
|
|
$account = Account::factory()->create();
|
|
|
|
$company = Company::factory()->create([
|
2020-03-21 06:37:30 +01:00
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
2020-06-25 13:52:04 +02:00
|
|
|
$user = User::where('email', 'user@example.com')->first();
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $user) {
|
2020-10-01 13:34:05 +02:00
|
|
|
$user = User::factory()->create([
|
2020-06-25 13:52:04 +02:00
|
|
|
'account_id' => $account->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'confirmation_code' => $this->createDbHash(config('database.default')),
|
2020-06-25 13:52:04 +02:00
|
|
|
]);
|
|
|
|
}
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$userPermissions = collect([
|
|
|
|
'view_invoice',
|
|
|
|
'view_client',
|
|
|
|
'edit_client',
|
|
|
|
'edit_invoice',
|
|
|
|
'create_invoice',
|
2020-09-06 11:38:10 +02:00
|
|
|
'create_client',
|
2019-04-24 07:18:48 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$userSettings = DefaultSettings::userSettings();
|
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
2020-03-09 10:38:15 +01:00
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
2019-04-24 07:18:48 +02:00
|
|
|
'permissions' => $userPermissions->toJson(),
|
|
|
|
'settings' => json_encode($userSettings),
|
|
|
|
'is_locked' => 0,
|
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
Client::factory()->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) {
|
|
|
|
ClientContact::factory()->create([
|
2019-04-24 07:18:48 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2019-04-24 07:18:48 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
ClientContact::factory()->count(2)->create([
|
2019-04-24 07:18:48 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $c->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_id' => $company->id,
|
2019-04-24 07:18:48 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
Invoice::factory()->count(5)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$invoice = Invoice::whereUserId($user->id)->whereCompanyId($company->id)->whereClientId($client->id)->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($invoice);
|
|
|
|
$this->assertNotNull($invoice->client);
|
|
|
|
$this->assertNotNull($invoice->client->primary_contact);
|
|
|
|
|
2019-06-02 08:36:31 +02:00
|
|
|
$i = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
2021-10-14 07:25:09 +02:00
|
|
|
$i->key = $this->createDbHash(config('database.default'));
|
2019-06-02 08:36:31 +02:00
|
|
|
$i->client_contact_id = $client->primary_contact->first()->id;
|
|
|
|
$i->invoice_id = $invoice->id;
|
|
|
|
$i->save();
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2019-06-02 08:36:31 +02:00
|
|
|
$this->assertNotNull($invoice->invitations);
|
2019-04-24 07:18:48 +02:00
|
|
|
}
|
|
|
|
}
|