Bug 8995: (follow-up) Add tests, move open_url/coins routines to Koha namespace
[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 => 6;
21
22 use C4::Biblio;
23 use Koha::Database;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 BEGIN {
29     use_ok('Koha::Biblio');
30     use_ok('Koha::Biblios');
31 }
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'metadata() tests' => sub {
37
38     plan tests => 4;
39
40     $schema->storage->txn_begin;
41
42     my $title = 'Oranges and Peaches';
43
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, '');
48
49     my $biblio = Koha::Biblios->find( $biblionumber );
50     is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
51
52     my $metadata = $biblio->metadata;
53     is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
54
55     my $record2 = $metadata->record;
56     is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
57
58     is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
59
60     $schema->storage->txn_rollback;
61 };
62
63 subtest 'hidden_in_opac() tests' => sub {
64
65     plan tests => 3;
66
67     $schema->storage->txn_begin;
68
69     my $biblio = $builder->build_sample_biblio();
70     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
71     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
72
73     $item_1->withdrawn( 1 )->store->discard_changes;
74     $item_2->withdrawn( 1 )->store->discard_changes;
75
76     ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
77
78     $item_2->withdrawn( 2 )->store->discard_changes;
79     $biblio->discard_changes; # refresh
80
81     ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
82
83     $item_1->withdrawn( 2 )->store->discard_changes;
84     $biblio->discard_changes; # refresh
85
86     ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
87
88     $schema->storage->txn_rollback;
89 };
90
91 subtest 'items() tests' => sub {
92
93     plan tests => 3;
94
95     $schema->storage->txn_begin;
96
97     my $biblio = $builder->build_sample_biblio();
98     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
99     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
100
101     my $items = $biblio->items;
102     is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
103     is( $items->count, 2, 'Two items in resultset' );
104
105     my @items = $biblio->items->as_list;
106     is( scalar @items, 2, 'Same result, but in list context' );
107
108     $schema->storage->txn_rollback;
109
110 };
111
112 subtest 'get_coins and get_openurl' => sub {
113
114     plan tests => 3;
115
116     $schema->storage->txn_begin;
117
118     my $builder = t::lib::TestBuilder->new;
119     my $biblio = $builder->build_sample_biblio({
120             title => 'Title 1',
121             author => 'Author 1'
122         });
123
124     is(
125         $biblio->get_coins,
126         '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',
127         'GetCOinsBiblio returned right metadata'
128     );
129
130     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
131     is(
132         $biblio->get_openurl,
133         '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',
134         'Koha::Biblio->get_openurl returned right URL'
135     );
136
137     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
138     is(
139         $biblio->get_openurl,
140         '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',
141         'Koha::Biblio->get_openurl returned right URL'
142     );
143
144     $schema->storage->txn_rollback;
145 };