Bug 28734: Clear cache to prevent random failures
[koha.git] / t / db_dependent / Koha / Biblio.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 16;
21
22 use C4::Biblio qw( AddBiblio ModBiblio );
23 use Koha::Database;
24 use Koha::Caches;
25 use Koha::Acquisition::Orders;
26 use Koha::AuthorisedValueCategories;
27 use Koha::AuthorisedValues;
28 use Koha::MarcSubfieldStructures;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 BEGIN {
34     use_ok('Koha::Biblio');
35     use_ok('Koha::Biblios');
36 }
37
38 my $schema  = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'metadata() tests' => sub {
42
43     plan tests => 4;
44
45     $schema->storage->txn_begin;
46
47     my $title = 'Oranges and Peaches';
48
49     my $record = MARC::Record->new();
50     my $field = MARC::Field->new('245','','','a' => $title);
51     $record->append_fields( $field );
52     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
53
54     my $biblio = Koha::Biblios->find( $biblionumber );
55     is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
56
57     my $metadata = $biblio->metadata;
58     is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
59
60     my $record2 = $metadata->record;
61     is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
62
63     is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
64
65     $schema->storage->txn_rollback;
66 };
67
68 subtest 'hidden_in_opac() tests' => sub {
69
70     plan tests => 6;
71
72     $schema->storage->txn_begin;
73
74     my $biblio = $builder->build_sample_biblio();
75     my $rules  = { withdrawn => [ 2 ] };
76
77     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
78
79     ok(
80         !$biblio->hidden_in_opac({ rules => $rules }),
81         'Biblio not hidden if there is no item attached (!OpacHiddenItemsHidesRecord)'
82     );
83
84     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 1 );
85
86     ok(
87         !$biblio->hidden_in_opac({ rules => $rules }),
88         'Biblio not hidden if there is no item attached (OpacHiddenItemsHidesRecord)'
89     );
90
91     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
92     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
93
94     $item_1->withdrawn( 1 )->store->discard_changes;
95     $item_2->withdrawn( 1 )->store->discard_changes;
96
97     ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
98
99     $item_2->withdrawn( 2 )->store->discard_changes;
100     $biblio->discard_changes; # refresh
101
102     ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
103
104     $item_1->withdrawn( 2 )->store->discard_changes;
105     $biblio->discard_changes; # refresh
106
107     ok( $biblio->hidden_in_opac({ rules => $rules }), 'Biblio hidden' );
108
109     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
110     ok(
111         !$biblio->hidden_in_opac( { rules => $rules } ),
112         'Biblio hidden (!OpacHiddenItemsHidesRecord)'
113     );
114
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'items() tests' => sub {
120
121     plan tests => 4;
122
123     $schema->storage->txn_begin;
124
125     my $biblio = $builder->build_sample_biblio();
126
127     is( $biblio->items->count, 0, 'No items, count is 0' );
128
129     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
130     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
131
132     my $items = $biblio->items;
133     is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
134     is( $items->count, 2, 'Two items in resultset' );
135
136     my @items = $biblio->items->as_list;
137     is( scalar @items, 2, 'Same result, but in list context' );
138
139     $schema->storage->txn_rollback;
140
141 };
142
143 subtest 'get_coins and get_openurl' => sub {
144
145     plan tests => 4;
146
147     $schema->storage->txn_begin;
148
149     my $builder = t::lib::TestBuilder->new;
150     my $biblio = $builder->build_sample_biblio({
151             title => 'Title 1',
152             author => 'Author 1'
153         });
154     is(
155         $biblio->get_coins,
156         'ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
157         'GetCOinsBiblio returned right metadata'
158     );
159
160     my $record = MARC::Record->new();
161     $record->append_fields( MARC::Field->new('100','','','a' => 'Author 2'), MARC::Field->new('880','','','a' => 'Something') );
162     my ( $biblionumber ) = C4::Biblio::AddBiblio($record, '');
163     my $biblio_no_title = Koha::Biblios->find($biblionumber);
164     is(
165         $biblio_no_title->get_coins,
166         'ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.au=Author%202',
167         'GetCOinsBiblio returned right metadata if biblio does not have a title'
168     );
169
170     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
171     is(
172         $biblio->get_openurl,
173         'https://koha.example.com/?ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
174         'Koha::Biblio->get_openurl returned right URL'
175     );
176
177     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
178     is(
179         $biblio->get_openurl,
180         'https://koha.example.com/?client_id=ci1&amp;ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
181         'Koha::Biblio->get_openurl returned right URL'
182     );
183
184     $schema->storage->txn_rollback;
185 };
186
187 subtest 'is_serial() tests' => sub {
188
189     plan tests => 3;
190
191     $schema->storage->txn_begin;
192
193     my $biblio = $builder->build_sample_biblio();
194
195     $biblio->serial( 1 )->store->discard_changes;
196     ok( $biblio->is_serial, 'Bibliographic record is serial' );
197
198     $biblio->serial( 0 )->store->discard_changes;
199     ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
200
201     my $record = $biblio->metadata->record;
202     $record->leader('00142nas a22     7a 4500');
203     ModBiblio($record, $biblio->biblionumber );
204     $biblio = Koha::Biblios->find($biblio->biblionumber);
205
206     ok( $biblio->is_serial, 'Bibliographic record is serial' );
207
208     $schema->storage->txn_rollback;
209 };
210
211 subtest 'pickup_locations' => sub {
212     plan tests => 9;
213
214     $schema->storage->txn_begin;
215
216     Koha::CirculationRules->search->delete;
217     Koha::CirculationRules->set_rules(
218         {
219             categorycode => undef,
220             itemtype     => undef,
221             branchcode   => undef,
222             rules        => {
223                 reservesallowed => 25,
224             }
225         }
226     );
227
228     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
229     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
230     my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
231
232     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'zzz' } } );
233     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'AAA' } } );
234     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'FFF' } } );
235     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'CCC' } } );
236     my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'eee' } } );
237     my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'BBB' } } );
238     my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'DDD' } } );
239     my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'GGG' } } );
240
241     our @branchcodes = map { $_->branchcode } ($library1, $library2, $library3, $library4, $library5, $library6, $library7, $library8);
242
243     Koha::CirculationRules->set_rules(
244         {
245             branchcode => $library1->branchcode,
246             itemtype   => undef,
247             rules => {
248                 holdallowed => 'from_home_library',
249                 hold_fulfillment_policy => 'any',
250                 returnbranch => 'any'
251             }
252         }
253     );
254
255     Koha::CirculationRules->set_rules(
256         {
257             branchcode => $library2->branchcode,
258             itemtype   => undef,
259             rules => {
260                 holdallowed => 'from_local_hold_group',
261                 hold_fulfillment_policy => 'holdgroup',
262                 returnbranch => 'any'
263             }
264         }
265     );
266
267     Koha::CirculationRules->set_rules(
268         {
269             branchcode => $library3->branchcode,
270             itemtype   => undef,
271             rules => {
272                 holdallowed => 'from_local_hold_group',
273                 hold_fulfillment_policy => 'patrongroup',
274                 returnbranch => 'any'
275             }
276         }
277     );
278
279     Koha::CirculationRules->set_rules(
280         {
281             branchcode => $library4->branchcode,
282             itemtype   => undef,
283             rules => {
284                 holdallowed => 'from_any_library',
285                 hold_fulfillment_policy => 'holdingbranch',
286                 returnbranch => 'any'
287             }
288         }
289     );
290
291     Koha::CirculationRules->set_rules(
292         {
293             branchcode => $library5->branchcode,
294             itemtype   => undef,
295             rules => {
296                 holdallowed => 'from_any_library',
297                 hold_fulfillment_policy => 'homebranch',
298                 returnbranch => 'any'
299             }
300         }
301     );
302
303     Koha::CirculationRules->set_rules(
304         {
305             branchcode => $library6->branchcode,
306             itemtype   => undef,
307             rules => {
308                 holdallowed => 'from_home_library',
309                 hold_fulfillment_policy => 'holdgroup',
310                 returnbranch => 'any'
311             }
312         }
313     );
314
315     Koha::CirculationRules->set_rules(
316         {
317             branchcode => $library7->branchcode,
318             itemtype   => undef,
319             rules => {
320                 holdallowed => 'from_local_hold_group',
321                 hold_fulfillment_policy => 'holdingbranch',
322                 returnbranch => 'any'
323             }
324         }
325     );
326
327
328     Koha::CirculationRules->set_rules(
329         {
330             branchcode => $library8->branchcode,
331             itemtype   => undef,
332             rules => {
333                 holdallowed => 'from_any_library',
334                 hold_fulfillment_policy => 'patrongroup',
335                 returnbranch => 'any'
336             }
337         }
338     );
339
340     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
341     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
342
343     my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
344     my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
345
346     my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
347     my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
348     my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
349     my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
350
351     my $biblio1  = $builder->build_sample_biblio({ title => '1' });
352     my $biblio2  = $builder->build_sample_biblio({ title => '2' });
353
354     my $item1_1  = $builder->build_sample_item({
355         biblionumber     => $biblio1->biblionumber,
356         homebranch       => $library1->branchcode,
357         holdingbranch    => $library2->branchcode,
358     })->store;
359
360     my $item1_3  = $builder->build_sample_item({
361         biblionumber     => $biblio1->biblionumber,
362         homebranch       => $library3->branchcode,
363         holdingbranch    => $library4->branchcode,
364     })->store;
365
366     my $item1_7  = $builder->build_sample_item({
367         biblionumber     => $biblio1->biblionumber,
368         homebranch       => $library7->branchcode,
369         holdingbranch    => $library4->branchcode,
370     })->store;
371
372     my $item2_2  = $builder->build_sample_item({
373         biblionumber     => $biblio2->biblionumber,
374         homebranch       => $library2->branchcode,
375         holdingbranch    => $library1->branchcode,
376     })->store;
377
378     my $item2_4  = $builder->build_sample_item({
379         biblionumber     => $biblio2->biblionumber,
380         homebranch       => $library4->branchcode,
381         holdingbranch    => $library3->branchcode,
382     })->store;
383
384     my $item2_6  = $builder->build_sample_item({
385         biblionumber     => $biblio2->biblionumber,
386         homebranch       => $library6->branchcode,
387         holdingbranch    => $library4->branchcode,
388     })->store;
389
390     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
391     my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
392
393     my $results = {
394         "ItemHomeLibrary-1-1" => 6,
395         "ItemHomeLibrary-1-8" => 1,
396         "ItemHomeLibrary-2-1" => 2,
397         "ItemHomeLibrary-2-8" => 0,
398         "PatronLibrary-1-1" => 6,
399         "PatronLibrary-1-8" => 3,
400         "PatronLibrary-2-1" => 0,
401         "PatronLibrary-2-8" => 3,
402     };
403
404     sub _doTest {
405         my ( $cbranch, $biblio, $patron, $results ) = @_;
406         t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
407
408         my @pl = map {
409             my $pickup_location = $_;
410             grep { $pickup_location->branchcode eq $_ } @branchcodes
411         } $biblio->pickup_locations( { patron => $patron } )->as_list;
412
413         ok(
414             scalar(@pl) == $results->{ $cbranch . '-'
415                   . $biblio->title . '-'
416                   . $patron->firstname },
417             'ReservesControlBranch: '
418               . $cbranch
419               . ', biblio'
420               . $biblio->title
421               . ', patron'
422               . $patron->firstname
423               . ' should return '
424               . $results->{ $cbranch . '-'
425                   . $biblio->title . '-'
426                   . $patron->firstname }
427               . ' but returns '
428               . scalar(@pl)
429         );
430     }
431
432     foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
433         foreach my $biblio ($biblio1, $biblio2) {
434             foreach my $patron ($patron1, $patron8) {
435                 _doTest($cbranch, $biblio, $patron, $results);
436             }
437         }
438     }
439
440     my @pl_names = map { $_->branchname } $biblio1->pickup_locations( { patron => $patron1 } )->as_list;
441     my $pl_ori_str = join('|', @pl_names);
442     my $pl_sorted_str = join('|', sort { lc($a) cmp lc($b) } @pl_names);
443     ok(
444         $pl_ori_str eq $pl_sorted_str,
445         'Libraries must be sorted by name'
446     );
447     $schema->storage->txn_rollback;
448 };
449
450 subtest 'to_api() tests' => sub {
451
452     $schema->storage->txn_begin;
453
454     my $biblio = $builder->build_sample_biblio();
455     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
456
457     my $biblioitem_api = $biblio->biblioitem->to_api;
458     my $biblio_api     = $biblio->to_api;
459
460     plan tests => (scalar keys %{ $biblioitem_api }) + 1;
461
462     foreach my $key ( keys %{ $biblioitem_api } ) {
463         is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
464     }
465
466     $biblio_api = $biblio->to_api({ embed => { items => {} } });
467     is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
468
469     $schema->storage->txn_rollback;
470 };
471
472 subtest 'suggestions() tests' => sub {
473
474     plan tests => 3;
475
476     $schema->storage->txn_begin;
477
478     my $biblio     = $builder->build_sample_biblio();
479
480     is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
481
482     is_deeply(
483         $biblio->suggestions->unblessed,
484         [],
485         '->suggestions returns an empty Koha::Suggestions resultset'
486     );
487
488     my $suggestion = $builder->build_object(
489         {
490             class => 'Koha::Suggestions',
491             value => { biblionumber => $biblio->biblionumber }
492         }
493     );
494
495     my $suggestions = $biblio->suggestions->unblessed;
496
497     is_deeply(
498         $biblio->suggestions->unblessed,
499         [ $suggestion->unblessed ],
500         '->suggestions returns the related Koha::Suggestion objects'
501     );
502
503     $schema->storage->txn_rollback;
504 };
505
506 subtest 'orders() and active_orders() tests' => sub {
507
508     plan tests => 5;
509
510     $schema->storage->txn_begin;
511
512     my $biblio = $builder->build_sample_biblio();
513
514     my $orders        = $biblio->orders;
515     my $active_orders = $biblio->active_orders;
516
517     is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
518     is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
519
520     # Add a couple orders
521     foreach (1..2) {
522         $builder->build_object(
523             {
524                 class => 'Koha::Acquisition::Orders',
525                 value => {
526                     biblionumber => $biblio->biblionumber,
527                     datecancellationprinted => '2019-12-31'
528                 }
529             }
530         );
531     }
532
533     $builder->build_object(
534         {
535             class => 'Koha::Acquisition::Orders',
536             value => {
537                 biblionumber => $biblio->biblionumber,
538                 datecancellationprinted => undef
539             }
540         }
541     );
542
543     $orders = $biblio->orders;
544     $active_orders = $biblio->active_orders;
545
546     is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
547     is( ref($active_orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
548     is( $orders->count, $active_orders->count + 2, '->active_orders->count returns the rigt count' );
549
550     $schema->storage->txn_rollback;
551 };
552
553 subtest 'subscriptions() tests' => sub {
554
555     plan tests => 4;
556
557     $schema->storage->txn_begin;
558
559     my $biblio = $builder->build_sample_biblio;
560
561     my $subscriptions = $biblio->subscriptions;
562     is( ref($subscriptions), 'Koha::Subscriptions',
563         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
564     );
565     is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
566
567     # Add two subscriptions
568     foreach (1..2) {
569         $builder->build_object(
570             {
571                 class => 'Koha::Subscriptions',
572                 value => { biblionumber => $biblio->biblionumber }
573             }
574         );
575     }
576
577     $subscriptions = $biblio->subscriptions;
578     is( ref($subscriptions), 'Koha::Subscriptions',
579         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
580     );
581     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
582
583     $schema->storage->txn_rollback;
584 };
585
586 subtest 'get_marc_notes() MARC21 tests' => sub {
587     plan tests => 13;
588
589     $schema->storage->txn_begin;
590
591     t::lib::Mocks::mock_preference( 'NotesToHide', '520' );
592
593     my $biblio = $builder->build_sample_biblio;
594     my $record = $biblio->metadata->record;
595     $record->append_fields(
596         MARC::Field->new( '500', '', '', a => 'Note1' ),
597         MARC::Field->new( '505', '', '', a => 'Note2', u => 'http://someserver.com' ),
598         MARC::Field->new( '520', '', '', a => 'Note3 skipped' ),
599         MARC::Field->new( '541', '0', '', a => 'Note4 skipped on opac' ),
600         MARC::Field->new( '541', '', '', a => 'Note5' ),
601         MARC::Field->new( '590', '', '', a => 'CODE' ),
602     );
603
604     Koha::AuthorisedValueCategory->new({ category_name => 'TEST' })->store;
605     Koha::AuthorisedValue->new({ category => 'TEST', authorised_value => 'CODE', lib => 'Description should show', lib_opac => 'Description should show OPAC' })->store;
606     my $mss = Koha::MarcSubfieldStructures->find({tagfield => "590", tagsubfield => "a", frameworkcode => $biblio->frameworkcode });
607     $mss->update({ authorised_value => "TEST" });
608
609     my $cache = Koha::Caches->get_instance;
610     $cache->clear_from_cache("MarcStructure-0-");
611     $cache->clear_from_cache("MarcStructure-1-");
612     $cache->clear_from_cache("default_value_for_mod_marc-");
613     $cache->clear_from_cache("MarcSubfieldStructure-");
614
615     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
616     $biblio = Koha::Biblios->find( $biblio->biblionumber);
617
618     my $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21' });
619     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
620     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
621     is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
622     is( $notes->[3]->{marcnote}, 'Note4 skipped on opac',"Not shows if not opac" );
623     is( $notes->[4]->{marcnote}, 'Note5', 'Fifth note' );
624     is( $notes->[5]->{marcnote}, 'Description should show', 'Authorised value is correctly parsed to show description rather than code' );
625     is( @$notes, 6, 'No more notes' );
626     $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21', opac => 1 });
627     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
628     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
629     is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
630     is( $notes->[3]->{marcnote}, 'Note5', 'Fifth note shows after fourth skipped' );
631     is( $notes->[4]->{marcnote}, 'Description should show OPAC', 'Authorised value is correctly parsed for OPAC to show description rather than code' );
632     is( @$notes, 5, 'No more notes' );
633
634     $cache->clear_from_cache("MarcStructure-0-");
635     $cache->clear_from_cache("MarcStructure-1-");
636     $cache->clear_from_cache("default_value_for_mod_marc-");
637     $cache->clear_from_cache("MarcSubfieldStructure-");
638
639     $schema->storage->txn_rollback;
640 };
641
642 subtest 'get_marc_notes() UNIMARC tests' => sub {
643     plan tests => 3;
644
645     $schema->storage->txn_begin;
646
647     t::lib::Mocks::mock_preference( 'NotesToHide', '310' );
648
649     my $biblio = $builder->build_sample_biblio;
650     my $record = $biblio->metadata->record;
651     $record->append_fields(
652         MARC::Field->new( '300', '', '', a => 'Note1' ),
653         MARC::Field->new( '300', '', '', a => 'Note2' ),
654         MARC::Field->new( '310', '', '', a => 'Note3 skipped' ),
655     );
656     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
657     $biblio = Koha::Biblios->find( $biblio->biblionumber);
658     my $notes = $biblio->get_marc_notes({ marcflavour => 'UNIMARC' });
659     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
660     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
661     is( @$notes, 2, 'No more notes' );
662
663     $schema->storage->txn_rollback;
664 };
665
666 subtest 'host_items() tests' => sub {
667     plan tests => 6;
668
669     $schema->storage->txn_begin;
670
671     my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
672
673     t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 );
674     my $host_items = $biblio->host_items;
675     is( ref($host_items),   'Koha::Items' );
676     is( $host_items->count, 0 );
677
678     my $item_1 =
679       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
680     my $host_item_1 = $builder->build_sample_item;
681     my $host_item_2 = $builder->build_sample_item;
682
683     my $record = $biblio->metadata->record;
684     $record->append_fields(
685         MARC::Field->new(
686             '773', '', '',
687             9 => $host_item_1->itemnumber,
688             9 => $host_item_2->itemnumber
689         ),
690     );
691     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
692     $biblio = $biblio->get_from_storage;
693     $host_items = $biblio->host_items;
694     is( $host_items->count, 2 );
695     is_deeply( [ $host_items->get_column('itemnumber') ],
696         [ $host_item_1->itemnumber, $host_item_2->itemnumber ] );
697
698     t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 0 );
699     $host_items = $biblio->host_items;
700     is( ref($host_items),   'Koha::Items' );
701     is( $host_items->count, 0 );
702
703     $schema->storage->txn_rollback;
704 };
705
706 subtest 'article_requests() tests' => sub {
707
708     plan tests => 3;
709
710     $schema->storage->txn_begin;
711
712     my $item   = $builder->build_sample_item;
713     my $biblio = $item->biblio;
714
715     my $article_requests = $biblio->article_requests;
716     is( ref($article_requests), 'Koha::ArticleRequests',
717         'In scalar context, type is correct' );
718     is( $article_requests->count, 0, 'No article requests' );
719
720     foreach my $i ( 0 .. 3 ) {
721
722         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
723
724         Koha::ArticleRequest->new(
725             {
726                 borrowernumber => $patron->id,
727                 biblionumber   => $biblio->id,
728                 itemnumber     => $item->id,
729                 title          => $biblio->title,
730             }
731         )->request;
732     }
733
734     $article_requests = $biblio->article_requests;
735     is( $article_requests->count, 4, '4 article requests' );
736
737     $schema->storage->txn_rollback;
738 };