Bug 14334: Remove AutoCommit from 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 => 5;
23 use Test::Exception;
24
25 use C4::Items;
26 use C4::Biblio;
27 use C4::Reserves;
28
29 use Koha::DateUtils qw( dt_from_string output_pref );
30 use Koha::Biblios;
31 use Koha::Patrons;
32 use Koha::Subscriptions;
33 use t::lib::TestBuilder;
34 use t::lib::Mocks;
35
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
38
39 my $builder = t::lib::TestBuilder->new;
40 my $patron = $builder->build( { source => 'Borrower' } );
41 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
42
43 my $biblio = Koha::Biblio->new()->store();
44
45 my $biblioitem = $schema->resultset('Biblioitem')->new(
46     {
47         biblionumber => $biblio->id
48     }
49 )->insert();
50
51 subtest 'store' => sub {
52     plan tests => 1;
53     is(
54         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
55         output_pref(
56             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
57         ),
58         "datecreated must be set to today if not passed to the constructor"
59     );
60 };
61
62 subtest 'holds + current_holds' => sub {
63     plan tests => 5;
64     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
65     my $holds = $biblio->holds;
66     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
67     is( $holds->count, 1, '->holds should only return 1 hold' );
68     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
69     $holds->delete;
70
71     # Add a hold in the future
72     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
73     $holds = $biblio->holds;
74     is( $holds->count, 1, '->holds should return future holds' );
75     $holds = $biblio->current_holds;
76     is( $holds->count, 0, '->current_holds should not return future holds' );
77     $holds->delete;
78
79 };
80
81 subtest 'subscriptions' => sub {
82     plan tests => 2;
83     $builder->build(
84         { source => 'Subscription', value => { biblionumber => $biblio->id } }
85     );
86     $builder->build(
87         { source => 'Subscription', value => { biblionumber => $biblio->id } }
88     );
89     my $biblio        = Koha::Biblios->find( $biblio->id );
90     my $subscriptions = $biblio->subscriptions;
91     is( ref($subscriptions), 'Koha::Subscriptions',
92         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
93     );
94     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
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 ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1');
147     my ($item_bibnum, $item_bibitemnum, $itemnumber)
148         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $bibnum);
149     my $item  = Koha::Items->find($itemnumber);
150     my $biblio = Koha::Biblios->find($bibnum);
151
152     is(Koha::Item::Transfer::Limits->search({
153         fromBranch => $library1->branchcode,
154         toBranch => $library2->branchcode,
155     })->count, 0, 'There are no transfer limits between libraries.');
156     ok($biblio->can_be_transferred({ to => $library2 }),
157         'Some items of this biblio can be transferred between libraries.');
158
159     my $limit = Koha::Item::Transfer::Limit->new({
160         fromBranch => $library1->branchcode,
161         toBranch => $library2->branchcode,
162         itemtype => $item->effective_itemtype,
163     })->store;
164     is(Koha::Item::Transfer::Limits->search({
165         fromBranch => $library1->branchcode,
166         toBranch => $library2->branchcode,
167     })->count, 1, 'Given we have added a transfer limit that applies for all '
168         .'of this biblio\s items,');
169     is($biblio->can_be_transferred({ to => $library2 }), 0,
170         'None of the items of biblio can no longer be transferred between '
171         .'libraries.');
172     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
173          'We get the same result also if we pass the from-library parameter.');
174     $item->holdingbranch($library2->branchcode)->store;
175     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
176          .'items is already located at to-library, then the transfer is possible.');
177     $item->holdingbranch($library1->branchcode)->store;
178     my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
179         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $bibnum);
180     my $item2  = Koha::Items->find($itemnumber2);
181     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
182         .'another item that should have no transfer limits applying on, then '
183         .'the transfer is possible.');
184     $item2->holdingbranch($library1->branchcode)->store;
185     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
186         .' of the biblio are from same, transfer limited library, then transfer'
187         .' is not possible.');
188 };
189
190 $schema->storage->txn_rollback;
191
192 # Helper method to set up a Biblio.
193 sub create_helper_biblio {
194     my $itemtype = shift;
195     my ($bibnum, $title, $bibitemnum);
196     my $bib = MARC::Record->new();
197     $title = 'Silence in the library';
198     $bib->append_fields(
199         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
200         MARC::Field->new('245', ' ', ' ', a => $title),
201         MARC::Field->new('942', ' ', ' ', c => $itemtype),
202     );
203     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
204 }