From 9b6def477810ab1080c5bbb13bd8b8c2b8987370 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 21 Aug 2023 07:46:14 +1000 Subject: [PATCH] Tests for previews --- app/Export/CSV/BaseExport.php | 5 +- app/Export/CSV/CreditExport.php | 24 +++--- app/Jobs/Report/PreviewReport.php | 2 +- .../Export/ReportCsvGenerationTest.php | 44 ++++++++++- tests/Feature/Export/ReportPreviewTest.php | 77 +++++++++++++++++++ 5 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 tests/Feature/Export/ReportPreviewTest.php diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 9e5944ddf747..e4c743923c4c 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -839,7 +839,7 @@ class BaseExport foreach (array_merge($this->input['report_keys'], $this->forced_keys) as $value) { $key = array_search($value, $this->entity_keys); - + nlog("{$key} => {$value}"); $prefix = ''; if(!$key) { @@ -862,7 +862,6 @@ class BaseExport $key = array_search($value, $this->payment_report_keys); } - if(!$key) { $prefix = ctrans('texts.quote')." "; $key = array_search($value, $this->quote_report_keys); @@ -914,7 +913,7 @@ class BaseExport $key = str_replace('contact.', '', $key); $key = str_replace('payment.', '', $key); $key = str_replace('expense.', '', $key); - +// nlog($key); if(in_array($key, ['quote1','quote2','quote3','quote4','credit1','credit2','credit3','credit4','purchase_order1','purchase_order2','purchase_order3','purchase_order4'])) { $number = substr($key, -1); diff --git a/app/Export/CSV/CreditExport.php b/app/Export/CSV/CreditExport.php index 82de37a0cd04..234a9f9c10e4 100644 --- a/app/Export/CSV/CreditExport.php +++ b/app/Export/CSV/CreditExport.php @@ -34,11 +34,12 @@ class CreditExport extends BaseExport 'amount' => 'amount', 'balance' => 'balance', 'client' => 'client_id', + 'country' => 'country_id', 'custom_surcharge1' => 'custom_surcharge1', 'custom_surcharge2' => 'custom_surcharge2', 'custom_surcharge3' => 'custom_surcharge3', 'custom_surcharge4' => 'custom_surcharge4', - 'country' => 'country_id', + 'currency' => 'currency', 'custom_value1' => 'custom_value1', 'custom_value2' => 'custom_value2', 'custom_value3' => 'custom_value3', @@ -65,7 +66,6 @@ class CreditExport extends BaseExport 'tax_rate3' => 'tax_rate3', 'terms' => 'terms', 'total_taxes' => 'total_taxes', - 'currency' => 'currency', ]; private array $decorate_keys = [ @@ -106,12 +106,14 @@ class CreditExport extends BaseExport $report_keys = explode(".", $value); $column_key = str_replace("credit.", "", $value); + $column_key = array_search($column_key, $this->entity_keys); + $clean_row[$key]['entity'] = $report_keys[0]; - $clean_row[$key]['id'] = $report_keys[1]; + $clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0]; $clean_row[$key]['hashed_id'] = $report_keys[0] == 'credit' ? null : $credit->{$report_keys[0]}->hashed_id ?? null; $clean_row[$key]['value'] = $row[$column_key]; - if(in_array($report_keys[1], ['amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price'])) + if(in_array($clean_row[$key]['id'], ['amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price'])) $clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $credit->client); else $clean_row[$key]['display_value'] = $row[$column_key]; @@ -131,10 +133,14 @@ class CreditExport extends BaseExport $t->replace(Ninja::transformTranslations($this->company->settings)); if (count($this->input['report_keys']) == 0) { + $this->input['report_keys'] = array_values($this->entity_keys); + // $this->input['report_keys'] = collect(array_values($this->entity_keys))->map(function ($value){ - $this->input['report_keys'] = collect(array_values($this->entity_keys))->map(function ($value){ - return 'credit.'.$value; - })->toArray(); + // // if(in_array($value,['client_id','country_id'])) + // // return $value; + // // else + // return 'credit.'.$value; + // })->toArray(); } @@ -156,11 +162,11 @@ class CreditExport extends BaseExport //insert the header $this->csv->insertOne($this->buildHeader()); -// nlog($this->input['report_keys']); + // nlog($this->input['report_keys']); $query->cursor() ->each(function ($credit) { - nlog($this->buildRow($credit)); + // nlog($this->buildRow($credit)); $this->csv->insertOne($this->buildRow($credit)); }); diff --git a/app/Jobs/Report/PreviewReport.php b/app/Jobs/Report/PreviewReport.php index a5ce2b6a5d6d..abddca938758 100644 --- a/app/Jobs/Report/PreviewReport.php +++ b/app/Jobs/Report/PreviewReport.php @@ -40,7 +40,7 @@ class PreviewReport implements ShouldQueue $export = new $this->report_class($this->company, $this->request); $report = $export->returnJson(); - nlog($report); + // nlog($report); Cache::put($this->hash, $report, 60 * 60); } diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index 872166c906c7..a0ead0a357f2 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -70,7 +70,47 @@ class ReportCsvGenerationTest extends TestCase public $cu; - private $all_client_report_keys = ["client.name","client.user","client.assigned_user","client.balance","client.paid_to_date","client.currency_id","client.website","client.private_notes","client.industry_id","client.size_id","client.address1","client.address2","client.city","client.state","client.postal_code","client.country_id","contact.custom_value4","client.shipping_address1","client.shipping_address2","client.shipping_city","client.shipping_state","client.shipping_postal_code","client.shipping_country_id","client.payment_terms","client.vat_number","client.id_number","client.public_notes","client.phone","contact.first_name","contact.last_name","contact.email","contact.phone"]; + private $all_client_report_keys = [ + "client.name", + "client.user", + "client.assigned_user", + "client.balance", + "client.address1", + "client.address2", + "client.city", + "client.country_id", + "client.currency_id", + "client.custom_value1", + "client.custom_value2", + "client.custom_value3", + "client.custom_value4", + "client.industry_id", + "client.id_number", + "client.paid_to_date", + "client.payment_terms", + "client.phone", + "client.postal_code", + "client.private_notes", + "client.public_notes", + "client.size_id", + "client.shipping_address1", + "client.shipping_address2", + "client.shipping_city", + "client.shipping_state", + "client.shipping_postal_code", + "client.shipping_country_id", + "client.state", + "client.vat_number", + "client.website", + "contact.first_name", + "contact.last_name", + "contact.email", + "contact.phone", + "contact.custom_value1", + "contact.custom_value2", + "contact.custom_value3", + "contact.custom_value4", + ]; private $all_payment_report_keys = [ 'payment.date', @@ -1366,7 +1406,7 @@ class ReportCsvGenerationTest extends TestCase $response->assertStatus(200); $csv = $response->streamedContent(); - +nlog($csv); $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount')); $this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Balance')); $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Discount')); diff --git a/tests/Feature/Export/ReportPreviewTest.php b/tests/Feature/Export/ReportPreviewTest.php new file mode 100644 index 000000000000..75516cea6e58 --- /dev/null +++ b/tests/Feature/Export/ReportPreviewTest.php @@ -0,0 +1,77 @@ +faker = \Faker\Factory::create(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + + $this->withoutExceptionHandling(); + + $this->makeTestData(); + + } + + public function testCreditExportPreview() + { + + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $p = (new PreviewReport($this->company, $data, CreditExport::class, '123'))->handle(); + + $this->assertNull($p); + + } + + public function testCreditPreview() + { + $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/credits?output=json', $data) + ->assertStatus(200); + + } +} \ No newline at end of file