1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Import Company Test

This commit is contained in:
David Bomba 2021-05-27 20:03:26 +10:00
parent 3c528ca16e
commit 442e416840
3 changed files with 173 additions and 45 deletions

View File

@ -215,7 +215,7 @@ class CompanyExport implements ShouldQueue
})->all();
$this->export_data['expense_categories'] = $this->company->expenses->map(function ($expense_category){
$this->export_data['expense_categories'] = $this->company->expense_categories->map(function ($expense_category){
$expense_category = $this->transformArrayOfKeys($expense_category, ['user_id', 'company_id']);

View File

@ -14,11 +14,15 @@ use App\Jobs\Import\CSVImport;
use App\Models\Account;
use App\Models\Client;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\CompanyUser;
use App\Models\Expense;
use App\Models\ExpenseCategory;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Models\Product;
use App\Models\TaxRate;
use App\Models\User;
use App\Models\Vendor;
use App\Utils\Traits\MakesHash;
@ -40,6 +44,8 @@ class ImportCompanyTest extends TestCase
public $account;
public $company;
public $backup_json_object;
public $ids;
public function setUp() :void
{
@ -57,6 +63,20 @@ class ImportCompanyTest extends TestCase
$this->account = Account::factory()->create();
$this->company = Company::factory()->create(['account_id' => $this->account->id]);
$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";
$this->backup_json_object = json_decode(file_get_contents($backup_json_file));
}
public function testBackupJsonRead()
@ -78,51 +98,18 @@ class ImportCompanyTest extends TestCase
unlink($backup_json_file);
}
private function unpackZip()
public function testAppVersion()
{
$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";
$backup_json_object = json_decode(file_get_contents($backup_json_file));
return $backup_json_object;
}
private function testAppVersion()
{
$obj = $this->unpackZip();
$this->assertEquals("5.1.52", $obj->app_version);
$this->assertEquals("5.1.65", $this->backup_json_object->app_version);
}
public function testImportUsers()
{
$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();
}
$this->assertTrue(property_exists($this->backup_json_object, 'app_version'));
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
$backup_json_object = json_decode(file_get_contents($backup_json_file));
$this->assertTrue(property_exists($backup_json_object, 'app_version'));
$this->assertTrue(property_exists($backup_json_object, 'users'));
unlink($backup_json_file);
/***************************** Users *****************************/
$this->assertTrue(property_exists($this->backup_json_object, 'users'));
User::all()->each(function ($user){
$user->forceDelete();
@ -130,9 +117,9 @@ class ImportCompanyTest extends TestCase
User::unguard();
$this->assertEquals(2, count($backup_json_object->users));
$this->assertEquals(2, count($this->backup_json_object->users));
foreach ($backup_json_object->users as $user)
foreach ($this->backup_json_object->users as $user)
{
$user_array = (array)$user;
unset($user_array['laravel_through_key']);
@ -152,12 +139,16 @@ class ImportCompanyTest extends TestCase
User::reguard();
$this->assertEquals(2, User::count());
/***************************** Users *****************************/
$this->assertEquals(2, count($backup_json_object->company_users));
/***************************** Company Users *****************************/
$this->assertEquals(2, count($this->backup_json_object->company_users));
CompanyUser::unguard();
foreach($backup_json_object->company_users as $cu)
foreach($this->backup_json_object->company_users as $cu)
{
$user_id = $this->transformId('users', $cu->user_id);
@ -170,7 +161,7 @@ class ImportCompanyTest extends TestCase
$new_cu = CompanyUser::firstOrNew(
['user_id' => $user_id, 'company_id' => $this->company->id],
(array)$cu_array,
$cu_array,
);
$new_cu->account_id = $this->account->id;
@ -181,11 +172,140 @@ class ImportCompanyTest extends TestCase
CompanyUser::reguard();
$this->assertEquals(2, CompanyUser::count());
/***************************** Company Users *****************************/
/***************************** 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 *****************************/
}
private function transformId(string $resource, string $old): int
{
if (! array_key_exists($resource, $this->ids)) {
@ -199,4 +319,12 @@ class ImportCompanyTest extends TestCase
return $this->ids[$resource]["{$old}"];
}
public function tearDown() :void
{
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
// unlink($backup_json_file);
}
}

Binary file not shown.