1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/tests/Feature/Import/ImportCompanyTest.php

331 lines
9.6 KiB
PHP
Raw Normal View History

2021-05-14 08:00:25 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\Import;
use App\Jobs\Import\CSVImport;
2021-05-15 08:54:27 +02:00
use App\Models\Account;
2021-05-14 08:00:25 +02:00
use App\Models\Client;
2021-05-27 07:57:07 +02:00
use App\Models\Company;
2021-05-27 12:03:26 +02:00
use App\Models\CompanyToken;
2021-05-27 07:57:07 +02:00
use App\Models\CompanyUser;
2021-05-14 08:00:25 +02:00
use App\Models\Expense;
2021-05-27 12:03:26 +02:00
use App\Models\ExpenseCategory;
2021-05-14 08:00:25 +02:00
use App\Models\Invoice;
use App\Models\Payment;
2021-05-27 12:03:26 +02:00
use App\Models\PaymentTerm;
2021-05-14 08:00:25 +02:00
use App\Models\Product;
2021-05-27 12:03:26 +02:00
use App\Models\TaxRate;
2021-05-15 06:50:07 +02:00
use App\Models\User;
2021-05-14 08:00:25 +02:00
use App\Models\Vendor;
use App\Utils\Traits\MakesHash;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use League\Csv\Reader;
use League\Csv\Statement;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*
*/
class ImportCompanyTest extends TestCase
{
use MakesHash;
2021-05-15 08:54:27 +02:00
public $account;
2021-05-27 07:57:07 +02:00
public $company;
2021-05-27 12:03:26 +02:00
public $backup_json_object;
public $ids;
2021-05-15 08:54:27 +02:00
2021-05-14 08:00:25 +02:00
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
$this->withoutExceptionHandling();
2021-05-15 08:54:27 +02:00
2021-05-27 09:21:30 +02:00
Account::all()->each(function ($account){
$account->delete();
});
2021-05-15 08:54:27 +02:00
$this->account = Account::factory()->create();
2021-05-27 07:57:07 +02:00
$this->company = Company::factory()->create(['account_id' => $this->account->id]);
2021-05-14 08:00:25 +02:00
2021-05-15 08:54:27 +02:00
$backup_json_file_zip = base_path().'/tests/Feature/Import/backup.zip';
$zip = new \ZipArchive;
$res = $zip->open($backup_json_file_zip);
if ($res === TRUE) {
$zip->extractTo(sys_get_temp_dir());
$zip->close();
}
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
2021-05-14 08:00:25 +02:00
2021-05-27 12:03:26 +02:00
$this->backup_json_object = json_decode(file_get_contents($backup_json_file));
2021-05-15 08:54:27 +02:00
2021-05-14 08:00:25 +02:00
}
2021-05-27 12:03:26 +02:00
public function testBackupJsonRead()
2021-05-27 03:19:07 +02:00
{
2021-05-27 12:03:26 +02:00
$backup_json_file_zip = base_path().'/tests/Feature/Import/backup.zip';
2021-05-27 03:19:07 +02:00
$zip = new \ZipArchive;
$res = $zip->open($backup_json_file_zip);
2021-05-27 12:03:26 +02:00
2021-05-27 03:19:07 +02:00
if ($res === TRUE) {
$zip->extractTo(sys_get_temp_dir());
$zip->close();
}
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
2021-05-27 12:03:26 +02:00
$this->assertTrue(is_array(json_decode(file_get_contents($backup_json_file),1)));
2021-05-27 03:19:07 +02:00
2021-05-27 12:03:26 +02:00
unlink($backup_json_file);
2021-05-27 03:19:07 +02:00
}
2021-05-27 12:03:26 +02:00
public function testAppVersion()
2021-05-27 03:19:07 +02:00
{
2021-05-27 12:03:26 +02:00
$this->assertEquals("5.1.65", $this->backup_json_object->app_version);
2021-05-27 03:19:07 +02:00
}
2021-05-15 06:29:19 +02:00
public function testImportUsers()
{
2021-05-27 12:03:26 +02:00
$this->assertTrue(property_exists($this->backup_json_object, 'app_version'));
2021-05-15 06:29:19 +02:00
2021-05-27 12:03:26 +02:00
/***************************** Users *****************************/
$this->assertTrue(property_exists($this->backup_json_object, 'users'));
2021-05-15 08:54:27 +02:00
User::all()->each(function ($user){
$user->forceDelete();
});
2021-05-15 06:29:19 +02:00
2021-05-15 06:50:07 +02:00
User::unguard();
2021-05-15 06:29:19 +02:00
2021-05-27 12:03:26 +02:00
$this->assertEquals(2, count($this->backup_json_object->users));
2021-05-27 07:57:07 +02:00
2021-05-27 12:03:26 +02:00
foreach ($this->backup_json_object->users as $user)
2021-05-15 06:50:07 +02:00
{
2021-05-15 08:54:27 +02:00
$user_array = (array)$user;
unset($user_array['laravel_through_key']);
unset($user_array['hashed_id']);
2021-05-15 06:29:19 +02:00
2021-05-15 06:50:07 +02:00
$new_user = User::firstOrNew(
['email' => $user->email],
2021-05-15 08:54:27 +02:00
array_merge($user_array, ['account_id' => $this->account->id]),
2021-05-15 06:50:07 +02:00
);
2021-05-15 06:29:19 +02:00
2021-05-15 06:50:07 +02:00
$new_user->save(['timestamps' => false]);
2021-05-15 06:29:19 +02:00
2021-05-15 08:54:27 +02:00
$this->ids['users']["{$user->hashed_id}"] = $new_user->id;
2021-05-15 06:50:07 +02:00
}
User::reguard();
2021-05-15 08:54:27 +02:00
$this->assertEquals(2, User::count());
2021-05-27 12:03:26 +02:00
/***************************** Users *****************************/
2021-05-27 07:57:07 +02:00
2021-05-27 12:03:26 +02:00
/***************************** Company Users *****************************/
$this->assertEquals(2, count($this->backup_json_object->company_users));
2021-05-27 07:57:07 +02:00
CompanyUser::unguard();
2021-05-27 12:03:26 +02:00
foreach($this->backup_json_object->company_users as $cu)
2021-05-27 07:57:07 +02:00
{
$user_id = $this->transformId('users', $cu->user_id);
$cu_array = (array)$cu;
unset($cu_array['user_id']);
unset($cu_array['company_id']);
unset($cu_array['account_id']);
unset($cu_array['hashed_id']);
unset($cu_array['id']);
$new_cu = CompanyUser::firstOrNew(
['user_id' => $user_id, 'company_id' => $this->company->id],
2021-05-27 12:03:26 +02:00
$cu_array,
2021-05-27 07:57:07 +02:00
);
$new_cu->account_id = $this->account->id;
$new_cu->save(['timestamps' => false]);
}
CompanyUser::reguard();
$this->assertEquals(2, CompanyUser::count());
2021-05-27 12:03:26 +02:00
/***************************** Company Users *****************************/
2021-05-27 07:57:07 +02:00
2021-05-15 06:29:19 +02:00
2021-05-27 12:03:26 +02:00
/***************************** Company Tokens *****************************/
$this->assertEquals(2, count($this->backup_json_object->company_tokens));
CompanyToken::unguard();
foreach($this->backup_json_object->company_tokens as $ct)
{
$user_id = $this->transformId('users', $ct->user_id);
$ct_array = (array)$ct;
unset($ct_array['user_id']);
unset($ct_array['company_id']);
unset($ct_array['account_id']);
unset($ct_array['hashed_id']);
unset($ct_array['id']);
$new_ct = CompanyToken::firstOrNew(
['user_id' => $user_id, 'company_id' => $this->company->id],
$ct_array,
);
$new_ct->account_id = $this->account->id;
$new_ct->save(['timestamps' => false]);
}
CompanyToken::reguard();
$this->assertEquals(2, CompanyToken::count());
/***************************** Company Tokens *****************************/
/***************************** Payment Terms *****************************/
PaymentTerm::unguard();
$this->assertEquals(8, count($this->backup_json_object->payment_terms));
foreach($this->backup_json_object->payment_terms as $obj)
{
$user_id = $this->transformId('users', $obj->user_id);
$obj_array = (array)$obj;
unset($obj_array['user_id']);
unset($obj_array['company_id']);
unset($obj_array['account_id']);
unset($obj_array['hashed_id']);
unset($obj_array['id']);
$new_obj = PaymentTerm::firstOrNew(
['num_days' => $obj->num_days, 'company_id' => $this->company->id],
$obj_array,
);
$new_obj->save(['timestamps' => false]);
}
PaymentTerm::reguard();
$this->assertEquals(8, PaymentTerm::count());
/***************************** Payment Terms *****************************/
/***************************** Tax Rates *****************************/
TaxRate::unguard();
$this->assertEquals(2, count($this->backup_json_object->tax_rates));
foreach($this->backup_json_object->tax_rates as $obj)
{
$user_id = $this->transformId('users', $obj->user_id);
$obj_array = (array)$obj;
unset($obj_array['user_id']);
unset($obj_array['company_id']);
unset($obj_array['account_id']);
unset($obj_array['hashed_id']);
unset($obj_array['id']);
unset($obj_array['tax_rate_id']);
$new_obj = TaxRate::firstOrNew(
['name' => $obj->name, 'company_id' => $this->company->id, 'rate' => $obj->rate],
$obj_array,
);
$new_obj->save(['timestamps' => false]);
}
TaxRate::reguard();
$this->assertEquals(2, TaxRate::count());
/***************************** Tax Rates *****************************/
/***************************** Expense Category *****************************/
ExpenseCategory::unguard();
$this->assertEquals(2, count($this->backup_json_object->tax_rates));
foreach($this->backup_json_object->expense_categories as $obj)
{
$user_id = $this->transformId('users', $obj->user_id);
$obj_array = (array)$obj;
unset($obj_array['user_id']);
unset($obj_array['company_id']);
unset($obj_array['account_id']);
unset($obj_array['hashed_id']);
unset($obj_array['id']);
unset($obj_array['tax_rate_id']);
$new_obj = ExpenseCategory::firstOrNew(
['name' => $obj->name, 'company_id' => $this->company->id],
$obj_array,
);
$new_obj->save(['timestamps' => false]);
}
ExpenseCategory::reguard();
$this->assertEquals(2, ExpenseCategory::count());
/***************************** Expense Category *****************************/
}
2021-05-15 06:29:19 +02:00
2021-05-27 07:57:07 +02:00
private function transformId(string $resource, string $old): int
{
if (! array_key_exists($resource, $this->ids)) {
throw new \Exception("Resource {$resource} not available.");
}
if (! array_key_exists("{$old}", $this->ids[$resource])) {
throw new \Exception("Missing resource key: {$old}");
}
return $this->ids[$resource]["{$old}"];
}
2021-05-27 12:03:26 +02:00
public function tearDown() :void
{
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
// unlink($backup_json_file);
}
2021-05-14 08:00:25 +02:00
}