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 => 5;
30 use Koha::DateUtils qw( dt_from_string output_pref );
33 use Koha::Subscriptions;
34 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 my $dbh = C4::Context->dbh;
42 my $builder = t::lib::TestBuilder->new;
43 my $patron = $builder->build( { source => 'Borrower' } );
44 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
46 my $biblio = Koha::Biblio->new()->store();
48 my $biblioitem = $schema->resultset('Biblioitem')->new(
50 biblionumber => $biblio->id
54 subtest 'store' => sub {
57 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
59 { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
61 "datecreated must be set to today if not passed to the constructor"
65 subtest 'holds + current_holds' => sub {
67 C4::Reserves::AddReserve(
69 branchcode => $patron->branchcode,
70 borrowernumber => $patron->borrowernumber,
71 biblionumber => $biblio->biblionumber,
74 my $holds = $biblio->holds;
75 is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
76 is( $holds->count, 1, '->holds should only return 1 hold' );
77 is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
80 # Add a hold in the future
81 C4::Reserves::AddReserve(
83 branchcode => $patron->branchcode,
84 borrowernumber => $patron->borrowernumber,
85 biblionumber => $biblio->biblionumber,
86 reservation_date => dt_from_string->add( days => 2 ),
89 $holds = $biblio->holds;
90 is( $holds->count, 1, '->holds should return future holds' );
91 $holds = $biblio->current_holds;
92 is( $holds->count, 0, '->current_holds should not return future holds' );
97 subtest 'waiting_or_in_transit' => sub {
99 my $item = $builder->build_sample_item;
100 my $reserve = $builder->build({
103 biblionumber => $item->biblionumber,
108 $reserve = Koha::Holds->find($reserve->{reserve_id});
109 $biblio = $item->biblio;
111 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
113 $reserve->found('W')->store;
114 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
116 $reserve->found('T')->store;
117 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
119 my $transfer = $builder->build({
120 source => 'Branchtransfer',
122 itemnumber => $item->itemnumber,
123 datearrived => undef,
124 datecancelled => undef,
127 my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
128 $reserve->found(undef)->store;
129 is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
132 subtest 'can_be_transferred' => sub {
135 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
136 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
138 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
139 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
140 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
141 my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
142 my $item = $builder->build_sample_item(
144 biblionumber => $biblio->biblionumber,
145 library => $library1->branchcode
149 is(Koha::Item::Transfer::Limits->search({
150 fromBranch => $library1->branchcode,
151 toBranch => $library2->branchcode,
152 })->count, 0, 'There are no transfer limits between libraries.');
153 ok($biblio->can_be_transferred({ to => $library2 }),
154 'Some items of this biblio can be transferred between libraries.');
156 my $limit = Koha::Item::Transfer::Limit->new({
157 fromBranch => $library1->branchcode,
158 toBranch => $library2->branchcode,
159 itemtype => $item->effective_itemtype,
161 is(Koha::Item::Transfer::Limits->search({
162 fromBranch => $library1->branchcode,
163 toBranch => $library2->branchcode,
164 })->count, 1, 'Given we have added a transfer limit that applies for all '
165 .'of this biblio\s items,');
166 is($biblio->can_be_transferred({ to => $library2 }), 0,
167 'None of the items of biblio can no longer be transferred between '
169 is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
170 'We get the same result also if we pass the from-library parameter.');
171 $item->holdingbranch($library2->branchcode)->store;
172 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
173 .'items is already located at to-library, then the transfer is possible.');
174 $item->holdingbranch($library1->branchcode)->store;
176 my $item2 = $builder->build_sample_item(
178 biblionumber => $biblio->biblionumber,
179 homebranch => $library1->branchcode,
180 holdingbranch => $library3->branchcode,
183 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
184 .'another item that should have no transfer limits applying on, then '
185 .'the transfer is possible.');
186 $item2->holdingbranch($library1->branchcode)->store;
187 is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
188 .' of the biblio are from same, transfer limited library, then transfer'
189 .' is not possible.');
192 subtest 'custom_cover_image_url' => sub {
195 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
197 my $isbn = '0553573403 | 9780553573404 (pbk.).png';
198 my $issn = 'my_issn';
199 my $marc_record = MARC::Record->new;
200 my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
202 my $biblio = Koha::Biblios->find( $biblionumber );
203 my $biblioitem = $biblio->biblioitem->set(
204 { isbn => $isbn, issn => $issn });
205 is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
207 my $marc_024a = '710347104926';
208 $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
209 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
211 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
212 is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
214 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
215 my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
216 is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
218 $biblio->biblioitem->isbn('')->store;
219 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 $schema->storage->txn_rollback;