Bug 22445: Replace %% with {}
[koha.git] / t / db_dependent / Koha / Biblios.t
blob2f62524505e7e3b542ef6e6e3e2c151c2a1f523c
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 6;
23 use Test::Exception;
24 use MARC::Field;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Reserves;
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Biblios;
32 use Koha::Patrons;
33 use Koha::Subscriptions;
34 use t::lib::TestBuilder;
35 use t::lib::Mocks;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
41 my $patron = $builder->build( { source => 'Borrower' } );
42 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
44 my $biblio = Koha::Biblio->new()->store();
46 my $biblioitem = $schema->resultset('Biblioitem')->new(
48 biblionumber => $biblio->id
50 )->insert();
52 subtest 'store' => sub {
53 plan tests => 1;
54 is(
55 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
56 output_pref(
57 { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
59 "datecreated must be set to today if not passed to the constructor"
63 subtest 'holds + current_holds' => sub {
64 plan tests => 5;
65 C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
66 my $holds = $biblio->holds;
67 is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
68 is( $holds->count, 1, '->holds should only return 1 hold' );
69 is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
70 $holds->delete;
72 # Add a hold in the future
73 C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
74 $holds = $biblio->holds;
75 is( $holds->count, 1, '->holds should return future holds' );
76 $holds = $biblio->current_holds;
77 is( $holds->count, 0, '->current_holds should not return future holds' );
78 $holds->delete;
82 subtest 'subscriptions' => sub {
83 plan tests => 2;
84 $builder->build(
85 { source => 'Subscription', value => { biblionumber => $biblio->id } }
87 $builder->build(
88 { source => 'Subscription', value => { biblionumber => $biblio->id } }
90 my $biblio = Koha::Biblios->find( $biblio->id );
91 my $subscriptions = $biblio->subscriptions;
92 is( ref($subscriptions), 'Koha::Subscriptions',
93 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
95 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
98 subtest 'waiting_or_in_transit' => sub {
99 plan tests => 4;
100 my $biblio = $builder->build( { source => 'Biblio' } );
101 my $item = $builder->build({
102 source => 'Item',
103 value => {
104 biblionumber => $biblio->{biblionumber}
107 my $reserve = $builder->build({
108 source => 'Reserve',
109 value => {
110 biblionumber => $biblio->{biblionumber},
111 found => undef
115 $reserve = Koha::Holds->find($reserve->{reserve_id});
116 $biblio = Koha::Biblios->find($biblio->{biblionumber});
118 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
120 $reserve->found('W')->store;
121 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
123 $reserve->found('T')->store;
124 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
126 my $transfer = $builder->build({
127 source => 'Branchtransfer',
128 value => {
129 itemnumber => $item->{itemnumber},
130 datearrived => undef
133 my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
134 $reserve->found(undef)->store;
135 is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
138 subtest 'can_be_transferred' => sub {
139 plan tests => 8;
141 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
142 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
144 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
145 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
146 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
147 my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
148 my ($item_bibnum, $item_bibitemnum, $itemnumber)
149 = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber);
150 my $item = Koha::Items->find($itemnumber);
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.');
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 my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
179 = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $biblio->biblionumber);
180 my $item2 = Koha::Items->find($itemnumber2);
181 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
182 .'another item that should have no transfer limits applying on, then '
183 .'the transfer is possible.');
184 $item2->holdingbranch($library1->branchcode)->store;
185 is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
186 .' of the biblio are from same, transfer limited library, then transfer'
187 .' is not possible.');
190 subtest 'custom_cover_image_url' => sub {
191 plan tests => 3;
193 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
195 my $isbn = '0553573403 | 9780553573404 (pbk.).png';
196 my $issn = 'my_issn';
197 my $marc_record = MARC::Record->new;
198 my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
200 my $biblio = Koha::Biblios->find( $biblionumber );
201 my $biblioitem = $biblio->biblioitem->set(
202 { isbn => $isbn, issn => $issn });
203 is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
205 my $marc_024a = '710347104926';
206 $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
207 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
209 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
210 is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
212 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
213 my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
214 is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
217 $schema->storage->txn_rollback;