Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / Biblios.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 6;
23
24 use Test::Exception;
25 use Test::MockModule;
26
27 use MARC::Field;
28
29 use C4::Items;
30 use C4::Biblio qw( AddBiblio ModBiblio );
31 use C4::Reserves qw( AddReserve );
32
33 use Koha::DateUtils qw( dt_from_string output_pref );
34 use Koha::Biblios;
35 use Koha::Patrons;
36 use Koha::Subscriptions;
37 use t::lib::TestBuilder;
38 use t::lib::Mocks;
39
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42
43 my $dbh     = C4::Context->dbh;
44
45 my $builder = t::lib::TestBuilder->new;
46 my $patron = $builder->build( { source => 'Borrower' } );
47 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
48
49 my $biblio = Koha::Biblio->new()->store();
50
51 my $biblioitem = $schema->resultset('Biblioitem')->new(
52     {
53         biblionumber => $biblio->id
54     }
55 )->insert();
56
57 subtest 'store' => sub {
58     plan tests => 1;
59     is(
60         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
61         output_pref(
62             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
63         ),
64         "datecreated must be set to today if not passed to the constructor"
65     );
66 };
67
68 subtest 'holds + current_holds' => sub {
69     plan tests => 5;
70     C4::Reserves::AddReserve(
71         {
72             branchcode     => $patron->branchcode,
73             borrowernumber => $patron->borrowernumber,
74             biblionumber   => $biblio->biblionumber,
75         }
76     );
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' );
81     $holds->delete;
82
83     # Add a hold in the future
84     C4::Reserves::AddReserve(
85         {
86             branchcode       => $patron->branchcode,
87             borrowernumber   => $patron->borrowernumber,
88             biblionumber     => $biblio->biblionumber,
89             reservation_date => dt_from_string->add( days => 2 ),
90         }
91     );
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' );
96     $holds->delete;
97
98 };
99
100 subtest 'waiting_or_in_transit' => sub {
101     plan tests => 4;
102     my $item = $builder->build_sample_item;
103     my $reserve = $builder->build({
104         source => 'Reserve',
105         value => {
106             biblionumber => $item->biblionumber,
107             found => undef
108         }
109     });
110
111     $reserve = Koha::Holds->find($reserve->{reserve_id});
112     $biblio = $item->biblio;
113
114     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
115
116     $reserve->found('W')->store;
117     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
118
119     $reserve->found('T')->store;
120     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
121
122     my $transfer = $builder->build({
123         source => 'Branchtransfer',
124         value => {
125             itemnumber => $item->itemnumber,
126             datearrived => undef,
127             datecancelled => undef,
128         }
129     });
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');
133 };
134
135 subtest 'can_be_transferred' => sub {
136     plan tests => 8;
137
138     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
139     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
140
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(
146         {
147             biblionumber => $biblio->biblionumber,
148             library      => $library1->branchcode
149         }
150     );
151
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.');
158
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
179     my $item2 = $builder->build_sample_item(
180         {
181             biblionumber  => $biblio->biblionumber,
182             homebranch    => $library1->branchcode,
183             holdingbranch => $library3->branchcode,
184         }
185     );
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.');
193 };
194
195 subtest 'custom_cover_image_url' => sub {
196     plan tests => 6;
197
198     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
199
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, '');
205
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" );
210
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 );
214
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" );
217
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" );
221
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" );
224
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' );
231 };
232
233 $schema->storage->txn_rollback;
234
235 subtest 'pickup_locations() tests' => sub {
236
237     plan tests => 1;
238
239     $schema->storage->txn_begin;
240
241     # Build 8 libraries
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 } });
250
251     # Mock Koha::Item->pickup_locations so we have control on the output
252     # The $switch variable controls the output.
253     my $switch  = 0;
254     my $queries = [
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 ] }
259     ];
260
261     my $mock_item = Test::MockModule->new('Koha::Item');
262     $mock_item->mock(
263         'pickup_locations',
264         sub {
265             my $query = $queries->[$switch];
266             $switch++;
267             return Koha::Libraries->search($query);
268         }
269     );
270
271     # Two biblios
272     my $biblio_1 = $builder->build_sample_biblio;
273     my $biblio_2 = $builder->build_sample_biblio;
274
275     # Two items each
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 });
280
281     my $biblios = Koha::Biblios->search(
282         {
283             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
284         }
285     );
286
287     my $library_ids = [
288         Koha::Libraries->search(
289             {
290                 branchcode => [
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
294                 ]
295             },
296             { order_by => ['branchname'] }
297         )->_resultset->get_column('branchcode')->all
298     ];
299
300     my $pickup_locations_ids = [
301         $biblios->pickup_locations->_resultset->get_column('branchcode')->all
302     ];
303
304     is_deeply(
305         $library_ids,
306         $pickup_locations_ids,
307         'The addition of all biblios+items pickup locations is returned'
308     );
309
310     $schema->storage->txn_rollback;
311 };