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 => 9;
23 use t::lib::TestBuilder;
28 use Koha::DateUtils qw(dt_from_string);
30 use_ok('Koha::Acquisition::Basket');
31 use_ok('Koha::Acquisition::Baskets');
33 my $schema = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'create_items + effective_create_items tests' => sub {
40 $schema->storage->txn_begin;
42 my $basket = $builder->build_object(
44 class => 'Koha::Acquisition::Baskets',
45 value => { create_items => undef }
48 my $created_basketno = C4::Acquisition::NewBasket(
49 $basket->booksellerid, $basket->authorisedby,
50 $basket->basketname, $basket->note,
51 $basket->booksellernote, $basket->contractnumber,
52 $basket->deliveryplace, $basket->billingplace,
53 $basket->is_standing, $basket->create_items
55 my $created_basket = Koha::Acquisition::Baskets->find($created_basketno);
56 is( $created_basket->basketno, $created_basketno,
57 "Basket created by NewBasket matches db basket" );
58 is( $basket->create_items, undef, "Create items value can be null" );
60 t::lib::Mocks::mock_preference( 'AcqCreateItem', 'cataloguing' );
61 is( $basket->effective_create_items,
63 "We use AcqCreateItem if basket create items is not set" );
64 C4::Acquisition::ModBasketHeader(
65 $basket->basketno, $basket->basketname,
66 $basket->note, $basket->booksellernote,
67 $basket->contractnumber, $basket->booksellerid,
68 $basket->deliveryplace, $basket->billingplace,
69 $basket->is_standing, "ordering"
71 my $retrieved_basket = Koha::Acquisition::Baskets->find( $basket->basketno );
72 $basket->create_items("ordering");
73 is( $retrieved_basket->create_items, "ordering", "Should be able to set with ModBasketHeader" );
74 is( $basket->create_items, "ordering", "Should be able to set with object methods" );
75 is_deeply( $retrieved_basket->unblessed,
76 $basket->unblessed, "Correct basket found and updated" );
77 is( $retrieved_basket->effective_create_items,
78 "ordering", "We use basket create items if it is set" );
80 $schema->storage->txn_rollback;
83 subtest 'basket_group' => sub {
86 $schema->storage->txn_begin;
87 my $b = $builder->build_object(
89 class => 'Koha::Acquisition::Baskets',
90 value => { basketgroupid => undef }, # not linked to a basketgroupid
94 my $basket = Koha::Acquisition::Baskets->find( $b->basketno );
95 is( $basket->basket_group, undef,
96 '->basket_group should return undef if not linked to a basket group');
98 $b = $builder->build_object(
100 class => 'Koha::Acquisition::Baskets',
101 # Will be linked to a basket group by TestBuilder
105 $basket = Koha::Acquisition::Baskets->find( $b->basketno );
106 is( ref( $basket->basket_group ), 'Koha::Acquisition::BasketGroup',
107 '->basket_group should return a Koha::Acquisition::BasketGroup object if linked to a basket group');
109 $schema->storage->txn_rollback;
112 subtest 'creator() tests' => sub {
116 $schema->storage->txn_begin;
118 my $basket = $builder->build_object(
120 class => 'Koha::Acquisition::Baskets',
121 value => { authorisedby => undef }
125 is( $basket->creator, undef, 'Undef is handled as expected' );
127 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
128 $basket->authorisedby( $patron->borrowernumber )->store->discard_changes;
130 my $creator = $basket->creator;
131 is( ref($creator), 'Koha::Patron', 'Return type is correct' );
132 is( $creator->borrowernumber, $patron->borrowernumber, 'Returned object is the right one' );
137 is( $basket->creator, undef, 'Undef is handled as expected' );
139 $schema->storage->txn_rollback;
142 subtest 'to_api() tests' => sub {
146 $schema->storage->txn_begin;
148 my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
149 my $basket = $builder->build_object(
151 class => 'Koha::Acquisition::Baskets',
158 my $closed = $basket->to_api->{closed};
159 ok( defined $closed, 'closed is defined' );
160 ok( !$closed, 'closedate is undef, closed evaluates to false' );
162 $basket->closedate( dt_from_string )->store->discard_changes;
163 $closed = $basket->to_api->{closed};
164 ok( defined $closed, 'closed is defined' );
165 ok( $closed, 'closedate is defined, closed evaluates to true' );
167 $basket->booksellerid( $vendor->id )->store->discard_changes;
168 my $basket_json = $basket->to_api({ embed => { bookseller => {} } });
169 ok( exists $basket_json->{bookseller} );
170 is_deeply( $basket_json->{bookseller}, $vendor->to_api );
172 $schema->storage->txn_rollback;
175 subtest 'estimated_delivery_date' => sub {
178 $schema->storage->txn_begin;
179 my $bookseller = $builder->build_object(
181 class => 'Koha::Acquisition::Booksellers',
183 deliverytime => undef, # Does not have a delivery time defined
188 my $basket = $builder->build_object(
190 class => 'Koha::Acquisition::Baskets',
192 booksellerid => $bookseller->id,
193 closedate => undef, # Still open
198 my $now = dt_from_string;
199 is( $basket->estimated_delivery_date,
200 undef, 'return undef if closedate and deliverytime are not defined' );
202 $basket->closedate( $now->clone->subtract( days => 1 ) )
203 ->store; #Closing the basket
204 is( $basket->estimated_delivery_date,
205 undef, 'return undef if deliverytime is not defined' );
207 $basket->closedate(undef)->store; #Reopening
208 $bookseller->deliverytime(2)->store; # 2 delivery days
209 is( $basket->estimated_delivery_date,
210 undef, 'return undef if closedate is not defined (basket stil open)' );
212 $bookseller->deliverytime(2)->store; # 2 delivery days
213 $basket->closedate( $now->clone->subtract( days => 1 ) )->store; #Closing the basket
215 $basket->get_from_storage->estimated_delivery_date,
216 $now->clone->add( days => 1 )->truncate( to => 'day' ),
217 'Estimated delivery date should be tomorrow if basket closed on yesterday and delivery takes 2 days'
220 $schema->storage->txn_rollback;
223 subtest 'late_since_days' => sub {
226 $schema->storage->txn_begin;
227 my $basket = $builder->build_object(
229 class => 'Koha::Acquisition::Baskets',
233 my $now = dt_from_string;
234 $basket->closedate(undef)->store; # Basket is open
235 is( $basket->late_since_days, undef, 'return undef if basket is still open');
237 $basket->closedate( $now )->store; #Closing the basket today
238 is( $basket->late_since_days, 0, 'return 0 if basket has been closed on today' );
240 $basket->closedate( $now->clone->subtract( days => 2 ) )->store;
241 is( $basket->late_since_days, 2, 'return 2 if basket has been closed 2 days ago' );
243 $schema->storage->txn_rollback;
246 subtest 'authorizer' => sub {
249 $schema->storage->txn_begin;
250 my $basket = $builder->build_object(
252 class => 'Koha::Acquisition::Baskets',
253 value => { authorisedby => undef },
257 my $basket_creator = $builder->build_object( { class => 'Koha::Patrons' } );
259 is( $basket->authorizer, undef,
260 'authorisedby is null, ->authorized should return undef' );
262 $basket->authorisedby( $basket_creator->borrowernumber )->store;
264 is( ref( $basket->authorizer ),
265 'Koha::Patron', '->authorized should return a Koha::Patron object' );
267 $basket->authorizer->borrowernumber,
268 $basket_creator->borrowernumber,
269 '->authorized should return the correct creator'
272 $schema->storage->txn_rollback;