Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Koha / Acquisition / Basket.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 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 => 12;
23 use Test::Exception;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Acquisition qw( NewBasket ModBasket ModBasketHeader );
29 use Koha::Database;
30 use Koha::DateUtils qw(dt_from_string);
31
32 use_ok('Koha::Acquisition::Basket');
33 use_ok('Koha::Acquisition::Baskets');
34
35 my $schema  = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
37
38 subtest 'create_items + effective_create_items tests' => sub {
39
40     plan tests => 7;
41
42     $schema->storage->txn_begin;
43
44     my $basket = $builder->build_object(
45         {
46             class => 'Koha::Acquisition::Baskets',
47             value => { create_items => undef }
48         }
49     );
50     my $created_basketno = C4::Acquisition::NewBasket(
51         $basket->booksellerid,   $basket->authorisedby,
52         $basket->basketname,     $basket->note,
53         $basket->booksellernote, $basket->contractnumber,
54         $basket->deliveryplace,  $basket->billingplace,
55         $basket->is_standing,    $basket->create_items
56     );
57     my $created_basket = Koha::Acquisition::Baskets->find($created_basketno);
58     is( $created_basket->basketno, $created_basketno,
59         "Basket created by NewBasket matches db basket" );
60     is( $basket->create_items, undef, "Create items value can be null" );
61
62     t::lib::Mocks::mock_preference( 'AcqCreateItem', 'cataloguing' );
63     is( $basket->effective_create_items,
64         "cataloguing",
65         "We use AcqCreateItem if basket create items is not set" );
66     C4::Acquisition::ModBasketHeader(
67         $basket->basketno,       $basket->basketname,
68         $basket->note,           $basket->booksellernote,
69         $basket->contractnumber, $basket->booksellerid,
70         $basket->deliveryplace,  $basket->billingplace,
71         $basket->is_standing,    "ordering"
72     );
73     my $retrieved_basket = Koha::Acquisition::Baskets->find( $basket->basketno );
74     $basket->create_items("ordering");
75     is( $retrieved_basket->create_items, "ordering", "Should be able to set with ModBasketHeader" );
76     is( $basket->create_items, "ordering", "Should be able to set with object methods" );
77     is_deeply( $retrieved_basket->unblessed,
78         $basket->unblessed, "Correct basket found and updated" );
79     is( $retrieved_basket->effective_create_items,
80         "ordering", "We use basket create items if it is set" );
81
82     $schema->storage->txn_rollback;
83 };
84
85 subtest 'basket_group' => sub {
86     plan tests => 2;
87
88     $schema->storage->txn_begin;
89     my $b = $builder->build_object(
90         {
91             class => 'Koha::Acquisition::Baskets',
92             value => { basketgroupid => undef }, # not linked to a basketgroupid
93         }
94     );
95
96     my $basket = Koha::Acquisition::Baskets->find( $b->basketno );
97     is( $basket->basket_group, undef,
98         '->basket_group should return undef if not linked to a basket group');
99
100     $b = $builder->build_object(
101         {
102             class => 'Koha::Acquisition::Baskets',
103             # Will be linked to a basket group by TestBuilder
104         }
105     );
106
107     $basket = Koha::Acquisition::Baskets->find( $b->basketno );
108     is( ref( $basket->basket_group ), 'Koha::Acquisition::BasketGroup',
109         '->basket_group should return a Koha::Acquisition::BasketGroup object if linked to a basket group');
110
111     $schema->storage->txn_rollback;
112 };
113
114 subtest 'creator() tests' => sub {
115
116     plan tests => 4;
117
118     $schema->storage->txn_begin;
119
120     my $basket = $builder->build_object(
121         {
122             class => 'Koha::Acquisition::Baskets',
123             value => { authorisedby => undef }
124         }
125     );
126
127     is( $basket->creator, undef, 'Undef is handled as expected' );
128
129     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
130     $basket->authorisedby( $patron->borrowernumber )->store->discard_changes;
131
132     my $creator = $basket->creator;
133     is( ref($creator), 'Koha::Patron', 'Return type is correct' );
134     is( $creator->borrowernumber, $patron->borrowernumber, 'Returned object is the right one' );
135
136     # Delete the patron
137     $patron->delete;
138
139     is( $basket->creator, undef, 'Undef is handled as expected' );
140
141     $schema->storage->txn_rollback;
142 };
143
144 subtest 'to_api() tests' => sub {
145
146     plan tests => 6;
147
148     $schema->storage->txn_begin;
149
150     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
151     my $basket = $builder->build_object(
152         {
153             class => 'Koha::Acquisition::Baskets',
154             value => {
155                 closedate => undef
156             }
157         }
158     );
159
160     my $closed = $basket->to_api->{closed};
161     ok( defined $closed, 'closed is defined' );
162     ok( !$closed, 'closedate is undef, closed evaluates to false' );
163
164     $basket->closedate( dt_from_string )->store->discard_changes;
165     $closed = $basket->to_api->{closed};
166     ok( defined $closed, 'closed is defined' );
167     ok( $closed, 'closedate is defined, closed evaluates to true' );
168
169     $basket->booksellerid( $vendor->id )->store->discard_changes;
170     my $basket_json = $basket->to_api({ embed => { bookseller => {} } });
171     ok( exists $basket_json->{bookseller} );
172     is_deeply( $basket_json->{bookseller}, $vendor->to_api );
173
174     $schema->storage->txn_rollback;
175 };
176
177 subtest 'estimated_delivery_date' => sub {
178     plan tests => 4;
179
180     $schema->storage->txn_begin;
181     my $bookseller = $builder->build_object(
182         {
183             class => 'Koha::Acquisition::Booksellers',
184             value => {
185                 deliverytime => undef,   # Does not have a delivery time defined
186             }
187         }
188     );
189
190     my $basket = $builder->build_object(
191         {
192             class => 'Koha::Acquisition::Baskets',
193             value => {
194                 booksellerid => $bookseller->id,
195                 closedate    => undef,             # Still open
196             }
197         }
198     );
199
200     my $now = dt_from_string;
201     is( $basket->estimated_delivery_date,
202         undef, 'return undef if closedate and deliverytime are not defined' );
203
204     $basket->closedate( $now->clone->subtract( days => 1 ) )
205       ->store;                                     #Closing the basket
206     is( $basket->estimated_delivery_date,
207         undef, 'return undef if deliverytime is not defined' );
208
209     $basket->closedate(undef)->store;              #Reopening
210     $bookseller->deliverytime(2)->store;           # 2 delivery days
211     is( $basket->estimated_delivery_date,
212         undef, 'return undef if closedate is not defined (basket stil open)' );
213
214     $bookseller->deliverytime(2)->store;           # 2 delivery days
215     $basket->closedate( $now->clone->subtract( days => 1 ) )->store; #Closing the basket
216     is(
217         $basket->get_from_storage->estimated_delivery_date,
218         $now->clone->add( days => 1 )->truncate( to => 'day' ),
219         'Estimated delivery date should be tomorrow if basket closed on yesterday and delivery takes 2 days'
220     );
221
222     $schema->storage->txn_rollback;
223 };
224
225 subtest 'late_since_days' => sub {
226     plan tests => 3;
227
228     $schema->storage->txn_begin;
229     my $basket  = $builder->build_object(
230         {
231             class => 'Koha::Acquisition::Baskets',
232         }
233     );
234
235     my $now = dt_from_string;
236     $basket->closedate(undef)->store; # Basket is open
237     is( $basket->late_since_days, undef, 'return undef if basket is still open');
238
239     $basket->closedate( $now )->store; #Closing the basket today
240     is( $basket->late_since_days, 0, 'return 0 if basket has been closed on today' );
241
242     $basket->closedate( $now->clone->subtract( days => 2 ) )->store;
243     is( $basket->late_since_days, 2, 'return 2 if basket has been closed 2 days ago' );
244
245     $schema->storage->txn_rollback;
246 };
247
248 subtest 'authorizer' => sub {
249     plan tests => 3;
250
251     $schema->storage->txn_begin;
252     my $basket = $builder->build_object(
253         {
254             class => 'Koha::Acquisition::Baskets',
255             value => { authorisedby => undef },
256         }
257     );
258
259     my $basket_creator = $builder->build_object( { class => 'Koha::Patrons' } );
260
261     is( $basket->authorizer, undef,
262         'authorisedby is null, ->authorized should return undef' );
263
264     $basket->authorisedby( $basket_creator->borrowernumber )->store;
265
266     is( ref( $basket->authorizer ),
267         'Koha::Patron', '->authorized should return a Koha::Patron object' );
268     is(
269         $basket->authorizer->borrowernumber,
270         $basket_creator->borrowernumber,
271         '->authorized should return the correct creator'
272     );
273
274     $schema->storage->txn_rollback;
275 };
276
277 subtest 'orders' => sub {
278
279     plan tests => 4;
280
281     $schema->storage->txn_begin;
282
283     my $basket = $builder->build_object(
284         {
285             class => 'Koha::Acquisition::Baskets'
286         }
287     );
288
289     my $orders = $basket->orders;
290     is( ref($orders), 'Koha::Acquisition::Orders', 'Type is correct with no attached orders' );
291     is( $orders->count, 0, 'No orders attached, count 0' );
292
293     my @actual_orders;
294
295     for ( 1..3 ) {
296         push @actual_orders, $builder->build_object({ class => 'Koha::Acquisition::Orders', value => { basketno => $basket->id } });
297     }
298
299     $orders = $basket->orders;
300     is( ref($orders), 'Koha::Acquisition::Orders', 'Type is correct with no attached orders' );
301     is( $orders->count, 3, '3 orders attached, count 3' );
302
303     $schema->storage->txn_rollback;
304 };
305
306 subtest 'is_closed() tests' => sub {
307
308     plan tests => 2;
309
310     $schema->storage->txn_begin;
311
312     my $open_basket = $builder->build_object(
313         {
314             class => 'Koha::Acquisition::Baskets',
315             value => {
316                 closedate => undef
317             }
318         }
319     );
320
321     my $closed_basket = $builder->build_object(
322         {
323             class => 'Koha::Acquisition::Baskets',
324             value => {
325                 closedate => \'NOW()'
326             }
327         }
328     );
329
330     ok( $closed_basket->is_closed, 'Closed basket is tested as closed' );
331     ok( !$open_basket->is_closed, 'Open basket is tested as open' );
332
333     $schema->storage->txn_rollback;
334 };
335
336 subtest 'close() tests' => sub {
337
338     plan tests => 4;
339
340     # Turn on acquisitions logging and ensure the logs are empty
341     t::lib::Mocks::mock_preference('AcquisitionLog', 1);
342     Koha::ActionLogs->delete;
343
344     $schema->storage->txn_begin;
345
346     # Create an open basket
347     my $basket = $builder->build_object(
348         {
349             class => 'Koha::Acquisition::Baskets',
350             value => {
351                 closedate => undef
352             }
353         }
354     );
355
356     for my $status ( qw( new ordered partial complete cancelled ) ) {
357         $builder->build_object(
358             {
359                 class => 'Koha::Acquisition::Orders',
360                 value => {
361                     basketno    => $basket->id,
362                     orderstatus => $status
363                 }
364             }
365         );
366     }
367
368     $basket->close;
369
370     ok( $basket->is_closed, 'Basket is closed' );
371     my $ordered_orders = $basket->orders->search({ orderstatus => 'ordered' });
372     is( $ordered_orders->count, 3, 'Only open orders have been marked as ordered' );
373
374     throws_ok
375         { $basket->close; }
376         'Koha::Exceptions::Acquisition::Basket::AlreadyClosed',
377         'Trying to close an already closed basket throws an exception';
378
379     my @close_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'CLOSE_BASKET', object => $basket->id });
380     is (scalar @close_logs, 1, 'Basket closure is logged');
381
382     $schema->storage->txn_rollback;
383 };