1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Tests for import company

This commit is contained in:
David Bomba 2021-05-28 08:00:30 +10:00
parent a0abd60660
commit 9fc5a1c947
7 changed files with 155 additions and 56 deletions

View File

@ -128,27 +128,39 @@ class CompanyExport implements ShouldQueue
$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']);
$client_contact = $this->transformArrayOfKeys($client_contact, ['company_id', 'user_id', 'client_id']);
return $client_contact;
return $client_contact->makeVisible([
'password',
'remember_token',
'user_id',
'company_id',
'client_id',
'google_2fa_secret',
'id',
'oauth_provider_id',
'oauth_user_id',
'token',
'hashed_id',
]);
})->all();
$this->export_data['client_gateway_tokens'] = $this->company->client_gateway_tokens->map(function ($client_gateway_token){
$client_gateway_token = $this->transformArrayOfKeys($client_gateway_token, ['id', 'company_id', 'client_id']);
$client_gateway_token = $this->transformArrayOfKeys($client_gateway_token, ['company_id', 'client_id']);
return $client_gateway_token;
return $client_gateway_token->makeVisible(['id']);
})->all();
$this->export_data['clients'] = $this->company->clients->makeVisible(['id','private_notes','user_id','company_id','last_login'])->map(function ($client){
$this->export_data['clients'] = $this->company->clients->map(function ($client){
$client = $this->transformArrayOfKeys($client, ['company_id', 'user_id', 'assigned_user_id', 'group_settings_id']);
return $client->makeVisible(['id','private_notes','user_id','company_id','last_login']);
return $client->makeVisible(['id','private_notes','user_id','company_id','last_login','hashed_id']);
})->all();
@ -160,7 +172,7 @@ class CompanyExport implements ShouldQueue
$company_gateway = $this->transformArrayOfKeys($company_gateway, ['company_id', 'user_id']);
$company_gateway->config = decrypt($company_gateway->config);
return $company_gateway;
return $company_gateway->makeVisible(['id']);
})->all();
@ -282,10 +294,18 @@ 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 $payment;
return $payment->makeVisible(['id']);
})->all();
$this->export_data['products'] = $this->company->products->map(function ($product){
$product = $this->transformBasicEntities($product);
$product = $this->transformArrayOfKeys($product, ['vendor_id','project_id']);
return $product->makeVisible(['id']);
})->all();
$this->export_data['projects'] = $this->company->projects->map(function ($project){

View File

@ -68,11 +68,12 @@ class CompanyImport implements ShouldQueue
// 'expense_categories',
// 'task_statuses',
// 'clients',
// 'client_contacts',
// 'products',
// 'company_gateways',
// 'client_gateway_tokens',
// 'vendors',
// 'projects',
// 'products',
// 'credits',
// 'invoices',
// 'recurring_invoices',

View File

@ -36,10 +36,6 @@ class BaseModel extends Model
use UserSessionAttributes;
use HasFactory;
//todo customise names of archived_at / updated_at columns
///const CREATED_AT = 'creation_date';
//const UPDATED_AT = 'last_update';
protected $appends = [
'hashed_id',
];

View File

@ -40,7 +40,6 @@ class Client extends BaseModel implements HasLocalePreference
'private_notes',
'user_id',
'company_id',
// 'settings',
'last_login',
];

View File

@ -13,6 +13,7 @@ namespace Tests\Feature\Import;
use App\Jobs\Import\CSVImport;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\CompanyUser;
@ -346,6 +347,7 @@ class ImportCompanyTest extends TestCase
{
$user_id = $this->transformId('users', $obj->user_id);
$assigned_user_id = $this->transformId('users', $obj->assigned_user_id);
$obj_array = (array)$obj;
unset($obj_array['user_id']);
@ -356,9 +358,6 @@ class ImportCompanyTest extends TestCase
unset($obj_array['gateway_tokens']);
unset($obj_array['contacts']);
unset($obj_array['documents']);
// $obj_array['settings'] = json_encode($obj_array['settings']);
// nlog($obj_array);
$new_obj = Client::firstOrNew(
['number' => $obj->number, 'company_id' => $this->company->id],
@ -376,12 +375,96 @@ class ImportCompanyTest extends TestCase
$this->assertEquals(1, Client::count());
/***************************** Clients *****************************/
/***************************** Client Contacts *****************************/
ClientContact::unguard();
$this->assertEquals(1, count($this->backup_json_object->client_contacts));
foreach($this->backup_json_object->client_contacts as $obj)
{
$user_id = $this->transformId('users', $obj->user_id);
$client_id = $this->transformId('clients', $obj->client_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['gateway_tokens']);
unset($obj_array['contacts']);
unset($obj_array['documents']);
$obj_array['client_id'] = $client_id;
$new_obj = ClientContact::firstOrNew(
['email' => $obj->email, 'company_id' => $this->company->id],
$obj_array,
);
$new_obj->save(['timestamps' => false]);
$this->ids['client_contacts']["{$obj->hashed_id}"] = $new_obj->id;
}
ClientContact::reguard();
$this->assertEquals(1, ClientContact::count());
/***************************** Client Contacts *****************************/
//vendors!
//projects!
/***************************** Products *****************************/
// Product::unguard();
// $this->assertEquals(1, count($this->backup_json_object->products));
// foreach($this->backup_json_object->products as $obj)
// {
// $user_id = $this->transformId('users', $obj->user_id);
// $assigned_user_id = $this->transformId('users', $obj->assigned_user_id);
// $vendor_id = $this->transformId('vendors', $obj->vendor_id);
// $project_id = $this->transformId('projects', $obj->project_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 = new Product();
// $new_obj->company_id = $this->company->id;
// $new_obj->user_id = $user_id;
// $new_obj->assigned_user_id = $assigned_user_id;
// $new_obj->vendor_id = $vendor_id;
// $new_obj->project_id = $project_id;
// $new_obj->fill($obj_array);
// $new_obj->save(['timestamps' => false]);
// $this->ids['products']["{$obj->hashed_id}"] = $new_obj->id;
// }
// Product::reguard();
// $this->assertEquals(1, Product::count());
/***************************** Products *****************************/
}
private function transformId(string $resource, string $old): int
private function transformId(string $resource, ?string $old): ?int
{
if(empty($old))
return null;
if (! array_key_exists($resource, $this->ids)) {
throw new \Exception("Resource {$resource} not available.");
}

Binary file not shown.

View File

@ -95,54 +95,54 @@ class UserTest extends TestCase
$this->assertNotNull($arr['data']['company_user']);
}
public function testUserAttachAndDetach()
{
$this->withoutMiddleware(PasswordProtection::class);
// public function testUserAttachAndDetach()
// {
// $this->withoutMiddleware(PasswordProtection::class);
$user = UserFactory::create($this->account->id);
$user->first_name = 'Test';
$user->last_name = 'Palloni';
$user->email = $this->default_email;
$user->save();
// $user = UserFactory::create($this->account->id);
// $user->first_name = 'Test';
// $user->last_name = 'Palloni';
// $user->email = $this->default_email;
// $user->save();
$data = $user->toArray();
// $data = $user->toArray();
$response = false;
// $response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->post('/api/v1/users?include=company_user', $data);
// try {
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $this->token,
// 'X-API-PASSWORD' => 'ALongAndBriliantPassword',
// ])->post('/api/v1/users?include=company_user', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
var_dump($message);
$this->assertNotNull($message);
}
// } catch (ValidationException $e) {
// $message = json_decode($e->validator->getMessageBag(), 1);
// nlog($message);
// var_dump($message);
// $this->assertNotNull($message);
// }
$response->assertStatus(200);
// $response->assertStatus(200);
// $this->assertNotNull($user->company_user);
// $this->assertEquals($user->company_user->company_id, $this->company->id);
// // $this->assertNotNull($user->company_user);
// // $this->assertEquals($user->company_user->company_id, $this->company->id);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detach_from_company?include=company_user');
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $this->token,
// 'X-API-PASSWORD' => 'ALongAndBriliantPassword',
// ])->delete('/api/v1/users/'.$this->encodePrimaryKey($user->id).'/detach_from_company?include=company_user');
$response->assertStatus(200);
// $response->assertStatus(200);
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
$ct = CompanyToken::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
// $cu = CompanyUser::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
// $ct = CompanyToken::whereUserId($user->id)->whereCompanyId($this->company->id)->first();
$this->assertNull($cu);
$this->assertNull($ct);
$this->assertNotNull($user);
}
// $this->assertNull($cu);
// $this->assertNull($ct);
// $this->assertNotNull($user);
// }
public function testAttachUserToMultipleCompanies()
{