3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 12;
25 use t::lib::TestBuilder;
28 use C4::Acquisition qw( NewBasket ModBasket ModBasketHeader );
30 use Koha::DateUtils qw(dt_from_string);
32 use_ok('Koha::Acquisition::Basket');
33 use_ok('Koha::Acquisition::Baskets');
35 my $schema = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'create_items + effective_create_items tests' => sub {
42 $schema->storage->txn_begin;
44 my $basket = $builder->build_object(
46 class => 'Koha::Acquisition::Baskets',
47 value => { create_items => undef }
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
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" );
62 t::lib::Mocks::mock_preference( 'AcqCreateItem', 'cataloguing' );
63 is( $basket->effective_create_items,
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"
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" );
82 $schema->storage->txn_rollback;
85 subtest 'basket_group' => sub {
88 $schema->storage->txn_begin;
89 my $b = $builder->build_object(
91 class => 'Koha::Acquisition::Baskets',
92 value => { basketgroupid => undef }, # not linked to a basketgroupid
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');
100 $b = $builder->build_object(
102 class => 'Koha::Acquisition::Baskets',
103 # Will be linked to a basket group by TestBuilder
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');
111 $schema->storage->txn_rollback;
114 subtest 'creator() tests' => sub {
118 $schema->storage->txn_begin;
120 my $basket = $builder->build_object(
122 class => 'Koha::Acquisition::Baskets',
123 value => { authorisedby => undef }
127 is( $basket->creator, undef, 'Undef is handled as expected' );
129 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
130 $basket->authorisedby( $patron->borrowernumber )->store->discard_changes;
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' );
139 is( $basket->creator, undef, 'Undef is handled as expected' );
141 $schema->storage->txn_rollback;
144 subtest 'to_api() tests' => sub {
148 $schema->storage->txn_begin;
150 my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
151 my $basket = $builder->build_object(
153 class => 'Koha::Acquisition::Baskets',
160 my $closed = $basket->to_api->{closed};
161 ok( defined $closed, 'closed is defined' );
162 ok( !$closed, 'closedate is undef, closed evaluates to false' );
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' );
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 );
174 $schema->storage->txn_rollback;
177 subtest 'estimated_delivery_date' => sub {
180 $schema->storage->txn_begin;
181 my $bookseller = $builder->build_object(
183 class => 'Koha::Acquisition::Booksellers',
185 deliverytime => undef, # Does not have a delivery time defined
190 my $basket = $builder->build_object(
192 class => 'Koha::Acquisition::Baskets',
194 booksellerid => $bookseller->id,
195 closedate => undef, # Still open
200 my $now = dt_from_string;
201 is( $basket->estimated_delivery_date,
202 undef, 'return undef if closedate and deliverytime are not defined' );
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' );
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)' );
214 $bookseller->deliverytime(2)->store; # 2 delivery days
215 $basket->closedate( $now->clone->subtract( days => 1 ) )->store; #Closing the basket
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'
222 $schema->storage->txn_rollback;
225 subtest 'late_since_days' => sub {
228 $schema->storage->txn_begin;
229 my $basket = $builder->build_object(
231 class => 'Koha::Acquisition::Baskets',
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');
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' );
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' );
245 $schema->storage->txn_rollback;
248 subtest 'authorizer' => sub {
251 $schema->storage->txn_begin;
252 my $basket = $builder->build_object(
254 class => 'Koha::Acquisition::Baskets',
255 value => { authorisedby => undef },
259 my $basket_creator = $builder->build_object( { class => 'Koha::Patrons' } );
261 is( $basket->authorizer, undef,
262 'authorisedby is null, ->authorized should return undef' );
264 $basket->authorisedby( $basket_creator->borrowernumber )->store;
266 is( ref( $basket->authorizer ),
267 'Koha::Patron', '->authorized should return a Koha::Patron object' );
269 $basket->authorizer->borrowernumber,
270 $basket_creator->borrowernumber,
271 '->authorized should return the correct creator'
274 $schema->storage->txn_rollback;
277 subtest 'orders' => sub {
281 $schema->storage->txn_begin;
283 my $basket = $builder->build_object(
285 class => 'Koha::Acquisition::Baskets'
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' );
296 push @actual_orders, $builder->build_object({ class => 'Koha::Acquisition::Orders', value => { basketno => $basket->id } });
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' );
303 $schema->storage->txn_rollback;
306 subtest 'is_closed() tests' => sub {
310 $schema->storage->txn_begin;
312 my $open_basket = $builder->build_object(
314 class => 'Koha::Acquisition::Baskets',
321 my $closed_basket = $builder->build_object(
323 class => 'Koha::Acquisition::Baskets',
325 closedate => \'NOW()'
330 ok( $closed_basket->is_closed, 'Closed basket is tested as closed' );
331 ok( !$open_basket->is_closed, 'Open basket is tested as open' );
333 $schema->storage->txn_rollback;
336 subtest 'close() tests' => sub {
340 # Turn on acquisitions logging and ensure the logs are empty
341 t::lib::Mocks::mock_preference('AcquisitionLog', 1);
342 Koha::ActionLogs->delete;
344 $schema->storage->txn_begin;
346 # Create an open basket
347 my $basket = $builder->build_object(
349 class => 'Koha::Acquisition::Baskets',
356 for my $status ( qw( new ordered partial complete cancelled ) ) {
357 $builder->build_object(
359 class => 'Koha::Acquisition::Orders',
361 basketno => $basket->id,
362 orderstatus => $status
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' );
376 'Koha::Exceptions::Acquisition::Basket::AlreadyClosed',
377 'Trying to close an already closed basket throws an exception';
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');
382 $schema->storage->txn_rollback;