2019-10-09 14:21:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2019-10-09 14:21:21 +02:00
|
|
|
use App\DataMapper\DefaultSettings;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Faker\Factory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2019-12-14 06:49:48 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2019-10-09 14:21:21 +02:00
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-11-12 22:26:40 +01:00
|
|
|
* @covers App\Utils\Traits\CompanySettingsSaver
|
2019-10-09 14:21:21 +02:00
|
|
|
*/
|
|
|
|
class CompanySettingsTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use MockAccountData;
|
|
|
|
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->withoutExceptionHandling();
|
2019-10-09 14:21:21 +02:00
|
|
|
Model::reguard();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientNumberCantBeModified()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$settings = $this->company->settings;
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$settings->client_number_counter = 200;
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->company->saveSettings($settings, $this->company);
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2019-12-14 06:49:48 +01:00
|
|
|
//$this->withoutExceptionHandling();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = false;
|
2019-12-14 06:49:48 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->withHeaders([
|
2019-10-09 14:21:21 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
|
|
|
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
|
2020-03-21 06:37:30 +01:00
|
|
|
} catch (ValidationException $e) {
|
|
|
|
$message = json_decode($e->validator->getMessageBag(), 1);
|
2019-12-14 06:49:48 +01:00
|
|
|
}
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($response) {
|
2019-12-14 06:49:48 +01:00
|
|
|
$response->assertStatus(200);
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2019-12-14 06:49:48 +01:00
|
|
|
$arr = $response->json();
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data']['settings']['timezone_id'], 1);
|
2019-12-14 06:49:48 +01:00
|
|
|
}
|
2019-11-12 22:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullValuesInSettings()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$settings = $this->company->settings;
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$settings->reset_counter_date = null;
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->company->saveSettings($settings, $this->company);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
|
|
|
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
|
|
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data']['settings']['reset_counter_date'], '');
|
2019-10-09 14:21:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:20:38 +02:00
|
|
|
public function testIntegerEdgeCases()
|
2019-10-09 14:21:21 +02:00
|
|
|
{
|
|
|
|
$settings = $this->company->settings;
|
|
|
|
|
|
|
|
$settings->client_number_counter = "a";
|
2019-10-09 23:36:50 +02:00
|
|
|
$settings->invoice_number_counter = 1000;
|
2019-10-10 00:20:38 +02:00
|
|
|
$settings->quote_number_counter = 1.2;
|
|
|
|
$settings->credit_number_counter = 10.1;
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
$this->company->saveSettings($settings, $this->company);
|
2019-10-09 14:21:21 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
|
|
|
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2019-10-09 14:21:21 +02:00
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
$this->assertTrue(is_int($arr['data']['settings']['client_number_counter']));
|
|
|
|
$this->assertTrue(is_int($arr['data']['settings']['invoice_number_counter']));
|
|
|
|
$this->assertTrue(is_int($arr['data']['settings']['quote_number_counter']));
|
|
|
|
$this->assertTrue(is_int($arr['data']['settings']['credit_number_counter']));
|
2019-10-09 14:21:21 +02:00
|
|
|
}
|
2019-10-10 00:20:38 +02:00
|
|
|
|
|
|
|
public function testFloatEdgeCases()
|
|
|
|
{
|
|
|
|
$settings = $this->company->settings;
|
|
|
|
|
|
|
|
$settings->default_task_rate = "a";
|
|
|
|
$settings->tax_rate1 = 10.0;
|
|
|
|
$settings->tax_rate2 = "10.0";
|
|
|
|
$settings->tax_rate3 = "10.5";
|
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
$this->company->saveSettings($settings, $this->company);
|
2019-10-10 00:20:38 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
|
|
|
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data']['settings']['default_task_rate'], 0);
|
|
|
|
$this->assertEquals($arr['data']['settings']['tax_rate1'], 10.0);
|
|
|
|
$this->assertEquals($arr['data']['settings']['tax_rate2'], 10.0);
|
|
|
|
$this->assertEquals($arr['data']['settings']['tax_rate3'], 10.5);
|
2019-10-10 00:20:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 01:15:35 +02:00
|
|
|
public function testBoolEdgeCases()
|
|
|
|
{
|
|
|
|
$settings = $this->company->settings;
|
|
|
|
|
|
|
|
$settings->require_invoice_signature = true;
|
|
|
|
$settings->require_quote_signature = true;
|
|
|
|
$settings->show_accept_quote_terms = false;
|
|
|
|
$settings->show_accept_invoice_terms = "TRUE";
|
2019-11-04 01:22:59 +01:00
|
|
|
$settings->enable_client_portal_tasks = "FALSE";
|
2019-10-10 01:15:35 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->company->saveSettings($settings, $this->company);
|
2019-10-10 01:15:35 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
|
|
|
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-10-10 01:15:35 +02:00
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
$arr = $response->json();
|
2019-10-10 01:15:35 +02:00
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
$this->assertTrue($arr['data']['settings']['require_invoice_signature']);
|
|
|
|
$this->assertTrue($arr['data']['settings']['require_quote_signature']);
|
|
|
|
$this->assertFalse($arr['data']['settings']['show_accept_quote_terms']);
|
|
|
|
$this->assertTrue($arr['data']['settings']['show_accept_invoice_terms']);
|
|
|
|
$this->assertFalse($arr['data']['settings']['enable_client_portal_tasks']);
|
2019-10-10 01:15:35 +02:00
|
|
|
}
|
2019-10-10 00:20:38 +02:00
|
|
|
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
public function testCompanyNullValueMatrixPOST()
|
|
|
|
{
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->reset_counter_date = null;
|
|
|
|
|
|
|
|
|
|
|
|
$this->company->saveSettings($settings, $this->company);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-12 22:26:40 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post('/api/v1/companies?include=company', $this->company->toArray());
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'], '');
|
2019-11-12 22:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyWrongValueMatrixPOST()
|
|
|
|
{
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->reset_counter_date = 1;
|
|
|
|
|
|
|
|
$this->company->saveSettings($settings, $this->company);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-12 22:26:40 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post('/api/v1/companies?include=company', $this->company->toArray());
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'], '');
|
2019-11-12 22:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCompanyWrong2ValueMatrixPOST()
|
|
|
|
{
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->reset_counter_date = '1';
|
|
|
|
|
|
|
|
$this->company->saveSettings($settings, $this->company);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-12 22:26:40 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post('/api/v1/companies?include=company', $this->company->toArray());
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'], '1');
|
2019-11-12 22:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompanyrightValueMatrixPOST()
|
|
|
|
{
|
|
|
|
$settings = CompanySettings::defaults();
|
|
|
|
$settings->reset_counter_date = '1/1/2000';
|
|
|
|
|
|
|
|
$this->company->saveSettings($settings, $this->company);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$response = $this->withHeaders([
|
2019-11-12 22:26:40 +01:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-Token' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post('/api/v1/companies?include=company', $this->company->toArray());
|
2019-11-12 22:26:40 +01:00
|
|
|
|
|
|
|
$arr = $response->json();
|
2020-03-21 06:37:30 +01:00
|
|
|
$response->assertStatus(200);
|
2019-11-12 22:26:40 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'], '1/1/2000');
|
2019-11-12 22:26:40 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|