diff --git a/app/Factory/QuoteFactory.php b/app/Factory/QuoteFactory.php index 0c52cf7205ef..21232de5271d 100644 --- a/app/Factory/QuoteFactory.php +++ b/app/Factory/QuoteFactory.php @@ -35,7 +35,6 @@ class QuoteFactory $quote->partial_due_date = null; $quote->is_deleted = false; $quote->line_items = json_encode([]); - $quote->settings = ClientSettings::buildClientSettings(CompanySettings::defaults(), ClientSettings::defaults()); //todo need to embed the settings here $quote->backup = json_encode([]); $quote->tax_name1 = ''; $quote->tax_rate1 = 0; diff --git a/app/Http/Requests/Company/StoreCompanyRequest.php b/app/Http/Requests/Company/StoreCompanyRequest.php index 1d2091bc3d91..551616a4a18d 100644 --- a/app/Http/Requests/Company/StoreCompanyRequest.php +++ b/app/Http/Requests/Company/StoreCompanyRequest.php @@ -34,7 +34,7 @@ class StoreCompanyRequest extends Request //$this->sanitize(); $rules = []; - $rules['name'] = 'required'; + //$rules['name'] = 'required'; $rules['company_logo'] = 'mimes:jpeg,jpg,png,gif|max:10000'; // max 10000kb $rules['settings'] = new ValidSettingsRule(); diff --git a/app/Jobs/Company/CreateCompany.php b/app/Jobs/Company/CreateCompany.php index 520160698c93..055ab58bf6be 100644 --- a/app/Jobs/Company/CreateCompany.php +++ b/app/Jobs/Company/CreateCompany.php @@ -50,7 +50,10 @@ class CreateCompany public function handle() : ?Company { $settings = CompanySettings::defaults(); - $settings->name = isset($this->request['name']) ? $this->request['name'] : $this->request['first_name'] . ' ' . $this->request['last_name']; + + $default_company_name = isset($this->request['first_name']) ?: '' . ' ' . isset($this->request['last_name']) ?: ''; + + $settings->name = isset($this->request['name']) ? $this->request['name'] : $default_company_name; $company = new Company(); $company->account_id = $this->account->id; diff --git a/app/Models/Company.php b/app/Models/Company.php index 2c9773e2db3e..2e75076b26b2 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -30,7 +30,7 @@ use App\Models\TaxRate; use App\Models\Timezone; use App\Models\Traits\AccountTrait; use App\Models\User; -use App\Utils\Traits\SettingsSaver; +use App\Utils\Traits\CompanySettingsSaver; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Log; @@ -40,7 +40,7 @@ class Company extends BaseModel { use PresentableTrait; use MakesHash; - use SettingsSaver; + use CompanySettingsSaver; protected $presenter = 'App\Models\Presenters\CompanyPresenter'; diff --git a/app/Utils/Traits/SettingsSaver.php b/app/Utils/Traits/SettingsSaver.php index 017006e72b82..66b107d0a56e 100644 --- a/app/Utils/Traits/SettingsSaver.php +++ b/app/Utils/Traits/SettingsSaver.php @@ -144,6 +144,10 @@ trait SettingsSaver /*Catch all filter */ if($this->checkAttribute($value, $settings->{$key})){ + + if($value == 'string' && is_null($settings->{$key})) + $settings->{$key} = ''; + settype($settings->{$key}, $value); } else { diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index a5e67cd8900d..281395a23551 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -88,7 +88,7 @@ class CreateUsersTable extends Migration $table->string('site_url', 200)->nullable(); $table->boolean('is_offsite')->default(false); $table->boolean('is_secure')->default(false); - $table->text('fields')->nullable(); + $table->mediumText('fields')->nullable(); $table->unsignedInteger('default_gateway_type_id')->default(1); $table->timestamps(6); }); @@ -162,8 +162,8 @@ class CreateUsersTable extends Migration $table->string('first_day_of_week')->nullable(); $table->string('first_month_of_year')->nullable(); $table->smallInteger('enable_modules')->default(0); - $table->text('custom_fields'); - $table->text('settings'); + $table->mediumText('custom_fields'); + $table->mediumText('settings'); $table->timestamps(6); //$table->softDeletes('deleted_at', 6); @@ -184,8 +184,8 @@ class CreateUsersTable extends Migration $table->unsignedInteger('company_id'); $table->unsignedInteger('account_id'); $table->unsignedInteger('user_id')->index(); - $table->text('permissions')->nullable(); - $table->text('settings')->nullable(); + $table->mediumText('permissions')->nullable(); + $table->mediumText('settings')->nullable(); $table->boolean('is_owner')->default(false); $table->boolean('is_admin')->default(false); $table->boolean('is_locked')->default(false); // locks user out of account @@ -243,7 +243,7 @@ class CreateUsersTable extends Migration $table->unsignedInteger('avatar_height')->nullable(); $table->unsignedInteger('avatar_size')->nullable(); $table->datetime('last_login')->nullable(); - $table->text('signature')->nullable(); + $table->mediumText('signature')->nullable(); $table->string('password'); $table->rememberToken(); @@ -309,7 +309,7 @@ class CreateUsersTable extends Migration $table->string('shipping_state')->nullable(); $table->string('shipping_postal_code')->nullable(); $table->unsignedInteger('shipping_country_id')->nullable(); - $table->text('settings')->nullable(); + $table->mediumText('settings')->nullable(); $table->boolean('is_deleted')->default(false); $table->unsignedInteger('group_settings_id')->nullable(); @@ -378,7 +378,7 @@ class CreateUsersTable extends Migration $table->boolean('show_billing_address')->default(true)->nullable(); $table->boolean('show_shipping_address')->default(true)->nullable(); $table->boolean('update_details')->default(false)->nullable(); - $table->text('config'); + $table->mediumText('config'); $table->text('fees_and_limits'); $table->timestamps(6); @@ -413,9 +413,9 @@ class CreateUsersTable extends Migration $t->boolean('is_deleted')->default(false); - $t->text('line_items')->nullable(); + $t->mediumText('line_items')->nullable(); //$t->text('settings')->nullable(); - $t->text('backup')->nullable(); + $t->mediumText('backup')->nullable(); $t->text('footer')->nullable(); $t->text('public_notes')->nullable(); @@ -483,9 +483,8 @@ class CreateUsersTable extends Migration $t->boolean('is_deleted')->default(false); - $t->text('line_items')->nullable(); - $t->text('settings')->nullable(); - $t->text('backup')->nullable(); + $t->mediumText('line_items')->nullable(); + $t->mediumText('backup')->nullable(); $t->text('footer')->nullable(); $t->text('public_notes')->nullable(); @@ -547,9 +546,8 @@ class CreateUsersTable extends Migration $t->boolean('is_deleted')->default(false); - $t->text('line_items')->nullable(); - $t->text('settings')->nullable(); - $t->text('backup')->nullable(); + $t->mediumText('line_items')->nullable(); + $t->mediumText('backup')->nullable(); $t->text('footer')->nullable(); $t->text('public_notes')->nullable(); @@ -609,9 +607,8 @@ class CreateUsersTable extends Migration $t->boolean('is_deleted')->default(false); - $t->text('line_items')->nullable(); - $t->text('settings')->nullable(); - $t->text('backup')->nullable(); + $t->mediumText('line_items')->nullable(); + $t->mediumText('backup')->nullable(); $t->text('footer')->nullable(); $t->text('public_notes')->nullable(); @@ -659,7 +656,7 @@ class CreateUsersTable extends Migration $t->string('key')->index(); $t->string('transaction_reference')->nullable(); $t->string('message_id')->nullable(); - $t->text('email_error')->nullable(); + $t->mediumText('email_error')->nullable(); $t->text('signature_base64')->nullable(); $t->datetime('signature_date')->nullable(); @@ -791,7 +788,7 @@ class CreateUsersTable extends Migration $table->string('description')->nullable(); $table->boolean('is_deleted')->default(false); $table->boolean('is_running')->default(false); - $table->text('time_log')->nullable(); + $table->mediumText('time_log')->nullable(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); @@ -894,7 +891,7 @@ class CreateUsersTable extends Migration Schema::create('backups', function ($table) { $table->increments('id'); $table->unsignedInteger('activity_id'); - $table->text('json_backup')->nullable(); + $table->mediumText('json_backup')->nullable(); $table->timestamps(6); $table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade'); @@ -950,7 +947,7 @@ class CreateUsersTable extends Migration $table->unsignedInteger('company_id'); $table->unsignedInteger('user_id')->nullable(); $table->string('name')->nullable(); - $table->text('settings')->nullable(); + $table->mediumText('settings')->nullable(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); }); @@ -978,7 +975,7 @@ class CreateUsersTable extends Migration $table->unsignedInteger('category_id')->nullable(); $table->unsignedInteger('event_id')->nullable(); $table->unsignedInteger('type_id')->nullable(); - $table->text('log'); + $table->mediumText('log'); $table->timestamps(6); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); diff --git a/tests/Feature/CompanySettingsTest.php b/tests/Feature/CompanySettingsTest.php index 120dbe3b9dd2..d0cf10e13b32 100644 --- a/tests/Feature/CompanySettingsTest.php +++ b/tests/Feature/CompanySettingsTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; use App\Models\Account; use App\Models\Client; @@ -15,14 +16,13 @@ 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 - * @covers App\Utils\Traits\SettingsSaver + * @covers App\Utils\Traits\CompanySettingsSaver */ class CompanySettingsTest extends TestCase { @@ -47,11 +47,11 @@ class CompanySettingsTest extends TestCase public function testClientNumberCantBeModified() { - // $settings = $this->company->settings; + $settings = $this->company->settings; - // $settings->client_number_counter = 200; + $settings->client_number_counter = 200; - // $this->company->settings = $settings; + $this->company->saveSettings($settings, $this->company); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), @@ -61,8 +61,30 @@ class CompanySettingsTest extends TestCase $response->assertStatus(200); + $arr = $response->json(); -// $this->assertEquals($arr['data']['settings']['timezone_id'],15); + $this->assertEquals($arr['data']['settings']['timezone_id'],15); + } + + public function testNullValuesInSettings() + { + $settings = $this->company->settings; + + $settings->reset_counter_date = null; + + $this->company->saveSettings($settings, $this->company); + + $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']['reset_counter_date'],''); } public function testIntegerEdgeCases() @@ -74,15 +96,21 @@ class CompanySettingsTest extends TestCase $settings->quote_number_counter = 1.2; $settings->credit_number_counter = 10.1; - $this->company->settings = $settings; + $this->company->saveSettings($settings, $this->company); $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(302); + $response->assertStatus(200); + $arr = $response->json(); + + $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'])); } public function testFloatEdgeCases() @@ -94,14 +122,22 @@ class CompanySettingsTest extends TestCase $settings->tax_rate2 = "10.0"; $settings->tax_rate3 = "10.5"; - $this->company->settings = $settings; + $this->company->saveSettings($settings, $this->company); $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(302); + $response->assertStatus(200); + + $arr = $response->json(); + + $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); + } @@ -115,16 +151,96 @@ class CompanySettingsTest extends TestCase $settings->show_accept_invoice_terms = "TRUE"; $settings->enable_client_portal_tasks = "FALSE"; - $this->company->settings = $settings; + $this->company->saveSettings($settings, $this->company); $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(302); + $response->assertStatus(200); + $arr = $response->json(); + $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']); } + + public function testCompanyNullValueMatrixPOST() + { + $settings = CompanySettings::defaults(); + $settings->reset_counter_date = null; + + + $this->company->saveSettings($settings, $this->company); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-Token' => $this->token, + ])->post('/api/v1/companies?include=company',$this->company->toArray()); + + $arr = $response->json(); + $response->assertStatus(200); + + $this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'],''); + } + + public function testCompanyWrongValueMatrixPOST() + { + $settings = CompanySettings::defaults(); + $settings->reset_counter_date = 1; + + $this->company->saveSettings($settings, $this->company); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-Token' => $this->token, + ])->post('/api/v1/companies?include=company',$this->company->toArray()); + + $arr = $response->json(); + $response->assertStatus(200); + + $this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'],''); + } + + + public function testCompanyWrong2ValueMatrixPOST() + { + $settings = CompanySettings::defaults(); + $settings->reset_counter_date = '1'; + + $this->company->saveSettings($settings, $this->company); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-Token' => $this->token, + ])->post('/api/v1/companies?include=company',$this->company->toArray()); + + $arr = $response->json(); + $response->assertStatus(200); + + $this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'],'1'); + } + + public function testCompanyrightValueMatrixPOST() + { + $settings = CompanySettings::defaults(); + $settings->reset_counter_date = '1/1/2000'; + + $this->company->saveSettings($settings, $this->company); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-Token' => $this->token, + ])->post('/api/v1/companies?include=company',$this->company->toArray()); + + $arr = $response->json(); + $response->assertStatus(200); + + $this->assertEquals($arr['data'][0]['company']['settings']['reset_counter_date'],'1/1/2000'); + } } \ No newline at end of file diff --git a/tests/Feature/QuoteTest.php b/tests/Feature/QuoteTest.php index fc7312b3671c..670021b1cbca 100644 --- a/tests/Feature/QuoteTest.php +++ b/tests/Feature/QuoteTest.php @@ -46,8 +46,8 @@ class QuoteTest extends TestCase $data = [ 'first_name' => $this->faker->firstName, 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, + 'name' => $this->faker->company, + 'email' => $this->faker->unique()->safeEmail, 'password' => 'ALongAndBrilliantPassword123', '_token' => csrf_token(), 'privacy_policy' => 1, @@ -160,7 +160,6 @@ class QuoteTest extends TestCase factory(\App\Models\Quote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); $quote = Quote::where('user_id',$user->id)->first(); - $quote->settings = $client->getMergedSettings(); $quote->save(); $response = $this->withHeaders([ @@ -183,9 +182,7 @@ class QuoteTest extends TestCase ]; $this->assertNotNull($quote); - $this->assertNotNull($quote->settings); - $this->assertTrue(property_exists($quote->settings, 'military_time')); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index d12140fe9e45..6b5c91d10d5e 100644 --- a/tests/Feature/RecurringInvoiceTest.php +++ b/tests/Feature/RecurringInvoiceTest.php @@ -159,7 +159,6 @@ class RecurringInvoiceTest extends TestCase factory(\App\Models\RecurringInvoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); $RecurringInvoice = RecurringInvoice::where('user_id',$user->id)->first(); - $RecurringInvoice->settings = $client->getMergedSettings(); $RecurringInvoice->save(); @@ -183,9 +182,6 @@ class RecurringInvoiceTest extends TestCase ]; $this->assertNotNull($RecurringInvoice); - $this->assertNotNull($RecurringInvoice->settings); - - $this->assertTrue(property_exists($RecurringInvoice->settings, 'military_time')); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), diff --git a/tests/Feature/RecurringQuoteTest.php b/tests/Feature/RecurringQuoteTest.php index a0a1d7a52079..ebb07490129f 100644 --- a/tests/Feature/RecurringQuoteTest.php +++ b/tests/Feature/RecurringQuoteTest.php @@ -160,7 +160,6 @@ class RecurringQuoteTest extends TestCase factory(\App\Models\RecurringQuote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); $RecurringQuote = RecurringQuote::where('user_id',$user->id)->first(); - $RecurringQuote->settings = $client->getMergedSettings(); $RecurringQuote->save(); @@ -184,9 +183,7 @@ class RecurringQuoteTest extends TestCase ]; $this->assertNotNull($RecurringQuote); - $this->assertNotNull($RecurringQuote->settings); - $this->assertTrue(property_exists($RecurringQuote->settings, 'military_time')); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'),