Bug 23129: Unit test
[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::Context;
20 use C4::Circulation;
21 use C4::Biblio;
22 use C4::Items;
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 => 25;
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
100 my $item_obj = Koha::Items->find({ itemnumber => $item_id1 });
101 is( $item_obj->holdingbranch, $branchcode_1, "Item should be held at branch that initiates transfer");
102
103 ModItemTransfer(
104     $item_id2,
105     $branchcode_1,
106     $branchcode_2
107 );
108
109 #Begin Tests
110 #Test CreateBranchTransferLimit
111 is(
112     CreateBranchTransferLimit(
113         $branchcode_2,
114         $branchcode_1, 'CODE'
115     ),
116     1,
117     "A Branch TransferLimit has been added"
118 );
119 is(CreateBranchTransferLimit(),undef,
120     "Without parameters CreateBranchTransferLimit returns undef");
121 is(CreateBranchTransferLimit($branchcode_2),undef,
122     "With only tobranch CreateBranchTransferLimit returns undef");
123 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
124     "With only frombranch CreateBranchTransferLimit returns undef");
125 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
126 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
127 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
128
129 #Test GetTransfers
130 my @transfers = GetTransfers($item_id1);
131 cmp_deeply(
132     \@transfers,
133     [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*') ],
134     "Transfers of the item1"
135 );    #NOTE: Only the first transfer is returned
136 @transfers = GetTransfers;
137 is_deeply( \@transfers, [],
138     "GetTransfers without params returns an empty array" );
139 @transfers = GetTransfers(-1);
140 is_deeply( \@transfers, [],
141     "GetTransfers with a wrong item id returns an empty array" );
142
143 #Test GetTransfersFromTo
144 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
145     $branchcode_2 );
146 cmp_deeply(
147     \@transferfrom1to2,
148     [
149         {
150             branchtransfer_id => re('[0-9]*'),
151             itemnumber        => $item_id1,
152             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
153             frombranch        => $branchcode_1
154         },
155         {
156             branchtransfer_id => re('[0-9]*'),
157             itemnumber        => $item_id2,
158             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
159             frombranch        => $branchcode_1
160         }
161     ],
162     "Item1 and Item2 has been transferred from branch1 to branch2"
163 );
164 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
165 is_deeply( \@transferto, [],
166     "GetTransfersfromTo without frombranch returns an empty array" );
167 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
168 is_deeply( \@transferfrom, [],
169     "GetTransfersfromTo without tobranch returns an empty array" );
170 @transferfrom = GetTransfersFromTo();
171 is_deeply( \@transferfrom, [],
172     "GetTransfersfromTo without params returns an empty array" );
173
174 #Test DeleteBranchTransferLimits
175 is(
176     C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
177     1,
178     "A Branch TransferLimit has been deleted"
179 );
180 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
181 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
182
183 #Test DeleteTransfer
184 is( C4::Circulation::DeleteTransfer($item_id1),
185     1, "A the item1's transfer has been deleted" );
186 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
187 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
188
189 #Test TransferSlip
190 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
191     undef, "No tranferslip if invalid or undef itemnumber or barcode" );
192 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
193     'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
194 cmp_deeply(
195     C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
196     C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
197     "Barcode and itemnumber for same item both generate same TransferSlip"
198     );
199
200 $dbh->do("DELETE FROM branchtransfers");
201 ModItemTransfer(
202     $item_id1,
203     $branchcode_1,
204     $branchcode_2
205 );
206 my $transfer = Koha::Item::Transfers->search()->next();
207 ModItemTransfer(
208     $item_id1,
209     $branchcode_1,
210     $branchcode_2
211 );
212 $transfer->{_result}->discard_changes;
213 ok( $transfer->datearrived, 'Date arrived is set when new transfer is initiated' );
214 is( $transfer->comments, "Canceled, new transfer from $branchcode_1 to $branchcode_2 created", 'Transfer comment is set as expected when new transfer is initiated' );
215
216 $schema->storage->txn_rollback;
217