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