Merge pull request #3922 from turbo124/v2

Validation for locked invoices
This commit is contained in:
David Bomba 2020-07-22 10:03:40 +10:00 committed by GitHub
commit 532e3fd484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 748 additions and 281 deletions

View File

@ -12,6 +12,7 @@
namespace App\Http\Requests\Invoice;
use App\Http\Requests\Request;
use App\Http\ValidationRules\Invoice\LockedInvoiceRule;
use App\Utils\Traits\ChecksEntityStatus;
use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash;
@ -38,6 +39,7 @@ class UpdateInvoiceRequest extends Request
public function rules()
{
$rules = [];
if ($this->input('documents') && is_array($this->input('documents'))) {
@ -50,6 +52,8 @@ class UpdateInvoiceRequest extends Request
$rules['documents'] = 'file|mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx|max:20000';
}
$rules['id'] = new LockedInvoiceRule($this->invoice);
return $rules;
}
@ -86,6 +90,7 @@ class UpdateInvoiceRequest extends Request
}
}
$input['id'] = $this->invoice->id;
$input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : [];
$this->replace($input);

View File

@ -0,0 +1,84 @@
<?php
/**
* 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 App\Http\ValidationRules\Invoice;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use App\Models\User;
use Illuminate\Contracts\Validation\Rule;
/**
* Class LockedInvoiceRule
* @package App\Http\ValidationRules
*/
class LockedInvoiceRule implements Rule
{
public $invoice;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return $this->checkIfInvoiceLocked(); //if it exists, return false!
}
/**
* @return string
*/
public function message()
{
return ctrans('texts.locked_invoice');
}
/**
* @param $email
*
* //off,when_sent,when_paid
*
* @return bool
*/
private function checkIfInvoiceLocked() : bool
{
$lock_invoices = $this->invoice->client->getSetting('lock_invoices');
switch ($lock_invoices) {
case 'off':
return true;
break;
case 'when_sent':
if($this->invoice->status_id == Invoice::STATUS_SENT)
return false;
return true;
break;
case 'when_paid':
if($this->invoice->status_id == Invoice::STATUS_PAID)
return false;
return true;
break;
default:
return true;
break;
}
}
}

776
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3240,5 +3240,5 @@ return [
'node_status_not_found' => 'I could not find Node anywhere. Is it installed?',
'npm_status_not_found' => 'I could not find NPM anywhere. Is it installed?',
'locked_invoice' => 'This invoice is locked and unable to be modified',
];

View File

@ -133,6 +133,8 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML());
$this->assertTrue(true);
}
public function testClean()
@ -242,6 +244,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testModern()
@ -355,8 +360,12 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testBold()
{
$state = [
@ -468,6 +477,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testPlain()
@ -573,6 +585,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testHipster()
@ -682,6 +697,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testElegant()
@ -795,6 +813,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testCreative()
@ -908,6 +929,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
public function testPlayful()
@ -1014,5 +1038,9 @@ class PdfMakerDesignsTest extends TestCase
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
}

View File

@ -12,6 +12,11 @@ class PdfMakerTest extends TestCase
'variables' => [],
];
public function setUp() :void
{
$this->markTestSkipped();
}
public function testDesignLoadsCorrectly()
{
$maker = new PdfMaker($this->state);

View File

@ -0,0 +1,127 @@
<?php
namespace Tests\Integration;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Cache;
use JsonSchema\Exception\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\ValidationRules\Invoice\LockedInvoiceRule
*/
class CheckLockedInvoiceValidationTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
public function setUp() :void
{
parent::setUp();
$this->makeTestData();
}
public function testValidationWorksForLockedInvoiceWhenOff()
{
$invoice_update = [
'po_number' => 'test',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update)
->assertStatus(200);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
\Log::error($message);
}
}
public function testValidationFailsForLockedInvoiceWhenSent()
{
$this->company->settings->lock_invoices = 'when_sent';
$this->company->save();
$settings = $this->client->settings;
$settings->lock_invoices = 'when_sent';
$this->client->settings = $settings;
$this->client->save();
$this->invoice = $this->invoice->service()->markSent()->save();
$invoice_update = [
'po_number' => 'test',
];
$this->assertEquals($this->invoice->status_id, \App\Models\Invoice::STATUS_SENT);
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
\Log::error($message);
}
if ($response) {
$response->assertStatus(302);
}
}
public function testValidationFailsForLockedInvoiceWhenPaid()
{
$this->company->settings->lock_invoices = 'when_paid';
$this->company->save();
$settings = $this->client->settings;
$settings->lock_invoices = 'when_paid';
$this->client->settings = $settings;
$this->client->save();
$this->invoice = $this->invoice->service()->markPaid()->save();
$invoice_update = [
'po_number' => 'test',
];
$this->assertEquals($this->invoice->status_id, \App\Models\Invoice::STATUS_PAID);
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
$this->assertNotNull($message);
\Log::error($message);
}
if ($response) {
$response->assertStatus(302);
}
}
}