Bug 24757: (RM follow-up) Restore test count
[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 => 5;
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(
68         {
69             branchcode     => $patron->branchcode,
70             borrowernumber => $patron->borrowernumber,
71             biblionumber   => $biblio->biblionumber,
72         }
73     );
74     my $holds = $biblio->holds;
75     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
76     is( $holds->count, 1, '->holds should only return 1 hold' );
77     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
78     $holds->delete;
79
80     # Add a hold in the future
81     C4::Reserves::AddReserve(
82         {
83             branchcode       => $patron->branchcode,
84             borrowernumber   => $patron->borrowernumber,
85             biblionumber     => $biblio->biblionumber,
86             reservation_date => dt_from_string->add( days => 2 ),
87         }
88     );
89     $holds = $biblio->holds;
90     is( $holds->count, 1, '->holds should return future holds' );
91     $holds = $biblio->current_holds;
92     is( $holds->count, 0, '->current_holds should not return future holds' );
93     $holds->delete;
94
95 };
96
97 subtest 'waiting_or_in_transit' => sub {
98     plan tests => 4;
99     my $biblio = $builder->build( { source => 'Biblio' } );
100     my $item = $builder->build({
101         source => 'Item',
102         value => {
103             biblionumber => $biblio->{biblionumber}
104         }
105     });
106     my $reserve = $builder->build({
107         source => 'Reserve',
108         value => {
109             biblionumber => $biblio->{biblionumber},
110             found => undef
111         }
112     });
113
114     $reserve = Koha::Holds->find($reserve->{reserve_id});
115     $biblio = Koha::Biblios->find($biblio->{biblionumber});
116
117     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
118
119     $reserve->found('W')->store;
120     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
121
122     $reserve->found('T')->store;
123     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
124
125     my $transfer = $builder->build({
126         source => 'Branchtransfer',
127         value => {
128             itemnumber => $item->{itemnumber},
129             datearrived => undef
130         }
131     });
132     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
133     $reserve->found(undef)->store;
134     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
135 };
136
137 subtest 'can_be_transferred' => sub {
138     plan tests => 8;
139
140     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
141     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
142
143     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
144     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
145     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
146     my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
147     my ($item_bibnum, $item_bibitemnum, $itemnumber)
148         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber);
149     my $item  = Koha::Items->find($itemnumber);
150
151     is(Koha::Item::Transfer::Limits->search({
152         fromBranch => $library1->branchcode,
153         toBranch => $library2->branchcode,
154     })->count, 0, 'There are no transfer limits between libraries.');
155     ok($biblio->can_be_transferred({ to => $library2 }),
156         'Some items of this biblio can be transferred between libraries.');
157
158     my $limit = Koha::Item::Transfer::Limit->new({
159         fromBranch => $library1->branchcode,
160         toBranch => $library2->branchcode,
161         itemtype => $item->effective_itemtype,
162     })->store;
163     is(Koha::Item::Transfer::Limits->search({
164         fromBranch => $library1->branchcode,
165         toBranch => $library2->branchcode,
166     })->count, 1, 'Given we have added a transfer limit that applies for all '
167         .'of this biblio\s items,');
168     is($biblio->can_be_transferred({ to => $library2 }), 0,
169         'None of the items of biblio can no longer be transferred between '
170         .'libraries.');
171     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
172          'We get the same result also if we pass the from-library parameter.');
173     $item->holdingbranch($library2->branchcode)->store;
174     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
175          .'items is already located at to-library, then the transfer is possible.');
176     $item->holdingbranch($library1->branchcode)->store;
177     my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
178         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $biblio->biblionumber);
179     my $item2  = Koha::Items->find($itemnumber2);
180     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
181         .'another item that should have no transfer limits applying on, then '
182         .'the transfer is possible.');
183     $item2->holdingbranch($library1->branchcode)->store;
184     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
185         .' of the biblio are from same, transfer limited library, then transfer'
186         .' is not possible.');
187 };
188
189 subtest 'custom_cover_image_url' => sub {
190     plan tests => 3;
191
192     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
193
194     my $isbn       = '0553573403 | 9780553573404 (pbk.).png';
195     my $issn       = 'my_issn';
196     my $marc_record = MARC::Record->new;
197     my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
198
199     my $biblio = Koha::Biblios->find( $biblionumber );
200     my $biblioitem = $biblio->biblioitem->set(
201         { isbn => $isbn, issn => $issn });
202     is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
203
204     my $marc_024a = '710347104926';
205     $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
206     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
207
208     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
209     is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
210
211     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
212     my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
213     is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
214 };
215
216 $schema->storage->txn_rollback;