2019-05-03 00:29:04 +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-05-03 00:29:04 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2021-07-15 03:31:11 +02:00
|
|
|
use App\Factory\InvoiceToRecurringInvoiceFactory;
|
|
|
|
use App\Factory\RecurringInvoiceToInvoiceFactory;
|
2019-05-03 00:29:04 +02:00
|
|
|
use App\Models\Client;
|
2020-10-01 13:34:05 +02:00
|
|
|
use App\Models\ClientContact;
|
2019-05-03 00:29:04 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2020-02-28 13:11:56 +01:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
2019-05-03 00:29:04 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2020-05-19 00:22:18 +02:00
|
|
|
use Tests\MockAccountData;
|
2019-05-03 00:29:04 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\RecurringInvoiceController
|
|
|
|
*/
|
|
|
|
class RecurringInvoiceTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
2020-05-19 00:22:18 +02:00
|
|
|
use MockAccountData;
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
|
2020-02-28 13:11:56 +01:00
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
2020-05-19 00:22:18 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->makeTestData();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 05:12:53 +02:00
|
|
|
public function testPostRecurringInvoice()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'frequency_id' => 1,
|
|
|
|
'status_id' => 1,
|
|
|
|
'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),
|
|
|
|
'line_items' => $this->buildLineItems(),
|
|
|
|
'remaining_cycles' => -1,
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/recurring_invoices/', $data)
|
2022-03-30 05:12:53 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
$this->assertEquals(RecurringInvoice::STATUS_DRAFT, $arr['data']['status_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPostRecurringInvoiceWithStartAndStop()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'frequency_id' => 1,
|
|
|
|
'status_id' => 1,
|
|
|
|
'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),
|
|
|
|
'line_items' => $this->buildLineItems(),
|
|
|
|
'remaining_cycles' => -1,
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->post('/api/v1/recurring_invoices?start=true', $data)
|
2022-03-30 05:12:53 +02:00
|
|
|
->assertStatus(200);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-30 05:12:53 +02:00
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals(RecurringInvoice::STATUS_ACTIVE, $arr['data']['status_id']);
|
|
|
|
|
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/recurring_invoices/'.$arr['data']['id'].'?stop=true', $data)
|
2022-03-30 05:12:53 +02:00
|
|
|
->assertStatus(200);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-30 05:12:53 +02:00
|
|
|
$arr = $response->json();
|
|
|
|
$this->assertEquals(RecurringInvoice::STATUS_PAUSED, $arr['data']['status_id']);
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
public function testRecurringInvoiceList()
|
|
|
|
{
|
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-19 00:22:18 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
'client_id' => $c->id,
|
2020-05-19 00:22:18 +02:00
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2019-05-03 00:29:04 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
ClientContact::factory()->create([
|
2020-05-19 00:22:18 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
'client_id' => $c->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_id' => $this->company->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
]);
|
|
|
|
});
|
2020-05-19 00:22:18 +02:00
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
$client = Client::all()->first();
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
RecurringInvoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
2019-05-03 00:29:04 +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/recurring_invoices');
|
2019-05-03 00:29:04 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecurringInvoiceRESTEndPoints()
|
|
|
|
{
|
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-19 00:22:18 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
'client_id' => $c->id,
|
2020-05-19 00:22:18 +02:00
|
|
|
'company_id' => $this->company->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'is_primary' => 1,
|
2019-05-03 00:29:04 +02:00
|
|
|
]);
|
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
ClientContact::factory()->create([
|
2020-05-19 00:22:18 +02:00
|
|
|
'user_id' => $this->user->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
'client_id' => $c->id,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_id' => $this->company->id,
|
2019-05-03 00:29:04 +02:00
|
|
|
]);
|
|
|
|
});
|
2022-10-19 00:54:35 +02:00
|
|
|
|
|
|
|
$client = Client::query()->orderBy('id', 'DESC')->first();
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2020-10-01 13:34:05 +02:00
|
|
|
RecurringInvoice::factory()->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]);
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2022-10-19 00:54:35 +02:00
|
|
|
$RecurringInvoice = RecurringInvoice::query()->where('user_id', $this->user->id)->orderBy('id', 'DESC')->first();
|
2019-05-03 00:29:04 +02:00
|
|
|
$RecurringInvoice->save();
|
|
|
|
|
|
|
|
$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/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id));
|
2019-05-03 00:29:04 +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/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id).'/edit');
|
2019-05-03 00:29:04 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$RecurringInvoice_update = [
|
2019-07-05 00:36:40 +02:00
|
|
|
'status_id' => RecurringInvoice::STATUS_DRAFT,
|
2020-07-01 06:37:05 +02:00
|
|
|
'client_id' => $this->encodePrimaryKey($RecurringInvoice->client_id),
|
2022-06-21 11:57:17 +02:00
|
|
|
'number' => 'customnumber',
|
2019-05-03 00:29:04 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertNotNull($RecurringInvoice);
|
|
|
|
|
|
|
|
$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/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id), $RecurringInvoice_update)
|
2019-05-03 00:29:04 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
|
2021-03-20 01:21:50 +01:00
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals('customnumber', $arr['data']['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/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id), $RecurringInvoice_update)
|
2021-03-20 01:21:50 +01:00
|
|
|
->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,
|
|
|
|
])->post('/api/v1/recurring_invoices/', $RecurringInvoice_update)
|
2021-03-20 01:21:50 +01:00
|
|
|
->assertStatus(302);
|
|
|
|
|
2019-05-03 00:29:04 +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,
|
|
|
|
])->delete('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id));
|
2019-05-03 00:29:04 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
2021-07-15 03:31:11 +02:00
|
|
|
|
|
|
|
public function testSubscriptionIdPassesToInvoice()
|
|
|
|
{
|
|
|
|
$recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice);
|
|
|
|
$recurring_invoice->user_id = $this->user->id;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE;
|
|
|
|
$recurring_invoice->remaining_cycles = 2;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice);
|
|
|
|
$recurring_invoice->subscription_id = 10;
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client);
|
|
|
|
|
|
|
|
$this->assertEquals(10, $invoice->subscription_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubscriptionIdPassesToInvoiceIfNull()
|
|
|
|
{
|
|
|
|
$recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice);
|
|
|
|
$recurring_invoice->user_id = $this->user->id;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE;
|
|
|
|
$recurring_invoice->remaining_cycles = 2;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice);
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $invoice->subscription_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubscriptionIdPassesToInvoiceIfNothingSet()
|
|
|
|
{
|
|
|
|
$recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice);
|
|
|
|
$recurring_invoice->user_id = $this->user->id;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE;
|
|
|
|
$recurring_invoice->remaining_cycles = 2;
|
|
|
|
$recurring_invoice->next_send_date = \Carbon\Carbon::now()->addDays(10);
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$recurring_invoice->number = $this->getNextRecurringInvoiceNumber($this->invoice->client, $this->invoice);
|
|
|
|
$recurring_invoice->save();
|
|
|
|
|
|
|
|
$invoice = RecurringInvoiceToInvoiceFactory::create($recurring_invoice, $this->client);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $invoice->subscription_id);
|
|
|
|
}
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|