Bug 22390: Never copy items if original order has been created from a subscription
[srvgit] / t / db_dependent / Koha / Acquisition / Order.t
1 #!/usr/bin/perl
2
3 # Copyright 2017 Koha Development team
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 => 6;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'basket() tests' => sub {
33
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $basket = $builder->build_object(
39         {
40             class => 'Koha::Acquisition::Baskets'
41         }
42     );
43     my $order = $builder->build_object(
44         {
45             class => 'Koha::Acquisition::Orders',
46             value => { basketno => $basket->basketno }
47         }
48     );
49
50     my $retrieved_basket = $order->basket;
51     is( ref($retrieved_basket), 'Koha::Acquisition::Basket',
52         'Type is correct for ->basket' );
53     is_deeply( $retrieved_basket->unblessed,
54         $basket->unblessed, "Correct basket found and updated" );
55
56     $schema->storage->txn_rollback;
57 };
58
59 subtest 'store' => sub {
60     plan tests => 1;
61
62     $schema->storage->txn_begin;
63     my $o = $builder->build_object(
64         {
65             class => 'Koha::Acquisition::Orders'
66         }
67     );
68
69     subtest 'entrydate' => sub {
70         plan tests => 2;
71
72         my $order;
73
74         t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
75         $order = Koha::Acquisition::Order->new(
76             {
77                 basketno     => $o->basketno,
78                 biblionumber => $o->biblionumber,
79                 budget_id    => $o->budget_id,
80                 quantity     => 1,
81             }
82         )->store;
83         $order->discard_changes;
84         like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
85
86         t::lib::Mocks::mock_preference( 'TimeFormat', '24hr' );
87         $order = Koha::Acquisition::Order->new(
88             {
89                 basketno     => $o->basketno,
90                 biblionumber => $o->biblionumber,
91                 budget_id    => $o->budget_id,
92                 quantity     => 1,
93             }
94         )->store;
95         $order->discard_changes;
96         like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
97     };
98     $schema->storage->txn_rollback;
99 };
100
101 subtest 'fund' => sub {
102     plan tests => 1;
103
104     $schema->storage->txn_begin;
105     my $o = $builder->build_object(
106         {
107             class => 'Koha::Acquisition::Orders',
108         }
109     );
110
111     my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
112     is( ref( $order->fund ),
113         'Koha::Acquisition::Fund',
114         '->fund should return a Koha::Acquisition::Fund object' );
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'invoice' => sub {
119     plan tests => 2;
120
121     $schema->storage->txn_begin;
122     my $o = $builder->build_object(
123         {
124             class => 'Koha::Acquisition::Orders',
125             value => { cancellationreason => 'XXXXXXXX', invoiceid => undef }, # not received yet
126         }
127     );
128
129     my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
130     is( $order->invoice, undef,
131         '->invoice should return undef if no invoice defined yet');
132
133     my $invoice = $builder->build_object(
134         {
135             class => 'Koha::Acquisition::Invoices',
136         },
137     );
138
139     $o->invoiceid( $invoice->invoiceid )->store;
140     $order = Koha::Acquisition::Orders->find( $o->ordernumber );
141     is( ref( $order->invoice ), 'Koha::Acquisition::Invoice',
142         '->invoice should return a Koha::Acquisition::Invoice object if an invoice is defined');
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'subscription' => sub {
148     plan tests => 2;
149
150     $schema->storage->txn_begin;
151     my $o = $builder->build_object(
152         {
153             class => 'Koha::Acquisition::Orders',
154             value => { subscriptionid => undef }, # not linked to a subscription
155         }
156     );
157
158     my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
159     is( $order->subscription, undef,
160         '->subscription should return undef if not created from a subscription');
161
162     $o = $builder->build_object(
163         {
164             class => 'Koha::Acquisition::Orders',
165             # Will be linked to a subscription by TestBuilder
166         }
167     );
168
169     $order = Koha::Acquisition::Orders->find( $o->ordernumber );
170     is( ref( $order->subscription ), 'Koha::Subscription',
171         '->subscription should return a Koha::Subscription object if created from a subscription');
172
173     $schema->storage->txn_rollback;
174 };
175
176 subtest 'duplicate_to | add_item' => sub {
177     plan tests => 2;
178
179     $schema->storage->txn_begin;
180
181     my $item = $builder->build_sample_item;
182     my $order_no_sub = $builder->build_object(
183         {
184             class => 'Koha::Acquisition::Orders',
185             value =>
186               {
187                   biblionumber => $item->biblionumber,
188                   subscriptionid => undef, # not linked to a subscription
189               }
190         }
191     );
192     $order_no_sub->basket->create_items(undef)->store; # use syspref
193     $order_no_sub->add_item( $item->itemnumber );
194
195     $item = $builder->build_sample_item;
196     my $order_from_sub = $builder->build_object(
197         {
198             class => 'Koha::Acquisition::Orders',
199             value =>
200               {
201                   biblionumber => $item->biblionumber,
202                   # Will be linked to a subscription by TestBuilder
203               }
204         }
205     );
206     $order_from_sub->basket->create_items(undef)->store; # use syspref
207     $order_from_sub->add_item( $item->itemnumber );
208
209     my $basket_to = $builder->build_object(
210          { class => 'Koha::Acquisition::Baskets' });
211
212     subtest 'Create item on receiving' => sub {
213         plan tests => 2;
214
215         t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
216
217         my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
218         is( $duplicated_order->items->count, 0,
219             'Items should not be copied if the original order did not create items on ordering'
220         );
221
222         $duplicated_order = $order_from_sub->duplicate_to($basket_to);
223         is( $duplicated_order->items->count, 0,
224             'Items should not be copied if the original order is created from a subscription'
225         );
226     };
227
228     subtest 'Create item on ordering' => sub {
229         plan tests => 2;
230
231         t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
232
233         my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
234         is( $duplicated_order->items->count, 1,
235             'Items should be copied if items are created on ordering'
236         );
237
238         $duplicated_order = $order_from_sub->duplicate_to($basket_to);
239         is( $duplicated_order->items->count, 0,
240             'Items should never be copied if the original order is created from a subscription'
241         );
242     };
243
244     $schema->storage->txn_rollback;
245 };