Bug 18072: Add Koha::Biblio->can_be_transferred
[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 => 4;
23
24 use C4::Biblio;
25 use C4::Items;
26 use C4::Reserves;
27
28 use Koha::DateUtils qw( dt_from_string output_pref );
29 use Koha::Biblios;
30 use Koha::Patrons;
31 use Koha::Subscriptions;
32 use t::lib::TestBuilder;
33 use t::lib::Mocks;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $builder = t::lib::TestBuilder->new;
39 my $patron = $builder->build( { source => 'Borrower' } );
40 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
41
42 my $biblio = Koha::Biblio->new()->store();
43
44 my $biblioitem = $schema->resultset('Biblioitem')->new(
45     {
46         biblionumber => $biblio->id
47     }
48 )->insert();
49
50 subtest 'store' => sub {
51     plan tests => 1;
52     is(
53         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
54         output_pref(
55             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
56         ),
57         "datecreated must be set to today if not passed to the constructor"
58     );
59 };
60
61 subtest 'holds + current_holds' => sub {
62     plan tests => 5;
63     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
64     my $holds = $biblio->holds;
65     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
66     is( $holds->count, 1, '->holds should only return 1 hold' );
67     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
68     $holds->delete;
69
70     # Add a hold in the future
71     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
72     $holds = $biblio->holds;
73     is( $holds->count, 1, '->holds should return future holds' );
74     $holds = $biblio->current_holds;
75     is( $holds->count, 0, '->current_holds should not return future holds' );
76     $holds->delete;
77
78 };
79
80 subtest 'subscriptions' => sub {
81     plan tests => 2;
82     $builder->build(
83         { source => 'Subscription', value => { biblionumber => $biblio->id } }
84     );
85     $builder->build(
86         { source => 'Subscription', value => { biblionumber => $biblio->id } }
87     );
88     my $biblio        = Koha::Biblios->find( $biblio->id );
89     my $subscriptions = $biblio->subscriptions;
90     is( ref($subscriptions), 'Koha::Subscriptions',
91         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
92     );
93     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
94 };
95
96 subtest 'waiting_or_in_transit' => sub {
97     plan tests => 4;
98     my $biblio = $builder->build( { source => 'Biblio' } );
99     my $item = $builder->build({
100         source => 'Item',
101         value => {
102             biblionumber => $biblio->{biblionumber}
103         }
104     });
105     my $reserve = $builder->build({
106         source => 'Reserve',
107         value => {
108             biblionumber => $biblio->{biblionumber},
109             found => undef
110         }
111     });
112
113     $reserve = Koha::Holds->find($reserve->{reserve_id});
114     $biblio = Koha::Biblios->find($biblio->{biblionumber});
115
116     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
117
118     $reserve->found('W')->store;
119     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
120
121     $reserve->found('T')->store;
122     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
123
124     my $transfer = $builder->build({
125         source => 'Branchtransfer',
126         value => {
127             itemnumber => $item->{itemnumber},
128             datearrived => undef
129         }
130     });
131     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
132     $reserve->found(undef)->store;
133     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
134 };
135
136 subtest 'can_be_transferred' => sub {
137     plan tests => 11;
138
139     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
140     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
141
142     my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
143     my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
144     my $library3 = $builder->build( { source => 'Branch' } )->{branchcode};
145     my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
146     my ($item_bibnum, $item_bibitemnum, $itemnumber)
147         = AddItem({ homebranch => $library1, holdingbranch => $library1 }, $bibnum);
148     my $item  = Koha::Items->find($itemnumber);
149     my $biblio = Koha::Biblios->find($bibnum);
150
151     is(Koha::Item::Transfer::Limits->search({
152         fromBranch => $library1,
153         toBranch => $library2,
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,
160         toBranch => $library2,
161         itemtype => $item->effective_itemtype,
162     })->store;
163     is(Koha::Item::Transfer::Limits->search({
164         fromBranch => $library1,
165         toBranch => $library2,
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)->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)->store;
177     my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
178         = AddItem({ homebranch => $library1, holdingbranch => $library3 }, $bibnum);
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)->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     eval { $biblio->can_be_transferred({ to => undef }); };
188     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no'
189         .' library given.');
190     eval { $biblio->can_be_transferred({ to => 'heaven' }); };
191     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
192         .' invalid library is given.');
193     eval { $biblio->can_be_transferred({ to => $library2, from => 'hell' }); };
194     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when'
195         .' invalid library is given.');
196 };
197
198 $schema->storage->txn_rollback;
199
200 # Helper method to set up a Biblio.
201 sub create_helper_biblio {
202     my $itemtype = shift;
203     my ($bibnum, $title, $bibitemnum);
204     my $bib = MARC::Record->new();
205     $title = 'Silence in the library';
206     $bib->append_fields(
207         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
208         MARC::Field->new('245', ' ', ' ', a => $title),
209         MARC::Field->new('942', ' ', ' ', c => $itemtype),
210     );
211     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
212 }