Bug 17600: Standardize our EXPORT_OK
[koha.git] / t / db_dependent / Koha / Biblios.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 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 Test::Exception;
25 use Test::MockModule;
26
27 use MARC::Field;
28
29 use C4::Items;
30 use C4::Biblio qw( AddBiblio ModBiblio );
31 use C4::Reserves qw( AddReserve );
32
33 use Koha::DateUtils qw( dt_from_string output_pref );
34 use Koha::Biblios;
35 use Koha::Patrons;
36 use Koha::Subscriptions;
37 use t::lib::TestBuilder;
38 use t::lib::Mocks;
39
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42
43 my $dbh     = C4::Context->dbh;
44
45 my $builder = t::lib::TestBuilder->new;
46 my $patron = $builder->build( { source => 'Borrower' } );
47 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
48
49 my $biblio = Koha::Biblio->new()->store();
50
51 my $biblioitem = $schema->resultset('Biblioitem')->new(
52     {
53         biblionumber => $biblio->id
54     }
55 )->insert();
56
57 subtest 'store' => sub {
58     plan tests => 1;
59     is(
60         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
61         output_pref(
62             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
63         ),
64         "datecreated must be set to today if not passed to the constructor"
65     );
66 };
67
68 subtest 'holds + current_holds' => sub {
69     plan tests => 5;
70     C4::Reserves::AddReserve(
71         {
72             branchcode     => $patron->branchcode,
73             borrowernumber => $patron->borrowernumber,
74             biblionumber   => $biblio->biblionumber,
75         }
76     );
77     my $holds = $biblio->holds;
78     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
79     is( $holds->count, 1, '->holds should only return 1 hold' );
80     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
81     $holds->delete;
82
83     # Add a hold in the future
84     C4::Reserves::AddReserve(
85         {
86             branchcode       => $patron->branchcode,
87             borrowernumber   => $patron->borrowernumber,
88             biblionumber     => $biblio->biblionumber,
89             reservation_date => dt_from_string->add( days => 2 ),
90         }
91     );
92     $holds = $biblio->holds;
93     is( $holds->count, 1, '->holds should return future holds' );
94     $holds = $biblio->current_holds;
95     is( $holds->count, 0, '->current_holds should not return future holds' );
96     $holds->delete;
97
98 };
99
100 subtest 'waiting_or_in_transit' => sub {
101     plan tests => 4;
102     my $item = $builder->build_sample_item;
103     my $reserve = $builder->build({
104         source => 'Reserve',
105         value => {
106             biblionumber => $item->biblionumber,
107             found => undef
108         }
109     });
110
111     $reserve = Koha::Holds->find($reserve->{reserve_id});
112     $biblio = $item->biblio;
113
114     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
115
116     $reserve->found('W')->store;
117     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
118
119     $reserve->found('T')->store;
120     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
121
122     my $transfer = $builder->build({
123         source => 'Branchtransfer',
124         value => {
125             itemnumber => $item->itemnumber,
126             datearrived => undef,
127             datecancelled => undef,
128         }
129     });
130     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
131     $reserve->found(undef)->store;
132     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
133 };
134
135 subtest 'can_be_transferred' => sub {
136     plan tests => 8;
137
138     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
139     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
140
141     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
142     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
143     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
144     my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
145     my $item = $builder->build_sample_item(
146         {
147             biblionumber => $biblio->biblionumber,
148             library      => $library1->branchcode
149         }
150     );
151
152     is(Koha::Item::Transfer::Limits->search({
153         fromBranch => $library1->branchcode,
154         toBranch => $library2->branchcode,
155     })->count, 0, 'There are no transfer limits between libraries.');
156     ok($biblio->can_be_transferred({ to => $library2 }),
157         'Some items of this biblio can be transferred between libraries.');
158
159     my $limit = Koha::Item::Transfer::Limit->new({
160         fromBranch => $library1->branchcode,
161         toBranch => $library2->branchcode,
162         itemtype => $item->effective_itemtype,
163     })->store;
164     is(Koha::Item::Transfer::Limits->search({
165         fromBranch => $library1->branchcode,
166         toBranch => $library2->branchcode,
167     })->count, 1, 'Given we have added a transfer limit that applies for all '
168         .'of this biblio\s items,');
169     is($biblio->can_be_transferred({ to => $library2 }), 0,
170         'None of the items of biblio can no longer be transferred between '
171         .'libraries.');
172     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
173          'We get the same result also if we pass the from-library parameter.');
174     $item->holdingbranch($library2->branchcode)->store;
175     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
176          .'items is already located at to-library, then the transfer is possible.');
177     $item->holdingbranch($library1->branchcode)->store;
178
179     my $item2 = $builder->build_sample_item(
180         {
181             biblionumber  => $biblio->biblionumber,
182             homebranch    => $library1->branchcode,
183             holdingbranch => $library3->branchcode,
184         }
185     );
186     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
187         .'another item that should have no transfer limits applying on, then '
188         .'the transfer is possible.');
189     $item2->holdingbranch($library1->branchcode)->store;
190     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
191         .' of the biblio are from same, transfer limited library, then transfer'
192         .' is not possible.');
193 };
194
195 subtest 'custom_cover_image_url' => sub {
196     plan tests => 4;
197
198     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
199
200     my $isbn       = '0553573403 | 9780553573404 (pbk.).png';
201     my $issn       = 'my_issn';
202     my $marc_record = MARC::Record->new;
203     my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
204
205     my $biblio = Koha::Biblios->find( $biblionumber );
206     my $biblioitem = $biblio->biblioitem->set(
207         { isbn => $isbn, issn => $issn });
208     is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
209
210     my $marc_024a = '710347104926';
211     $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
212     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
213
214     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
215     is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
216
217     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
218     my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
219     is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
220
221     $biblio->biblioitem->isbn('')->store;
222     is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" );
223
224 };
225
226 $schema->storage->txn_rollback;
227
228 subtest 'pickup_locations() tests' => sub {
229
230     plan tests => 1;
231
232     $schema->storage->txn_begin;
233
234     # Build 8 libraries
235     my $l_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
236     my $l_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
237     my $l_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
238     my $l_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
239     my $l_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
240     my $l_6 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
241     my $l_7 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
242     my $l_8 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
243
244     # Mock Koha::Item->pickup_locations so we have control on the output
245     # The $switch variable controls the output.
246     my $switch  = 0;
247     my $queries = [
248         { branchcode => [ $l_1->branchcode, $l_2->branchcode ] },
249         { branchcode => [ $l_3->branchcode, $l_4->branchcode ] },
250         { branchcode => [ $l_5->branchcode, $l_6->branchcode ] },
251         { branchcode => [ $l_7->branchcode, $l_8->branchcode ] }
252     ];
253
254     my $mock_item = Test::MockModule->new('Koha::Item');
255     $mock_item->mock(
256         'pickup_locations',
257         sub {
258             my $query = $queries->[$switch];
259             $switch++;
260             return Koha::Libraries->search($query);
261         }
262     );
263
264     # Two biblios
265     my $biblio_1 = $builder->build_sample_biblio;
266     my $biblio_2 = $builder->build_sample_biblio;
267
268     # Two items each
269     my $item_1_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
270     my $item_1_2 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
271     my $item_2_1 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
272     my $item_2_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
273
274     my $biblios = Koha::Biblios->search(
275         {
276             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
277         }
278     );
279
280     my $library_ids = [
281         Koha::Libraries->search(
282             {
283                 branchcode => [
284                     $l_1->branchcode, $l_2->branchcode, $l_3->branchcode,
285                     $l_4->branchcode, $l_5->branchcode, $l_6->branchcode,
286                     $l_7->branchcode, $l_8->branchcode
287                 ]
288             },
289             { order_by => ['branchname'] }
290         )->_resultset->get_column('branchcode')->all
291     ];
292
293     my $pickup_locations_ids = [
294         $biblios->pickup_locations->_resultset->get_column('branchcode')->all
295     ];
296
297     is_deeply(
298         $library_ids,
299         $pickup_locations_ids,
300         'The addition of all biblios+items pickup locations is returned'
301     );
302
303     $schema->storage->txn_rollback;
304 };