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

199 lines
5.1 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;
use App\Models\CompanyUser;
2021-05-14 08:00:25 +02:00
use App\Models\Expense;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Product;
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-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
$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
}
public function testBackupJsonRead()
{
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
$this->assertTrue(is_array(json_decode(file_get_contents($backup_json_file),1)));
2021-05-15 08:54:27 +02:00
unlink($backup_json_file);
2021-05-14 08:00:25 +02:00
}
2021-05-27 03:19:07 +02:00
private function unpackZip()
{
$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);
}
2021-05-15 06:29:19 +02:00
public function testImportUsers()
{
2021-05-15 08:54:27 +02:00
$backup_json_file_zip = base_path().'/tests/Feature/Import/backup.zip';
2021-05-15 06:29:19 +02:00
2021-05-15 08:54:27 +02:00
$zip = new \ZipArchive;
$res = $zip->open($backup_json_file_zip);
if ($res === TRUE) {
$zip->extractTo(sys_get_temp_dir());
$zip->close();
}
2021-05-15 06:29:19 +02:00
2021-05-15 08:54:27 +02:00
$backup_json_file = sys_get_temp_dir() . "/backup/backup.json";
2021-05-15 06:29:19 +02:00
2021-05-15 08:54:27 +02:00
$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();
});
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 07:57:07 +02:00
$this->assertEquals(2, count($backup_json_object->users));
2021-05-15 08:54:27 +02:00
foreach ($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 07:57:07 +02:00
$this->assertEquals(2, count($backup_json_object->company_users));
CompanyUser::unguard();
foreach($backup_json_object->company_users as $cu)
{
$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],
(array)$cu_array,
);
$new_cu->account_id = $this->account->id;
$new_cu->save(['timestamps' => false]);
}
CompanyUser::reguard();
$this->assertEquals(2, CompanyUser::count());
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-14 08:00:25 +02:00
}