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 => 6;
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( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
68 my $holds = $biblio->holds;
69 is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
70 is( $holds->count, 1, '->holds should only return 1 hold' );
71 is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
74 # Add a hold in the future
75 C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
76 $holds = $biblio->holds;
77 is( $holds->count, 1, '->holds should return future holds' );
78 $holds = $biblio->current_holds;
79 is( $holds->count, 0, '->current_holds should not return future holds' );
84 subtest 'subscriptions' => sub {
87 { source => 'Subscription', value => { biblionumber => $biblio->id } }
90 { source => 'Subscription', value => { biblionumber => $biblio->id } }
92 my $biblio = Koha::Biblios->find( $biblio->id );
93 my $subscriptions = $biblio->subscriptions;
94 is( ref($subscriptions), 'Koha::Subscriptions',
95 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
97 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
100 subtest 'waiting_or_in_transit' => sub {
102 my $biblio = $builder->build( { source => 'Biblio' } );
103 my $item = $builder->build({
106 biblionumber => $biblio->{biblionumber}
109 my $reserve = $builder->build({
112 biblionumber => $biblio->{biblionumber},
117 $reserve = Koha::Holds->find($reserve->{reserve_id});
118 $biblio = Koha::Biblios->find($biblio->{biblionumber});
120 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
122 $reserve->found('W')->store;
123 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
125 $reserve->found('T')->store;
126 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
128 my $transfer = $builder->build({
129 source => 'Branchtransfer',
131 itemnumber => $item->{itemnumber},
135 my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
136 $reserve->found(undef)->store;
137 is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
140 subtest 'can_be_transferred' => sub {
143 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
144 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
146 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
147 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
148 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
149 my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
150 my ($item_bibnum, $item_bibitemnum, $itemnumber)
151 = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber);
152 my $item = Koha::Items->find($itemnumber);
154 is(Koha::Item::Transfer::Limits->search({
155 fromBranch => $library1->branchcode,
156 toBranch => $library2->branchcode,
157 })->count, 0, 'There are no transfer limits between libraries.');
158 ok($biblio->can_be_transferred({ to => $library2 }),
159 'Some items of this biblio can be transferred between libraries.');
161 my $limit = Koha::Item::Transfer::Limit->new({
162 fromBranch => $library1->branchcode,
163 toBranch => $library2->branchcode,
164 itemtype => $item->effective_itemtype,
166 is(Koha::Item::Transfer::Limits->search({
167 fromBranch => $library1->branchcode,
168 toBranch => $library2->branchcode,
169 })->count, 1, 'Given we have added a transfer limit that applies for all '
170 .'of this biblio\s items,');
171 is($biblio->can_be_transferred({ to => $library2 }), 0,
172 'None of the items of biblio can no longer be transferred between '
174 is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
175 'We get the same result also if we pass the from-library parameter.');
176 $item->holdingbranch($library2->branchcode)->store;
177 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
178 .'items is already located at to-library, then the transfer is possible.');
179 $item->holdingbranch($library1->branchcode)->store;
180 my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
181 = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $biblio->biblionumber);
182 my $item2 = Koha::Items->find($itemnumber2);
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" );
219 $schema->storage->txn_rollback;