Tests for import

This commit is contained in:
David Bomba 2021-05-15 16:54:27 +10:00
parent 48135b6dfe
commit f9fae690a0
6 changed files with 61 additions and 32 deletions

View File

@ -128,7 +128,7 @@ class DemoMode extends Command
{
$faker = \Faker\Factory::create();
$this->count = 50;
$this->count = 25;
$this->info('Creating Small Account and Company');
@ -486,7 +486,7 @@ class DemoMode extends Command
if (rand(0, 1)) {
$invoice->assigned_user_id = $assigned_user_id;
}
$invoice->number = $this->getNextRecurringInvoiceNumber($client);
$invoice->save();
}

View File

@ -163,6 +163,7 @@ class Kernel extends HttpKernel
protected $middlewarePriority = [
SetDomainNameDb::class,
SetDb::class,
SetWebDb::class,
UrlSetDb::class,
@ -170,7 +171,6 @@ class Kernel extends HttpKernel
SetEmailDb::class,
SetInviteDb::class,
SetDbByCompanyKey::class,
SetDomainNameDb::class,
TokenAuth::class,
ContactTokenAuth::class,
ContactKeyLogin::class,

View File

@ -71,6 +71,8 @@ class CompanyExport implements ShouldQueue
MultiDB::setDb($this->company->db);
$this->company = Company::where('company_key', $this->company->company_key)->first();
set_time_limit(0);
$this->export_data['app_version'] = config('ninja.app_version');
@ -114,6 +116,16 @@ class CompanyExport implements ShouldQueue
})->toArray();
$this->export_data['users'] = $this->company->users()->withTrashed()->cursor()->map(function ($user){
$user->account_id = $this->encodePrimaryKey($user->account_id);
$user->id = $this->encodePrimaryKey($user->id);
return $user;
})->toArray();
$this->export_data['client_contacts'] = $this->company->client_contacts->map(function ($client_contact){
$client_contact = $this->transformArrayOfKeys($client_contact, ['id', 'company_id', 'user_id',' client_id']);
@ -140,11 +152,7 @@ class CompanyExport implements ShouldQueue
})->toArray();
$temp_co = $this->company;
$temp_co->id = $this->encodePrimaryKey($temp_co->id);
$temp_co->account_id = $this->encodePrimaryKey($temp_co->account_id);
$this->export_data['company'] = $temp_co->toArray();
$this->export_data['company'] = $this->company->toArray();
$this->export_data['company_gateways'] = $this->company->company_gateways->map(function ($company_gateway){
@ -273,7 +281,7 @@ class CompanyExport implements ShouldQueue
$payment = $this->transformBasicEntities($payment);
$payment = $this->transformArrayOfKeys($payment, ['client_id','project_id', 'vendor_id', 'client_contact_id', 'invitation_id', 'company_gateway_id']);
return $project;
return $payment;
})->toArray();
@ -370,15 +378,6 @@ class CompanyExport implements ShouldQueue
})->makeHidden(['id'])->toArray();
$this->export_data['users'] = $this->company->users()->withTrashed()->cursor()->map(function ($user){
$user->account_id = $this->encodePrimaryKey($user->account_id);
$user->id = $this->encodePrimaryKey($user->id);
return $user;
})->makeHidden(['ip'])->toArray();
$this->export_data['vendors'] = $this->company->vendors->map(function ($vendor){
return $this->transformBasicEntities($vendor);

View File

@ -11,6 +11,7 @@
namespace Tests\Feature\Import;
use App\Jobs\Import\CSVImport;
use App\Models\Account;
use App\Models\Client;
use App\Models\Expense;
use App\Models\Invoice;
@ -35,6 +36,8 @@ class ImportCompanyTest extends TestCase
{
use MakesHash;
public $account;
public function setUp() :void
{
parent::setUp();
@ -45,47 +48,75 @@ class ImportCompanyTest extends TestCase
$this->withoutExceptionHandling();
$this->account = Account::factory()->create();
}
public function testBackupJsonRead()
{
$backup_json_file = base_path().'/tests/Feature/Import/backup.json';
$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->assertTrue(is_array(json_decode(file_get_contents($backup_json_file),1)));
unlink($backup_json_file);
}
public function testImportUsers()
{
$backup_json_file_zip = base_path().'/tests/Feature/Import/backup.zip';
$backup_json_file = base_path().'/tests/Feature/Import/backup.json';
$zip = new \ZipArchive;
$res = $zip->open($backup_json_file_zip);
if ($res === TRUE) {
$zip->extractTo(sys_get_temp_dir());
$zip->close();
}
$backup_json_file = json_decode(file_get_contents($backup_json_file));
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
$this->assertTrue(property_exists($backup_json_file, 'app_version'));
$this->assertTrue(property_exists($backup_json_file, 'users'));
$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);
User::all()->each(function ($user){
$user->forceDelete();
});
User::unguard();
foreach ($backup_json_file->users as $user)
foreach ($backup_json_object->users as $user)
{
$user_array = (array)$user;
unset($user_array['laravel_through_key']);
unset($user_array['hashed_id']);
$new_user = User::firstOrNew(
['email' => $user->email],
(array)$user,
array_merge($user_array, ['account_id' => $this->account->id]),
);
$new_user->account_id = $this->account->id;
$new_user->save(['timestamps' => false]);
nlog($new_user->toArray());
$this->ids['users']["{$user->id}"] = $new_user->id;
nlog($this->ids);
$this->ids['users']["{$user->hashed_id}"] = $new_user->id;
}
User::reguard();
$this->assertEquals(2, User::count());
}

File diff suppressed because one or more lines are too long

Binary file not shown.