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 C4::Biblio qw( AddBiblio ModBiblio );
31 use C4::Reserves qw( AddReserve );
33 use Koha::DateUtils qw( dt_from_string output_pref );
36 use Koha::Subscriptions;
37 use t::lib::TestBuilder;
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
43 my $dbh = C4::Context->dbh;
45 my $builder = t::lib::TestBuilder->new;
46 my $patron = $builder->build( { source => 'Borrower' } );
47 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
49 my $biblio = Koha::Biblio->new()->store();
51 my $biblioitem = $schema->resultset('Biblioitem')->new(
53 biblionumber => $biblio->id
57 subtest 'store' => sub {
60 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
62 { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
64 "datecreated must be set to today if not passed to the constructor"
68 subtest 'holds + current_holds' => sub {
70 C4::Reserves::AddReserve(
72 branchcode => $patron->branchcode,
73 borrowernumber => $patron->borrowernumber,
74 biblionumber => $biblio->biblionumber,
77 my $holds = $biblio->holds;
78 is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
79 is( $holds->count, 1, '->holds should only return 1 hold' );
80 is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
83 # Add a hold in the future
84 C4::Reserves::AddReserve(
86 branchcode => $patron->branchcode,
87 borrowernumber => $patron->borrowernumber,
88 biblionumber => $biblio->biblionumber,
89 reservation_date => dt_from_string->add( days => 2 ),
92 $holds = $biblio->holds;
93 is( $holds->count, 1, '->holds should return future holds' );
94 $holds = $biblio->current_holds;
95 is( $holds->count, 0, '->current_holds should not return future holds' );
100 subtest 'waiting_or_in_transit' => sub {
102 my $item = $builder->build_sample_item;
103 my $reserve = $builder->build({
106 biblionumber => $item->biblionumber,
111 $reserve = Koha::Holds->find($reserve->{reserve_id});
112 $biblio = $item->biblio;
114 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
116 $reserve->found('W')->store;
117 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
119 $reserve->found('T')->store;
120 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
122 my $transfer = $builder->build({
123 source => 'Branchtransfer',
125 itemnumber => $item->itemnumber,
126 datearrived => undef,
127 datecancelled => undef,
130 my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
131 $reserve->found(undef)->store;
132 is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
135 subtest 'can_be_transferred' => sub {
138 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
139 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
141 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
142 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
143 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
144 my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
145 my $item = $builder->build_sample_item(
147 biblionumber => $biblio->biblionumber,
148 library => $library1->branchcode
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,
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 '
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;
179 my $item2 = $builder->build_sample_item(
181 biblionumber => $biblio->biblionumber,
182 homebranch => $library1->branchcode,
183 holdingbranch => $library3->branchcode,
186 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
187 .'another item that should have no transfer limits applying on, then '
188 .'the transfer is possible.');
189 $item2->holdingbranch($library1->branchcode)->store;
190 is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
191 .' of the biblio are from same, transfer limited library, then transfer'
192 .' is not possible.');
195 subtest 'custom_cover_image_url' => sub {
198 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
200 my $isbn = '0553573403 | 9780553573404 (pbk.).png';
201 my $issn = 'my_issn';
202 my $cf_value = 'from_control_field';
203 my $marc_record = MARC::Record->new;
204 my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
206 my $biblio = Koha::Biblios->find( $biblionumber );
207 my $biblioitem = $biblio->biblioitem->set(
208 { isbn => $isbn, issn => $issn });
209 is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
211 my $marc_024a = '710347104926';
212 $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
213 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
215 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
216 is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
218 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
219 my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
220 is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
222 $biblio->biblioitem->isbn('')->store;
223 is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" );
225 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{001}.png' );
226 is( $biblio->custom_cover_image_url, undef, 'Record does not have 001' );
227 $marc_record->append_fields(MARC::Field->new('001', $cf_value));
228 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
229 $biblio = Koha::Biblios->find( $biblionumber );
230 is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/$cf_value.png", 'URL generated using 001' );
233 $schema->storage->txn_rollback;
235 subtest 'pickup_locations() tests' => sub {
239 $schema->storage->txn_begin;
242 my $l_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
243 my $l_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
244 my $l_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
245 my $l_4 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
246 my $l_5 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
247 my $l_6 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
248 my $l_7 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
249 my $l_8 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
251 # Mock Koha::Item->pickup_locations so we have control on the output
252 # The $switch variable controls the output.
255 { branchcode => [ $l_1->branchcode, $l_2->branchcode ] },
256 { branchcode => [ $l_3->branchcode, $l_4->branchcode ] },
257 { branchcode => [ $l_5->branchcode, $l_6->branchcode ] },
258 { branchcode => [ $l_7->branchcode, $l_8->branchcode ] }
261 my $mock_item = Test::MockModule->new('Koha::Item');
265 my $query = $queries->[$switch];
267 return Koha::Libraries->search($query);
272 my $biblio_1 = $builder->build_sample_biblio;
273 my $biblio_2 = $builder->build_sample_biblio;
276 my $item_1_1 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
277 my $item_1_2 = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
278 my $item_2_1 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
279 my $item_2_2 = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
281 my $biblios = Koha::Biblios->search(
283 biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
288 Koha::Libraries->search(
291 $l_1->branchcode, $l_2->branchcode, $l_3->branchcode,
292 $l_4->branchcode, $l_5->branchcode, $l_6->branchcode,
293 $l_7->branchcode, $l_8->branchcode
296 { order_by => ['branchname'] }
297 )->_resultset->get_column('branchcode')->all
300 my $pickup_locations_ids = [
301 $biblios->pickup_locations->_resultset->get_column('branchcode')->all
306 $pickup_locations_ids,
307 'The addition of all biblios+items pickup locations is returned'
310 $schema->storage->txn_rollback;