Bug 15496: (QA follow-up) Fix new uses of Koha::Biblio::items in list context
[koha-equinox.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 => 5;
21
22 use t::lib::TestBuilder;
23
24 use C4::Biblio;
25 use Koha::Database;
26
27 BEGIN {
28     use_ok('Koha::Biblio');
29     use_ok('Koha::Biblios');
30 }
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'metadata() tests' => sub {
36
37     plan tests => 4;
38
39     $schema->storage->txn_begin;
40
41     my $title = 'Oranges and Peaches';
42
43     my $record = MARC::Record->new();
44     my $field = MARC::Field->new('245','','','a' => $title);
45     $record->append_fields( $field );
46     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
47
48     my $biblio = Koha::Biblios->find( $biblionumber );
49     is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
50
51     my $metadata = $biblio->metadata;
52     is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
53
54     my $record2 = $metadata->record;
55     is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
56
57     is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
58
59     $schema->storage->txn_rollback;
60 };
61
62 subtest 'hidden_in_opac() tests' => sub {
63
64     plan tests => 3;
65
66     $schema->storage->txn_begin;
67
68     my $biblio = $builder->build_sample_biblio();
69     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
70     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
71
72     $item_1->withdrawn( 1 )->store->discard_changes;
73     $item_2->withdrawn( 1 )->store->discard_changes;
74
75     ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
76
77     $item_2->withdrawn( 2 )->store->discard_changes;
78     $biblio->discard_changes; # refresh
79
80     ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
81
82     $item_1->withdrawn( 2 )->store->discard_changes;
83     $biblio->discard_changes; # refresh
84
85     ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
86
87     $schema->storage->txn_rollback;
88 };
89
90 subtest 'items() tests' => sub {
91
92     plan tests => 3;
93
94     $schema->storage->txn_begin;
95
96     my $biblio = $builder->build_sample_biblio();
97     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
98     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
99
100     my $items = $biblio->items;
101     is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
102     is( $items->count, 2, 'Two items in resultset' );
103
104     my @items = $biblio->items->as_list;
105     is( scalar @items, 2, 'Same result, but in list context' );
106
107     $schema->storage->txn_rollback;
108
109 };