Bug 17653: Remove itemtype-related t/db_dependent/Circulation* warnings
[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
27 use t::lib::TestBuilder;
28
29 use Test::More tests => 22;
30 use Test::Deep;
31
32 BEGIN {
33     use_ok('C4::Circulation');
34 }
35 can_ok(
36     'C4::Circulation',
37     qw(
38       CreateBranchTransferLimit
39       DeleteBranchTransferLimits
40       DeleteTransfer
41       GetTransfers
42       GetTransfersFromTo
43       )
44 );
45
46 my $schema = Koha::Database->schema;
47 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new;
49
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|);
57
58 ## Create sample datas
59 # Add branches
60 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
61 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
62 # Add itemtype
63 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
64
65 #Add biblio and items
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, '', );
70
71 my @sampleitem1 = C4::Items::AddItem(
72     {   barcode        => 1,
73         itemcallnumber => 'callnumber1',
74         homebranch     => $branchcode_1,
75         holdingbranch  => $branchcode_1,
76         itype          => $itemtype
77     },
78     $biblionumber
79 );
80 my $item_id1    = $sampleitem1[2];
81 my @sampleitem2 = C4::Items::AddItem(
82     {   barcode        => 2,
83         itemcallnumber => 'callnumber2',
84         homebranch     => $branchcode_1,
85         holdingbranch  => $branchcode_1,
86         itype          => $itemtype
87     },
88     $biblionumber
89 );
90 my $item_id2 = $sampleitem2[2];
91
92 #Add transfers
93 ModItemTransfer(
94     $item_id1,
95     $branchcode_1,
96     $branchcode_2
97 );
98 ModItemTransfer(
99     $item_id2,
100     $branchcode_1,
101     $branchcode_2
102 );
103
104 #Begin Tests
105 #Test CreateBranchTransferLimit
106 is(
107     CreateBranchTransferLimit(
108         $branchcode_2,
109         $branchcode_1, 'CODE'
110     ),
111     1,
112     "A Branch TransferLimit has been added"
113 );
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");
123
124 #Test GetTransfers
125 my @transfers = GetTransfers($item_id1);
126 cmp_deeply(
127     \@transfers,
128     [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2 ],
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" );
137
138 #Test GetTransfersFromTo
139 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
140     $branchcode_2 );
141 cmp_deeply(
142     \@transferfrom1to2,
143     [
144         {
145             itemnumber => $item_id1,
146             datesent   => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
147             frombranch => $branchcode_1
148         },
149         {
150             itemnumber => $item_id2,
151             datesent   => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
152             frombranch => $branchcode_1
153         }
154     ],
155     "Item1 and Item2 has been transferred from branch1 to branch2"
156 );
157 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
158 is_deeply( \@transferto, [],
159     "GetTransfersfromTo without frombranch returns an empty array" );
160 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
161 is_deeply( \@transferfrom, [],
162     "GetTransfersfromTo without tobranch returns an empty array" );
163 @transferfrom = GetTransfersFromTo();
164 is_deeply( \@transferfrom, [],
165     "GetTransfersfromTo without params returns an empty array" );
166
167 #Test DeleteBranchTransferLimits
168 is(
169     C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
170     1,
171     "A Branch TransferLimit has been deleted"
172 );
173 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
174 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
175
176 #Test DeleteTransfer
177 is( C4::Circulation::DeleteTransfer($item_id1),
178     1, "A the item1's transfer has been deleted" );
179 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
180 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
181
182 #Test TransferSlip
183 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
184     undef, "No tranferslip if invalid or undef itemnumber or barcode" );
185 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
186     'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
187 cmp_deeply(
188     C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
189     C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
190     "Barcode and itemnumber for same item both generate same TransferSlip"
191     );
192
193 $schema->storage->txn_rollback;
194
195 1;