Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Koha / Biblios.t
blobfcba56dcd7713ac20099f1044eab1bb2fce6cd81
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 => 5;
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 $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
52 )->insert();
54 subtest 'store' => sub {
55 plan tests => 1;
56 is(
57 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
58 output_pref(
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 {
66 plan tests => 5;
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' );
78 $holds->delete;
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' );
93 $holds->delete;
97 subtest 'waiting_or_in_transit' => sub {
98 plan tests => 4;
99 my $item = $builder->build_sample_item;
100 my $reserve = $builder->build({
101 source => 'Reserve',
102 value => {
103 biblionumber => $item->biblionumber,
104 found => undef
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',
121 value => {
122 itemnumber => $item->itemnumber,
123 datearrived => undef
126 my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
127 $reserve->found(undef)->store;
128 is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
131 subtest 'can_be_transferred' => sub {
132 plan tests => 8;
134 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
135 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
137 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
138 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
139 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
140 my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
141 my $item = $builder->build_sample_item(
143 biblionumber => $biblio->biblionumber,
144 library => $library1->branchcode
148 is(Koha::Item::Transfer::Limits->search({
149 fromBranch => $library1->branchcode,
150 toBranch => $library2->branchcode,
151 })->count, 0, 'There are no transfer limits between libraries.');
152 ok($biblio->can_be_transferred({ to => $library2 }),
153 'Some items of this biblio can be transferred between libraries.');
155 my $limit = Koha::Item::Transfer::Limit->new({
156 fromBranch => $library1->branchcode,
157 toBranch => $library2->branchcode,
158 itemtype => $item->effective_itemtype,
159 })->store;
160 is(Koha::Item::Transfer::Limits->search({
161 fromBranch => $library1->branchcode,
162 toBranch => $library2->branchcode,
163 })->count, 1, 'Given we have added a transfer limit that applies for all '
164 .'of this biblio\s items,');
165 is($biblio->can_be_transferred({ to => $library2 }), 0,
166 'None of the items of biblio can no longer be transferred between '
167 .'libraries.');
168 is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
169 'We get the same result also if we pass the from-library parameter.');
170 $item->holdingbranch($library2->branchcode)->store;
171 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
172 .'items is already located at to-library, then the transfer is possible.');
173 $item->holdingbranch($library1->branchcode)->store;
175 my $item2 = $builder->build_sample_item(
177 biblionumber => $biblio->biblionumber,
178 homebranch => $library1->branchcode,
179 holdingbranch => $library3->branchcode,
182 is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
183 .'another item that should have no transfer limits applying on, then '
184 .'the transfer is possible.');
185 $item2->holdingbranch($library1->branchcode)->store;
186 is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
187 .' of the biblio are from same, transfer limited library, then transfer'
188 .' is not possible.');
191 subtest 'custom_cover_image_url' => sub {
192 plan tests => 3;
194 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
196 my $isbn = '0553573403 | 9780553573404 (pbk.).png';
197 my $issn = 'my_issn';
198 my $marc_record = MARC::Record->new;
199 my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
201 my $biblio = Koha::Biblios->find( $biblionumber );
202 my $biblioitem = $biblio->biblioitem->set(
203 { isbn => $isbn, issn => $issn });
204 is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
206 my $marc_024a = '710347104926';
207 $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
208 C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
210 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
211 is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
213 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
214 my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
215 is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
218 $schema->storage->txn_rollback;