2020-07-23 05:55:11 +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
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2021-08-18 14:12:10 +02:00
|
|
|
use App\Models\CompanyToken;
|
2021-10-14 07:25:09 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-07-23 05:55:11 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2021-08-18 14:12:10 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2020-07-23 05:55:11 +02:00
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class InvitationTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
2021-10-14 07:25:09 +02:00
|
|
|
use MakesHash;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2020-07-23 05:55:11 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
2021-08-18 14:12:10 +02:00
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
2020-07-23 05:55:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvitationSanity()
|
|
|
|
{
|
|
|
|
$this->assertEquals($this->invoice->invitations->count(), 2);
|
|
|
|
|
|
|
|
$invitations = $this->invoice->invitations()->get();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invites = $invitations->reject(function ($invitation) {
|
2020-07-23 05:55:11 +02:00
|
|
|
return $invitation->contact->is_primary == false;
|
|
|
|
})->toArray();
|
|
|
|
|
2021-07-03 05:47:15 +02:00
|
|
|
$this->assertEquals(1, count($invites));
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
$this->invoice->invitations = $invites;
|
|
|
|
|
|
|
|
$this->invoice->line_items = [];
|
|
|
|
|
2021-02-10 05:10:05 +01:00
|
|
|
$response = null;
|
|
|
|
|
|
|
|
try {
|
2022-06-21 11:57:17 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $this->invoice->toArray());
|
2021-08-18 14:12:10 +02:00
|
|
|
} catch (ValidationException $e) {
|
2021-02-10 05:10:05 +01:00
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2020-07-23 05:55:11 +02:00
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
2021-07-03 05:47:15 +02:00
|
|
|
$this->assertEquals(2, count($arr['data']['invitations']));
|
2020-07-23 05:55:11 +02:00
|
|
|
|
|
|
|
//test pushing a contact invitation back on
|
|
|
|
|
|
|
|
$contact = $this->invoice->client->contacts->where('is_primary', false)->first();
|
|
|
|
|
|
|
|
$new_invite = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
|
|
|
$new_invite->client_contact_id = $contact->hashed_id;
|
2021-10-14 07:25:09 +02:00
|
|
|
$new_invite->key = $this->createDbHash(config('database.default'));
|
2020-07-23 05:55:11 +02:00
|
|
|
|
|
|
|
$invitations = $this->invoice->invitations()->get();
|
|
|
|
|
|
|
|
$invitations->push($new_invite);
|
|
|
|
|
|
|
|
$this->invoice->invitations = $invitations->toArray();
|
|
|
|
$this->invoice->line_items = [];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $this->invoice->toArray())
|
|
|
|
->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($arr['data']['invitations']));
|
|
|
|
}
|
|
|
|
}
|