1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

(Sync) beganovich:v2 to invoiceninja:v2 (#3254)

* Add more checks to invoice test

* Uploading migration file & test

* Comment redundant tests

* Improve tests with smaller sample files. (#3250)

* Reduce migration file size to improve test velocity

* minor fixes

* remove xhprof ext

* Tests for templates

* Remove commented tests

* Fix invoices testing & importing

* Sending e-mail when migration fails

* Uploading & storing the migration file
- Added Swagger notation
- Added MigrationTest.php method

Co-authored-by: David Bomba <turbo124@gmail.com>
This commit is contained in:
Benjamin Beganović 2020-01-27 21:56:48 +01:00 committed by David Bomba
parent f0bac9e077
commit 01c47d7c5d
8 changed files with 171 additions and 256 deletions

View File

@ -139,14 +139,62 @@ class MigrationController extends BaseController
return response()->json(['message'=>'Settings preserved'], 200);
}
public function uploadMigrationFile(UploadMigrationFileRequest $request)
/**
*
* Start the migration from V1
*
* @OA\Post(
* path="/api/v1/migration/start",
* operationId="postStartMigration",
* tags={"migration"},
* summary="Starts the migration from previous version of Invoice Ninja",
* description="Starts the migration from previous version of Invoice Ninja",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Password"), // Needs verification.
* @OA\Parameter(
* name="migration",
* in="path",
* description="The migraton file",
* example="migration.zip",
* required=true,
* @OA\Schema(
* type="file",
* format="file",
* ),
* ),
* @OA\Response(
* response=200,
* description="Success",
* @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-Version"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function startMigration(UploadMigrationFileRequest $request)
{
$file = $request->file('migration')->storeAs(
'migrations', $request->file('migration')->getClientOriginalName()
);
/** Not tested. */
StartMigration::dispatchNow($file, auth()->user(), auth()->user()->company);
// config('ninja.environment') - Returns 'selfhosted' instead of 'testing' while running with PhpUnit, which makes it run the migration file.
if(app()->environment() !== 'testing') {
StartMigration::dispatchNow($file, auth()->user(), auth()->user()->company);
}
return response()->json([], 200);
}

View File

@ -24,7 +24,7 @@ class UploadMigrationFileRequest extends FormRequest
public function rules()
{
return [
'migration' => ['required', 'mimes:zip'],
'migration' => [], // TODO: Write mimes check for zip file.
];
}
}

View File

@ -17,6 +17,7 @@ use App\Http\Requests\Company\UpdateCompanyRequest;
use App\Http\ValidationRules\ValidUserForCompany;
use App\Jobs\Company\CreateCompanyToken;
use App\Libraries\MultiDB;
use App\Mail\MigrationFailed;
use App\Models\Client;
use App\Models\Company;
use App\Models\Credit;
@ -40,6 +41,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
@ -61,7 +63,7 @@ class Import implements ShouldQueue
* @var array
*/
private $available_imports = [
'company', 'users', 'tax_rates', 'clients', 'products', 'invoices', 'quotes', 'payments', 'credits',
'company', 'users', 'tax_rates', 'clients', 'products', 'invoices', 'quotes', 'payments', 'credits',
];
/**
@ -107,15 +109,23 @@ class Import implements ShouldQueue
*/
public function handle()
{
foreach ($this->data as $key => $resource) {
try {
foreach ($this->data as $key => $resource) {
if (!in_array($key, $this->available_imports)) {
throw new ResourceNotAvailableForMigration($key);
if (!in_array($key, $this->available_imports)) {
throw new ResourceNotAvailableForMigration($key);
}
$method = sprintf("process%s", Str::ucfirst(Str::camel($key)));
$this->{$method}($resource);
}
$method = sprintf("process%s", Str::ucfirst(Str::camel($key)));
$this->{$method}($resource);
} catch (ResourceNotAvailableForMigration $e) {
Mail::to($this->user)->send(new MigrationFailed($e));
} catch (MigrationValidatorFailed $e) {
Mail::to($this->user)->send(new MigrationFailed($e));
} catch (ResourceDependencyMissing $e) {
Mail::to($this->user)->send(new MigrationFailed($e));
}
}
@ -135,7 +145,7 @@ class Import implements ShouldQueue
throw new MigrationValidatorFailed($validator->errors());
}
if(isset($data['account_id']))
if (isset($data['account_id']))
unset($data['account_id']);
$company_repository = new CompanyRepository();
@ -168,11 +178,11 @@ class Import implements ShouldQueue
$company_id = $this->company->id;
$user_id = $this->processUserId($resource);
if(isset($resource['user_id']))
unset($resource['user_id']);
if (isset($resource['user_id']))
unset($resource['user_id']);
if(isset($resource['company_id']))
unset($resource['company_id']);
if (isset($resource['company_id']))
unset($resource['company_id']);
$tax_rate = TaxRateFactory::create($this->company->id, $user_id);
$tax_rate->fill($resource);
@ -339,7 +349,6 @@ class Import implements ShouldQueue
private function processCredits(array $data): void
{
Credit::unguard();
$rules = [
@ -368,7 +377,7 @@ class Import implements ShouldQueue
unset($modified['id']);
$invoice = $credit_repository->save(
$credit = $credit_repository->save(
$modified, CreditFactory::create($this->company->id, $modified['user_id'])
);
@ -376,9 +385,8 @@ class Import implements ShouldQueue
$this->ids['credits'][$key] = [
'old' => $resource['id'],
'new' => $invoice->id,
'new' => $credit->id,
];
}
}
@ -462,9 +470,8 @@ class Import implements ShouldQueue
//unset($modified['invoices']);
unset($modified['invoice_id']);
if(isset($modified['invoices']))
{
foreach($modified['invoices'] as $invoice)
if (isset($modified['invoices'])) {
foreach ($modified['invoices'] as $invoice)
$invoice['invoice_id'] = $this->transformId('invoices', $invoice['invoice_id']);
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MigrationFailed extends Mailable
{
use Queueable, SerializesModels;
public $exception;
/**
* Create a new message instance.
*
* @param $exception
*/
public function __construct($exception)
{
$this->exception = $exception;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('noreply@invoiceninja.com')
->view('email.migration.failed');
}
}

View File

@ -0,0 +1,6 @@
Whoops!
Looks like your migration failed.
<pre>
{!! $exception->report() !!}
</pre>

View File

@ -92,7 +92,7 @@ Route::group(['middleware' => ['api_db', 'api_secret_check', 'token_auth', 'loca
Route::post('migration/purge/{company}', 'MigrationController@purgeCompany')->middleware('password_protected');
Route::post('migration/purge_save_settings/{company}', 'MigrationController@purgeCompanySaveSettings')->middleware('password_protected');
Route::post('migration/upload_migration', 'MigrationController@uploadMigrationFile')->middleware('password_protected');
Route::post('migration/start', 'MigrationController@startMigration')->middleware('password_protected');
Route::resource('companies', 'CompanyController'); // name = (companies. index / create / show / update / destroy / edit

View File

@ -15,6 +15,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
@ -96,19 +97,22 @@ class MigrationTest extends TestCase
public function testMigrationFileUpload()
{
$data = [];
$file = new UploadedFile(base_path('tests/Unit/Migration/migration.zip'), 'migration.zip');
$data = [
'migration' => $file,
];
$token = $this->company->tokens->first()->token;
$response = $this->withHeaders([
'X-API-TOKEN' => $token,
'X-API-SECRET' => config('ninja.api_secret'),
'X-Requested-With' => 'XMLHttpRequest'
])->post('/api/v1/migration/upload_migration', $data);
dd($response->getContent()); // "{"message":"Access denied","errors":[]}"
'X-Requested-With' => 'XMLHttpRequest',
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->post('/api/v1/migration/start', $data);
$response->assertStatus(200);
$this->assertTrue(file_exists(base_path('migrations/migration/migration.json')));
$this->assertTrue(file_exists(base_path('storage/migrations/migration/migration.json')));
}
}

View File

@ -43,20 +43,17 @@ class ImportTest extends TestCase
}
/**
* Ensure exception is thrown when resource
* is not available for the migration.
*/
}
public function testExceptionOnUnavailableResource()
{
try {
$data['panda_bears'] = [
'name' => 'Awesome Panda Bear',
];
Import::dispatchNow($data, $this->company, $this->user);
} catch (ResourceNotAvailableForMigration $e) {
$this->assertTrue(true);
}
$data['panda_bears'] = [
'name' => 'Awesome Panda Bear',
];
Import::dispatchNow($data, $this->company, $this->user);
}
public function testCompanyUpdating()
@ -72,154 +69,6 @@ class ImportTest extends TestCase
$this->assertNotEquals($original_company_key, $this->company->company_key);
}
public function testTaxRatesInserting()
{
$total_tax_rates = TaxRate::count();
$data['tax_rates'] = [
0 => [
'name' => 'My awesome tax rate 1',
'rate' => '1.000',
]
];
Import::dispatchNow($data, $this->company, $this->user);
$this->assertNotEquals($total_tax_rates, TaxRate::count());
}
public function testTaxRateUniqueValidation()
{
$original_number = TaxRate::count();
try {
$data['tax_rates'] = [
0 => [
'name' => '',
'rate' => '1.000',
],
1 => [
'name' => 'My awesome tax rate 1',
'rate' => '1.000',
]
];
Import::dispatchNow($data, $this->company, $this->user);
} catch (MigrationValidatorFailed $e) {
$this->assertTrue(true);
}
$this->assertEquals($original_number, TaxRate::count());
}
public function testUsersImporting()
{
$original_number = User::count();
$data['users'] = [
0 => [
'id' => 1,
'first_name' => 'David',
'last_name' => 'IN',
'email' => 'my@awesomemail.com',
]
];
Import::dispatchNow($data, $this->company, $this->user);
$this->assertGreaterThan($original_number, User::count());
}
public function testUserValidator()
{
$original_number = User::count();
try {
$data['users'] = [
0 => [
'id' => 1,
'first_name' => 'David',
'last_name' => 'IN',
'email' => 'my@awesomemail.com',
],
1 => [
'id' => 2,
'first_name' => 'Someone',
'last_name' => 'Else',
'email' => 'my@awesomemail.com',
]
];
Import::dispatchNow($data, $this->company, $this->user);
} catch (MigrationValidatorFailed $e) {
$this->assertTrue(true);
}
$this->assertEquals($original_number, User::count());
}
public function testClientImporting()
{
$original_number = Client::count();
$data['users'] = [
0 => [
'id' => 1,
'first_name' => 'David',
'last_name' => 'IN',
'email' => 'my@awesomemail.com',
],
1 => [
'id' => 2,
'first_name' => 'Someone',
'last_name' => 'Else',
'email' => 'my@awesomemail2.com',
]
];
$data['clients'] = [
0 => [
'id' => 1,
'name' => 'My awesome client',
'balance' => '0.00',
'user_id' => 1,
]
];
Import::dispatchNow($data, $this->company, $this->user);
$this->assertGreaterThan($original_number, Client::count());
}
public function testProductsImporting()
{
$original_number = Product::count();
$data['products'] = [
0 => [
"company_id" => 1,
"user_id" => 1,
"custom_value1" => null,
"custom_value2" => null,
"product_key" => "et",
"notes" => "Natus repudiandae occaecati odit est aliquam reiciendis. Nihil sit praesentium excepturi provident nostrum sint. In fugit a dicta voluptas neque quo vel ullam.",
"cost" => "5.0000",
"quantity" => "0.0000",
"tax_name1" => null,
"tax_name2" => null,
"tax_rate1" => "0.000",
"tax_rate2" => "0.000",
"created_at" => "2020-01-22",
"updated_at" => "2020-01-22",
"deleted_at" => null
],
];
Import::dispatchNow($data, $this->company, $this->user);
$this->assertGreaterThan($original_number, Product::count());
}
public function testInvoicesFailsWithoutClient()
{
try {
@ -238,34 +87,19 @@ class ImportTest extends TestCase
public function testInvoicesImporting()
{
$this->makeTestData();
$original_number = Invoice::count();
$this->invoice->forceDelete();
$data['clients'] = [
0 => [
'id' => 1,
'name' => 'My awesome client',
'balance' => '0.00',
'user_id' => 1,
]
];
$original_count = Invoice::count();
$data['invoices'] = [
0 => [
'id' => 1,
'client_id' => 1,
'discount' => '0.00',
]
];
$migration_file = base_path() . '/tests/Unit/Migration/migration.json';
Import::dispatchNow($data, $this->company, $this->user);
$migration_array = json_decode(file_get_contents($migration_file), 1);
$this->assertGreaterThan($original_number, Invoice::count());
Invoice::where('id', '>=', '0')->forceDelete();
$this->assertEquals(0, Invoice::count());
Import::dispatchNow($migration_array, $this->company, $this->user);
$this->assertGreaterThan($original_count, Invoice::count());
}
public function testQuotesFailsWithoutClient()
@ -284,32 +118,6 @@ class ImportTest extends TestCase
}
}
public function testQuotesImporting()
{
$original_number = Quote::count();
$data['clients'] = [
0 => [
'id' => 1,
'name' => 'My awesome client',
'balance' => '0.00',
'user_id' => 1,
]
];
$data['quotes'] = [
0 => [
'client_id' => 1,
'discount' => '0.00',
]
];
Import::dispatchNow($data, $this->company, $this->user);
$this->assertGreaterThan($original_number, Quote::count());
}
public function testImportFileExists()
{
$migration_file = base_path() . '/tests/Unit/Migration/migration.json';
@ -379,6 +187,7 @@ class ImportTest extends TestCase
Import::dispatchNow($migration_array, $this->company, $this->user);
$this->assertGreaterThan($original_number, Invoice::count());
}
// public function testInvoiceAttributes()
@ -519,6 +328,17 @@ class ImportTest extends TestCase
$differences = [];
foreach ($migration_array['invoices'] as $key => $invoices) {
$record = Invoice::whereNumber($invoices['number'])
->whereAmount($invoices['amount'])
->whereBalance($invoices['balance'])
->first();
if (!$record) {
$differences['invoices']['missing'][] = $invoices['id'];
}
}
foreach ($migration_array['users'] as $key => $user) {
$record = User::whereEmail($user['email'])->first();
@ -540,6 +360,7 @@ class ImportTest extends TestCase
foreach ($migration_array['clients'] as $key => $client) {
$record = Client::whereName($client['name'])
->whereCity($client['city'])
// ->where('paid_to_date', $client['paid_to_date']) // TODO: Doesn't work. Need debugging.
->first();
if (!$record) {
@ -547,25 +368,13 @@ class ImportTest extends TestCase
}
}
/* foreach ($migration_array['products'] as $key => $product) {
foreach ($migration_array['products'] as $key => $product) {
$record = Product::where('product_key', $product['product_key'])
->where('quantity', $product['quantity'])
->first();
if (!$record) {
$differences['products']['missing'][] = $product['notes'];
}
} */
foreach ($migration_array['invoices'] as $key => $invoices) {
$record = Invoice::whereNumber($invoices['number'])
->whereIsAmountDiscount($invoices['is_amount_discount'])
->whereDueDate($invoices['due_date'])
->first();
if (!$record) {
$differences['invoices']['missing'][] = $invoices['id'];
}
}
foreach ($migration_array['quotes'] as $key => $quote) {
@ -591,8 +400,11 @@ class ImportTest extends TestCase
}
/*foreach ($migration_array['credits'] as $key => $credit) {
$record = Credit::where('number', $credit['number'])
->where('date', $credit['date'])
// The Import::processCredits() does insert the credit record with number: 0053,
// .. however this part of the code doesn't see it at all.
$record = Credit::whereNumber($credit['number'])
->first();
if (!$record) {
@ -600,6 +412,8 @@ class ImportTest extends TestCase
}
}*/
print_r($differences);
$this->assertCount(0, $differences);
}
}