Bug 30130: Unit tests
[koha.git] / t / db_dependent / Koha / Edifact / Order.t
1 #!/usr/bin/perl
2
3 # Copyright 2021 Joonas Kylmälä <joonas.kylmala@iki.fi>
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
22 use Test::More tests => 3;
23
24 use Koha::Edifact::Order;
25
26 use t::lib::TestBuilder;
27
28 my $schema = Koha::Database->new->schema;
29
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'beggining_of_message tests' => sub {
33     plan tests => 2;
34
35     $schema->storage->txn_begin;
36
37     my $basketno = sprintf '%011d', '123456';
38     my $edi_vendor = $builder->build(
39         {
40             source => 'VendorEdiAccount',
41             value  => {
42                 standard => 'EUR',
43             }
44         }
45     );
46     my $dbic_edi_vendor = $schema->resultset('VendorEdiAccount')->find($edi_vendor->{id});
47
48     my $bgm = Koha::Edifact::Order::beginning_of_message($basketno, $dbic_edi_vendor, 1);
49     is( $bgm, qq{BGM+220+$basketno+9'}, "When vendor is set to EDItEUR standard we use 220 in BGM segment");
50
51     $dbic_edi_vendor->update({ standard => 'BIC'});
52     $bgm = Koha::Edifact::Order::beginning_of_message($basketno, $dbic_edi_vendor, 1);
53     is( $bgm, qq{BGM+220+$basketno+9'}, "When vendor is set to BiC standard we use 22V in BGM segment");
54
55     $schema->storage->txn_rollback;
56 };
57
58 subtest 'order_line() tests' => sub {
59     # TODO: Split up order_line() to smaller methods in order
60     #       to allow better testing
61     plan tests => 24;
62
63     $schema->storage->txn_begin;
64
65     my $biblio = $builder->build_sample_biblio();
66     my $biblioitem = $biblio->biblioitem;
67     $biblioitem->update({ isbn => '979-8572072303' });
68     my $biblioitem_itype = $biblioitem->itemtype;
69
70     my $item1 = $builder->build_sample_item(
71         {
72             biblionumber   => $biblio->biblionumber,
73             location       => 'PROCESSING',
74             itemcallnumber => '000.101'
75         }
76     );
77     my $item1_homebranch = $item1->homebranch;
78     my $item1_itype = $item1->effective_itemtype;
79     my $item2 = $builder->build_sample_item(
80         {
81             biblionumber   => $biblio->biblionumber,
82             location       => 'PROCESSING',
83             itemcallnumber => '000.102'
84         }
85     );
86     my $item2_homebranch = $item2->homebranch;
87     my $item2_itype = $item2->effective_itemtype;
88
89     my $ean    = $builder->build( { source => 'EdifactEan' } );
90     my $dbic_ean = $schema->resultset('EdifactEan')->find($ean->{ee_id});
91     my $order = $builder->build_object(
92         {
93             class => 'Koha::Acquisition::Orders',
94             value => {
95                 biblionumber => $biblio->biblionumber,
96                 quantity     => 2,
97                 line_item_id => 'EDILINEID1',
98                 order_vendornote => 'A not so pretty note',
99                 listprice => '1.50'
100             }
101         }
102     );
103     my $ordernumber = $order->ordernumber;
104     my $supplier_qualifier = $order->suppliers_reference_qualifier;
105     my $supplier_ordernumber = $order->suppliers_reference_number;
106     my $budgetcode = $order->fund->budget_code;
107     my $deliveryplace = $order->basket->deliveryplace;
108     $order->add_item($item1->itemnumber);
109     $order->add_item($item2->itemnumber);
110
111     my $vendor = $builder->build(
112         {
113             source => 'VendorEdiAccount',
114             value  => {
115                 vendor_id => $order->basket->bookseller->id,
116             }
117         }
118     );
119     my $dbic_vendor = $schema->resultset('VendorEdiAccount')->find($vendor->{id});
120
121     my @orders = $schema->resultset('Aqorder')
122       ->search( { basketno => $order->basket->basketno } )->all;
123
124     my $edi_order = Koha::Edifact::Order->new(
125         {
126             orderlines => \@orders,
127             vendor     => $dbic_vendor,
128             ean        => $dbic_ean
129         }
130     );
131
132     # FIXME: Add test for an order where the attached biblio has been deleted.
133
134     $order->basket->create_items('ordering')->store;
135     is( $edi_order->order_line( 1, $orders[0] ),
136         undef, 'order_line run for message formed with effective_create_items = "ordering"' );
137
138     my $segs = $edi_order->{segs};
139     is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
140     is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
141     is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
142     is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
143     is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
144     is(
145         $segs->[5],
146         'GIR+001'
147           . "+$budgetcode:LFN"
148           . "+$item1_homebranch:LLO"
149           . "+$item1_itype:LST"
150           . "+PROCESSING:LSQ"
151           . "+000.101:LSM"
152           . "'",
153         'GIR segment added for first item and contains item record data'
154     );
155     is(
156         $segs->[6],
157         'GIR+002'
158           . "+$budgetcode:LFN"
159           . "+$item2_homebranch:LLO"
160           . "+$item2_itype:LST"
161           . "+PROCESSING:LSQ"
162           . "+000.102:LSM"
163           . "'",
164         'GIR segment added for second item and contains item record data'
165     );
166     is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
167     is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
168     is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
169     is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
170
171     # Reset segments for effective_create_items = 'receiving'
172     $edi_order->{segs} = [];
173
174     $order->basket->create_items('receiving')->store;
175     is( $edi_order->order_line( 1, $orders[0] ),
176         undef, 'order_line run for message formed with effective_create_items = "receiving"' );
177
178     $segs = $edi_order->{segs};
179     is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
180     is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
181     is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
182     is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
183     is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
184     is(
185         $segs->[5],
186         'GIR+001'
187           . "+$budgetcode:LFN"
188           . "+$deliveryplace:LLO"
189           . "+$biblioitem_itype:LST"
190           . "'",
191         'GIR segment added for first item and contains biblioitem data'
192     );
193     is(
194         $segs->[6],
195         'GIR+002'
196           . "+$budgetcode:LFN"
197           . "+$deliveryplace:LLO"
198           . "+$biblioitem_itype:LST"
199           . "'",
200         'GIR segment added for second item and contains biblioitem data'
201     );
202     is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
203     is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
204     is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
205     is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
206
207     $schema->storage->txn_rollback;
208 };
209
210 subtest 'filename() tests' => sub {
211     plan tests => 1;
212
213     $schema->storage->txn_begin;
214
215     my $ean = $builder->build( { source => 'EdifactEan' } );
216     my $order =
217       $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
218     my $vendor = $builder->build(
219         {
220             source => 'VendorEdiAccount',
221             value  => { vendor_id => $order->basket->bookseller->id }
222         }
223     );
224
225     my @orders = $schema->resultset('Aqorder')
226       ->search( { basketno => $order->basket->basketno } )->all;
227
228     my $edi_order = Koha::Edifact::Order->new(
229         {
230             orderlines => \@orders,
231             vendor     => $vendor,
232             ean        => $ean
233         }
234     );
235
236     my $expected_filename = 'ordr' . $order->basket->basketno . '.CEP';
237     is( $edi_order->filename, $expected_filename,
238         'Filename is formed from the basket number' );
239
240     $schema->storage->txn_rollback;
241 };