Bug 24430: Unit tests
[koha.git] / t / db_dependent / Koha / Biblios.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 6;
23 use Test::Exception;
24 use MARC::Field;
25
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Reserves;
29
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Biblios;
32 use Koha::Patrons;
33 use Koha::Subscriptions;
34 use t::lib::TestBuilder;
35 use t::lib::Mocks;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
40 my $dbh     = C4::Context->dbh;
41
42 my $builder = t::lib::TestBuilder->new;
43 my $patron = $builder->build( { source => 'Borrower' } );
44 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
45
46 my $biblio = Koha::Biblio->new()->store();
47
48 my $biblioitem = $schema->resultset('Biblioitem')->new(
49     {
50         biblionumber => $biblio->id
51     }
52 )->insert();
53
54 subtest 'store' => sub {
55     plan tests => 1;
56     is(
57         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
58         output_pref(
59             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
60         ),
61         "datecreated must be set to today if not passed to the constructor"
62     );
63 };
64
65 subtest 'holds + current_holds' => sub {
66     plan tests => 5;
67     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
68     my $holds = $biblio->holds;
69     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
70     is( $holds->count, 1, '->holds should only return 1 hold' );
71     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
72     $holds->delete;
73
74     # Add a hold in the future
75     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
76     $holds = $biblio->holds;
77     is( $holds->count, 1, '->holds should return future holds' );
78     $holds = $biblio->current_holds;
79     is( $holds->count, 0, '->current_holds should not return future holds' );
80     $holds->delete;
81
82 };
83
84 subtest 'subscriptions' => sub {
85     plan tests => 2;
86     $builder->build(
87         { source => 'Subscription', value => { biblionumber => $biblio->id } }
88     );
89     $builder->build(
90         { source => 'Subscription', value => { biblionumber => $biblio->id } }
91     );
92     my $biblio        = Koha::Biblios->find( $biblio->id );
93     my $subscriptions = $biblio->subscriptions;
94     is( ref($subscriptions), 'Koha::Subscriptions',
95         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
96     );
97     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
98 };
99
100 subtest 'waiting_or_in_transit' => sub {
101     plan tests => 4;
102     my $biblio = $builder->build( { source => 'Biblio' } );
103     my $item = $builder->build({
104         source => 'Item',
105         value => {
106             biblionumber => $biblio->{biblionumber}
107         }
108     });
109     my $reserve = $builder->build({
110         source => 'Reserve',
111         value => {
112             biblionumber => $biblio->{biblionumber},
113             found => undef
114         }
115     });
116
117     $reserve = Koha::Holds->find($reserve->{reserve_id});
118     $biblio = Koha::Biblios->find($biblio->{biblionumber});
119
120     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
121
122     $reserve->found('W')->store;
123     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
124
125     $reserve->found('T')->store;
126     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
127
128     my $transfer = $builder->build({
129         source => 'Branchtransfer',
130         value => {
131             itemnumber => $item->{itemnumber},
132             datearrived => undef
133         }
134     });
135     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
136     $reserve->found(undef)->store;
137     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
138 };
139
140 subtest 'can_be_transferred' => sub {
141     plan tests => 8;
142
143     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
144     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
145
146     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
147     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
148     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
149     my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
150     my ($item_bibnum, $item_bibitemnum, $itemnumber)
151         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber);
152     my $item  = Koha::Items->find($itemnumber);
153
154     is(Koha::Item::Transfer::Limits->search({
155         fromBranch => $library1->branchcode,
156         toBranch => $library2->branchcode,
157     })->count, 0, 'There are no transfer limits between libraries.');
158     ok($biblio->can_be_transferred({ to => $library2 }),
159         'Some items of this biblio can be transferred between libraries.');
160
161     my $limit = Koha::Item::Transfer::Limit->new({
162         fromBranch => $library1->branchcode,
163         toBranch => $library2->branchcode,
164         itemtype => $item->effective_itemtype,
165     })->store;
166     is(Koha::Item::Transfer::Limits->search({
167         fromBranch => $library1->branchcode,
168         toBranch => $library2->branchcode,
169     })->count, 1, 'Given we have added a transfer limit that applies for all '
170         .'of this biblio\s items,');
171     is($biblio->can_be_transferred({ to => $library2 }), 0,
172         'None of the items of biblio can no longer be transferred between '
173         .'libraries.');
174     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
175          'We get the same result also if we pass the from-library parameter.');
176     $item->holdingbranch($library2->branchcode)->store;
177     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
178          .'items is already located at to-library, then the transfer is possible.');
179     $item->holdingbranch($library1->branchcode)->store;
180     my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
181         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $biblio->biblionumber);
182     my $item2  = Koha::Items->find($itemnumber2);
183     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
184         .'another item that should have no transfer limits applying on, then '
185         .'the transfer is possible.');
186     $item2->holdingbranch($library1->branchcode)->store;
187     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
188         .' of the biblio are from same, transfer limited library, then transfer'
189         .' is not possible.');
190 };
191
192 subtest 'custom_cover_image_url' => sub {
193     plan tests => 3;
194
195     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
196
197     my $isbn       = '0553573403 | 9780553573404 (pbk.).png';
198     my $issn       = 'my_issn';
199     my $marc_record = MARC::Record->new;
200     my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
201
202     my $biblio = Koha::Biblios->find( $biblionumber );
203     my $biblioitem = $biblio->biblioitem->set(
204         { isbn => $isbn, issn => $issn });
205     is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
206
207     my $marc_024a = '710347104926';
208     $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
209     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
210
211     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
212     is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
213
214     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
215     my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
216     is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
217 };
218
219 $schema->storage->txn_rollback;