Bug 19530: Prevent multiple transfers from existing for one item
[koha.git] / t / db_dependent / Circulation / transfers.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 use C4::Biblio;
20 use C4::Context;
21 use C4::Items;
22 use C4::Circulation;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use DateTime::Duration;
26 use Koha::Item::Transfers;
27
28 use t::lib::TestBuilder;
29
30 use Test::More tests => 24;
31 use Test::Deep;
32
33 BEGIN {
34     use_ok('C4::Circulation');
35 }
36 can_ok(
37     'C4::Circulation',
38     qw(
39       CreateBranchTransferLimit
40       DeleteBranchTransferLimits
41       DeleteTransfer
42       GetTransfers
43       GetTransfersFromTo
44       )
45 );
46
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
49 my $builder = t::lib::TestBuilder->new;
50
51 my $dbh = C4::Context->dbh;
52 $dbh->do(q|DELETE FROM issues|);
53 $dbh->do(q|DELETE FROM borrowers|);
54 $dbh->do(q|DELETE FROM items|);
55 $dbh->do(q|DELETE FROM branches|);
56 $dbh->do(q|DELETE FROM branch_transfer_limits|);
57 $dbh->do(q|DELETE FROM branchtransfers|);
58
59 ## Create sample datas
60 # Add branches
61 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
62 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
63 # Add itemtype
64 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
65
66 #Add biblio and items
67 my $record = MARC::Record->new();
68 $record->append_fields(
69     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
70 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
71
72 my @sampleitem1 = C4::Items::AddItem(
73     {   barcode        => 1,
74         itemcallnumber => 'callnumber1',
75         homebranch     => $branchcode_1,
76         holdingbranch  => $branchcode_1,
77         itype          => $itemtype
78     },
79     $biblionumber
80 );
81 my $item_id1    = $sampleitem1[2];
82 my @sampleitem2 = C4::Items::AddItem(
83     {   barcode        => 2,
84         itemcallnumber => 'callnumber2',
85         homebranch     => $branchcode_1,
86         holdingbranch  => $branchcode_1,
87         itype          => $itemtype
88     },
89     $biblionumber
90 );
91 my $item_id2 = $sampleitem2[2];
92
93 #Add transfers
94 ModItemTransfer(
95     $item_id1,
96     $branchcode_1,
97     $branchcode_2
98 );
99 ModItemTransfer(
100     $item_id2,
101     $branchcode_1,
102     $branchcode_2
103 );
104
105 #Begin Tests
106 #Test CreateBranchTransferLimit
107 is(
108     CreateBranchTransferLimit(
109         $branchcode_2,
110         $branchcode_1, 'CODE'
111     ),
112     1,
113     "A Branch TransferLimit has been added"
114 );
115 is(CreateBranchTransferLimit(),undef,
116     "Without parameters CreateBranchTransferLimit returns undef");
117 is(CreateBranchTransferLimit($branchcode_2),undef,
118     "With only tobranch CreateBranchTransferLimit returns undef");
119 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
120     "With only frombranch CreateBranchTransferLimit returns undef");
121 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
122 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
123 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
124
125 #Test GetTransfers
126 my @transfers = GetTransfers($item_id1);
127 cmp_deeply(
128     \@transfers,
129     [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*') ],
130     "Transfers of the item1"
131 );    #NOTE: Only the first transfer is returned
132 @transfers = GetTransfers;
133 is_deeply( \@transfers, [],
134     "GetTransfers without params returns an empty array" );
135 @transfers = GetTransfers(-1);
136 is_deeply( \@transfers, [],
137     "GetTransfers with a wrong item id returns an empty array" );
138
139 #Test GetTransfersFromTo
140 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
141     $branchcode_2 );
142 cmp_deeply(
143     \@transferfrom1to2,
144     [
145         {
146             branchtransfer_id => re('[0-9]*'),
147             itemnumber        => $item_id1,
148             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
149             frombranch        => $branchcode_1
150         },
151         {
152             branchtransfer_id => re('[0-9]*'),
153             itemnumber        => $item_id2,
154             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
155             frombranch        => $branchcode_1
156         }
157     ],
158     "Item1 and Item2 has been transferred from branch1 to branch2"
159 );
160 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
161 is_deeply( \@transferto, [],
162     "GetTransfersfromTo without frombranch returns an empty array" );
163 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
164 is_deeply( \@transferfrom, [],
165     "GetTransfersfromTo without tobranch returns an empty array" );
166 @transferfrom = GetTransfersFromTo();
167 is_deeply( \@transferfrom, [],
168     "GetTransfersfromTo without params returns an empty array" );
169
170 #Test DeleteBranchTransferLimits
171 is(
172     C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
173     1,
174     "A Branch TransferLimit has been deleted"
175 );
176 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
177 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
178
179 #Test DeleteTransfer
180 is( C4::Circulation::DeleteTransfer($item_id1),
181     1, "A the item1's transfer has been deleted" );
182 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
183 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
184
185 #Test TransferSlip
186 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
187     undef, "No tranferslip if invalid or undef itemnumber or barcode" );
188 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
189     'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
190 cmp_deeply(
191     C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
192     C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
193     "Barcode and itemnumber for same item both generate same TransferSlip"
194     );
195
196 $dbh->do("DELETE FROM branchtransfers");
197 ModItemTransfer(
198     $item_id1,
199     $branchcode_1,
200     $branchcode_2
201 );
202 my $transfer = Koha::Item::Transfers->search()->next();
203 ModItemTransfer(
204     $item_id1,
205     $branchcode_1,
206     $branchcode_2
207 );
208 $transfer->{_result}->discard_changes;
209 ok( $transfer->datearrived, 'Date arrived is set when new transfer is initiated' );
210 is( $transfer->comments, "Canceled, new transfer from $branchcode_1 to $branchcode_2 created" );
211
212 $schema->storage->txn_rollback;
213