mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
fixes for tests
This commit is contained in:
parent
ac8fffcdc2
commit
bc4e6711d9
@ -73,13 +73,6 @@ class ClientExport extends BaseExport
|
|||||||
'status' => 'status'
|
'status' => 'status'
|
||||||
];
|
];
|
||||||
|
|
||||||
private array $decorate_keys = [
|
|
||||||
'client.country_id',
|
|
||||||
'client.shipping_country_id',
|
|
||||||
'client.currency',
|
|
||||||
'client.industry',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
|
@ -112,14 +112,13 @@ class ContactExport extends BaseExport
|
|||||||
|
|
||||||
foreach (array_values($this->input['report_keys']) as $key) {
|
foreach (array_values($this->input['report_keys']) as $key) {
|
||||||
$parts = explode('.', $key);
|
$parts = explode('.', $key);
|
||||||
$keyval = array_search($key, $this->entity_keys);
|
|
||||||
|
|
||||||
if ($parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
|
if ($parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
|
||||||
$entity[$keyval] = $transformed_client[$parts[1]];
|
$entity[$key] = $transformed_client[$parts[1]];
|
||||||
} elseif ($parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) {
|
} elseif ($parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) {
|
||||||
$entity[$keyval] = $transformed_contact[$parts[1]];
|
$entity[$key] = $transformed_contact[$parts[1]];
|
||||||
} else {
|
} else {
|
||||||
$entity[$keyval] = '';
|
$entity[$key] = '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ use App\Models\Company;
|
|||||||
use App\Models\Document;
|
use App\Models\Document;
|
||||||
use App\Transformers\DocumentTransformer;
|
use App\Transformers\DocumentTransformer;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use League\Csv\Writer;
|
use League\Csv\Writer;
|
||||||
|
|
||||||
@ -30,16 +31,11 @@ class DocumentExport extends BaseExport
|
|||||||
|
|
||||||
public array $entity_keys = [
|
public array $entity_keys = [
|
||||||
'record_type' => 'record_type',
|
'record_type' => 'record_type',
|
||||||
// 'record_name' => 'record_name',
|
|
||||||
'name' => 'name',
|
'name' => 'name',
|
||||||
'type' => 'type',
|
'type' => 'type',
|
||||||
'created_at' => 'created_at',
|
'created_at' => 'created_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
private array $decorate_keys = [
|
|
||||||
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
@ -47,28 +43,55 @@ class DocumentExport extends BaseExport
|
|||||||
$this->entity_transformer = new DocumentTransformer();
|
$this->entity_transformer = new DocumentTransformer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run()
|
public function returnJson()
|
||||||
{
|
{
|
||||||
|
$query = $this->init();
|
||||||
|
|
||||||
|
$headerdisplay = $this->buildHeader();
|
||||||
|
|
||||||
|
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
|
||||||
|
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
$report = $query->cursor()
|
||||||
|
->map(function ($document) {
|
||||||
|
$row = $this->buildRow($document);
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
return array_merge(['columns' => $header], $report);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function init(): Builder
|
||||||
|
{
|
||||||
|
|
||||||
MultiDB::setDb($this->company->db);
|
MultiDB::setDb($this->company->db);
|
||||||
App::forgetInstance('translator');
|
App::forgetInstance('translator');
|
||||||
App::setLocale($this->company->locale());
|
App::setLocale($this->company->locale());
|
||||||
$t = app('translator');
|
$t = app('translator');
|
||||||
$t->replace(Ninja::transformTranslations($this->company->settings));
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
||||||
|
|
||||||
//load the CSV document from a string
|
|
||||||
$this->csv = Writer::createFromString();
|
|
||||||
|
|
||||||
if (count($this->input['report_keys']) == 0) {
|
if (count($this->input['report_keys']) == 0) {
|
||||||
$this->input['report_keys'] = array_values($this->entity_keys);
|
$this->input['report_keys'] = array_values($this->entity_keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
//insert the header
|
|
||||||
$this->csv->insertOne($this->buildHeader());
|
|
||||||
|
|
||||||
$query = Document::query()->where('company_id', $this->company->id);
|
$query = Document::query()->where('company_id', $this->company->id);
|
||||||
|
|
||||||
$query = $this->addDateRange($query);
|
$query = $this->addDateRange($query);
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$query = $this->init();
|
||||||
|
|
||||||
|
//load the CSV document from a string
|
||||||
|
$this->csv = Writer::createFromString();
|
||||||
|
|
||||||
|
//insert the header
|
||||||
|
$this->csv->insertOne($this->buildHeader());
|
||||||
|
|
||||||
$query->cursor()
|
$query->cursor()
|
||||||
->each(function ($entity) {
|
->each(function ($entity) {
|
||||||
$this->csv->insertOne($this->buildRow($entity));
|
$this->csv->insertOne($this->buildRow($entity));
|
||||||
|
@ -16,6 +16,7 @@ use App\Models\Company;
|
|||||||
use App\Models\Expense;
|
use App\Models\Expense;
|
||||||
use App\Transformers\ExpenseTransformer;
|
use App\Transformers\ExpenseTransformer;
|
||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use League\Csv\Writer;
|
use League\Csv\Writer;
|
||||||
|
|
||||||
@ -28,51 +29,6 @@ class ExpenseExport extends BaseExport
|
|||||||
|
|
||||||
public Writer $csv;
|
public Writer $csv;
|
||||||
|
|
||||||
public array $entity_keys = [
|
|
||||||
'amount' => 'expense.amount',
|
|
||||||
'category' => 'expense.category',
|
|
||||||
'client' => 'expense.client_id',
|
|
||||||
'custom_value1' => 'expense.custom_value1',
|
|
||||||
'custom_value2' => 'expense.custom_value2',
|
|
||||||
'custom_value3' => 'expense.custom_value3',
|
|
||||||
'custom_value4' => 'expense.custom_value4',
|
|
||||||
'currency' => 'expense.currency_id',
|
|
||||||
'date' => 'expense.date',
|
|
||||||
'exchange_rate' => 'expense.exchange_rate',
|
|
||||||
'converted_amount' => 'expense.foreign_amount',
|
|
||||||
'invoice_currency_id' => 'expense.invoice_currency_id',
|
|
||||||
'payment_date' => 'expense.payment_date',
|
|
||||||
'number' => 'expense.number',
|
|
||||||
'payment_type_id' => 'expense.payment_type_id',
|
|
||||||
'private_notes' => 'expense.private_notes',
|
|
||||||
'project' => 'expense.project_id',
|
|
||||||
'public_notes' => 'expense.public_notes',
|
|
||||||
'tax_amount1' => 'expense.tax_amount1',
|
|
||||||
'tax_amount2' => 'expense.tax_amount2',
|
|
||||||
'tax_amount3' => 'expense.tax_amount3',
|
|
||||||
'tax_name1' => 'expense.tax_name1',
|
|
||||||
'tax_name2' => 'expense.tax_name2',
|
|
||||||
'tax_name3' => 'expense.tax_name3',
|
|
||||||
'tax_rate1' => 'expense.tax_rate1',
|
|
||||||
'tax_rate2' => 'expense.tax_rate2',
|
|
||||||
'tax_rate3' => 'expense.tax_rate3',
|
|
||||||
'transaction_reference' => 'expense.transaction_reference',
|
|
||||||
'vendor' => 'expense.vendor_id',
|
|
||||||
'invoice' => 'expense.invoice_id',
|
|
||||||
'user' => 'expense.user',
|
|
||||||
'assigned_user' => 'expense.assigned_user',
|
|
||||||
];
|
|
||||||
|
|
||||||
private array $decorate_keys = [
|
|
||||||
'client',
|
|
||||||
'currency',
|
|
||||||
'invoice',
|
|
||||||
'category',
|
|
||||||
'vendor',
|
|
||||||
'project',
|
|
||||||
'payment_type_id',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
@ -80,24 +36,38 @@ class ExpenseExport extends BaseExport
|
|||||||
$this->expense_transformer = new ExpenseTransformer();
|
$this->expense_transformer = new ExpenseTransformer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run()
|
|
||||||
|
public function returnJson()
|
||||||
{
|
{
|
||||||
|
$query = $this->init();
|
||||||
|
|
||||||
|
$headerdisplay = $this->buildHeader();
|
||||||
|
|
||||||
|
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
|
||||||
|
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
$report = $query->cursor()
|
||||||
|
->map(function ($resource) {
|
||||||
|
return $this->buildRow($resource);
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
return array_merge(['columns' => $header], $report);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function init(): Builder
|
||||||
|
{
|
||||||
|
|
||||||
MultiDB::setDb($this->company->db);
|
MultiDB::setDb($this->company->db);
|
||||||
App::forgetInstance('translator');
|
App::forgetInstance('translator');
|
||||||
App::setLocale($this->company->locale());
|
App::setLocale($this->company->locale());
|
||||||
$t = app('translator');
|
$t = app('translator');
|
||||||
$t->replace(Ninja::transformTranslations($this->company->settings));
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
||||||
|
|
||||||
//load the CSV document from a string
|
|
||||||
$this->csv = Writer::createFromString();
|
|
||||||
|
|
||||||
if (count($this->input['report_keys']) == 0) {
|
if (count($this->input['report_keys']) == 0) {
|
||||||
$this->input['report_keys'] = array_values($this->entity_keys);
|
$this->input['report_keys'] = array_values($this->expense_report_keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
//insert the header
|
|
||||||
$this->csv->insertOne($this->buildHeader());
|
|
||||||
|
|
||||||
$query = Expense::query()
|
$query = Expense::query()
|
||||||
->with('client')
|
->with('client')
|
||||||
->withTrashed()
|
->withTrashed()
|
||||||
@ -106,6 +76,20 @@ class ExpenseExport extends BaseExport
|
|||||||
|
|
||||||
$query = $this->addDateRange($query);
|
$query = $this->addDateRange($query);
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$query = $this->init();
|
||||||
|
|
||||||
|
//load the CSV document from a string
|
||||||
|
$this->csv = Writer::createFromString();
|
||||||
|
|
||||||
|
//insert the header
|
||||||
|
$this->csv->insertOne($this->buildHeader());
|
||||||
|
|
||||||
$query->cursor()
|
$query->cursor()
|
||||||
->each(function ($expense) {
|
->each(function ($expense) {
|
||||||
$this->csv->insertOne($this->buildRow($expense));
|
$this->csv->insertOne($this->buildRow($expense));
|
||||||
@ -122,7 +106,6 @@ class ExpenseExport extends BaseExport
|
|||||||
|
|
||||||
foreach (array_values($this->input['report_keys']) as $key) {
|
foreach (array_values($this->input['report_keys']) as $key) {
|
||||||
$parts = explode('.', $key);
|
$parts = explode('.', $key);
|
||||||
$keyval = array_search($key, $this->entity_keys);
|
|
||||||
|
|
||||||
if (is_array($parts) && $parts[0] == 'expense' && array_key_exists($parts[1], $transformed_expense)) {
|
if (is_array($parts) && $parts[0] == 'expense' && array_key_exists($parts[1], $transformed_expense)) {
|
||||||
$entity[$key] = $transformed_expense[$parts[1]];
|
$entity[$key] = $transformed_expense[$parts[1]];
|
||||||
|
@ -1347,17 +1347,17 @@ nlog($csv);
|
|||||||
$reader = Reader::createFromString($csv);
|
$reader = Reader::createFromString($csv);
|
||||||
$reader->setHeaderOffset(0);
|
$reader->setHeaderOffset(0);
|
||||||
|
|
||||||
$res = $reader->fetchColumnByName('First Name');
|
$res = $reader->fetchColumnByName('Contact First Name');
|
||||||
$res = iterator_to_array($res, true);
|
$res = iterator_to_array($res, true);
|
||||||
|
|
||||||
$this->assertEquals('john', $res[1]);
|
$this->assertEquals('john', $res[1]);
|
||||||
|
|
||||||
$res = $reader->fetchColumnByName('Last Name');
|
$res = $reader->fetchColumnByName('Contact Last Name');
|
||||||
$res = iterator_to_array($res, true);
|
$res = iterator_to_array($res, true);
|
||||||
|
|
||||||
$this->assertEquals('doe', $res[1]);
|
$this->assertEquals('doe', $res[1]);
|
||||||
|
|
||||||
$res = $reader->fetchColumnByName('Email');
|
$res = $reader->fetchColumnByName('Contact Email');
|
||||||
$res = iterator_to_array($res, true);
|
$res = iterator_to_array($res, true);
|
||||||
|
|
||||||
$this->assertEquals('john@doe.com', $res[1]);
|
$this->assertEquals('john@doe.com', $res[1]);
|
||||||
@ -1696,10 +1696,10 @@ nlog($csv);
|
|||||||
|
|
||||||
$csv = $response->streamedContent();
|
$csv = $response->streamedContent();
|
||||||
|
|
||||||
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
|
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Expense Amount'));
|
||||||
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
|
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Expense Public Notes'));
|
||||||
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
|
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Expense Private Notes'));
|
||||||
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'User'));
|
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'Expense User'));
|
||||||
|
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
@ -1758,8 +1758,8 @@ nlog($csv);
|
|||||||
|
|
||||||
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
|
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
|
||||||
$this->assertEquals('Vendor 1', $this->getFirstValueByColumn($csv, 'Vendor Name'));
|
$this->assertEquals('Vendor 1', $this->getFirstValueByColumn($csv, 'Vendor Name'));
|
||||||
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
|
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Expense Amount'));
|
||||||
$this->assertEquals('USD', $this->getFirstValueByColumn($csv, 'Currency'));
|
$this->assertEquals('USD', $this->getFirstValueByColumn($csv, 'Expense Currency'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,13 +11,18 @@
|
|||||||
|
|
||||||
namespace Tests\Feature\Export;
|
namespace Tests\Feature\Export;
|
||||||
|
|
||||||
use App\Export\CSV\ActivityExport;
|
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Expense;
|
||||||
|
use App\Models\Document;
|
||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use App\Export\CSV\ClientExport;
|
use App\Export\CSV\ClientExport;
|
||||||
use App\Export\CSV\CreditExport;
|
use App\Export\CSV\CreditExport;
|
||||||
use App\Export\CSV\ContactExport;
|
use App\Export\CSV\ContactExport;
|
||||||
|
use App\Export\CSV\ExpenseExport;
|
||||||
|
use App\Export\CSV\ActivityExport;
|
||||||
|
use App\Export\CSV\DocumentExport;
|
||||||
use App\Jobs\Report\PreviewReport;
|
use App\Jobs\Report\PreviewReport;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
@ -49,6 +54,66 @@ class ReportPreviewTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testExpenseJsonExport()
|
||||||
|
{
|
||||||
|
Expense::factory()->count(5)->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'send_email' => false,
|
||||||
|
'date_range' => 'all',
|
||||||
|
'report_keys' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson('/api/v1/reports/expenses?output=json', $data)
|
||||||
|
->assertStatus(200);
|
||||||
|
|
||||||
|
$p = (new PreviewReport($this->company, $data, ExpenseExport::class, '123'))->handle();
|
||||||
|
|
||||||
|
$this->assertNull($p);
|
||||||
|
|
||||||
|
$r = Cache::pull('123');
|
||||||
|
|
||||||
|
$this->assertNotNull($r);
|
||||||
|
nlog($r);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDocumentJsonExport()
|
||||||
|
{
|
||||||
|
Document::factory()->count(5)->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'documentable_type' => Client::class,
|
||||||
|
'documentable_id' => $this->client->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'send_email' => false,
|
||||||
|
'date_range' => 'all',
|
||||||
|
'report_keys' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->postJson('/api/v1/reports/documents?output=json', $data)
|
||||||
|
->assertStatus(200);
|
||||||
|
|
||||||
|
$p = (new PreviewReport($this->company, $data, DocumentExport::class, '123'))->handle();
|
||||||
|
|
||||||
|
$this->assertNull($p);
|
||||||
|
|
||||||
|
$r = Cache::pull('123');
|
||||||
|
|
||||||
|
$this->assertNotNull($r);
|
||||||
|
nlog($r);
|
||||||
|
}
|
||||||
|
|
||||||
public function testClientExportJson()
|
public function testClientExportJson()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
@ -107,12 +172,10 @@ class ReportPreviewTest extends TestCase
|
|||||||
|
|
||||||
$this->assertNull($p);
|
$this->assertNull($p);
|
||||||
|
|
||||||
|
|
||||||
$r = Cache::pull('123');
|
$r = Cache::pull('123');
|
||||||
|
|
||||||
$this->assertNotNull($r);
|
$this->assertNotNull($r);
|
||||||
|
|
||||||
nlog($r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testActivityCSVExportJson()
|
public function testActivityCSVExportJson()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user