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