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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-16 07:28:30 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
use App\DataMapper\ClientSettings;
|
|
|
|
use App\DataMapper\CompanySettings;
|
2019-10-24 06:46:24 +02:00
|
|
|
use App\Factory\InvoiceFactory;
|
2019-04-17 02:58:23 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
2019-04-17 08:20:32 +02:00
|
|
|
use App\Models\Invoice;
|
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\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
2019-04-24 12:01:40 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
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
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceList()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
factory(\App\Models\Client::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) {
|
2020-03-21 06:37:30 +01:00
|
|
|
factory(\App\Models\ClientContact::class, 1)->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-03-21 06:37:30 +01:00
|
|
|
factory(\App\Models\ClientContact::class, 1)->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-05-16 04:04:24 +02:00
|
|
|
factory(\App\Models\Invoice::class, 1)->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([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-16 04:04:24 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2019-04-17 02:58:23 +02:00
|
|
|
])->get('/api/v1/invoices');
|
|
|
|
|
|
|
|
$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([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-10-10 12:43:50 +02:00
|
|
|
'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([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-10-10 12:43:50 +02:00
|
|
|
'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([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-10-10 12:43:50 +02:00
|
|
|
'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([
|
2019-10-24 06:46:24 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $invoice)
|
|
|
|
->assertStatus(200);
|
2020-07-25 01:02:32 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
//test that the same request should produce a validation error due
|
2020-07-25 01:02:32 +02:00
|
|
|
//to duplicate number being used.
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/invoices/', $invoice)
|
|
|
|
->assertStatus(302);
|
2019-10-24 06:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteInvoice()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-04-17 08:20:32 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2019-10-10 12:43:50 +02:00
|
|
|
'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 */
|
|
|
|
$invoice = factory(\App\Models\Invoice::class)->create([
|
|
|
|
'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);
|
2020-09-06 11:38:10 +02:00
|
|
|
info('inside update invoice validator');
|
2020-08-12 12:11:13 +02:00
|
|
|
info($message);
|
|
|
|
$this->assertNotNull($message);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$data = [
|
2020-08-12 12:11:13 +02:00
|
|
|
'client_id' => $this->client->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'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([
|
2020-08-12 12:11:13 +02:00
|
|
|
'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(200);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$data = [
|
2020-08-12 12:11:13 +02:00
|
|
|
'client_id' => $this->client->hashed_id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'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([
|
2020-08-12 12:11:13 +02:00
|
|
|
'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(200);
|
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|