3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 8;
25 use t::lib::TestBuilder;
29 use_ok('Koha::Biblio');
30 use_ok('Koha::Biblios');
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'metadata() tests' => sub {
40 $schema->storage->txn_begin;
42 my $title = 'Oranges and Peaches';
44 my $record = MARC::Record->new();
45 my $field = MARC::Field->new('245','','','a' => $title);
46 $record->append_fields( $field );
47 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
49 my $biblio = Koha::Biblios->find( $biblionumber );
50 is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
52 my $metadata = $biblio->metadata;
53 is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
55 my $record2 = $metadata->record;
56 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
58 is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
60 $schema->storage->txn_rollback;
63 subtest 'hidden_in_opac() tests' => sub {
67 $schema->storage->txn_begin;
69 my $biblio = $builder->build_sample_biblio();
71 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden if there is no item attached' );
73 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
74 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
76 $item_1->withdrawn( 1 )->store->discard_changes;
77 $item_2->withdrawn( 1 )->store->discard_changes;
79 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
81 $item_2->withdrawn( 2 )->store->discard_changes;
82 $biblio->discard_changes; # refresh
84 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
86 $item_1->withdrawn( 2 )->store->discard_changes;
87 $biblio->discard_changes; # refresh
89 ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
91 $schema->storage->txn_rollback;
94 subtest 'items() tests' => sub {
98 $schema->storage->txn_begin;
100 my $biblio = $builder->build_sample_biblio();
101 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
102 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
104 my $items = $biblio->items;
105 is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
106 is( $items->count, 2, 'Two items in resultset' );
108 my @items = $biblio->items->as_list;
109 is( scalar @items, 2, 'Same result, but in list context' );
111 $schema->storage->txn_rollback;
115 subtest 'get_coins and get_openurl' => sub {
119 $schema->storage->txn_begin;
121 my $builder = t::lib::TestBuilder->new;
122 my $biblio = $builder->build_sample_biblio({
129 'ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
130 'GetCOinsBiblio returned right metadata'
133 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
135 $biblio->get_openurl,
136 'https://koha.example.com/?ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
137 'Koha::Biblio->get_openurl returned right URL'
140 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
142 $biblio->get_openurl,
143 'https://koha.example.com/?client_id=ci1&ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
144 'Koha::Biblio->get_openurl returned right URL'
147 $schema->storage->txn_rollback;
150 subtest 'is_serial() tests' => sub {
154 $schema->storage->txn_begin;
156 my $biblio = $builder->build_sample_biblio();
158 $biblio->serial( 1 )->store->discard_changes;
159 ok( $biblio->is_serial, 'Bibliographic record is serial' );
161 $biblio->serial( 0 )->store->discard_changes;
162 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
164 my $record = $biblio->metadata->record;
165 $record->leader('00142nas a22 7a 4500');
166 ModBiblio($record, $biblio->biblionumber );
167 $biblio = Koha::Biblios->find($biblio->biblionumber);
169 ok( $biblio->is_serial, 'Bibliographic record is serial' );
171 $schema->storage->txn_rollback;
174 subtest 'pickup_locations' => sub {
177 $schema->storage->txn_begin;
179 my $dbh = C4::Context->dbh;
182 Koha::Holds->search->delete;
183 Koha::Patrons->search->delete;
184 Koha::Items->search->delete;
185 Koha::Libraries->search->delete;
186 $dbh->do('DELETE FROM issues');
187 $dbh->do('DELETE FROM issuingrules');
189 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
190 VALUES (?, ?, ?, ?)},
194 $dbh->do('DELETE FROM circulation_rules');
196 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
197 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
198 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
200 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
201 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
202 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
203 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
204 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
205 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
206 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
207 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
209 Koha::CirculationRules->set_rules(
211 branchcode => $library1->branchcode,
213 categorycode => undef,
216 hold_fulfillment_policy => 'any',
217 returnbranch => 'any'
222 Koha::CirculationRules->set_rules(
224 branchcode => $library2->branchcode,
226 categorycode => undef,
229 hold_fulfillment_policy => 'holdgroup',
230 returnbranch => 'any'
235 Koha::CirculationRules->set_rules(
237 branchcode => $library3->branchcode,
239 categorycode => undef,
242 hold_fulfillment_policy => 'patrongroup',
243 returnbranch => 'any'
248 Koha::CirculationRules->set_rules(
250 branchcode => $library4->branchcode,
252 categorycode => undef,
255 hold_fulfillment_policy => 'holdingbranch',
256 returnbranch => 'any'
261 Koha::CirculationRules->set_rules(
263 branchcode => $library5->branchcode,
265 categorycode => undef,
268 hold_fulfillment_policy => 'homebranch',
269 returnbranch => 'any'
274 Koha::CirculationRules->set_rules(
276 branchcode => $library6->branchcode,
278 categorycode => undef,
281 hold_fulfillment_policy => 'holdgroup',
282 returnbranch => 'any'
287 Koha::CirculationRules->set_rules(
289 branchcode => $library7->branchcode,
291 categorycode => undef,
294 hold_fulfillment_policy => 'holdingbranch',
295 returnbranch => 'any'
301 Koha::CirculationRules->set_rules(
303 branchcode => $library8->branchcode,
305 categorycode => undef,
308 hold_fulfillment_policy => 'patrongroup',
309 returnbranch => 'any'
314 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
315 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
317 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
318 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
320 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
321 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
322 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
323 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
325 my $biblio1 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '1'} } );
326 my $biblioitem1 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio1->biblionumber } } );
327 my $biblio2 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '2'} } );
328 my $biblioitem2 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio2->biblionumber } } );
330 my $item1_1 = Koha::Item->new({
331 biblionumber => $biblio1->biblionumber,
332 biblioitemnumber => $biblioitem1->biblioitemnumber,
333 homebranch => $library1->branchcode,
334 holdingbranch => $library2->branchcode,
336 barcode => "item11barcode",
339 my $item1_3 = Koha::Item->new({
340 biblionumber => $biblio1->biblionumber,
341 biblioitemnumber => $biblioitem1->biblioitemnumber,
342 homebranch => $library3->branchcode,
343 holdingbranch => $library4->branchcode,
345 barcode => "item13barcode",
348 my $item1_7 = Koha::Item->new({
349 biblionumber => $biblio1->biblionumber,
350 biblioitemnumber => $biblioitem1->biblioitemnumber,
351 homebranch => $library7->branchcode,
352 holdingbranch => $library4->branchcode,
354 barcode => "item17barcode",
357 my $item2_2 = Koha::Item->new({
358 biblionumber => $biblio2->biblionumber,
359 biblioitemnumber => $biblioitem2->biblioitemnumber,
360 homebranch => $library2->branchcode,
361 holdingbranch => $library1->branchcode,
363 barcode => "item22barcode",
366 my $item2_4 = Koha::Item->new({
367 biblionumber => $biblio2->biblionumber,
368 biblioitemnumber => $biblioitem2->biblioitemnumber,
369 homebranch => $library4->branchcode,
370 holdingbranch => $library3->branchcode,
372 barcode => "item23barcode",
375 my $item2_6 = Koha::Item->new({
376 biblionumber => $biblio2->biblionumber,
377 biblioitemnumber => $biblioitem2->biblioitemnumber,
378 homebranch => $library6->branchcode,
379 holdingbranch => $library4->branchcode,
381 barcode => "item26barcode",
384 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
385 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
388 "ItemHomeLibrary-1-1" => 6,
389 "ItemHomeLibrary-1-8" => 1,
390 "ItemHomeLibrary-2-1" => 2,
391 "ItemHomeLibrary-2-8" => 0,
392 "PatronLibrary-1-1" => 6,
393 "PatronLibrary-1-8" => 3,
394 "PatronLibrary-2-1" => 0,
395 "PatronLibrary-2-8" => 3,
399 my ( $cbranch, $biblio, $patron, $results ) = @_;
400 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
402 my @pl = $biblio->pickup_locations( { patron => $patron} );
404 foreach my $pickup_location (@pl) {
405 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
409 scalar(@pl) == $results->{ $cbranch . '-'
410 . $biblio->title . '-'
411 . $patron->firstname },
412 'ReservesControlBranch: '
419 . $results->{ $cbranch . '-'
420 . $biblio->title . '-'
421 . $patron->firstname }
427 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
428 foreach my $biblio ($biblio1, $biblio2) {
429 foreach my $patron ($patron1, $patron8) {
430 _doTest($cbranch, $biblio, $patron, $results);
435 $schema->storage->txn_rollback;