1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/tests/Feature/InvitationTest.php

132 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Feature;
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings;
use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Factory\InvoiceInvitationFactory;
use App\Jobs\Account\CreateAccount;
use App\Models\Account;
use App\Models\Client;
2020-10-01 13:34:05 +02:00
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes;
use Faker\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;
/**
* @test
* @covers App\Models\InvoiceInvitation
*/
class InvitationTest extends TestCase
{
use MakesHash;
2020-06-25 13:52:04 +02:00
//use DatabaseTransactions;
//use RefreshDatabase;
2019-04-24 12:01:40 +02:00
public function setUp() :void
{
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([
'account_id' => $account->id,
]);
$account->default_company_id = $company->id;
$account->save();
2020-06-25 13:52:04 +02:00
$user = User::where('email', 'user@example.com')->first();
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,
'confirmation_code' => $this->createDbHash(config('database.default')),
2020-06-25 13:52:04 +02:00
]);
}
$userPermissions = collect([
'view_invoice',
'view_client',
'edit_client',
'edit_invoice',
'create_invoice',
'create_client',
]);
$userSettings = DefaultSettings::userSettings();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
'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([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1,
]);
2020-10-01 13:34:05 +02:00
ClientContact::factory()->count(2)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
]);
});
$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]);
$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);
$i = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
$i->client_contact_id = $client->primary_contact->first()->id;
$i->invoice_id = $invoice->id;
$i->save();
$this->assertNotNull($invoice->invitations);
}
}