mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 07:04:34 -04:00
Tests for company settings
This commit is contained in:
parent
0e9ca5c1ce
commit
056b2cbdb7
@ -26,7 +26,6 @@ class CompanySettings extends BaseSettings
|
|||||||
public $military_time = false;
|
public $military_time = false;
|
||||||
|
|
||||||
public $language_id = '';
|
public $language_id = '';
|
||||||
public $precision = 2;
|
|
||||||
public $show_currency_code = false;
|
public $show_currency_code = false;
|
||||||
|
|
||||||
public $payment_terms = 1;
|
public $payment_terms = 1;
|
||||||
@ -200,7 +199,7 @@ class CompanySettings extends BaseSettings
|
|||||||
'military_time' => 'bool',
|
'military_time' => 'bool',
|
||||||
'language_id' => 'string',
|
'language_id' => 'string',
|
||||||
'show_currency_code' => 'bool',
|
'show_currency_code' => 'bool',
|
||||||
'payment_terms' => 'int',
|
'payment_terms' => 'integer',
|
||||||
'custom_label1' => 'string',
|
'custom_label1' => 'string',
|
||||||
'custom_label2' => 'string',
|
'custom_label2' => 'string',
|
||||||
'custom_label3' => 'string',
|
'custom_label3' => 'string',
|
||||||
@ -247,21 +246,21 @@ class CompanySettings extends BaseSettings
|
|||||||
'inclusive_taxes' => 'bool',
|
'inclusive_taxes' => 'bool',
|
||||||
'invoice_number_prefix' => 'string',
|
'invoice_number_prefix' => 'string',
|
||||||
'invoice_number_pattern' => 'string',
|
'invoice_number_pattern' => 'string',
|
||||||
'invoice_number_counter' => 'int',
|
'invoice_number_counter' => 'integer',
|
||||||
'quote_number_prefix' => 'string',
|
'quote_number_prefix' => 'string',
|
||||||
'quote_number_pattern' => 'string',
|
'quote_number_pattern' => 'string',
|
||||||
'quote_number_counter' => 'int',
|
'quote_number_counter' => 'integer',
|
||||||
'client_number_prefix' => 'string',
|
'client_number_prefix' => 'string',
|
||||||
'client_number_pattern' => 'string',
|
'client_number_pattern' => 'string',
|
||||||
'client_number_counter' => 'int',
|
'client_number_counter' => 'integer',
|
||||||
'credit_number_prefix' => 'string',
|
'credit_number_prefix' => 'string',
|
||||||
'credit_number_pattern' => 'string',
|
'credit_number_pattern' => 'string',
|
||||||
'credit_number_counter' => 'int',
|
'credit_number_counter' => 'integer',
|
||||||
'shared_invoice_quote_counter' => 'bool',
|
'shared_invoice_quote_counter' => 'bool',
|
||||||
'recurring_invoice_number_prefix' => 'string',
|
'recurring_invoice_number_prefix' => 'string',
|
||||||
'reset_counter_frequency_id' => 'int',
|
'reset_counter_frequency_id' => 'integer',
|
||||||
'reset_counter_date' => 'string',
|
'reset_counter_date' => 'string',
|
||||||
'counter_padding' => 'int',
|
'counter_padding' => 'integer',
|
||||||
'design' => 'string',
|
'design' => 'string',
|
||||||
'company_gateways' => 'string',
|
'company_gateways' => 'string',
|
||||||
];
|
];
|
||||||
@ -271,9 +270,9 @@ class CompanySettings extends BaseSettings
|
|||||||
* cannot be modified client side
|
* cannot be modified client side
|
||||||
*/
|
*/
|
||||||
public static $protected_fields = [
|
public static $protected_fields = [
|
||||||
'credit_number_counter',
|
// 'credit_number_counter',
|
||||||
'invoice_number_counter',
|
// 'invoice_number_counter',
|
||||||
'quote_number_counter',
|
// 'quote_number_counter',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -406,7 +406,7 @@ class CompanyController extends BaseController
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function update(UpdateCompanyRequest $request, Company $company)
|
public function update(UpdateCompanyRequest $request, Company $company)
|
||||||
{\Log::error($request->all());
|
{
|
||||||
$company = $this->company_repo->save($request->all(), $company);
|
$company = $this->company_repo->save($request->all(), $company);
|
||||||
|
|
||||||
$company->saveSettings($request->input('settings'));
|
$company->saveSettings($request->input('settings'));
|
||||||
|
@ -41,7 +41,7 @@ class Company extends BaseModel
|
|||||||
use PresentableTrait;
|
use PresentableTrait;
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
use CompanySettingsSaver;
|
use CompanySettingsSaver;
|
||||||
|
|
||||||
protected $presenter = 'App\Models\Presenters\CompanyPresenter';
|
protected $presenter = 'App\Models\Presenters\CompanyPresenter';
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
@ -65,14 +65,14 @@ class Company extends BaseModel
|
|||||||
|
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'id',
|
'id',
|
||||||
'settings',
|
// 'settings',
|
||||||
'account_id',
|
// 'account_id',
|
||||||
'company_key',
|
// 'company_key',
|
||||||
'db',
|
'db',
|
||||||
'domain',
|
// 'domain',
|
||||||
'ip',
|
'ip',
|
||||||
'industry_id',
|
// 'industry_id',
|
||||||
'size_id',
|
// 'size_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
|
@ -22,16 +22,22 @@ trait CompanySettingsSaver
|
|||||||
|
|
||||||
public function saveSettings($settings)
|
public function saveSettings($settings)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(!$settings)
|
if(!$settings)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
\Log::error(print_r($settings));
|
|
||||||
$company_settings = $this->settings;
|
$company_settings = $this->settings;
|
||||||
|
|
||||||
//unset protected properties.
|
//unset protected properties.
|
||||||
foreach(CompanySettings::$protected_fields as $field)
|
foreach(CompanySettings::$protected_fields as $field)
|
||||||
unset($settings[$field]);
|
unset($settings[$field]);
|
||||||
|
|
||||||
|
//make sure the inbound settings have the correct casts!
|
||||||
|
//$settings = CompanySettings::setCasts($settings, CompanySettings::$casts);
|
||||||
|
|
||||||
|
//todo checks are here
|
||||||
|
// $settings = $this->checkSettingType($settings, CompanySettings::$casts);
|
||||||
|
|
||||||
//iterate through set properties with new values;
|
//iterate through set properties with new values;
|
||||||
foreach($settings as $key => $value)
|
foreach($settings as $key => $value)
|
||||||
$company_settings->{$key} = $value;
|
$company_settings->{$key} = $value;
|
||||||
@ -40,4 +46,83 @@ trait CompanySettingsSaver
|
|||||||
$this->save();
|
$this->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function checkSettingType($settings, $casts)
|
||||||
|
{
|
||||||
|
$settings = (object)$settings;
|
||||||
|
|
||||||
|
foreach ($casts as $key => $value){
|
||||||
|
|
||||||
|
\Log::error("the gettype of {$key} = ". gettype($settings->{$key}));
|
||||||
|
|
||||||
|
if(substr($key, -3) == '_id'){
|
||||||
|
$value = "integer";
|
||||||
|
|
||||||
|
if($this->checkAttribute($value, (int)$settings->{$key})){
|
||||||
|
//throw new \Exception($settings->{$key}. " " . $key . " is not type ". $value);
|
||||||
|
\Log::error($settings->{$key}. " " . $key . " is type ". $value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
\Log::error($settings->{$key}. " " . $key . " is nottype ". $value);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(is_null($settings->{$key}) || !isset($settings->{$key}) || $settings->{$key} == ''){
|
||||||
|
\Log::error("skipping ".$settings->{$key}. " " . $key . " is type ". $value);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
\Log::error("checking ".$settings->{$key}. " " . $key . " is type ". $value);
|
||||||
|
|
||||||
|
if($this->checkAttribute($value, $settings->{$key})){
|
||||||
|
//throw new \Exception($settings->{$key}. " " . $key . " is not type ". $value);
|
||||||
|
\Log::error($settings->{$key}. " " . $key . " is type ". $value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
\Log::error($settings->{$key}. " " . $key . " is nottype ". $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function checkAttribute($key, $value)
|
||||||
|
{
|
||||||
|
switch ($key)
|
||||||
|
{
|
||||||
|
case 'int':
|
||||||
|
case 'integer':
|
||||||
|
return is_int($value);
|
||||||
|
case 'real':
|
||||||
|
case 'float':
|
||||||
|
case 'double':
|
||||||
|
return is_float($value);
|
||||||
|
case 'string':
|
||||||
|
return method_exists($value, '__toString' ) || is_null($value) || is_string($value);
|
||||||
|
case 'bool':
|
||||||
|
case 'boolean':
|
||||||
|
return is_bool($value);
|
||||||
|
case 'object':
|
||||||
|
return is_object($value);
|
||||||
|
case 'array':
|
||||||
|
return is_array($value);
|
||||||
|
case 'json':
|
||||||
|
json_decode($string);
|
||||||
|
return (json_last_error() == JSON_ERROR_NONE);
|
||||||
|
default:
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
89
tests/Feature/CompanySettingsTest.php
Normal file
89
tests/Feature/CompanySettingsTest.php
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
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\Log;
|
||||||
|
use Illuminate\Support\Facades\Session;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
|
||||||
|
Model::reguard();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testClientNumberCantBeModified()
|
||||||
|
{
|
||||||
|
$settings = $this->company->settings;
|
||||||
|
|
||||||
|
$settings->client_number_counter = 200;
|
||||||
|
|
||||||
|
$this->company->settings = $settings;
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
$this->assertEquals($arr['data']['settings']['timezone_id'],15);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSettingCasts()
|
||||||
|
{
|
||||||
|
$settings = $this->company->settings;
|
||||||
|
|
||||||
|
$settings->client_number_counter = "a";
|
||||||
|
|
||||||
|
$this->company->settings = $settings;
|
||||||
|
|
||||||
|
$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();
|
||||||
|
//\Log::error($arr);
|
||||||
|
|
||||||
|
// $this->assertEquals($arr['data']['settings']['client_number_counter'],1);
|
||||||
|
}
|
||||||
|
}
|
@ -48,4 +48,5 @@ class CompanySettingsTest extends TestCase
|
|||||||
$this->assertTrue(isset($this->company_settings->timezone_id));
|
$this->assertTrue(isset($this->company_settings->timezone_id));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user