3 # This file is part of Koha.
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.
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.
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>.
25 use DateTime::Duration;
27 use t::lib::TestBuilder;
29 use Test::More tests => 22;
33 use_ok('C4::Circulation');
38 CreateBranchTransferLimit
39 DeleteBranchTransferLimits
46 my $schema = Koha::Database->schema;
47 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new;
50 my $dbh = C4::Context->dbh;
51 $dbh->do(q|DELETE FROM issues|);
52 $dbh->do(q|DELETE FROM borrowers|);
53 $dbh->do(q|DELETE FROM items|);
54 $dbh->do(q|DELETE FROM branches|);
55 $dbh->do(q|DELETE FROM branch_transfer_limits|);
56 $dbh->do(q|DELETE FROM branchtransfers|);
58 ## Create sample datas
60 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
61 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
63 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
66 my $record = MARC::Record->new();
67 $record->append_fields(
68 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
69 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
71 my @sampleitem1 = C4::Items::AddItem(
73 itemcallnumber => 'callnumber1',
74 homebranch => $branchcode_1,
75 holdingbranch => $branchcode_1,
80 my $item_id1 = $sampleitem1[2];
81 my @sampleitem2 = C4::Items::AddItem(
83 itemcallnumber => 'callnumber2',
84 homebranch => $branchcode_1,
85 holdingbranch => $branchcode_1,
90 my $item_id2 = $sampleitem2[2];
105 #Test CreateBranchTransferLimit
107 CreateBranchTransferLimit(
109 $branchcode_1, 'CODE'
112 "A Branch TransferLimit has been added"
114 is(CreateBranchTransferLimit(),undef,
115 "Without parameters CreateBranchTransferLimit returns undef");
116 is(CreateBranchTransferLimit($branchcode_2),undef,
117 "With only tobranch CreateBranchTransferLimit returns undef");
118 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
119 "With only frombranch CreateBranchTransferLimit returns undef");
120 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
121 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
122 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
125 my @transfers = GetTransfers($item_id1);
128 [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*') ],
129 "Transfers of the item1"
130 ); #NOTE: Only the first transfer is returned
131 @transfers = GetTransfers;
132 is_deeply( \@transfers, [],
133 "GetTransfers without params returns an empty array" );
134 @transfers = GetTransfers(-1);
135 is_deeply( \@transfers, [],
136 "GetTransfers with a wrong item id returns an empty array" );
138 #Test GetTransfersFromTo
139 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
145 branchtransfer_id => re('[0-9]*'),
146 itemnumber => $item_id1,
147 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
148 frombranch => $branchcode_1
151 branchtransfer_id => re('[0-9]*'),
152 itemnumber => $item_id2,
153 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
154 frombranch => $branchcode_1
157 "Item1 and Item2 has been transferred from branch1 to branch2"
159 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
160 is_deeply( \@transferto, [],
161 "GetTransfersfromTo without frombranch returns an empty array" );
162 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
163 is_deeply( \@transferfrom, [],
164 "GetTransfersfromTo without tobranch returns an empty array" );
165 @transferfrom = GetTransfersFromTo();
166 is_deeply( \@transferfrom, [],
167 "GetTransfersfromTo without params returns an empty array" );
169 #Test DeleteBranchTransferLimits
171 C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
173 "A Branch TransferLimit has been deleted"
175 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
176 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
179 is( C4::Circulation::DeleteTransfer($item_id1),
180 1, "A the item1's transfer has been deleted" );
181 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
182 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
185 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
186 undef, "No tranferslip if invalid or undef itemnumber or barcode" );
187 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
188 'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
190 C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
191 C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
192 "Barcode and itemnumber for same item both generate same TransferSlip"
195 $schema->storage->txn_rollback;