Bug 26384: Fix executable flags
[koha.git] / 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 => 9;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use C4::Acquisition;
27 use Koha::Database;
28 use Koha::DateUtils qw(dt_from_string);
29
30 use_ok('Koha::Acquisition::Basket');
31 use_ok('Koha::Acquisition::Baskets');
32
33 my $schema  = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'create_items + effective_create_items tests' => sub {
37
38     plan tests => 7;
39
40     $schema->storage->txn_begin;
41
42     my $basket = $builder->build_object(
43         {
44             class => 'Koha::Acquisition::Baskets',
45             value => { create_items => undef }
46         }
47     );
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
54     );
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" );
59
60     t::lib::Mocks::mock_preference( 'AcqCreateItem', 'cataloguing' );
61     is( $basket->effective_create_items,
62         "cataloguing",
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"
70     );
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" );
79
80     $schema->storage->txn_rollback;
81 };
82
83 subtest 'basket_group' => sub {
84     plan tests => 2;
85
86     $schema->storage->txn_begin;
87     my $b = $builder->build_object(
88         {
89             class => 'Koha::Acquisition::Baskets',
90             value => { basketgroupid => undef }, # not linked to a basketgroupid
91         }
92     );
93
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');
97
98     $b = $builder->build_object(
99         {
100             class => 'Koha::Acquisition::Baskets',
101             # Will be linked to a basket group by TestBuilder
102         }
103     );
104
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');
108
109     $schema->storage->txn_rollback;
110 };
111
112 subtest 'creator() tests' => sub {
113
114     plan tests => 4;
115
116     $schema->storage->txn_begin;
117
118     my $basket = $builder->build_object(
119         {
120             class => 'Koha::Acquisition::Baskets',
121             value => { authorisedby => undef }
122         }
123     );
124
125     is( $basket->creator, undef, 'Undef is handled as expected' );
126
127     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
128     $basket->authorisedby( $patron->borrowernumber )->store->discard_changes;
129
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' );
133
134     # Delete the patron
135     $patron->delete;
136
137     is( $basket->creator, undef, 'Undef is handled as expected' );
138
139     $schema->storage->txn_rollback;
140 };
141
142 subtest 'to_api() tests' => sub {
143
144     plan tests => 6;
145
146     $schema->storage->txn_begin;
147
148     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
149     my $basket = $builder->build_object(
150         {
151             class => 'Koha::Acquisition::Baskets',
152             value => {
153                 closedate => undef
154             }
155         }
156     );
157
158     my $closed = $basket->to_api->{closed};
159     ok( defined $closed, 'closed is defined' );
160     ok( !$closed, 'closedate is undef, closed evaluates to false' );
161
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' );
166
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 );
171
172     $schema->storage->txn_rollback;
173 };
174
175 subtest 'estimated_delivery_date' => sub {
176     plan tests => 4;
177
178     $schema->storage->txn_begin;
179     my $bookseller = $builder->build_object(
180         {
181             class => 'Koha::Acquisition::Booksellers',
182             value => {
183                 deliverytime => undef,   # Does not have a delivery time defined
184             }
185         }
186     );
187
188     my $basket = $builder->build_object(
189         {
190             class => 'Koha::Acquisition::Baskets',
191             value => {
192                 booksellerid => $bookseller->id,
193                 closedate    => undef,             # Still open
194             }
195         }
196     );
197
198     my $now = dt_from_string;
199     is( $basket->estimated_delivery_date,
200         undef, 'return undef if closedate and deliverytime are not defined' );
201
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' );
206
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)' );
211
212     $bookseller->deliverytime(2)->store;           # 2 delivery days
213     $basket->closedate( $now->clone->subtract( days => 1 ) )->store; #Closing the basket
214     is(
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'
218     );
219
220     $schema->storage->txn_rollback;
221 };
222
223 subtest 'late_since_days' => sub {
224     plan tests => 3;
225
226     $schema->storage->txn_begin;
227     my $basket  = $builder->build_object(
228         {
229             class => 'Koha::Acquisition::Baskets',
230         }
231     );
232
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');
236
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' );
239
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' );
242
243     $schema->storage->txn_rollback;
244 };
245
246 subtest 'authorizer' => sub {
247     plan tests => 3;
248
249     $schema->storage->txn_begin;
250     my $basket = $builder->build_object(
251         {
252             class => 'Koha::Acquisition::Baskets',
253             value => { authorisedby => undef },
254         }
255     );
256
257     my $basket_creator = $builder->build_object( { class => 'Koha::Patrons' } );
258
259     is( $basket->authorizer, undef,
260         'authorisedby is null, ->authorized should return undef' );
261
262     $basket->authorisedby( $basket_creator->borrowernumber )->store;
263
264     is( ref( $basket->authorizer ),
265         'Koha::Patron', '->authorized should return a Koha::Patron object' );
266     is(
267         $basket->authorizer->borrowernumber,
268         $basket_creator->borrowernumber,
269         '->authorized should return the correct creator'
270     );
271
272     $schema->storage->txn_rollback;
273 };