Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[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 => 7;
23
24 use Test::Exception;
25 use Test::MockModule;
26
27 use MARC::Field;
28 use Mojo::JSON qw(encode_json);
29
30 use C4::Items;
31 use C4::Biblio qw( AddBiblio ModBiblio GetMarcFromKohaField );
32 use C4::Reserves qw( AddReserve );
33
34 use Koha::DateUtils qw( dt_from_string output_pref );
35 use Koha::Biblios;
36 use Koha::Patrons;
37 use Koha::Subscriptions;
38 use t::lib::TestBuilder;
39 use t::lib::Mocks;
40
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
43
44 my $dbh     = C4::Context->dbh;
45
46 my $builder = t::lib::TestBuilder->new;
47 my $patron = $builder->build( { source => 'Borrower' } );
48 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
49
50 my $biblio = Koha::Biblio->new()->store();
51
52 my $biblioitem = $schema->resultset('Biblioitem')->new(
53     {
54         biblionumber => $biblio->id
55     }
56 )->insert();
57
58 subtest 'store' => sub {
59     plan tests => 1;
60     is(
61         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
62         output_pref(
63             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
64         ),
65         "datecreated must be set to today if not passed to the constructor"
66     );
67 };
68
69 subtest 'holds + current_holds' => sub {
70     plan tests => 5;
71     C4::Reserves::AddReserve(
72         {
73             branchcode     => $patron->branchcode,
74             borrowernumber => $patron->borrowernumber,
75             biblionumber   => $biblio->biblionumber,
76         }
77     );
78     my $holds = $biblio->holds;
79     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
80     is( $holds->count, 1, '->holds should only return 1 hold' );
81     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
82     $holds->delete;
83
84     # Add a hold in the future
85     C4::Reserves::AddReserve(
86         {
87             branchcode       => $patron->branchcode,
88             borrowernumber   => $patron->borrowernumber,
89             biblionumber     => $biblio->biblionumber,
90             reservation_date => dt_from_string->add( days => 2 ),
91         }
92     );
93     $holds = $biblio->holds;
94     is( $holds->count, 1, '->holds should return future holds' );
95     $holds = $biblio->current_holds;
96     is( $holds->count, 0, '->current_holds should not return future holds' );
97     $holds->delete;
98
99 };
100
101 subtest 'waiting_or_in_transit' => sub {
102     plan tests => 4;
103     my $item = $builder->build_sample_item;
104     my $reserve = $builder->build({
105         source => 'Reserve',
106         value => {
107             biblionumber => $item->biblionumber,
108             found => undef
109         }
110     });
111
112     $reserve = Koha::Holds->find($reserve->{reserve_id});
113     $biblio = $item->biblio;
114
115     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
116
117     $reserve->found('W')->store;
118     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
119
120     $reserve->found('T')->store;
121     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
122
123     my $transfer = $builder->build({
124         source => 'Branchtransfer',
125         value => {
126             itemnumber => $item->itemnumber,
127             datearrived => undef,
128             datecancelled => undef,
129         }
130     });
131     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
132     $reserve->found(undef)->store;
133     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
134 };
135
136 subtest 'can_be_transferred' => sub {
137     plan tests => 8;
138
139     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
140     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
141
142     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
143     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
144     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
145     my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
146     my $item = $builder->build_sample_item(
147         {
148             biblionumber => $biblio->biblionumber,
149             library      => $library1->branchcode
150         }
151     );
152
153     is(Koha::Item::Transfer::Limits->search({
154         fromBranch => $library1->branchcode,
155         toBranch => $library2->branchcode,
156     })->count, 0, 'There are no transfer limits between libraries.');
157     ok($biblio->can_be_transferred({ to => $library2 }),
158         'Some items of this biblio can be transferred between libraries.');
159
160     my $limit = Koha::Item::Transfer::Limit->new({
161         fromBranch => $library1->branchcode,
162         toBranch => $library2->branchcode,
163         itemtype => $item->effective_itemtype,
164     })->store;
165     is(Koha::Item::Transfer::Limits->search({
166         fromBranch => $library1->branchcode,
167         toBranch => $library2->branchcode,
168     })->count, 1, 'Given we have added a transfer limit that applies for all '
169         .'of this biblio\s items,');
170     is($biblio->can_be_transferred({ to => $library2 }), 0,
171         'None of the items of biblio can no longer be transferred between '
172         .'libraries.');
173     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
174          'We get the same result also if we pass the from-library parameter.');
175     $item->holdingbranch($library2->branchcode)->store;
176     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
177          .'items is already located at to-library, then the transfer is possible.');
178     $item->holdingbranch($library1->branchcode)->store;
179
180     my $item2 = $builder->build_sample_item(
181         {
182             biblionumber  => $biblio->biblionumber,
183             homebranch    => $library1->branchcode,
184             holdingbranch => $library3->branchcode,
185         }
186     );
187     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
188         .'another item that should have no transfer limits applying on, then '
189         .'the transfer is possible.');
190     $item2->holdingbranch($library1->branchcode)->store;
191     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
192         .' of the biblio are from same, transfer limited library, then transfer'
193         .' is not possible.');
194 };
195
196 subtest 'custom_cover_image_url' => sub {
197     plan tests => 6;
198
199     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
200
201     my $isbn       = '0553573403 | 9780553573404 (pbk.).png';
202     my $issn       = 'my_issn';
203     my $cf_value   = 'from_control_field';
204     my $biblio     = $builder->build_sample_biblio;
205     my $marc_record = $biblio->metadata->record;
206     my ( $isbn_tag, $isbn_subfield ) =  GetMarcFromKohaField( 'biblioitems.isbn' );
207     my ( $issn_tag, $issn_subfield ) =  GetMarcFromKohaField( 'biblioitems.issn' );
208     $marc_record->append_fields(
209         MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn ),
210         MARC::Field->new( $issn_tag, '', '', $issn_subfield => $issn ),
211     );
212     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
213
214     is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
215
216     my $marc_024a = '710347104926';
217     $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
218     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
219
220     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
221     is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/$marc_024a.png" );
222
223     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
224     my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
225     is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
226
227     $biblio->biblioitem->isbn('')->store;
228     is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" );
229
230     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{001}.png' );
231     is( $biblio->custom_cover_image_url, undef, 'Record does not have 001' );
232     $marc_record->append_fields(MARC::Field->new('001', $cf_value));
233     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
234     $biblio = Koha::Biblios->find( $biblio->biblionumber );
235     is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/$cf_value.png", 'URL generated using 001' );
236 };
237
238 $schema->storage->txn_rollback;
239
240 subtest 'pickup_locations() tests' => sub {
241
242     plan tests => 3;
243
244     $schema->storage->txn_begin;
245
246     # Build 8 libraries
247     my $l_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
248     my $l_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
249     my $l_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
250     my $l_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
251     my $l_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
252     my $l_6 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
253     my $l_7 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
254     my $l_8 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
255
256     # Mock Koha::Item->pickup_locations so we have control on the output
257     # The $switch variable controls the output.
258     my $switch  = 0;
259     my $queries = [
260         { branchcode => [ $l_1->branchcode, $l_2->branchcode ] },
261         { branchcode => [ $l_3->branchcode, $l_4->branchcode ] },
262         { branchcode => [ $l_5->branchcode, $l_6->branchcode ] },
263         { branchcode => [ $l_7->branchcode, $l_8->branchcode ] }
264     ];
265
266     my $mock_item = Test::MockModule->new('Koha::Item');
267     $mock_item->mock(
268         'pickup_locations',
269         sub {
270             my $query = $queries->[$switch];
271             $switch++;
272             return Koha::Libraries->search($query);
273         }
274     );
275
276     # Two biblios
277     my $biblio_1 = $builder->build_sample_biblio;
278     my $biblio_2 = $builder->build_sample_biblio;
279
280     # Two items each
281     my $item_1_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
282     my $item_1_2 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
283     my $item_2_1 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
284     my $item_2_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
285
286     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
287
288     my $biblios = Koha::Biblios->search(
289         {
290             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
291         }
292     );
293
294     throws_ok
295       { $biblios->pickup_locations }
296       'Koha::Exceptions::MissingParameter',
297       'Exception thrown on missing parameter';
298
299     is( $@->parameter, 'patron', 'Exception param correctly set' );
300
301     my $library_ids = [
302         Koha::Libraries->search(
303             {
304                 branchcode => [
305                     $l_1->branchcode, $l_2->branchcode, $l_3->branchcode,
306                     $l_4->branchcode, $l_5->branchcode, $l_6->branchcode,
307                     $l_7->branchcode, $l_8->branchcode
308                 ]
309             },
310             { order_by => ['branchname'] }
311         )->_resultset->get_column('branchcode')->all
312     ];
313
314     my $pickup_locations_ids = [
315         $biblios->pickup_locations({ patron => $patron })->_resultset->get_column('branchcode')->all
316     ];
317
318     is_deeply(
319         $library_ids,
320         $pickup_locations_ids,
321         'The addition of all biblios+items pickup locations is returned'
322     );
323
324     $schema->storage->txn_rollback;
325 };
326
327 subtest 'api_query_fixer() tests' => sub {
328
329     plan tests => 2;
330
331     my $rs = Koha::Biblios->new;
332
333     subtest 'JSON query tests' => sub {
334
335         plan tests => 6;
336
337         my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
338         is(
339             $rs->api_query_fixer($query), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
340             'Query adapted for biblioitem attributes'
341         );
342         $query = encode_json( { author => { "-like" => "\%asd" } } );
343         is( $rs->api_query_fixer($query), $query, 'Query unchanged for non-biblioitem attributes' );
344         $query = encode_json( { author => { "-like" => "an age_restriction" } } );
345         is( $rs->api_query_fixer($query), $query, 'Query unchanged because quotes are expected for the match' );
346
347         $query = encode_json( { "biblio.collection_issn" => { "-like" => "\%asd" } } );
348         is(
349             $rs->api_query_fixer( $query, 'biblio' ), '{"biblio.biblioitem.collection_issn":{"-like":"%asd"}}',
350             'Query adapted for biblioitem attributes, context is kept, match using context'
351         );
352         $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
353         is( $rs->api_query_fixer( $query, 'biblio' ), $query, 'Query unchanged because no match for context' );
354         $query = encode_json( { author => { "-like" => "a biblio.age_restriction" } } );
355         is(
356             $rs->api_query_fixer( $query, 'biblio' ), $query,
357             'Query unchanged because quotes are expected for the match'
358         );
359     };
360
361     subtest 'order_by tests' => sub {
362
363         plan tests => 6;
364
365         my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
366         is(
367             $rs->api_query_fixer( $query, undef, 1 ), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
368             'Query adapted for biblioitem attributes'
369         );
370         $query = encode_json( { author => { "-like" => "\%asd" } } );
371         is( $rs->api_query_fixer( $query, undef, 1 ), $query, 'Query unchanged for non-biblioitem attributes' );
372         $query = encode_json( { author => { "-like" => "an age_restriction" } } );
373         is(
374             $rs->api_query_fixer( $query, undef, 1 ), '{"author":{"-like":"an biblioitem.age_restriction"}}',
375             'Query changed because quotes are not expected for the match'
376         );
377
378         $query = encode_json( { "banana.collection_issn" => { "-like" => "\%asd" } } );
379         is(
380             $rs->api_query_fixer( $query, 'banana', 1 ), '{"banana.biblioitem.collection_issn":{"-like":"%asd"}}',
381             'Query adapted for biblioitem attributes'
382         );
383         $query = encode_json( { author => { "-like" => "\%asd" } } );
384         is( $rs->api_query_fixer( $query, 'banana', 1 ), $query, 'Query unchanged for non-biblioitem attributes' );
385         $query = encode_json( { author => { "-like" => "a banana.age_restriction" } } );
386         is(
387             $rs->api_query_fixer( $query, 'banana', 1 ), '{"author":{"-like":"a banana.biblioitem.age_restriction"}}',
388             'Query changed because quotes are not expected for the match'
389         );
390     }
391 };