3 # Copyright 2016 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 => 7;
28 use Mojo::JSON qw(encode_json);
31 use C4::Biblio qw( AddBiblio ModBiblio GetMarcFromKohaField );
32 use C4::Reserves qw( AddReserve );
34 use Koha::DateUtils qw( dt_from_string output_pref );
37 use Koha::Subscriptions;
38 use t::lib::TestBuilder;
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
44 my $dbh = C4::Context->dbh;
46 my $builder = t::lib::TestBuilder->new;
47 my $patron = $builder->build( { source => 'Borrower' } );
48 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
50 my $biblio = Koha::Biblio->new()->store();
52 my $biblioitem = $schema->resultset('Biblioitem')->new(
54 biblionumber => $biblio->id
58 subtest 'store' => sub {
61 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
63 { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
65 "datecreated must be set to today if not passed to the constructor"
69 subtest 'holds + current_holds' => sub {
71 C4::Reserves::AddReserve(
73 branchcode => $patron->branchcode,
74 borrowernumber => $patron->borrowernumber,
75 biblionumber => $biblio->biblionumber,
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' );
84 # Add a hold in the future
85 C4::Reserves::AddReserve(
87 branchcode => $patron->branchcode,
88 borrowernumber => $patron->borrowernumber,
89 biblionumber => $biblio->biblionumber,
90 reservation_date => dt_from_string->add( days => 2 ),
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' );
101 subtest 'waiting_or_in_transit' => sub {
103 my $item = $builder->build_sample_item;
104 my $reserve = $builder->build({
107 biblionumber => $item->biblionumber,
112 $reserve = Koha::Holds->find($reserve->{reserve_id});
113 $biblio = $item->biblio;
115 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
117 $reserve->found('W')->store;
118 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
120 $reserve->found('T')->store;
121 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
123 my $transfer = $builder->build({
124 source => 'Branchtransfer',
126 itemnumber => $item->itemnumber,
127 datearrived => undef,
128 datecancelled => undef,
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');
136 subtest 'can_be_transferred' => sub {
139 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
140 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
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(
148 biblionumber => $biblio->biblionumber,
149 library => $library1->branchcode
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.');
160 my $limit = Koha::Item::Transfer::Limit->new({
161 fromBranch => $library1->branchcode,
162 toBranch => $library2->branchcode,
163 itemtype => $item->effective_itemtype,
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 '
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;
180 my $item2 = $builder->build_sample_item(
182 biblionumber => $biblio->biblionumber,
183 homebranch => $library1->branchcode,
184 holdingbranch => $library3->branchcode,
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.');
196 subtest 'custom_cover_image_url' => sub {
199 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
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 ),
212 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
214 is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
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 );
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" );
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" );
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" );
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' );
238 $schema->storage->txn_rollback;
240 subtest 'pickup_locations() tests' => sub {
244 $schema->storage->txn_begin;
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 } });
256 # Mock Koha::Item->pickup_locations so we have control on the output
257 # The $switch variable controls the output.
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 ] }
266 my $mock_item = Test::MockModule->new('Koha::Item');
270 my $query = $queries->[$switch];
272 return Koha::Libraries->search($query);
277 my $biblio_1 = $builder->build_sample_biblio;
278 my $biblio_2 = $builder->build_sample_biblio;
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 });
286 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
288 my $biblios = Koha::Biblios->search(
290 biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
295 { $biblios->pickup_locations }
296 'Koha::Exceptions::MissingParameter',
297 'Exception thrown on missing parameter';
299 is( $@->parameter, 'patron', 'Exception param correctly set' );
302 Koha::Libraries->search(
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
310 { order_by => ['branchname'] }
311 )->_resultset->get_column('branchcode')->all
314 my $pickup_locations_ids = [
315 $biblios->pickup_locations({ patron => $patron })->_resultset->get_column('branchcode')->all
320 $pickup_locations_ids,
321 'The addition of all biblios+items pickup locations is returned'
324 $schema->storage->txn_rollback;
327 subtest 'api_query_fixer() tests' => sub {
331 my $rs = Koha::Biblios->new;
333 subtest 'JSON query tests' => sub {
337 my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
339 $rs->api_query_fixer($query), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
340 'Query adapted for biblioitem attributes'
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' );
347 $query = encode_json( { "biblio.collection_issn" => { "-like" => "\%asd" } } );
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'
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" } } );
356 $rs->api_query_fixer( $query, 'biblio' ), $query,
357 'Query unchanged because quotes are expected for the match'
361 subtest 'order_by tests' => sub {
365 my $query = encode_json( { collection_issn => { "-like" => "\%asd" } } );
367 $rs->api_query_fixer( $query, undef, 1 ), '{"biblioitem.collection_issn":{"-like":"%asd"}}',
368 'Query adapted for biblioitem attributes'
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" } } );
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'
378 $query = encode_json( { "banana.collection_issn" => { "-like" => "\%asd" } } );
380 $rs->api_query_fixer( $query, 'banana', 1 ), '{"banana.biblioitem.collection_issn":{"-like":"%asd"}}',
381 'Query adapted for biblioitem attributes'
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" } } );
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'