2019-04-16 07:28:30 +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
|
|
|
|
2019-04-16 07:28:30 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2022-02-06 03:46:19 +01:00
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
2019-04-17 02:58:23 +02:00
|
|
|
use App\Models\Client;
|
2020-10-01 13:34:05 +02:00
|
|
|
use App\Models\ClientContact;
|
2019-04-17 08:20:32 +02:00
|
|
|
use App\Models\Invoice;
|
2022-02-06 03:46:19 +01:00
|
|
|
use App\Repositories\InvoiceRepository;
|
2019-04-16 07:28:30 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-04-24 12:01:40 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-04-16 07:28:30 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2019-10-10 12:43:50 +02:00
|
|
|
use Tests\MockAccountData;
|
2019-04-16 07:28:30 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2019-04-20 01:02:49 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\InvoiceController
|
2019-04-17 08:20:32 +02:00
|
|
|
*/
|
2019-04-16 07:28:30 +02:00
|
|
|
class InvoiceTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2019-04-24 12:01:40 +02:00
|
|
|
use DatabaseTransactions;
|
2019-10-10 12:43:50 +02:00
|
|
|
use MockAccountData;
|
2019-04-16 07:28:30 +02:00
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2019-04-16 07:28:30 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
2019-10-10 12:43:50 +02:00
|
|
|
$this->makeTestData();
|
2019-04-17 02:58:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 06:08:50 +02:00
|
|
|
|
2023-01-19 01:24:40 +01:00
|
|
|
public function testInvoiceGetPaidInvoices()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2023-02-16 02:36:09 +01:00
|
|
|
])->get('/api/v1/invoices?client_status=paid', )
|
2023-01-19 01:24:40 +01:00
|
|
|
->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
2022-08-18 06:08:50 +02:00
|
|
|
public function testInvoiceArchiveAction()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2023-02-16 02:36:09 +01:00
|
|
|
])->get('/api/v1/invoices/'.$this->invoice->hashed_id.'/archive', )
|
2022-08-18 06:08:50 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-06 03:46:19 +01:00
|
|
|
public function testMarkingDeletedInvoiceAsSent()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
|
2022-02-06 03:46:19 +01:00
|
|
|
ClientContact::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'is_primary' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
ClientContact::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$client = Client::all()->first();
|
|
|
|
|
|
|
|
$invoice = Invoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
|
|
|
$invoice->status_id = Invoice::STATUS_DRAFT;
|
|
|
|
|
|
|
|
$invoice->line_items = $this->buildLineItems();
|
|
|
|
$invoice->uses_inclusive_taxes = false;
|
|
|
|
$invoice->tax_rate1 = 0;
|
|
|
|
$invoice->tax_rate2 = 0;
|
|
|
|
$invoice->tax_rate3 = 0;
|
|
|
|
$invoice->discount = 0;
|
|
|
|
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
$invoice_calc = new InvoiceSum($invoice);
|
|
|
|
$invoice_calc->build();
|
|
|
|
|
|
|
|
$invoice = $invoice_calc->getInvoice();
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
$this->assertEquals(Invoice::STATUS_DRAFT, $invoice->status_id);
|
|
|
|
$this->assertEquals(10, $invoice->amount);
|
|
|
|
$this->assertEquals(0, $invoice->balance);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice_repository = new InvoiceRepository();
|
2022-02-06 03:46:19 +01:00
|
|
|
$invoice = $invoice_repository->delete($invoice);
|
|
|
|
|
|
|
|
$this->assertEquals(10, $invoice->amount);
|
|
|
|
$this->assertEquals(0, $invoice->balance);
|
|
|
|
$this->assertTrue($invoice->is_deleted);
|
|
|
|
|
|
|
|
$invoice->service()->markSent()->save();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $invoice->balance);
|
|
|
|
}
|
|
|
|
|
2019-04-17 02:58:23 +02:00
|
|
|
public function testInvoiceList()
|
|
|
|
{
|
2020-10-01 13:34:05 +02:00
|
|
|
Client::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
|
|
|
|
ClientContact::factory()->create([
|
2020-05-16 04:04:24 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-04-17 02:58:23 +02:00
|
|
|
'client_id' => $c->id,
|
2020-05-16 04:04:24 +02:00
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2019-04-17 02:58:23 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
ClientContact::factory()->create([
|
2020-05-16 04:04:24 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-04-17 02:58:23 +02:00
|
|
|
'client_id' => $c->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_id' => $this->company->id,
|
2019-04-17 02:58:23 +02:00
|
|
|
]);
|
|
|
|
});
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-17 02:58:23 +02:00
|
|
|
$client = Client::all()->first();
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
Invoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
2019-04-17 02:58:23 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/invoices');
|
2019-04-17 02:58:23 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2019-04-16 07:28:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 08:20:32 +02:00
|
|
|
public function testInvoiceRESTEndPoints()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
|
2019-04-17 08:20:32 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id).'/edit');
|
2019-04-17 08:20:32 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$invoice_update = [
|
2019-10-10 12:43:50 +02:00
|
|
|
'tax_name1' => 'dippy',
|
2019-04-17 08:20:32 +02:00
|
|
|
];
|
|
|
|
|
2019-10-10 12:43:50 +02:00
|
|
|
$this->assertNotNull($this->invoice);
|
2019-10-07 11:39:22 +02:00
|
|
|
|
2019-04-17 08:20:32 +02:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update)
|
2019-04-17 08:20:32 +02:00
|
|
|
->assertStatus(200);
|
2019-10-24 06:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPostNewInvoice()
|
|
|
|
{
|
|
|
|
$invoice = [
|
|
|
|
'status_id' => 1,
|
2020-03-21 06:37:30 +01:00
|
|
|
'number' => 'dfdfd',
|
2019-10-24 06:46:24 +02:00
|
|
|
'discount' => 0,
|
|
|
|
'is_amount_discount' => 1,
|
|
|
|
'po_number' => '3434343',
|
|
|
|
'public_notes' => 'notes',
|
|
|
|
'is_deleted' => 0,
|
|
|
|
'custom_value1' => 0,
|
|
|
|
'custom_value2' => 0,
|
|
|
|
'custom_value3' => 0,
|
|
|
|
'custom_value4' => 0,
|
|
|
|
'status' => 1,
|
|
|
|
'client_id' => $this->encodePrimaryKey($this->client->id),
|
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $invoice)
|
2019-10-24 06:46:24 +02:00
|
|
|
->assertStatus(200);
|
2020-07-25 01:02:32 +02:00
|
|
|
|
2021-03-20 00:06:44 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$arr['data']['id'], $invoice)
|
2021-03-20 00:06:44 +01:00
|
|
|
->assertStatus(200);
|
|
|
|
|
2020-07-25 01:02:32 +02:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $invoice)
|
2020-07-25 01:02:32 +02:00
|
|
|
->assertStatus(302);
|
2019-10-24 06:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteInvoice()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->delete('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
|
2019-04-17 08:20:32 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
2020-08-12 12:11:13 +02:00
|
|
|
|
|
|
|
public function testUniqueNumberValidation()
|
|
|
|
{
|
|
|
|
/* stub a invoice in the DB that we will use to test against later */
|
2020-10-01 13:34:05 +02:00
|
|
|
$invoice = Invoice::factory()->create([
|
2020-08-12 12:11:13 +02:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'number' => 'test',
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* Test fire new invoice */
|
|
|
|
$data = [
|
|
|
|
'client_id' => $this->client->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'number' => 'dude',
|
2020-08-12 12:11:13 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $data)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals('dude', $arr['data']['number']);
|
|
|
|
|
|
|
|
/*test validation fires*/
|
|
|
|
$data = [
|
|
|
|
'client_id' => $this->client->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'number' => 'test',
|
2020-08-12 12:11:13 +02:00
|
|
|
];
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
try {
|
2020-08-12 12:11:13 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-09-06 11:38:10 +02:00
|
|
|
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
|
2020-08-12 12:11:13 +02:00
|
|
|
->assertStatus(302);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2021-01-14 05:31:45 +01:00
|
|
|
// nlog('inside update invoice validator');
|
|
|
|
// nlog($message);
|
2020-08-12 12:11:13 +02:00
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'client_id' => $this->client->hashed_id,
|
|
|
|
'number' => 'style',
|
|
|
|
];
|
2020-08-12 12:11:13 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/* test number passed validation*/
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
|
2020-08-12 12:11:13 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'client_id' => $this->client->hashed_id,
|
|
|
|
'number' => 'style',
|
|
|
|
];
|
2020-08-12 12:11:13 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/* Make sure we can UPDATE using the same number*/
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->put('/api/v1/invoices/'.$arr['data']['id'], $data)
|
2020-08-12 12:11:13 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
}
|
2022-01-06 03:31:44 +01:00
|
|
|
|
|
|
|
public function testClientedDeletedAttemptingToCreateInvoice()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
/* Test fire new invoice */
|
2022-01-06 03:31:44 +01:00
|
|
|
$data = [
|
|
|
|
'client_id' => $this->client->hashed_id,
|
|
|
|
'number' => 'dude',
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $data)
|
|
|
|
->assertStatus(200);
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|