From f384626d157573800d8330bd92b8626a2d110245 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 12 Dec 2024 11:27:30 +0000 Subject: [PATCH] Bug 38423: Add unit tests Sponsored-by: PTFS Europe Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Kyle M Hall --- Koha/EDI.pm | 2 +- t/db_dependent/Koha/EDI.t | 110 +++++++++++++++++++++++++++++++++++- t/edi_testfiles/INVOICE.CEI | 2 + 3 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 t/edi_testfiles/INVOICE.CEI diff --git a/Koha/EDI.pm b/Koha/EDI.pm index facdf31a863..09b3d6f74f4 100644 --- a/Koha/EDI.pm +++ b/Koha/EDI.pm @@ -290,7 +290,7 @@ sub process_invoice { )->single; } if ( !$vendor_acct ) { - carp "Cannot find vendor with ean $vendor_ean for invoice $invoicenumber in $invoice_message->filename"; + carp "Cannot find vendor with ean $vendor_ean for invoice $invoicenumber in ".$invoice_message->filename; next; } $invoice_message->edi_acct( $vendor_acct->id ); diff --git a/t/db_dependent/Koha/EDI.t b/t/db_dependent/Koha/EDI.t index e298719456b..1cc87267893 100755 --- a/t/db_dependent/Koha/EDI.t +++ b/t/db_dependent/Koha/EDI.t @@ -20,18 +20,20 @@ use Modern::Perl; use FindBin qw( $Bin ); -use Test::More tests => 2; +use Test::More tests => 3; use Test::Warn; use Test::MockModule; use t::lib::Mocks; +use t::lib::Mocks::Logger; use t::lib::TestBuilder; -use Koha::EDI qw(process_quote); +use Koha::EDI qw(process_quote process_invoice); use Koha::Edifact::Transport; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; +my $logger = t::lib::Mocks::Logger->new(); subtest 'process_quote' => sub { plan tests => 7; @@ -287,5 +289,107 @@ subtest '_handle_008_field' => sub { is( $record_field->{_data}, undef, 'Field has not been added' ); + $logger->clear(); $schema->storage->txn_rollback; - } +}; + +subtest 'process_invoice' => sub { + plan tests => 11; + + $schema->storage->txn_begin; + + # Add test EDI matching ean of test invoice file and ensure no plugins so we trigger core functions + my $account = $builder->build( + { + source => 'VendorEdiAccount', + value => { + description => 'test vendor', + transport => 'FILE', + plugin => '', + san => '5013546027173' + } + } + ); + + # Create a test basket and orders matching the invoice message + my $basket = $builder->build_object( + { + class => 'Koha::Acquisition::Baskets', + value => { + booksellerid => $account->{vendor_id}, + basketname => 'Test Basket', + } + } + ); + my $order1 = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + basketno => $basket->id, + orderstatus => 'new', + biblionumber => undef, + } + } + ); + my $ordernumber1 = $order1->ordernumber; + + my $order2 = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + basketno => $basket->id, + orderstatus => 'new', + } + } + ); + my $ordernumber2 = $order2->ordernumber; + + # Add test invoice file to the database for testing + my $dirname = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} ); + my $filename = 'INVOICE.CEI'; + ok( -e $dirname . $filename, 'File INVOICE.CEI found' ); + + my $trans = Koha::Edifact::Transport->new( $account->{id} ); + $trans->working_directory($dirname); + + my $mhash = $trans->message_hash(); + $mhash->{message_type} = 'INVOICE'; + $trans->ingest( $mhash, $filename ); + + my $invoice_message = $schema->resultset('EdifactMessage')->find( { filename => $filename } ); + my $raw_msg = $invoice_message->raw_msg; + $raw_msg =~ s/P12345/$ordernumber1/g; + $raw_msg =~ s/P54321/$ordernumber2/g; + $invoice_message->update( { raw_msg => $raw_msg } ); + + # Process the test invoice file + warnings_exist { process_invoice($invoice_message) } + [ + { carped => qr/Cannot find vendor with ean.*/i }, + ], + 'Invoice processed, with warnings, without dieing'; + + $logger->trace_like( qr/Adding invoice:.*/, "Trace recorded adding invoice" ) + ->trace_like( qr/Added as invoiceno.*/, "Trace recorded invoice added" )->error_is( + 'Skipping invoice line, no associated ordernumber', + "Received expected log line for missing ordernumber line" + )->error_like( + qr/Skipping invoice line, no order found for.*/, + 'Received expected log line for unmatched ordernumber line' + )->error_like( + qr/Skipping invoice line, no bibliographic.*/, + 'Received expected log line for unmatched biblionumber line' + )->trace_like( + qr/Receipting order:.*/, + 'Trace recorded invoice receipted' + )->trace_like( + qr/Updating bib:.*/, + 'Trace recorded bib updated' + )->clear(); + + my $invoice3 = Koha::Acquisition::Invoices->search( { invoicenumber => 'INV00003' }, { rows => 1 } )->single; + ok( $invoice3, "Invoice added to database" ); + is( $invoice3->booksellerid, $account->{vendor_id}, 'Invoice has test booksellerid' ); + + $schema->storage->txn_rollback; +}; diff --git a/t/edi_testfiles/INVOICE.CEI b/t/edi_testfiles/INVOICE.CEI new file mode 100644 index 00000000000..e8f8bcce387 --- /dev/null +++ b/t/edi_testfiles/INVOICE.CEI @@ -0,0 +1,2 @@ +UNA:+.? 'UNB+UNOC:3+5013546027173+5013546098818+230101:0000+0000000002'UNH+00003+INVOIC:D:96A:UN'BGM+380+INV00003+9'DTM+137:20231210:102'NAD+BY+12345::9'NAD+SU+5013546027173::9'LIN+1++987654321:EN'QTY+47:5'PRI+AAA:9.99'MOA+203:49.95'LIN+2++987654321:EN'QTY+47:5'PRI+AAA:9.99'MOA+203:49.95'RFF+LI:P28837'LIN+3++999999999:EN'QTY+47:10'PRI+AAA:15.00'MOA+203:150.00'RFF+LI:P12345'LIN+4++123456789:EN'QTY+47:1'PRI+AAA:30.00'MOA+203:600.00'RFF+LI:P54321'LIN+5++987654123:EN'QTY+47:1'PRI+AAA:5.00'MOA+203:5.00'RFF+LI:P54321'UNS+S'CNT+4:4'UNT+15+00003'UNZ+1+0000000002' +UNA:+.? 'UNB+UNOC:3+9999999999999+5013546098818+230101:0000+0000000001'UNH+00002+INVOIC:D:96A:UN'BGM+380+INV00002+9'DTM+137:20231210:102'NAD+BY+54321::9'NAD+SU+9999999999999::9'UNS+S'CNT+1:1'UNT+11+00002' -- 2.39.2