diff --git a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php index 01f2cbedc5dd..d48b84130834 100644 --- a/app/Http/Requests/Invoice/UpdateInvoiceRequest.php +++ b/app/Http/Requests/Invoice/UpdateInvoiceRequest.php @@ -54,6 +54,9 @@ class UpdateInvoiceRequest extends Request $rules['id'] = new LockedInvoiceRule($this->invoice); + if($this->input('number')) + $rules['number'] = 'unique:invoices,number,' . $this->id . ',id,company_id,' . $this->invoice->company_id; + return $rules; } diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index 5b6a4f19c6bc..0271636e360e 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -145,4 +145,76 @@ class InvoiceTest extends TestCase $response->assertStatus(200); } + + 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, + 'number' => 'dude' + ]; + + $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, + 'number' => 'test' + ]; + + try{ + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/invoices/' . $arr['data']['id'], $data) + ->assertStatus(302); + + } catch (ValidationException $e) { + + $message = json_decode($e->validator->getMessageBag(), 1); + info("inside update invoice validator"); + info($message); + $this->assertNotNull($message); + } + + $data = [ + 'client_id' => $this->client->hashed_id, + 'number' => 'style' + ]; + + /* test number passed validation*/ + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/invoices/' . $arr['data']['id'], $data) + ->assertStatus(200); + + $data = [ + 'client_id' => $this->client->hashed_id, + 'number' => 'style' + ]; + + /* Make sure we can UPDATE using the same number*/ + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/invoices/' . $arr['data']['id'], $data) + ->assertStatus(200); + } }