2019-06-20 00:18:34 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-06-20 00:18:34 +02:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
use App\Http\Middleware\PasswordProtection;
|
2019-06-20 00:18:34 +02:00
|
|
|
use App\Models\Company;
|
2019-06-25 07:08:07 +02:00
|
|
|
use App\Models\CompanyToken;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Models\TaxRate;
|
2019-06-20 00:18:34 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-06-26 05:25:14 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2019-06-20 00:18:34 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
2019-06-20 00:18:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Http\Controllers\CompanyController
|
|
|
|
*/
|
|
|
|
class CompanyTest extends TestCase
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-05-14 11:08:49 +02:00
|
|
|
use MockAccountData;
|
2019-06-20 00:18:34 +02:00
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2023-09-11 03:22:10 +02:00
|
|
|
public $faker;
|
|
|
|
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2019-06-20 00:18:34 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
2020-05-14 11:08:49 +02:00
|
|
|
|
|
|
|
$this->makeTestData();
|
2019-06-20 00:18:34 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 03:06:03 +01:00
|
|
|
public function testEnsureStrReplace()
|
|
|
|
{
|
|
|
|
$x = '**********';
|
|
|
|
|
|
|
|
$new_string = str_replace("*", "", $x);
|
|
|
|
|
|
|
|
$this->assertEquals(0, strlen($new_string));
|
|
|
|
}
|
|
|
|
|
2023-11-24 22:55:02 +01:00
|
|
|
public function testCompanyTaxInit()
|
|
|
|
{
|
|
|
|
TaxRate::query()->delete();
|
|
|
|
|
|
|
|
$settings = $this->company->settings;
|
|
|
|
$settings->country_id = '40';
|
|
|
|
$this->company->saveSettings($settings, $this->company);
|
|
|
|
|
|
|
|
$this->company->service()->localizeCompany($this->user);
|
|
|
|
|
|
|
|
$this->assertEquals(1, TaxRate::count());
|
|
|
|
}
|
2023-09-11 03:22:10 +02:00
|
|
|
|
2024-06-06 13:12:15 +02:00
|
|
|
public function testCompanyCurrent()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->postJson("/api/v1/companies/current");
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$arr = $response->json();
|
|
|
|
|
|
|
|
$this->assertEquals($arr['data']['id'], $this->company->hashed_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-11 03:22:10 +02:00
|
|
|
public function testCompanyLogoInline()
|
|
|
|
{
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2023-09-14 12:27:21 +02:00
|
|
|
])->get("/api/v1/companies/{$this->company->hashed_id}/logo");
|
2023-09-11 03:22:10 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->streamedContent();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:46:34 +01:00
|
|
|
public function testUpdateCompanyPropertyInvoiceTaskHours()
|
|
|
|
{
|
|
|
|
$company_update = [
|
|
|
|
'invoice_task_hours' => true
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2023-09-11 03:22:10 +02:00
|
|
|
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $company_update);
|
2023-01-17 21:46:34 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
$response->assertStatus(200);
|
2023-01-17 21:46:34 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$arr = $response->json();
|
2023-01-17 21:46:34 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->assertTrue($arr['data']['invoice_task_hours']);
|
2023-01-17 21:46:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
$company_update = [
|
|
|
|
'invoice_task_hours' => false
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $company_update)
|
|
|
|
->assertStatus(200);
|
|
|
|
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$arr = $response->json();
|
2023-01-17 21:46:34 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->assertFalse($arr['data']['invoice_task_hours']);
|
2023-01-17 21:46:34 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 00:18:34 +02:00
|
|
|
public function testCompanyList()
|
|
|
|
{
|
2021-08-31 10:21:06 +02:00
|
|
|
$this->withoutMiddleware(PasswordProtection::class);
|
|
|
|
|
2022-06-10 10:00:07 +02:00
|
|
|
// $cc = Company::first();
|
|
|
|
// $cc->delete();
|
2022-02-01 10:19:04 +01:00
|
|
|
|
2019-06-20 00:18:34 +02:00
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
|
|
|
])->get('/api/v1/companies');
|
2019-06-20 00:18:34 +02:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-14 11:08:49 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post(
|
|
|
|
'/api/v1/companies?include=company',
|
2019-06-20 00:18:34 +02:00
|
|
|
[
|
2019-06-26 05:25:14 +02:00
|
|
|
'name' => 'A New Company',
|
2020-09-06 11:38:10 +02:00
|
|
|
'logo' => UploadedFile::fake()->image('avatar.jpg'),
|
2019-06-20 00:18:34 +02:00
|
|
|
]
|
|
|
|
)
|
2019-06-25 07:08:07 +02:00
|
|
|
->assertStatus(200)->decodeResponseJson();
|
2019-10-23 03:01:25 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$company = Company::find($this->decodePrimaryKey($response['data'][0]['company']['id']));
|
2019-06-26 05:25:14 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-14 11:08:49 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2020-03-21 06:37:30 +01:00
|
|
|
])->post(
|
|
|
|
'/api/v1/companies/',
|
2019-06-26 05:25:14 +02:00
|
|
|
[
|
|
|
|
'name' => 'A New Company',
|
2020-09-06 11:38:10 +02:00
|
|
|
'company_logo' => UploadedFile::fake()->create('avatar.pdf', 100),
|
2019-06-26 05:25:14 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->assertStatus(302);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
// Log::error($company);
|
2019-06-26 05:25:14 +02:00
|
|
|
|
2020-05-14 11:08:49 +02:00
|
|
|
$this->token = CompanyToken::whereCompanyId($company->id)->first()->token;
|
2019-06-20 00:18:34 +02:00
|
|
|
|
2019-06-20 08:20:14 +02:00
|
|
|
$company_update = [
|
2019-06-26 05:25:14 +02:00
|
|
|
'name' => 'CHANGE NAME',
|
2022-06-21 11:57:17 +02:00
|
|
|
// 'logo' => UploadedFile::fake()->image('avatar.jpg')
|
2019-06-20 00:18:34 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
2022-06-21 11:57:17 +02:00
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-06-24 03:55:41 +02:00
|
|
|
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($company->id), $company_update)
|
2019-06-20 00:18:34 +02:00
|
|
|
->assertStatus(200);
|
|
|
|
|
2019-11-12 12:36:24 +01:00
|
|
|
$settings = CompanySettings::defaults();
|
2019-10-23 03:01:25 +02:00
|
|
|
$settings->custom_value1 = 'test';
|
2019-10-23 08:21:16 +02:00
|
|
|
$settings->invoice_design_id = '2';
|
2020-03-06 12:10:59 +01:00
|
|
|
$settings->quote_design_id = '1';
|
2019-10-23 03:01:25 +02:00
|
|
|
|
|
|
|
$company->settings = $settings;
|
|
|
|
|
2023-08-06 09:35:19 +02:00
|
|
|
// nlog($company->toArray());
|
2023-05-15 13:40:57 +02:00
|
|
|
|
2019-10-23 03:01:25 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-14 11:08:49 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2022-06-24 03:55:41 +02:00
|
|
|
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($company->id), $company->toArray())
|
2019-10-23 03:01:25 +02:00
|
|
|
->assertStatus(200)->decodeResponseJson();
|
2019-10-23 08:21:16 +02:00
|
|
|
|
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-14 11:08:49 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2019-10-23 08:21:16 +02:00
|
|
|
])->get('/api/v1/companies/'.$this->encodePrimaryKey($company->id))
|
|
|
|
->assertStatus(200)->decodeResponseJson();
|
|
|
|
|
2019-06-20 00:18:34 +02:00
|
|
|
$response = $this->withHeaders([
|
|
|
|
'X-API-SECRET' => config('ninja.api_secret'),
|
2020-05-14 11:08:49 +02:00
|
|
|
'X-API-TOKEN' => $this->token,
|
2021-08-31 07:50:34 +02:00
|
|
|
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
|
2019-06-20 08:20:14 +02:00
|
|
|
])->delete('/api/v1/companies/'.$this->encodePrimaryKey($company->id))
|
2019-06-20 00:18:34 +02:00
|
|
|
->assertStatus(200);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-06-20 00:18:34 +02:00
|
|
|
}
|