Bug 35398: Unit test
[koha.git] / t / db_dependent / Koha / EDI.t
1 #!/usr/bin/perl
2
3 # Copyright 2024 Martin Renvoize <martin.renvoize@ptfs-europe.com>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use FindBin qw( $Bin );
22
23 use Test::More tests => 1;
24 use Test::Warn;
25
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28
29 use Koha::EDI qw(process_quote);
30 use Koha::Edifact::Transport;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'process_quote' => sub {
36     plan tests => 7;
37
38     $schema->storage->txn_begin;
39
40     # Add our test quote file to the database for testing against
41     my $account = $builder->build(
42         {
43             source => 'VendorEdiAccount',
44             value  => {
45                 description => 'test vendor', transport => 'FILE',
46             }
47         }
48     );
49     my $dirname  = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} );
50     my $filename = 'QUOTES_SMALL.CEQ';
51     ok( -e $dirname . $filename, 'File QUOTES_SMALL.CEQ found' );
52
53     my $trans = Koha::Edifact::Transport->new( $account->{id} );
54     $trans->working_directory($dirname);
55
56     my $mhash = $trans->message_hash();
57     $mhash->{message_type} = 'QUOTE';
58     $trans->ingest( $mhash, $filename );
59
60     my $quote = $schema->resultset('EdifactMessage')->find( { filename => $filename } );
61
62     # Test quote expects REF to be a valid and active fund code
63     my $active_period = $builder->build(
64         {
65             source => 'Aqbudgetperiod',
66             value  => { budget_period_active => 1 }
67         }
68     );
69     my $fund = $builder->build(
70         {
71             source => 'Aqbudget',
72             value  => {
73                 budget_code      => 'REF',
74                 budget_period_id => $active_period->{budget_period_id}
75             }
76         }
77     );
78
79     # The quote expects a ROT1 stock rotation roata to exist
80     my $rota = $builder->build_object(
81         {
82             class => 'Koha::StockRotationRotas',
83             value => { title => 'ROT1' }
84         }
85     );
86     $builder->build({
87         source => 'Stockrotationstage',
88         value  => { rota_id => $rota->rota_id },
89     });
90
91     # Process the test quote file
92     process_quote($quote);
93
94     # Test for expected warnings for the passed quote file
95     #
96     # Test for quote status change
97     $quote->get_from_storage;
98     is( $quote->status, 'received', 'Quote status was set to received' );
99
100     # Tests for generated basket for passed quote file
101     my $baskets = Koha::Acquisition::Baskets->search( { booksellerid => $account->{vendor_id} } );
102     is( $baskets->count, 1, "1 basket created for a single quote file" );
103     my $basket = $baskets->next;
104
105     my $orders = $basket->orders;
106     is( $orders->count, 1, "1 order line attached to basket when only 1 order is in the edi message" );
107     my $order = $orders->next;
108
109     my $biblio = $order->biblio;
110
111     my $items = $order->items;
112     is( $items->count, 1, "1 item added when AcqCreateItem eq ordering and 1 item is in the EDI quote" );
113     my $item = $items->next;
114
115     # Test that item is added to rota appropriately
116     my $on_rota = Koha::StockRotationItems->search({ itemnumber_id => $item->itemnumber });
117     is($on_rota->count, 1, "Item added to stockrotation rota");
118
119     my $rota_item = $on_rota->next;
120     is($rota_item->stage->rota->id, $rota->id, "Item is on correct rota");
121
122     $schema->storage->txn_rollback;
123 };